Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move BuildInfo from sources into resources, defer use of resources during compilation #2425

Merged
merged 41 commits into from
Apr 8, 2023
Merged
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
504d6ff
.
lihaoyi Apr 6, 2023
1075d7e
build.sc compiles
lihaoyi Apr 7, 2023
482b52f
move to resource based buildinfo
lihaoyi Apr 7, 2023
18a54a4
.
lihaoyi Apr 7, 2023
c9382e5
cleanup
lihaoyi Apr 7, 2023
9eef9c3
fix
lihaoyi Apr 7, 2023
028c779
.
lihaoyi Apr 7, 2023
cddc254
transitiveCompileClasspath should include compile.classes
lihaoyi Apr 7, 2023
0dc7664
wip replacing contribu/buildinfo with new implementation
lihaoyi Apr 7, 2023
80b8d0b
contrib.buildinfo.test passes
lihaoyi Apr 7, 2023
1a5bb75
fix build.sc
lihaoyi Apr 7, 2023
ac0e98e
.
lihaoyi Apr 7, 2023
f6e96b0
.
lihaoyi Apr 7, 2023
5050886
.
lihaoyi Apr 7, 2023
f490919
update patch bootstrap
lihaoyi Apr 7, 2023
196c320
add mill-contrib-buildinfo import to patch
lihaoyi Apr 7, 2023
e2625ce
scaladoc
lihaoyi Apr 7, 2023
e28d97f
fixcompile
lihaoyi Apr 7, 2023
fe6aef7
fix-compile
lihaoyi Apr 7, 2023
a622dca
.
lihaoyi Apr 7, 2023
6600186
fixbsp
lihaoyi Apr 7, 2023
f351a10
add import to patch
lihaoyi Apr 7, 2023
2c2590d
.
lihaoyi Apr 7, 2023
a5fabc3
fixpatch
lihaoyi Apr 7, 2023
a5f9e54
BuildInfo.Value
lihaoyi Apr 7, 2023
147942e
update patch
lihaoyi Apr 7, 2023
8af9b5d
fixbuild
lihaoyi Apr 7, 2023
6590af0
.
lihaoyi Apr 7, 2023
1455fcd
rename to BuildInfo.buildinfo.properties
lihaoyi Apr 8, 2023
d2ebf93
use relative getResourceAsStream
lihaoyi Apr 8, 2023
818f7fc
.
lihaoyi Apr 8, 2023
6aa9984
update build.sc buildinfo
lihaoyi Apr 8, 2023
e229fc9
update
lihaoyi Apr 8, 2023
235fc7f
remove-println
lihaoyi Apr 8, 2023
d50c0ed
fix-patch
lihaoyi Apr 8, 2023
1ae58e4
.
lihaoyi Apr 8, 2023
f725a38
debug
lihaoyi Apr 8, 2023
8ea492f
.
lihaoyi Apr 8, 2023
8d7f669
add publishLocal to make test-mill-dev pass
lihaoyi Apr 8, 2023
a6e2084
bump timeout for flaky test
lihaoyi Apr 8, 2023
f36c0b8
scalafmt
lihaoyi Apr 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
.
  • Loading branch information
