-
Notifications
You must be signed in to change notification settings - Fork 303
/
Bootstrap.scala
309 lines (267 loc) · 10.2 KB
/
Bootstrap.scala
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package coursier.cli.bootstrap
import java.io.{File, PrintStream}
import java.nio.file.Files
import java.util.concurrent.ExecutorService
import caseapp.core.RemainingArgs
import caseapp.core.app.CaseApp
import coursier.bootstrap.{Assembly, ClassLoaderContent, ClasspathEntry, LauncherBat}
import coursier.cli.fetch.Fetch
import coursier.cli.launch.{Launch, LaunchException}
import coursier.cli.native.NativeBuilder
import coursier.cli.resolve.{Resolve, ResolveException}
import coursier.core.{Artifact, Resolution}
import coursier.util.{Sync, Task}
import scala.concurrent.ExecutionContext
object Bootstrap extends CaseApp[BootstrapOptions] {
def createNativeBootstrap(
params: BootstrapParams,
files: Seq[File],
mainClass: String,
pool: ExecutorService,
nativeVersion: String
): Unit = {
val log: String => Unit =
if (params.sharedLaunch.resolve.output.verbosity >= 0)
s => Console.err.println(s)
else
_ => ()
val logger = params.sharedLaunch.resolve.output.logger()
val cache = params.sharedLaunch.resolve.cache.cache(pool, logger)
val repositories = params.sharedLaunch.resolve.repositories.repositories
val fetch = coursier.Fetch(cache)
.withRepositories(repositories)
val nativeVersion0 = params.nativeBootstrap.nativeShortVersionOpt.getOrElse(
nativeVersion
)
val builder = NativeBuilder.load(fetch, nativeVersion0)
builder.build(
mainClass,
files,
params.specific.output.toFile,
params.nativeBootstrap,
log,
params.sharedLaunch.resolve.output.verbosity
)
}
def task(
params: BootstrapParams,
pool: ExecutorService,
dependencyArgs: Seq[String],
userArgs: Seq[String],
stdout: PrintStream = System.out,
stderr: PrintStream = System.err
): Task[(Resolution, Seq[(Artifact, File)], String)] =
for {
t <- Fetch.task(params.sharedLaunch.fetch, pool, dependencyArgs, stdout, stderr)
(res, files) = t
mainClass <- {
params.sharedLaunch.mainClassOpt match {
case Some(c) =>
Task.point(c)
case None =>
Task.delay(Launch.mainClasses(files.map(_._2) ++ params.sharedLaunch.extraJars.map(_.toFile))).flatMap { m =>
if (params.sharedLaunch.resolve.output.verbosity >= 2)
System.err.println(
"Found main classes:\n" +
m.map { case ((vendor, title), mainClass) => s" $mainClass (vendor: $vendor, title: $title)\n" }.mkString +
"\n"
)
Launch.retainedMainClassOpt(m, res.rootDependencies.headOption) match {
case Some(c) =>
Task.point(c)
case None =>
Task.fail(new LaunchException.NoMainClassFound)
}
}
}
}
} yield (res, files, mainClass)
def run(options: BootstrapOptions, args: RemainingArgs): Unit = {
var pool: ExecutorService = null
// get options and dependencies from apps if any
val (options0, deps) = BootstrapParams(options).toEither.toOption.fold((options, args.all)) { initialParams =>
val initialRepositories = initialParams.sharedLaunch.resolve.repositories.repositories
val channels = initialParams.sharedLaunch.resolve.repositories.channels
pool = Sync.fixedThreadPool(initialParams.sharedLaunch.resolve.cache.parallel)
val cache = initialParams.sharedLaunch.resolve.cache.cache(pool, initialParams.sharedLaunch.resolve.output.logger())
val res = Resolve.handleApps(options, args.all, channels, initialRepositories, cache)(_.addApp(_))
res
}
val params = BootstrapParams(options0).toEither match {
case Left(errors) =>
for (err <- errors.toList)
System.err.println(err)
sys.exit(1)
case Right(params0) =>
params0
}
if (pool == null)
pool = Sync.fixedThreadPool(params.sharedLaunch.resolve.cache.parallel)
val ec = ExecutionContext.fromExecutorService(pool)
val output0 = params.specific.output
if (!params.specific.force && Files.exists(output0)) {
System.err.println(s"Error: $output0 already exists, use -f option to force erasing it.")
sys.exit(1)
}
val t = task(
params,
pool,
deps,
Nil
)
val (res, files, mainClass) = t.attempt.unsafeRun()(ec) match {
case Left(e: ResolveException) if params.sharedLaunch.resolve.output.verbosity <= 1 =>
System.err.println(e.message)
sys.exit(1)
case Left(e: coursier.error.FetchError) if params.sharedLaunch.resolve.output.verbosity <= 1 =>
System.err.println(e.getMessage)
sys.exit(1)
case Left(e: LaunchException.NoMainClassFound) if params.sharedLaunch.resolve.output.verbosity <= 1 =>
System.err.println("Cannot find default main class. Specify one with -M or --main-class.")
sys.exit(1)
case Left(e: LaunchException) if params.sharedLaunch.resolve.output.verbosity <= 1 =>
System.err.println(e.getMessage)
sys.exit(1)
case Left(e) => throw e
case Right(t0) =>
t0
}
val nativeVersion = res
.rootDependencies
.map(_.module.name.value)
.flatMap { name =>
val elems = name.split('_')
if (elems.length >= 3)
Seq(elems(elems.length - 2))
else
Nil
}
.filter(_.startsWith("native"))
.map(_.stripPrefix("native"))
.distinct
.map(coursier.core.Version(_))
.sorted
.lastOption
.map(_.repr)
.getOrElse {
// FIXME Throw here?
"0.3"
}
// kind of lame, I know
var wroteBat = false
if (params.sharedLaunch.resolve.output.verbosity >= 1)
System.err.println(s"Using scala-native version $nativeVersion")
if (params.sharedLaunch.resolve.dependency.native)
createNativeBootstrap(params, files.map(_._2), mainClass, pool, nativeVersion)
else {
val (asFiles, asUrls) = files.partition {
case (a, _) =>
params.specific.assembly || params.specific.standalone || params.specific.hybrid || (params.specific.embedFiles && a.url.startsWith("file:"))
}
val files0 = asFiles.map(_._2)
val urls = asUrls.map(_._1.url)
if (params.specific.createBatFile && !params.specific.force && Files.exists(params.specific.batOutput)) {
System.err.println(s"Error: ${params.specific.batOutput} already exists, use -f option to force erasing it.")
sys.exit(1)
}
val javaOptions =
if (params.specific.assembly)
params.specific.javaOptions ++ params.sharedLaunch.properties.map { case (k, v) => s"-D$k=$v" }
else
params.specific.javaOptions
if (params.specific.assembly)
Assembly.create(
files0,
javaOptions,
mainClass,
output0,
rules = params.specific.assemblyRules,
withPreamble = params.specific.withPreamble,
disableJarChecking = params.specific.disableJarCheckingOpt.getOrElse(false)
)
else {
val artifacts = files.toMap
val (done, sharedFiles) =
params.sharedLaunch.sharedLoader.loaderNames.foldLeft((Set.empty[String], Map.empty[String, (Seq[String], Seq[File])])) {
case ((done, acc), name) =>
val deps = params.sharedLaunch.sharedLoader.loaderDependencies.getOrElse(name, Nil)
val subRes = res.subset(deps)
val m = coursier.Artifacts.artifacts0(
subRes,
params.sharedLaunch.artifact.classifiers,
Option(params.sharedLaunch.artifact.mainArtifacts).map(x => x),
Option(params.sharedLaunch.artifact.artifactTypes),
params.sharedLaunch.resolve.classpathOrder
).map(_._3)
val m0 = m.filter(a => !done(a.url)).map(a => a.url -> artifacts.getOrElse(a, sys.error("should not happen")))
val done0 = done ++ m0.map(_._1)
val (subUrls, subFiles) =
if (params.specific.standalone || params.specific.hybrid)
(Nil, m0.map(_._2))
else
(m0.map(_._1), Nil)
val updatedAcc = acc + (name -> ((subUrls, subFiles)))
(done0, updatedAcc)
}
val parents = params.sharedLaunch.sharedLoader.loaderNames.map { t =>
val e = sharedFiles.get(t)
val urls = e.map(_._1).getOrElse(Nil).map { url =>
ClasspathEntry.Url(url)
}
val files = e.map(_._2).getOrElse(Nil).map { f =>
ClasspathEntry.Resource(
f.getName,
f.lastModified(),
Files.readAllBytes(f.toPath)
)
}
ClassLoaderContent(
urls ++ files,
t
)
}
val main = {
val doneFiles = sharedFiles.toSeq.flatMap(_._2._2).toSet
val urls0 = urls.filterNot(done).map { url =>
ClasspathEntry.Url(url)
}
val files = files0.filterNot(doneFiles).map { f =>
ClasspathEntry.Resource(
f.getName,
f.lastModified(),
Files.readAllBytes(f.toPath)
)
}
ClassLoaderContent(urls0 ++ files)
}
val content = parents :+ main
coursier.bootstrap.Bootstrap.create(
content,
mainClass,
output0,
javaOptions,
javaProperties = params.sharedLaunch.properties,
deterministic = params.specific.deterministicOutput,
withPreamble = params.specific.withPreamble,
proguarded = params.specific.proguarded,
hybridAssembly = params.specific.hybrid,
disableJarChecking = params.specific.disableJarCheckingOpt.getOrElse[Boolean] {
coursier.bootstrap.Bootstrap.defaultDisableJarChecking(content)
}
)
}
if (params.specific.createBatFile) {
LauncherBat.create(
params.specific.batOutput,
javaOptions
)
wroteBat = true
}
}
if (params.sharedLaunch.resolve.output.verbosity >= 0) {
System.err.println(s"Wrote $output0")
if (wroteBat)
System.err.println(s"Wrote ${params.specific.batOutput}")
}
}
}