forked from VirtusLab/scala-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.sc
228 lines (214 loc) · 7.13 KB
/
publish.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.0`
import $ivy.`org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r`
import $file.settings, settings.{PublishLocalNoFluff, workspaceDirName}
import de.tobiasroeser.mill.vcs.version._
import mill._, scalalib._
import org.eclipse.jgit.api.Git
import java.nio.charset.Charset
import scala.concurrent.duration._
import scala.jdk.CollectionConverters._
lazy val (ghOrg, ghName) = {
def default = ("VirtusLab", "scala-cli")
val isCI = System.getenv("CI") != null
if (isCI) {
val repos = {
var git: Git = null
try {
git = Git.open(os.pwd.toIO)
git.remoteList().call().asScala.toVector
}
finally
if (git != null)
git.close()
}
val reposByName = repos.flatMap(r =>
r.getURIs.asScala.headOption.map(_.toASCIIString).toSeq.map((r.getName, _))
)
val repoUrlOpt =
if (reposByName.length == 1)
reposByName.headOption.map(_._2)
else {
val m = reposByName.toMap
m.get("origin").orElse(m.get("upstream"))
}
repoUrlOpt match {
case Some(url) if url.startsWith("https://github.com/") =>
url.stripPrefix("https://github.com/").stripSuffix(".git").split("/", 2) match {
case Array(org, name) => (org, name)
case Array(_) =>
System.err.println(
s"Warning: git remote URL $url doesn't look like a GitHub URL, using default GitHub org and name"
)
default
}
case Some(url) =>
System.err.println(
s"Warning: git remote URL $url doesn't look like a GitHub URL, using default GitHub org and name"
)
default
case _ =>
System.err.println("Warning: no git remote found, using default GitHub org and name")
default
}
}
else
default
}
private def computePublishVersion(state: VcsState, simple: Boolean): String =
if (state.commitsSinceLastTag > 0)
if (simple) {
val versionOrEmpty = state.lastTag
.filter(_ != "latest")
.filter(_ != "nightly")
.map(_.stripPrefix("v"))
.flatMap { tag =>
if (simple) {
val idx = tag.lastIndexOf(".")
if (idx >= 0)
Some(
tag.take(idx + 1) +
(tag.drop(idx + 1).replaceAll("-RC.", "").toInt + 1).toString +
"-SNAPSHOT"
)
else
None
}
else {
val idx = tag.indexOf("-")
if (idx >= 0) Some(tag.take(idx) + "+" + tag.drop(idx + 1) + "-SNAPSHOT")
else None
}
}
.getOrElse("0.0.1-SNAPSHOT")
Some(versionOrEmpty)
.filter(_.nonEmpty)
.getOrElse(state.format())
}
else {
val rawVersion = os.proc("git", "describe", "--tags").call().out.trim()
.stripPrefix("v")
.replace("latest", "0.0.0")
.replace("nightly", "0.0.0")
val idx = rawVersion.indexOf("-")
if (idx >= 0) rawVersion.take(idx) + "-" + rawVersion.drop(idx + 1) + "-SNAPSHOT"
else rawVersion
}
else
state
.lastTag
.getOrElse(state.format())
.stripPrefix("v")
def finalPublishVersion = {
val isCI = System.getenv("CI") != null
if (isCI)
T.persistent {
val state = VcsVersion.vcsState()
computePublishVersion(state, simple = false)
}
else
T {
val state = VcsVersion.vcsState()
computePublishVersion(state, simple = true)
}
}
def organization = "org.virtuslab.scala-cli"
trait ScalaCliPublishModule extends PublishModule with PublishLocalNoFluff {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = organization,
url = s"https://github.com/$ghOrg/$ghName",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github(ghOrg, ghName),
developers = Seq(
Developer("alexarchambault", "Alex Archambault", "https://github.com/alexarchambault"),
Developer("lwronski", "Łukasz Wroński", "https://github.com/lwronski"),
Developer("romanowski", "Krzysztof Romanowski", "https://github.com/romanowski"),
Developer("Gedochao", "Piotr Chabelski", "https://github.com/Gedochao"),
Developer("MaciejG604", "Maciej Gajek", "https://github.com/MaciejG604")
)
)
def publishVersion =
finalPublishVersion()
override def sourceJar = T {
import mill.util.Jvm.createJar
val allSources0 = allSources().map(_.path).filter(os.exists).toSet
createJar(
allSources0 ++ resources().map(_.path).filter(os.exists),
manifest(),
(input, relPath) =>
!allSources0(input) ||
(!relPath.segments.contains(".scala") && !relPath.segments.contains(workspaceDirName))
)
}
}
def publishSonatype(
data: Seq[PublishModule.PublishData],
log: mill.api.Logger
): Unit = {
val credentials = sys.env("SONATYPE_USERNAME") + ":" + sys.env("SONATYPE_PASSWORD")
val pgpPassword = sys.env("PGP_PASSWORD")
val timeout = 10.minutes
val artifacts = data.map {
case PublishModule.PublishData(a, s) =>
(s.map { case (p, f) => (p.path, f) }, a)
}
val isRelease = {
val versions = artifacts.map(_._2.version).toSet
val set = versions.map(!_.endsWith("-SNAPSHOT"))
assert(
set.size == 1,
s"Found both snapshot and non-snapshot versions: ${versions.toVector.sorted.mkString(", ")}"
)
set.head
}
val publisher = new scalalib.publish.SonatypePublisher(
uri = "https://oss.sonatype.org/service/local",
snapshotUri = "https://oss.sonatype.org/content/repositories/snapshots",
credentials = credentials,
signed = true,
// format: off
gpgArgs = Seq(
"--detach-sign",
"--batch=true",
"--yes",
"--pinentry-mode", "loopback",
"--passphrase", pgpPassword,
"--armor",
"--use-agent"
),
// format: on
readTimeout = timeout.toMillis.toInt,
connectTimeout = timeout.toMillis.toInt,
log = log,
workspace = os.pwd,
env = sys.env,
awaitTimeout = timeout.toMillis.toInt,
stagingRelease = isRelease
)
publisher.publishAll(isRelease, artifacts: _*)
}
// from https://github.com/sbt/sbt-ci-release/blob/35b3d02cc6c247e1bb6c10dd992634aa8b3fe71f/plugin/src/main/scala/com/geirsson/CiReleasePlugin.scala#L33-L39
def isTag: Boolean =
Option(System.getenv("TRAVIS_TAG")).exists(_.nonEmpty) ||
Option(System.getenv("CIRCLE_TAG")).exists(_.nonEmpty) ||
Option(System.getenv("CI_COMMIT_TAG")).exists(_.nonEmpty) ||
Option(System.getenv("BUILD_SOURCEBRANCH"))
.orElse(Option(System.getenv("GITHUB_REF")))
.exists(_.startsWith("refs/tags"))
def shouldPublish = T.persistent {
val isSnapshot = finalPublishVersion().endsWith("SNAPSHOT")
// not a snapshot, not a tag: let the tag job do the publishing
isSnapshot || isTag
}
def setShouldPublish() = T.command {
val envFile = System.getenv("GITHUB_ENV")
if (envFile == null)
sys.error("GITHUB_ENV not set")
val charSet = Charset.defaultCharset()
val nl = System.lineSeparator()
os.write.append(
os.Path(envFile, os.pwd),
s"SHOULD_PUBLISH=${shouldPublish()}$nl".getBytes(charSet)
)
}