lihaoyi committed Apr 7, 2023
commit 5050886930564b08191fe0ab3bd5ed4bbca1202b
275 changes: 80 additions & 195 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
import $file.ci.shared
import $file.ci.upload
import $ivy.`org.scalaj::scalaj-http:2.4.2`
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version_mill0.10:0.3.0`
import $ivy.`com.github.lolgab::mill-mima_mill0.10:0.0.13`
import $ivy.`net.sourceforge.htmlcleaner:htmlcleaner:2.25`

// imports
import com.github.lolgab.mill.mima
import com.github.lolgab.mill.mima.{
CheckDirection,
DirectMissingMethodProblem,
IncompatibleMethTypeProblem,
IncompatibleSignatureProblem,
ProblemFilter,
ReversedMissingMethodProblem
}
import coursier.maven.MavenRepository
import de.tobiasroeser.mill.vcs.version.VcsVersion
import mill._
import mill.define.{Command, Source, Sources, Target, Task}
import mill.eval.Evaluator
Expand All @@ -15,205 +27,12 @@ import mill.scalalib.publish._
import mill.modules.Jvm
import mill.define.SelectMode

case class Vcs(val name: String)

object Vcs {
def git = Vcs("git")

implicit val jsonify: upickle.default.ReadWriter[Vcs] = upickle.default.macroRW
}

import scala.util.Try

case class VcsState(
currentRevision: String,
lastTag: Option[String],
commitsSinceLastTag: Int,
dirtyHash: Option[String],
vcs: Option[Vcs]
) {

def format(
noTagFallback: String = "0.0.0",
countSep: String = "-",
commitCountPad: Byte = 0,
revSep: String = "-",
revHashDigits: Int = 6,
dirtySep: String = "-DIRTY",
dirtyHashDigits: Int = 8,
tagModifier: String => String = stripV,
untaggedSuffix: String = ""
): String = {
val versionPart = tagModifier(lastTag.getOrElse(noTagFallback))

val isUntagged = lastTag.isEmpty || commitsSinceLastTag > 0

val commitCountPart = if (isUntagged) {
s"$countSep${if (commitCountPad > 0) {
(10000000000000L + commitsSinceLastTag).toString().substring(14 - commitCountPad, 14)
} else if (commitCountPad == 0) commitsSinceLastTag
else ""}"
} else ""

val revisionPart = if (isUntagged) {
s"$revSep${currentRevision.take(revHashDigits)}"
} else ""

val dirtyPart = dirtyHash match {
case None => ""
case Some(d) => dirtySep + d.take(dirtyHashDigits)
}

val snapshotSuffix = if (isUntagged) untaggedSuffix else ""

s"$versionPart$commitCountPart$revisionPart$dirtyPart$snapshotSuffix"
}

/**
* By default we strip the leading v if a user uses it.
* Ex. v2.3.2 -> 2.3.2
* @param tag the tag to process
* @return either the stripped tag or the tag verbatim
*/
def stripV(tag: String): String =
tag match {
case t if t.startsWith("v") && Try(t.substring(1, 2).toInt).isSuccess =>
t.substring(1)
case t => t
}

@deprecated("Binary compatibility shim. Use other overload instead.", "mill-vcs-version after 0.2.0")
def format(
noTagFallback: String,
countSep: String,
commitCountPad: Byte,
revSep: String,
revHashDigits: Int,
dirtySep: String,
dirtyHashDigits: Int,
tagModifier: String => String
): String = format(
noTagFallback = noTagFallback,
countSep = countSep,
commitCountPad = commitCountPad,
revSep = revSep,
revHashDigits = revHashDigits,
dirtySep = dirtySep,
dirtyHashDigits = dirtyHashDigits,
tagModifier = tagModifier,
untaggedSuffix = ""
)
}

object VcsState {
implicit val jsonify: upickle.default.ReadWriter[VcsState] = upickle.default.macroRW
}

import scala.util.control.NonFatal
import mill.T
import mill.define.{Discover, ExternalModule, Input, Module, Task}
import mill.api.{Logger, Result}
import os.{CommandResult, SubprocessException}

trait VcsVersion extends Module {

def vcsBasePath: os.Path = build.millSourcePath

/**
* Calc a publishable version based on git tags and dirty state.
*
* @return A tuple of (the latest tag, the calculated version string)
*/
def vcsState: Input[VcsState] = T.input { calcVcsState(T.log) }

private[this] def calcVcsState(logger: Logger): VcsState = {
val curHeadRaw =
try {
Option(os.proc("git", "rev-parse", "HEAD").call(cwd = vcsBasePath).out.trim)
} catch {
case e: SubprocessException =>
logger.error(s"${vcsBasePath} is not a git repository.")
None
}

curHeadRaw match {
case None =>
VcsState("no-vcs", None, 0, None, None)

case curHead =>
// we have a proper git repo

val exactTag =
try {
curHead
.map(curHead =>
os.proc("git", "describe", "--exact-match", "--tags", "--always", curHead)
.call(cwd = vcsBasePath)
.out
.text()
.trim
)
.filter(_.nonEmpty)
} catch {
case NonFatal(_) => None
}

val lastTag: Option[String] = exactTag.orElse {
try {
Option(
os.proc("git", "describe", "--abbrev=0", "--tags")
.call()
.out
.text()
.trim()
)
.filter(_.nonEmpty)
} catch {
case NonFatal(_) => None
}
}

val commitsSinceLastTag =
if (exactTag.isDefined) 0
else {
curHead
.map { curHead =>
os.proc(
"git",
"rev-list",
curHead,
lastTag match {
case Some(tag) => Seq("--not", tag)
case _ => Seq()
},
"--count"
).call()
.out
.trim
.toInt
}
.getOrElse(0)
}

val dirtyHashCode: Option[String] = Option(os.proc("git", "diff").call().out.text().trim()).flatMap {
case "" => None
case s => Some(Integer.toHexString(s.hashCode))
}

new VcsState(
currentRevision = curHead.getOrElse(""),
lastTag = lastTag,
commitsSinceLastTag = commitsSinceLastTag,
dirtyHash = dirtyHashCode,
vcs = Option(Vcs.git)
)
}
}

}

object VcsVersion extends VcsVersion

object Settings {
val pomOrg = "com.lihaoyi"
val githubOrg = "com-lihaoyi"
Expand Down Expand Up @@ -601,8 +420,27 @@ trait MillCoursierModule extends CoursierModule {
)
}

trait MillMimaConfig extends Module {
trait MillMimaConfig extends mima.Mima {
def skipPreviousVersions: T[Seq[String]] = T(Seq.empty[String])
override def mimaPreviousVersions: T[Seq[String]] = Settings.mimaBaseVersions
override def mimaPreviousArtifacts: T[Agg[Dep]] = T {
Agg.from(
Settings.mimaBaseVersions
.filter(v => !skipPreviousVersions().contains(v))
.map(version =>
ivy"${pomSettings().organization}:${artifactId()}:${version}"
)
)
}
override def mimaExcludeAnnotations: T[Seq[String]] = Seq(
"mill.api.internal",
"mill.api.experimental"
)
override def mimaCheckDirection: Target[CheckDirection] = T { CheckDirection.Backward }
override def mimaBinaryIssueFilters: Target[Seq[ProblemFilter]] = T {
issueFilterByModule.getOrElse(this, Seq())
}
lazy val issueFilterByModule: Map[MillMimaConfig, Seq[ProblemFilter]] = Map()
}

/** A Module compiled with applied Mill-specific compiler plugins: mill-moduledefs. */
Expand Down Expand Up @@ -1856,8 +1694,55 @@ def launcher = T {
PathRef(outputPath)
}


def uploadToGithub(authKey: String) = T.command {
// never upload a bootstrapped version
val vcsState = VcsVersion.vcsState()
val label = vcsState.format()
if (label != millVersion()) sys.error("Modified mill version detected, aborting upload")
val releaseTag = vcsState.lastTag.getOrElse(sys.error(
"Incomplete git history. No tag found.\nIf on CI, make sure your git checkout job includes enough history."
))

if (releaseTag == label) {
// TODO: check if the tag already exists (e.g. because we created it manually) and do not fail
scalaj.http.Http(
s"https://api.github.com/repos/${Settings.githubOrg}/${Settings.githubRepo}/releases"
)
.postData(
ujson.write(
ujson.Obj(
"tag_name" -> releaseTag,
"name" -> releaseTag
)
)
)
.header("Authorization", "token " + authKey)
.asString
}

val exampleZips = Seq("example-1", "example-2", "example-3")
.map { example =>
os.copy(T.workspace / "example" / example, T.dest / example)
os.copy(launcher().path, T.dest / example / "mill")
os.proc("zip", "-r", T.dest / s"$example.zip", example).call(cwd = T.dest)
(T.dest / s"$example.zip", label + "-" + example + ".zip")
}

val zips = exampleZips ++ Seq(
(assembly().path, label + "-assembly"),
(launcher().path, label)
)

for ((zip, name) <- zips) {
upload.apply(
zip,
releaseTag,
name,
authKey,
Settings.githubOrg,
Settings.githubRepo
)
}
}

def validate(ev: Evaluator): Command[Unit] = T.command {
Expand Down