Skip to content

Commit

Permalink
Reviews Resolved ; Fixes: com-lihaoyi#3550
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshumahajan138 committed Oct 19, 2024
1 parent d05eb8b commit a73defd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package mill.kotlinlib.android

import mill.kotlinlib.KotlinModule
import mill.javalib.android.AndroidAppModule
import mill._
import coursier.Type
import upickle.default.ReadWriter

/**
* Trait for building Android applications using the Mill build tool.
Expand All @@ -21,66 +18,4 @@ import upickle.default.ReadWriter
* [[https://developer.android.com/studio Android Studio Documentation]]
*/
@mill.api.experimental
trait AndroidAppKotlinModule extends AndroidAppModule with KotlinModule {

/**
* Implicit `ReadWriter` for serializing and deserializing `coursier.Type` values.
* Converts a `coursier.Type` to a `String` and vice versa.
*/
implicit val coursierTypeRW: ReadWriter[Type] = upickle.default.readwriter[String].bimap(
_.value, // Serialize coursier.Type to String
Type(_) // Deserialize String to coursier.Type
)

/**
* Implicit `ReadWriter` for handling `Set[coursier.Type]`, allowing conversion
* between a set of `coursier.Type` and a set of `String`.
*/
implicit val coursierTypeSetRW: ReadWriter[Set[Type]] =
upickle.default.readwriter[Set[String]].bimap(
_.map(_.value), // Serialize Set[coursier.Type] to Set[String]
_.map(Type(_)) // Deserialize Set[String] to Set[coursier.Type]
)

/**
* Adds the "aar" type to the set of artifact types handled by this module.
*
* @return A task that yields an updated set of artifact types including "aar".
*/
override def artifactTypes: T[Set[Type]] =
T { super.artifactTypes() + coursier.Type("aar") }

/**
* Task to extract `classes.jar` files from AAR files in the classpath.
*
* @return A sequence of `PathRef` pointing to the extracted JAR files.
*/
def recompileAARs: T[Seq[PathRef]] = Task {
val aarFiles = super.compileClasspath().map(_.path).filter(_.ext == "aar").toSeq

aarFiles.map { aarFile =>
val extractDir = T.dest / aarFile.baseName
os.call(Seq("unzip", aarFile.toString, "-d", extractDir.toString))
PathRef(extractDir / "classes.jar")
}
}

/**
* Updates the compile classpath by removing `.aar` files and including the
* extracted `.jar` files from the AARs.
*
* @return A task yielding the updated classpath with `.jar` files.
*/
def updatedCompileClasspath: T[Agg[PathRef]] = Task {
super.compileClasspath().filter(_.path.ext == "jar") ++ Agg.from(recompileAARs())
}

/**
* Overrides the compile classpath to replace `.aar` files with the extracted
* `.jar` files.
*
* @return The updated classpath with `.jar` files only.
*/
override def compileClasspath: T[Agg[PathRef]] = updatedCompileClasspath()

}
trait AndroidAppKotlinModule extends AndroidAppModule with KotlinModule {}
34 changes: 33 additions & 1 deletion scalalib/src/mill/javalib/android/AndroidAppModule.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package mill.javalib.android

import mill._
import mill.scalalib._
import mill.api.PathRef
import mill.define.ModuleRef
import mill.scalalib.JavaModule

/**
* Trait for building Android applications using the Mill build tool.
Expand Down Expand Up @@ -35,6 +35,38 @@ trait AndroidAppModule extends JavaModule {
*/
def androidManifest: Task[PathRef] = Task.Source(millSourcePath / "AndroidManifest.xml")

/**
* Adds the "aar" type to the set of artifact types handled by this module.
*
* @return A task that yields an updated set of artifact types including "aar".
*/
def artifactTypes: T[Set[coursier.Type]] = Task { super.artifactTypes() + coursier.Type("aar") }

/**
* Task to extract `classes.jar` files from AAR files in the classpath.
*
* @return A sequence of `PathRef` pointing to the extracted JAR files.
*/
def androidUnpackArchives: T[Seq[PathRef]] = Task {
val aarFiles = super.compileClasspath().map(_.path).filter(_.ext == "aar").toSeq

aarFiles.map { aarFile =>
val extractDir = T.dest / aarFile.baseName
os.unzip(aarFile, extractDir)
PathRef(extractDir / "classes.jar")
}
}

/**
* Overrides the compile classpath to replace `.aar` files with the extracted
* `.jar` files.
*
* @return The updated classpath with `.jar` files only.
*/
override def compileClasspath: T[Agg[PathRef]] = Task {
super.compileClasspath().filter(_.path.ext == "jar") ++ Agg.from(androidUnpackArchives())
}

/**
* Generates the Android resources (such as layouts, strings, and other assets) needed
* for the application.
Expand Down
11 changes: 10 additions & 1 deletion scalalib/src/mill/scalalib/JsonFormatters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ trait JsonFormatters {
implicit lazy val orgFormat: RW[coursier.Organization] = upickle.default.macroRW
implicit lazy val modNameFormat: RW[coursier.ModuleName] = upickle.default.macroRW
implicit lazy val configurationFormat: RW[coursier.core.Configuration] = upickle.default.macroRW
implicit lazy val typeFormat: RW[coursier.core.Type] = upickle.default.macroRW
implicit lazy val classifierFormat: RW[coursier.core.Classifier] = upickle.default.macroRW
implicit lazy val coursierTypeRW: RW[coursier.core.Type] =
upickle.default.readwriter[String].bimap(
_.value,
coursier.core.Type(_)
)
implicit lazy val coursierTypeSetRW: RW[Set[coursier.core.Type]] =
upickle.default.readwriter[Set[String]].bimap(
_.map(_.value),
_.map(coursier.core.Type(_))
)

}
object JsonFormatters extends JsonFormatters

0 comments on commit a73defd

Please sign in to comment.