All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.autonomousapps.model.ProjectVariant.kt Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.autonomousapps.model

import com.autonomousapps.internal.unsafeLazy
import com.autonomousapps.internal.utils.flatMapToOrderedSet
import com.autonomousapps.internal.utils.flatMapToSet
import com.autonomousapps.internal.utils.fromJson
import com.autonomousapps.model.CodeSource.Kind
import com.autonomousapps.model.declaration.Variant
import com.squareup.moshi.JsonClass
import org.gradle.api.file.Directory

/** Represents a variant-specific view of the project under analysis. */
@Suppress("MemberVisibilityCanBePrivate") // deliberate API
@JsonClass(generateAdapter = false)
data class ProjectVariant(
  val coordinates: ProjectCoordinates,
  val buildType: String?,
  val flavor: String?,
  val variant: Variant,
  val sources: Set,
  val classpath: Set,
  val annotationProcessors: Set
) {

  val usedClassesBySrc: Set by unsafeLazy {
    codeSource.flatMapToSet {
      it.usedClasses
    }
  }

  val usedClassesByRes: Set by unsafeLazy {
    androidResSource.flatMapToSet {
      it.usedClasses
    }
  }

  val usedClasses: Set by unsafeLazy {
    usedClassesByRes + usedClassesBySrc
  }

  val exposedClasses: Set by unsafeLazy {
    codeSource.flatMapToSet {
      it.exposedClasses
    }
  }

  val implementationClasses: Set by unsafeLazy {
    usedClasses - exposedClasses
  }

  val codeSource: List by unsafeLazy {
    sources.filterIsInstance()
  }

  val androidResSource: List by unsafeLazy {
    sources.filterIsInstance()
  }

  val androidAssetsSource: List by unsafeLazy {
    sources.filterIsInstance()
  }

  val imports: Set by unsafeLazy {
    codeSource.flatMapToOrderedSet { it.imports }
  }

  val javaImports: Set by unsafeLazy {
    codeSource.filter { it.kind == Kind.JAVA }
      .flatMapToOrderedSet { it.imports }
  }

  val kotlinImports: Set by unsafeLazy {
    codeSource.filter { it.kind == Kind.KOTLIN }
      .flatMapToOrderedSet { it.imports }
  }

  val groovyImports: Set by unsafeLazy {
    codeSource.filter { it.kind == Kind.GROOVY }
      .flatMapToOrderedSet { it.imports }
  }

  val scalaImports: Set by unsafeLazy {
    codeSource.filter { it.kind == Kind.SCALA }
      .flatMapToOrderedSet { it.imports }
  }

  internal fun dependencies(dependenciesDir: Directory): Set {
    return classpath.asSequence()
      .plus(annotationProcessors)
      .map {
        val file = dependenciesDir.file(it.toFileName())
        if (file.asFile.exists()) {
          file.fromJson()
        } else {
          error("No file ${it.toFileName()}")
        }
      }
      .toSet()
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy