Skip to content

Commit

Permalink
Add aar support when importing sbt-android
Browse files Browse the repository at this point in the history
  • Loading branch information
pfn committed Jul 18, 2016
1 parent a8075f0 commit 231aaa0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object CreateTasks extends (State => State) with SbtStateOps {
UtilityTasks.acceptedProjects,
StructureKeys.extractProjects <<=
(Keys.state, StructureKeys.acceptedProjects) flatMap { (state, acceptedProjects) =>
StructureKeys.extractProject.forAllProjects(state, acceptedProjects).map(_.values.toSeq)
StructureKeys.extractProject.forAllProjects(state, acceptedProjects).map(_.values.toSeq.flatten)
},
StructureKeys.extractRepository <<=
RepositoryExtractor.taskDef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object StructureKeys {
lazy val extractAndroid = TaskKey[Option[AndroidData]]("ss-extract-android")
lazy val extractBuild = TaskKey[BuildData]("ss-extract-build")
lazy val extractDependencies = TaskKey[DependencyData]("ss-extract-dependencies")
lazy val extractProject = TaskKey[ProjectData]("ss-extract-project")
lazy val extractProject = TaskKey[Seq[ProjectData]]("ss-extract-project")
lazy val extractProjects = TaskKey[Seq[ProjectData]]("ss-extract-projects")
lazy val extractRepository = TaskKey[Option[RepositoryData]]("ss-extract-repository")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package extractors

import java.io.File

import org.jetbrains.sbt.structure.{ApkLib, AndroidData}
import org.jetbrains.sbt.structure.{Aar, ApkLib, AndroidData, BuildData, ConfigurationData, DependencyData, DirectoryData, ProjectData}
import sbt._
import sbt.Project.Initialize

Expand All @@ -27,14 +27,16 @@ object AndroidSdkPluginExtractor extends SbtStateOps with TaskOps {
.flatMap(_.in(projectRef).find(state))
val apklibsAsAnyTaskOpt = findTaskKeyIn(keys, "apklibs")
.flatMap(_.in(projectRef).find(state))
val aarsAsAnyTaskOpt = findTaskKeyIn(keys, "aars")
.flatMap(_.in(projectRef).find(state))

val androidTaskOpt = for {
manifestTask <- manifestFileTaskOpt
apk <- Keys.apkFile.in(projectRef).find(state)
isLibrary <- Keys.libraryProject.in(projectRef).find(state)
layoutAsAny <- layoutAsAnyOpt
apklibsAsAnyTask <- apklibsAsAnyTaskOpt
targetVersionTask <- targetVersionTaskOpt
aarsAsAnyTask <- aarsAsAnyTaskOpt
proguardConfigTask <- Keys.proguardConfig.in(projectRef).find(state)
proguardOptionsTask <- Keys.proguardOptions.in(projectRef).find(state)
} yield {
Expand All @@ -44,14 +46,16 @@ object AndroidSdkPluginExtractor extends SbtStateOps with TaskOps {
proguardConfig <- proguardConfigTask
proguardOptions <- proguardOptionsTask
apklibsAsAny <- apklibsAsAnyTask
aarsAsAny <- aarsAsAnyTask
} yield {
try {
val layout = layoutAsAny.asInstanceOf[ProjectLayout]
val layout = layoutAsAny.asInstanceOf[ProjectLayout]
val apklibs = apklibsAsAny.asInstanceOf[Seq[LibraryDependency]]
val aars = aarsAsAny.asInstanceOf[Seq[LibraryDependency]].map(libraryDepToAar(targetVersion) _)
Some(AndroidData(targetVersion, manifest, apk,
layout.res, layout.assets, layout.gen, layout.libs,
isLibrary, proguardConfig ++ proguardOptions,
apklibs.map(libraryDepToApkLib)))
apklibs.map(libraryDepToApkLib), aars))
} catch {
case _ : NoSuchMethodException => None
}
Expand Down Expand Up @@ -79,14 +83,17 @@ object AndroidSdkPluginExtractor extends SbtStateOps with TaskOps {
def res: File
def assets: File
def gen: File
def bin: File
def libs: File
def sources: File
def resources: File
def manifest: File
}

private type LibraryDependency = {
def layout: ProjectLayout
def getName: String
def getJarFile: File
}

private def findSettingKeyIn(keys: Seq[sbt.ScopedKey[_]], label: String): Option[SettingKey[Any]] =
Expand All @@ -103,6 +110,18 @@ object AndroidSdkPluginExtractor extends SbtStateOps with TaskOps {
ApkLib(fixedLibName, lib.layout.base, lib.layout.manifest, lib.layout.sources, lib.layout.res, lib.layout.libs, lib.layout.gen)
}

private def libraryDepToAar(targetSdkVersion: String)(lib: LibraryDependency): Aar = {
val fixedLibName = lib.getName.split(File.separatorChar).last
val android = AndroidData(targetSdkVersion, lib.layout.manifest, lib.layout.base,
lib.layout.res, lib.layout.assets, lib.layout.gen, lib.layout.libs,
true, Nil, Nil, Nil)
val project = ProjectData(fixedLibName, fixedLibName, "sbt-android-synthetic-organization", "0.1-SNAPSHOT-sbt-android",
lib.layout.base, Nil, lib.layout.bin, BuildData(Nil, Nil, Nil, Nil),
ConfigurationData("compile", Seq(DirectoryData(lib.layout.sources, true)), Seq(DirectoryData(lib.layout.resources, true)), Nil, lib.getJarFile) :: Nil, None, None, Some(android),
DependencyData(Nil, Nil, Nil), Set.empty, None)
Aar(fixedLibName, project)
}

private def isInAndroidScope(key: ScopedKey[_]) = key.scope.config match {
case Select(k) => k.name == Android.name
case _ => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package extractors

import org.jetbrains.sbt.structure._
import sbt.Project.Initialize
import sbt._
import sbt.{Configuration => _, _}

/**
* @author Nikolay Obedin
Expand Down Expand Up @@ -36,15 +36,27 @@ class ProjectExtractor(projectRef: ProjectRef,
play2: Option[Play2Data]) {


private[extractors] def extract: ProjectData = {
private[extractors] def extract: Seq[ProjectData] = {
val resolvers = fullResolvers.collect {
case MavenRepository(name, root) => ResolverData(name, root)
}.toSet
val configurations =
mergeConfigurations(sourceConfigurations.flatMap(extractConfiguration))
ProjectData(projectRef.id, name, organization, version, base,
val projectData = ProjectData(projectRef.id, name, organization, version, base,
basePackages, target, build, configurations,
extractJava, extractScala, android, dependencies, resolvers, play2)
android.fold(Seq(projectData)) { a =>
val deps = a.aars.map(aar => ProjectDependencyData(aar.name, Configuration.Compile :: Nil))
// add aar module dependencies
val updatedProject = projectData.copy(dependencies = dependencies.copy(projects = projectData.dependencies.projects ++ deps))
updatedProject +: a.aars.map(_.project.copy(
java = projectData.java,
scala = projectData.scala,
build = projectData.build,
resolvers = projectData.resolvers,
dependencies = projectData.dependencies
))
}
}

private def extractConfiguration(configuration: sbt.Configuration): Option[ConfigurationData] =
Expand Down Expand Up @@ -91,7 +103,7 @@ class ProjectExtractor(projectRef: ProjectRef,

object ProjectExtractor extends SbtStateOps with TaskOps {

def taskDef: Initialize[Task[ProjectData]] =
def taskDef: Initialize[Task[Seq[ProjectData]]] =
( sbt.Keys.state
, sbt.Keys.thisProjectRef
, StructureKeys.sbtStructureOpts
Expand Down
5 changes: 4 additions & 1 deletion shared/src/main/scala/org/jetbrains/sbt/structure/data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ case class AndroidData(targetVersion: String,
libs: File,
isLibrary: Boolean,
proguardConfig: Seq[String],
apklibs: Seq[ApkLib])
apklibs: Seq[ApkLib],
aars: Seq[Aar])

case class Aar(name: String, project: ProjectData)

/**
* Information about certain apklib used in Android project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ trait DataSerializers {
val apklibs = (what \ "apkLib").deserialize[ApkLib]
Right(AndroidData(version, file(manifestPath), file(apkPath),
file(resPath), file(assetsPath), file(genPath),
file(libsPath), isLibrary, proguardConfig, apklibs))
file(libsPath), isLibrary, proguardConfig, apklibs, Nil))
}
}

Expand Down

0 comments on commit 231aaa0

Please sign in to comment.