|
| 1 | +package build |
| 2 | +import mill.*, scalalib.* |
| 3 | + |
| 4 | +import mill.define._ |
| 5 | +import mill.define.SelectMode |
| 6 | +import mill.api.Result.create |
| 7 | +import mill.api.Result |
| 8 | + |
| 9 | +import java.time.LocalDateTime |
| 10 | +import java.time.format.DateTimeFormatter |
| 11 | + |
| 12 | +object `package` extends ScalaModule { |
| 13 | + def scalaVersion = "2.13.8" |
| 14 | + def mvnDeps = Seq( |
| 15 | + mvn"com.lihaoyi::cask:0.9.1", |
| 16 | + mvn"com.lihaoyi::scalatags:0.13.1" |
| 17 | + ) |
| 18 | + |
| 19 | + object test extends ScalaTests with TestModule.Utest { |
| 20 | + def mvnDeps = Seq( |
| 21 | + mvn"com.lihaoyi::utest::0.8.5", |
| 22 | + mvn"com.lihaoyi::requests::0.6.9" |
| 23 | + ) |
| 24 | + } |
| 25 | + |
| 26 | + // Helper function to format bytes into a human-readable string (KB, MB, GB) |
| 27 | + def formatBytes(bytes: Long): String = { |
| 28 | + if (bytes < 1024) s"$bytes B" |
| 29 | + else { |
| 30 | + val kilobyte = 1024.0 |
| 31 | + val megabyte = kilobyte * 1024 |
| 32 | + val gigabyte = megabyte * 1024 |
| 33 | + |
| 34 | + if (bytes < megabyte) f"${bytes / kilobyte}%.1f KB" |
| 35 | + else if (bytes < gigabyte) f"${bytes / megabyte}%.1f MB" |
| 36 | + else f"${bytes / gigabyte}%.1f GB" |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + val bundleArgs: Seq[String] = Seq("assembly", "test.assembly") |
| 41 | + val buildArgs: Seq[String] = Seq("compile", "test.compile") |
| 42 | + |
| 43 | + def buildReport(evaluator: Evaluator) = Task.Command(exclusive = true) { |
| 44 | + |
| 45 | + println("=== Build Report ===") |
| 46 | + val now = LocalDateTime.now() |
| 47 | + val formatted = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) |
| 48 | + println(s"Build Report started at: $formatted") |
| 49 | + println("=================") |
| 50 | + |
| 51 | + println(s"\nBundle Size Analysis:") |
| 52 | + println(s"---------------------") |
| 53 | + |
| 54 | + val resolvedBundleTasks = evaluator.resolveTasks(bundleArgs, SelectMode.Multi).get |
| 55 | + |
| 56 | + val execBundleResults = evaluator.execute(resolvedBundleTasks).values.get |
| 57 | + |
| 58 | + execBundleResults.foreach { bundleResult => |
| 59 | + val jarPathStr = bundleResult.toString.split(":", 4).last |
| 60 | + val jarPath = os.Path(jarPathStr) |
| 61 | + |
| 62 | + println(s"Checking JAR file: ${jarPath}") |
| 63 | + println(s"JAR size is ${formatBytes(os.size(jarPath))}") |
| 64 | + } |
| 65 | + |
| 66 | + println(s"\nBuild Performance Profile:") |
| 67 | + println(s"---------------------") |
| 68 | + |
| 69 | + val planStartTime = System.currentTimeMillis() |
| 70 | + |
| 71 | + println(s"\n--- Resolution and Planning Phase ---") |
| 72 | + println(s"Resolved tasks (for planning): ${resolvedBundleTasks}") |
| 73 | + |
| 74 | + val plan = evaluator.plan(resolvedBundleTasks) |
| 75 | + .sortedGroups |
| 76 | + .keys() |
| 77 | + .map(_.toString) |
| 78 | + .toArray |
| 79 | + |
| 80 | + val planEndTime = System.currentTimeMillis() |
| 81 | + |
| 82 | + println(s"Planning time: ${planEndTime - planStartTime} ms") |
| 83 | + println(s"Total planned tasks: ${plan.length}") |
| 84 | + |
| 85 | + val execStartTime = System.currentTimeMillis() |
| 86 | + val resolvedSegmentResults = evaluator.resolveSegments(buildArgs, SelectMode.Multi).get |
| 87 | + |
| 88 | + println(s"\n--- Execution Phase ---") |
| 89 | + |
| 90 | + val labelNames = resolvedSegmentResults.flatMap(_.parts) // Extract names for evaluate |
| 91 | + println(s"Resolved segments label names (for execution): ${labelNames}") |
| 92 | + |
| 93 | + val evalResults = evaluator.evaluate(labelNames, SelectMode.Multi) |
| 94 | + val execEndTime = System.currentTimeMillis() |
| 95 | + |
| 96 | + println(s"Execution time: ${execEndTime - execStartTime} ms") |
| 97 | + println(s"Total command time: ${execEndTime - planStartTime} ms") // Overall time |
| 98 | + |
| 99 | + () |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +// This build file defines a simple web application using Cask and Scalatags. |
| 105 | +// It includes a build report that analyzes the bundle size and build performance. |
| 106 | +// The report includes the size of the generated JAR files and the time taken for resolution, planning, and execution phases. |
| 107 | +// The build report can be executed with the command `mill buildReport`. |
| 108 | + |
| 109 | +/** Usage |
| 110 | + |
| 111 | +> ./mill buildReport |
| 112 | + |
| 113 | +=== Build Report === |
| 114 | + |
| 115 | +*/ |
0 commit comments