forked from sbt-android-mill/sbt-dependency-manager
/
Plugin.scala
731 lines (721 loc) · 45.7 KB
/
Plugin.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/**
* sbt-dependency-manager - fetch and merge byte code and source code jars, align broken sources within jars.
* For example, it is allow easy source code lookup for IDE while developing SBT plugins (not only).
*
* Copyright (c) 2012-2013 Alexey Aksenov ezh@ezh.msk.ru
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sbt.dependency.manager
import java.io.BufferedOutputStream
import java.io.ByteArrayOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.PrintWriter
import java.util.Date
import java.util.Properties
import java.util.jar.JarInputStream
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipException
import scala.Option.option2Iterable
import scala.collection.mutable.HashSet
import sbt._
import sbt.Keys._
import sbt.{ Keys => sk }
import sbt.dependency.manager.Keys._
import sbt.dependency.manager.{ Keys => dk }
import xsbti.AppConfiguration
/**
* sbt-dependency-manager plugin entry
*/
object Plugin extends sbt.Plugin {
implicit def option2rich[T](option: Option[T]): RichOption[T] = new RichOption(option)
def logPrefix(name: String) = "[Dep manager:%s] ".format(name)
lazy val defaultSettings = inConfig(Keys.DependencyConf)(Seq(
dependencyEnableCustom := true,
dependencyPackPath <<= (target, normalizedName) map { (target, name) => target / (name + "-development.jar") },
dependencyClasspathFilter <<= (dependencyLookupClasspath) map (cp =>
cp.flatMap(_.get(moduleID.key)).foldLeft(moduleFilter(NothingFilter, NothingFilter, NothingFilter))((acc, m) => acc |
moduleFilter(GlobFilter(m.organization), GlobFilter(m.name), GlobFilter(m.revision)))),
dependencyFilter <<= dependencyClasspathFilter map (dcf => Some(dcf -
moduleFilter(organization = GlobFilter("org.scala-lang"), name = GlobFilter("scala-library")))),
dependencyIgnoreConfiguration := true,
dependencyLookupClasspath <<= Classpaths.concatDistinct(externalDependencyClasspath in Compile, externalDependencyClasspath in Test),
dependencyOutput <<= (target in ThisProject) { path => Some(path / "deps") },
dependencyPluginInfo <<= dependencyPluginInfoTask,
dependencyResourceFilter := resourceFilter,
dependencySkipResolved := true,
// add the empty classifier ""
transitiveClassifiers in Global := Seq("", Artifact.SourceClassifier, Artifact.DocClassifier))) ++
// global settings
Seq(
dependencyTaskPack <<= dependencyTaskPackTask,
dependencyTaskPackWithArtifact <<= dependencyTaskPackWithArtifactTask,
dependencyTaskFetch <<= dependencyTaskFetchTask,
dependencyTaskFetchAlign <<= dependencyTaskFetchAlignTask,
dependencyTaskFetchWithSources <<= dependencyTaskFetchWithSourcesTask)
/** Show plugin information */
def dependencyPluginInfoTask = (state, streams, thisProjectRef) map { (state, streams, thisProjectRef) =>
val extracted: Extracted = Project.extract(state)
val thisScope = Load.projectScope(thisProjectRef).copy(config = Select(DependencyConf))
val name = (sbt.Keys.name in thisScope get extracted.structure.data) getOrElse thisProjectRef.project
Option(getClass().getClassLoader().getResourceAsStream("version-sbt-dependency-manager.properties")) match {
case Some(stream) =>
val properties = new Properties()
properties.load(stream)
val date = new Date(properties.getProperty("build").toLong * 1000)
streams.log.info(logPrefix(name) + "Name: " + properties.getProperty("name"))
streams.log.info(logPrefix(name) + "Version: " + properties.getProperty("version"))
streams.log.info(logPrefix(name) + "Build: " + date + " (" + properties.getProperty("build") + ")")
case None =>
streams.log.error(logPrefix(name) + "Dependency Mananger plugin information not found.")
}
}
/** Implementation of dependency-pack */
def dependencyTaskPackTask =
(classifiersModule in updateSbtClassifiers, dependencyPackPath in DependencyConf, dependencyFilter in DependencyConf,
dependencyLookupClasspath in DependencyConf, ivySbt, streams, state, thisProjectRef) map { (origClassifiersModule, pathPack,
dependencyFilter, dependencyClasspath, ivySbt, streams, state, thisProjectRef) =>
val extracted: Extracted = Project.extract(state)
val thisScope = Load.projectScope(thisProjectRef).copy(config = Select(DependencyConf))
val name = (sbt.Keys.name in thisScope get extracted.structure.data) getOrElse thisProjectRef.project
val output = dependencyOutput in thisScope get extracted.structure.data getOrThrow "dependencyOutput is undefined"
output match {
case Some(dependencyOutput) =>
streams.log.info(logPrefix(name) + "Fetch dependencies and align to consolidated jar.")
val appConfiguration = sk.appConfiguration in thisScope get extracted.structure.data getOrThrow "appConfiguration is undefined"
val ivyLoggingLevel = sk.ivyLoggingLevel in thisScope get extracted.structure.data getOrThrow "ivyLoggingLevel is undefined"
val ivyScala = sk.ivyScala in thisScope get extracted.structure.data getOrThrow "ivyScala is undefined"
val pathTarget = sk.target in thisScope get extracted.structure.data getOrThrow "pathTarget is undefined"
val updateConfiguration = sk.updateConfiguration in thisScope get extracted.structure.data getOrThrow "updateConfiguration is undefined"
val dependencyEnableCustom = dk.dependencyEnableCustom in thisScope get extracted.structure.data getOrThrow "dependencyEnableCustom is undefined"
val dependencyIgnoreConfiguration = dk.dependencyIgnoreConfiguration in thisScope get extracted.structure.data getOrThrow "dependencyIgnoreConfiguration is undefined"
val dependencyResourceFilter = dk.dependencyResourceFilter in thisScope get extracted.structure.data getOrThrow "dependencyResourceFilter is undefined"
val dependencySkipResolved = dk.dependencySkipResolved in thisScope get extracted.structure.data getOrThrow "dependencySkipResolved is undefined"
val libraryDependenciesCompile = sbt.Keys.libraryDependencies in thisScope in Compile get extracted.structure.data getOrThrow "libraryDependencies in Compile is undefined"
val libraryDependenciesTest = sbt.Keys.libraryDependencies in thisScope in Test get extracted.structure.data getOrThrow "libraryDependencies in Test is undefined"
val libraryDependencies = (libraryDependenciesCompile ++ libraryDependenciesTest).distinct
val argument = TaskArgument(appConfiguration, ivyLoggingLevel, ivySbt, ivyScala, libraryDependencies, name,
origClassifiersModule, new UpdateConfiguration(updateConfiguration.retrieve, true, ivyLoggingLevel),
pathPack, dependencyOutput, pathTarget, streams, dependencyEnableCustom, None, true, dependencyClasspath,
dependencyFilter, dependencyIgnoreConfiguration, dependencyResourceFilter, dependencySkipResolved)
commonFetchTask(argument, doFetchWithSources)
case None =>
streams.log.info(logPrefix(name) + "Fetch operation disabled")
}
() // Returns Unit. Return type isn't defined explicitly because it is different for different SBT versions.
}
/** Implementation of dependency-pack-with-artifact */
def dependencyTaskPackWithArtifactTask =
(classifiersModule in updateSbtClassifiers, dependencyPackPath in DependencyConf,
dependencyFilter in DependencyConf, dependencyLookupClasspath in DependencyConf,
ivySbt, packageBin in Compile, state, streams, thisProjectRef) map { (origClassifiersModule, pathPack,
dependencyFilter, dependencyClasspath, ivySbt, packageBin, state, streams, thisProjectRef) =>
val extracted: Extracted = Project.extract(state)
val thisScope = Load.projectScope(thisProjectRef).copy(config = Select(DependencyConf))
val name = (sbt.Keys.name in thisScope get extracted.structure.data) getOrElse thisProjectRef.project
val output = dependencyOutput in thisScope get extracted.structure.data getOrThrow "dependencyOutput is undefined"
output match {
case Some(dependencyOutput) =>
streams.log.info(logPrefix(name) + "Fetch dependencies with artifact and align to consolidated jar.")
val appConfiguration = sk.appConfiguration in thisScope get extracted.structure.data getOrThrow "appConfiguration is undefined"
val ivyLoggingLevel = sk.ivyLoggingLevel in thisScope get extracted.structure.data getOrThrow "ivyLoggingLevel is undefined"
val ivyScala = sk.ivyScala in thisScope get extracted.structure.data getOrThrow "ivyScala is undefined"
val pathTarget = sk.target in thisScope get extracted.structure.data getOrThrow "pathTarget is undefined"
val updateConfiguration = sk.updateConfiguration in thisScope get extracted.structure.data getOrThrow "updateConfiguration is undefined"
val dependencyEnableCustom = dk.dependencyEnableCustom in thisScope get extracted.structure.data getOrThrow "dependencyEnableCustom is undefined"
val dependencyIgnoreConfiguration = dk.dependencyIgnoreConfiguration in thisScope get extracted.structure.data getOrThrow "dependencyIgnoreConfiguration is undefined"
val dependencyResourceFilter = dk.dependencyResourceFilter in thisScope get extracted.structure.data getOrThrow "dependencyResourceFilter is undefined"
val dependencySkipResolved = dk.dependencySkipResolved in thisScope get extracted.structure.data getOrThrow "dependencySkipResolved is undefined"
val libraryDependenciesCompile = sbt.Keys.libraryDependencies in thisScope in Compile get extracted.structure.data getOrThrow "libraryDependencies in Compile is undefined"
val libraryDependenciesTest = sbt.Keys.libraryDependencies in thisScope in Test get extracted.structure.data getOrThrow "libraryDependencies in Test is undefined"
val libraryDependencies = (libraryDependenciesCompile ++ libraryDependenciesTest).distinct
val argument = TaskArgument(appConfiguration, ivyLoggingLevel, ivySbt, ivyScala, libraryDependencies, name,
origClassifiersModule, new UpdateConfiguration(updateConfiguration.retrieve, true, ivyLoggingLevel),
pathPack, dependencyOutput, pathTarget, streams, dependencyEnableCustom, Some(packageBin), true, dependencyClasspath,
dependencyFilter, dependencyIgnoreConfiguration, dependencyResourceFilter, dependencySkipResolved)
commonFetchTask(argument, doFetchWithSources)
case None =>
streams.log.info(logPrefix(name) + "Fetch operation disabled")
}
() // Returns Unit. Return type isn't defined explicitly because it is different for different SBT versions.
}
/** Implementation of dependency-fetch-align */
def dependencyTaskFetchAlignTask =
(classifiersModule in updateSbtClassifiers, dependencyPackPath in DependencyConf, dependencyFilter in DependencyConf,
dependencyLookupClasspath in DependencyConf, ivySbt, state, streams, thisProjectRef) map { (origClassifiersModule, pathPack,
dependencyFilter, dependencyClasspath, ivySbt, state, streams, thisProjectRef) =>
val extracted: Extracted = Project.extract(state)
val thisScope = Load.projectScope(thisProjectRef).copy(config = Select(DependencyConf))
val name = (sbt.Keys.name in thisScope get extracted.structure.data) getOrElse thisProjectRef.project
val output = dependencyOutput in thisScope get extracted.structure.data getOrThrow "dependencyOutput is undefined"
output match {
case Some(dependencyOutput) =>
streams.log.info(logPrefix(name) + "Fetch dependencies and align")
val appConfiguration = sk.appConfiguration in thisScope get extracted.structure.data getOrThrow "appConfiguration is undefined"
val ivyLoggingLevel = sk.ivyLoggingLevel in thisScope get extracted.structure.data getOrThrow "ivyLoggingLevel is undefined"
val ivyScala = sk.ivyScala in thisScope get extracted.structure.data getOrThrow "ivyScala is undefined"
val pathTarget = sk.target in thisScope get extracted.structure.data getOrThrow "pathTarget is undefined"
val updateConfiguration = sk.updateConfiguration in thisScope get extracted.structure.data getOrThrow "updateConfiguration is undefined"
val dependencyEnableCustom = dk.dependencyEnableCustom in thisScope get extracted.structure.data getOrThrow "dependencyEnableCustom is undefined"
val dependencyIgnoreConfiguration = dk.dependencyIgnoreConfiguration in thisScope get extracted.structure.data getOrThrow "dependencyIgnoreConfiguration is undefined"
val dependencyResourceFilter = dk.dependencyResourceFilter in thisScope get extracted.structure.data getOrThrow "dependencyResourceFilter is undefined"
val dependencySkipResolved = dk.dependencySkipResolved in thisScope get extracted.structure.data getOrThrow "dependencySkipResolved is undefined"
val libraryDependenciesCompile = sbt.Keys.libraryDependencies in thisScope in Compile get extracted.structure.data getOrThrow "libraryDependencies in Compile is undefined"
val libraryDependenciesTest = sbt.Keys.libraryDependencies in thisScope in Test get extracted.structure.data getOrThrow "libraryDependencies in Test is undefined"
val libraryDependencies = (libraryDependenciesCompile ++ libraryDependenciesTest).distinct
val argument = TaskArgument(appConfiguration, ivyLoggingLevel, ivySbt, ivyScala, libraryDependencies, name,
origClassifiersModule, new UpdateConfiguration(updateConfiguration.retrieve, true, ivyLoggingLevel),
pathPack, dependencyOutput, pathTarget, streams, dependencyEnableCustom, None, false, dependencyClasspath,
dependencyFilter, dependencyIgnoreConfiguration, dependencyResourceFilter, dependencySkipResolved)
commonFetchTask(argument, doFetchAlign)
case None =>
streams.log.info(logPrefix(name) + "Fetch operation disabled")
}
() // Returns Unit. Return type isn't defined explicitly because it is different for different SBT versions.
}
/** Implementation of dependency-fetch-with-sources */
def dependencyTaskFetchWithSourcesTask =
(classifiersModule in updateSbtClassifiers, dependencyPackPath in DependencyConf, dependencyFilter in DependencyConf,
dependencyLookupClasspath in DependencyConf, ivySbt, state, streams, thisProjectRef) map { (origClassifiersModule, pathPack,
dependencyFilter, dependencyClasspath, ivySbt, state, streams, thisProjectRef) =>
val extracted: Extracted = Project.extract(state)
val thisScope = Load.projectScope(thisProjectRef).copy(config = Select(DependencyConf))
val name = (sbt.Keys.name in thisScope get extracted.structure.data) getOrElse thisProjectRef.project
val output = dependencyOutput in thisScope get extracted.structure.data getOrThrow "dependencyOutput is undefined"
output match {
case Some(dependencyOutput) =>
streams.log.info(logPrefix(name) + "Fetch dependencies with source code")
val appConfiguration = sk.appConfiguration in thisScope get extracted.structure.data getOrThrow "appConfiguration is undefined"
val ivyLoggingLevel = sk.ivyLoggingLevel in thisScope get extracted.structure.data getOrThrow "ivyLoggingLevel is undefined"
val ivyScala = sk.ivyScala in thisScope get extracted.structure.data getOrThrow "ivyScala is undefined"
val pathTarget = sk.target in thisScope get extracted.structure.data getOrThrow "pathTarget is undefined"
val updateConfiguration = sk.updateConfiguration in thisScope get extracted.structure.data getOrThrow "updateConfiguration is undefined"
val dependencyEnableCustom = dk.dependencyEnableCustom in thisScope get extracted.structure.data getOrThrow "dependencyEnableCustom is undefined"
val dependencyIgnoreConfiguration = dk.dependencyIgnoreConfiguration in thisScope get extracted.structure.data getOrThrow "dependencyIgnoreConfiguration is undefined"
val dependencyResourceFilter = dk.dependencyResourceFilter in thisScope get extracted.structure.data getOrThrow "dependencyResourceFilter is undefined"
val dependencySkipResolved = dk.dependencySkipResolved in thisScope get extracted.structure.data getOrThrow "dependencySkipResolved is undefined"
val libraryDependenciesCompile = sbt.Keys.libraryDependencies in thisScope in Compile get extracted.structure.data getOrThrow "libraryDependencies in Compile is undefined"
val libraryDependenciesTest = sbt.Keys.libraryDependencies in thisScope in Test get extracted.structure.data getOrThrow "libraryDependencies in Test is undefined"
val libraryDependencies = (libraryDependenciesCompile ++ libraryDependenciesTest).distinct
val argument = TaskArgument(appConfiguration, ivyLoggingLevel, ivySbt, ivyScala, libraryDependencies, name,
origClassifiersModule, new UpdateConfiguration(updateConfiguration.retrieve, true, ivyLoggingLevel),
pathPack, dependencyOutput, pathTarget, streams,
dependencyEnableCustom, None, false, dependencyClasspath,
dependencyFilter, dependencyIgnoreConfiguration, dependencyResourceFilter, dependencySkipResolved)
commonFetchTask(argument, doFetchWithSources)
case None =>
streams.log.info(logPrefix(name) + "Fetch operation disabled")
}
() // Returns Unit. Return type isn't defined explicitly because it is different for different SBT versions.
}
/** Implementation of dependency-fetch */
def dependencyTaskFetchTask =
(classifiersModule in updateSbtClassifiers, dependencyPackPath in DependencyConf, dependencyFilter in DependencyConf,
dependencyLookupClasspath in DependencyConf, ivySbt, state, streams, thisProjectRef) map {
(origClassifiersModule, pathPack, dependencyFilter, dependencyClasspath, ivySbt, state, streams, thisProjectRef) =>
val extracted: Extracted = Project.extract(state)
val thisScope = Load.projectScope(thisProjectRef).copy(config = Select(DependencyConf))
val name = (sbt.Keys.name in thisScope get extracted.structure.data) getOrElse thisProjectRef.project
val output = dependencyOutput in thisScope get extracted.structure.data getOrThrow "dependencyOutput is undefined"
output match {
case Some(dependencyOutput) =>
streams.log.info(logPrefix(name) + "Fetch dependencies")
val appConfiguration = sk.appConfiguration in thisScope get extracted.structure.data getOrThrow "appConfiguration is undefined"
val ivyLoggingLevel = sk.ivyLoggingLevel in thisScope get extracted.structure.data getOrThrow "ivyLoggingLevel is undefined"
val ivyScala = sk.ivyScala in thisScope get extracted.structure.data getOrThrow "ivyScala is undefined"
val pathTarget = sk.target in thisScope get extracted.structure.data getOrThrow "pathTarget is undefined"
val updateConfiguration = sk.updateConfiguration in thisScope get extracted.structure.data getOrThrow "updateConfiguration is undefined"
val dependencyEnableCustom = dk.dependencyEnableCustom in thisScope get extracted.structure.data getOrThrow "dependencyEnableCustom is undefined"
val dependencyIgnoreConfiguration = dk.dependencyIgnoreConfiguration in thisScope get extracted.structure.data getOrThrow "dependencyIgnoreConfiguration is undefined"
val dependencyResourceFilter = dk.dependencyResourceFilter in thisScope get extracted.structure.data getOrThrow "dependencyResourceFilter is undefined"
val dependencySkipResolved = dk.dependencySkipResolved in thisScope get extracted.structure.data getOrThrow "dependencySkipResolved is undefined"
val libraryDependenciesCompile = sbt.Keys.libraryDependencies in thisScope in Compile get extracted.structure.data getOrThrow "libraryDependencies in Compile is undefined"
val libraryDependenciesTest = sbt.Keys.libraryDependencies in thisScope in Test get extracted.structure.data getOrThrow "libraryDependencies in Test is undefined"
val libraryDependencies = (libraryDependenciesCompile ++ libraryDependenciesTest).distinct
val argument = TaskArgument(appConfiguration, ivyLoggingLevel, ivySbt, ivyScala, libraryDependencies, name,
origClassifiersModule, new UpdateConfiguration(updateConfiguration.retrieve, true, ivyLoggingLevel),
pathPack, dependencyOutput, pathTarget, streams, dependencyEnableCustom, None, false, dependencyClasspath,
dependencyFilter, dependencyIgnoreConfiguration, dependencyResourceFilter, dependencySkipResolved)
commonFetchTask(argument, doFetch)
case None =>
streams.log.info(logPrefix(name) + "Fetch operation disabled")
}
() // Returns Unit. Return type isn't defined explicitly because it is different for different SBT versions.
}
/**
* Dependency resource filter
* It drops META-INF/ .SF .DSA .RSA files by default
*/
def resourceFilter(entry: ZipEntry): Boolean =
Seq("META-INF/.*\\.SF", "META-INF/.*\\.DSA", "META-INF/.*\\.RSA").find(entry.getName().toUpperCase().matches).nonEmpty
/** Repack sequence of jar artifacts */
protected def align(arg: TaskArgument, moduleTag: String, code: File, sources: File, targetDirectory: File, resourceFilter: ZipEntry => Boolean, s: TaskStreams,
alignEntries: HashSet[String] = HashSet[String](), output: JarOutputStream = null): Unit = {
if (!targetDirectory.exists())
if (!targetDirectory.mkdirs())
return s.log.error(logPrefix(arg.name) + "Unable to create " + targetDirectory)
val target = new File(targetDirectory, code.getName)
if (output == null) {
s.log.info(logPrefix(arg.name) + "Fetch and align " + moduleTag)
s.log.debug(logPrefix(arg.name) + "Save result to " + target.getAbsoluteFile())
} else
s.log.info(logPrefix(arg.name) + "Fetch and align " + moduleTag + ", target: consolidated jar.")
// align
var jarCode: JarInputStream = null
var jarSources: JarInputStream = null
var jarTarget: JarOutputStream = Option(output) getOrElse null
try {
jarCode = new JarInputStream(new FileInputStream(code))
jarSources = new JarInputStream(new FileInputStream(sources))
if (jarTarget == null && output == null) {
if (target.exists())
if (!target.delete()) {
try {
jarCode.close
jarSources.close
} catch {
case e: Throwable =>
}
return s.log.error(logPrefix(arg.name) + "Unable to delete " + target)
}
jarTarget = try {
new JarOutputStream(new BufferedOutputStream(new FileOutputStream(target, true)), jarCode.getManifest())
} catch {
case e: NullPointerException =>
s.log.warn(logPrefix(arg.name) + code + " has broken manifest")
new JarOutputStream(new BufferedOutputStream(new FileOutputStream(target, true)))
}
}
// copy across all entries from the original code jar
copy(arg, alignEntries, jarCode, jarTarget, resourceFilter, s)
// copy across all entries from the original sources jar
copy(arg, alignEntries, jarSources, jarTarget, resourceFilter, s)
} catch {
case e: Throwable =>
s.log.error(logPrefix(arg.name) + "Unable to align: " + e.getClass().getName() + " " + e.getMessage())
} finally {
if (jarTarget != null && output == null) {
jarTarget.flush()
jarTarget.close()
}
if (jarCode != null)
jarCode.close()
if (jarSources != null)
jarSources.close()
}
}
/** Common part for all sbt-dependency-manager tasks */
protected def commonFetchTask(arg: TaskArgument, userFunction: (TaskArgument, Seq[(sbt.ModuleID, File)], Seq[(sbt.ModuleID, File)]) => Unit): UpdateReport =
synchronized {
Classpaths.withExcludes(arg.pathTarget, arg.origClassifiersModule.classifiers, Defaults.lock(arg.appConfiguration)) { excludes =>
import arg.origClassifiersModule.{ id => origClassifiersModuleID, modules => origClassifiersModuleDeps }
arg.streams.log.debug(logPrefix(arg.name) + "Fetch dependencies to " + arg.pathDependency)
if (arg.dependencyPack)
arg.streams.log.info(logPrefix(arg.name) + "Create consolidated jar " + arg.pathPack)
// do default update-sbt-classifiers with libDeps
val libDeps = arg.dependencyClasspath.flatMap(_.get(moduleID.key))
val extClassifiersModuleDeps = {
val all = arg.dependencyFilter match {
case Some(filter) => (origClassifiersModuleDeps ++ libDeps).filter(filter)
case None => (origClassifiersModuleDeps ++ libDeps)
}
if (arg.dependencyIgnoreConfiguration)
all.map(_.copy(configurations = None))
else
all
}
arg.streams.log.debug(logPrefix(arg.name) + "Detected dependencies: " + extClassifiersModuleDeps.mkString(","))
// skip dependency that already have explicit artifacts which points to local resources
val extClassifiersModuleDepsFiltered = {
if (arg.dependencySkipResolved)
extClassifiersModuleDeps.filterNot(moduleId =>
arg.libraryDependencies.exists(id =>
id.name == moduleId.name && id.organization == moduleId.organization && id.revision == moduleId.revision &&
id.explicitArtifacts.nonEmpty && id.explicitArtifacts.forall(_.url.map(_.getProtocol()) == Some("file"))))
else
extClassifiersModuleDeps
}
arg.streams.log.debug(logPrefix(arg.name) + "Filtered dependencies: " + extClassifiersModuleDepsFiltered.mkString(","))
val customConfig = GetClassifiersConfiguration(arg.origClassifiersModule, excludes, arg.updateConfiguration, arg.ivyScala)
val customBaseModuleID = restrictedCopy(origClassifiersModuleID, true).copy(name = origClassifiersModuleID.name + "$sbt")
val customIvySbtModule = new arg.ivySbt.Module(InlineConfiguration(customBaseModuleID, ModuleInfo(customBaseModuleID.name), extClassifiersModuleDepsFiltered).copy(ivyScala = arg.ivyScala))
val customUpdateReport = IvyActions.update(customIvySbtModule, arg.updateConfiguration, arg.streams.log)
val newConfig = customConfig.copy(module = arg.origClassifiersModule.copy(modules = customUpdateReport.allModules))
val updateReport = IvyActions.updateClassifiers(arg.ivySbt, newConfig, arg.streams.log)
// process updateReport
// get all sources
val (sources, other) = updateReport.toSeq.partition {
case (_, _, Artifact(_, _, _, Some(Artifact.SourceClassifier), _, _, _), _) => true
case _ => false
}
val sourceObjects = sources.map { case (configuration, moduleId, artifact, file) => (moduleId, file) }
val codeObjects = other.map {
case (configuration, moduleId, artifact, file) if artifact.classifier == None || artifact.classifier == Some("") =>
Some((moduleId, file))
case _ =>
None
}.flatten
// process all jars
other.sortBy(_._2.toString).foreach { module => arg.streams.log.debug("add " + module._2) }
userFunction(arg, sourceObjects, codeObjects)
// add unprocessed modules
if (arg.dependencyEnableCustom) {
// get all unprocessed dependencies with ModuleID
val unprocessedUnfiltered = arg.dependencyFilter match {
case Some(filter) =>
extClassifiersModuleDeps.filterNot(other.map(_._2).contains).distinct.filter(filter)
case None =>
extClassifiersModuleDeps.filterNot(other.map(_._2).contains).distinct
}
unprocessedUnfiltered.sortBy(_.toString).foreach { module => arg.streams.log.debug("add unprocessed " + module) }
// get all unprocessed dependencies or dependencies without ModuleID
val unprocessed = arg.dependencyClasspath.sortBy(_.toString).map(classpath => classpath.get(moduleID.key) match {
case Some(moduleId) =>
if (unprocessedUnfiltered.contains(moduleId)) {
// lookup for original ModuleIDs with explicit artifacts that points to local file system
val originalModuleID = arg.libraryDependencies.find(id =>
id.name == moduleId.name && id.organization == moduleId.organization && id.revision == moduleId.revision &&
id.explicitArtifacts.nonEmpty && id.explicitArtifacts.forall(_.url.map(_.getProtocol()) == Some("file")))
Some(originalModuleID getOrElse moduleId.name % moduleId.organization % moduleId.revision from classpath.data.toURI().toURL().toString)
} else
None // already processed
case None =>
Some("UNKNOWN" % "UNKNOWN" % "UNKNOWN" from classpath.data.toURI().toURL().toString)
}).flatten
if (arg.dependencyPack)
unprocessed.foreach {
moduleId =>
val codeArtifact = moduleId.explicitArtifacts.find(_.classifier == None)
val sourceCodeArtifact = moduleId.explicitArtifacts.find(_.classifier == Some(Artifact.SourceClassifier))
(codeArtifact, sourceCodeArtifact) match {
case (Some(Artifact(_, _, _, _, _, Some(codeURL), _)), Some(Artifact(_, _, _, _, _, Some(sourceCodeURL), _))) =>
val code = new File(codeURL.toURI)
val source = new File(sourceCodeURL.toURI)
userFunction(arg, Seq((moduleId, source)), Seq((moduleId, code)))
case (Some(Artifact(_, _, _, _, _, Some(codeURL), _)), _) =>
val code = new File(codeURL.toURI)
arg.streams.log.info(logPrefix(arg.name) + "Fetch custom library " + code.getName())
copyToCodePack(arg, code)
copyToSourcePack(arg, code)
case _ =>
arg.streams.log.error(logPrefix(arg.name) + "Unable to aquire artifacts for module " + moduleId)
}
}
else
unprocessed.foreach {
moduleId =>
val codeArtifact = moduleId.explicitArtifacts.find(_.classifier == None)
val sourceCodeArtifact = moduleId.explicitArtifacts.find(_.classifier == Some(Artifact.SourceClassifier))
(codeArtifact, sourceCodeArtifact) match {
case (Some(Artifact(_, _, _, _, _, Some(codeURL), _)), Some(Artifact(_, _, _, _, _, Some(sourceCodeURL), _))) =>
val code = new File(codeURL.toURI)
val source = new File(sourceCodeURL.toURI)
userFunction(arg, Seq((moduleId, source)), Seq((moduleId, code)))
case (Some(Artifact(_, _, _, _, _, Some(codeURL), _)), _) =>
val code = new File(codeURL.toURI)
arg.streams.log.info(logPrefix(arg.name) + "Fetch custom library " + code.getName())
sbt.IO.copyFile(code, new File(arg.pathDependency, code.getName()), false)
case _ =>
arg.streams.log.error(logPrefix(arg.name) + "Unable to aquire artifacts for module " + moduleId)
}
}
}
if (arg.dependencyPack) {
// add artifact
arg.dependencyArtifact.foreach(copyToCodePack(arg, _))
arg.dependencyArtifact.foreach(copyToSourcePack(arg, _))
arg.packWithCode.flush()
arg.packWithCode.close()
arg.packWithSource.flush()
arg.packWithSource.close()
// create consolidated jar description
val directory = arg.pathPack.getParentFile()
val file = arg.pathPack.getName() + ".description"
val descriptionFile = new File(directory, file)
Some(new PrintWriter(descriptionFile)).foreach { writer =>
try {
writer.write(arg.packResources.toList.sorted.mkString("\n"))
} catch {
case e: Throwable =>
arg.streams.log.error(logPrefix(arg.name) + "Unable to create consolidated jar description " + descriptionFile.getAbsolutePath() + " " + e)
} finally {
try { writer.close } catch { case e: Throwable => }
}
}
}
updateReport
}
}
/** Specific part for tasks dependency-fetch-align, dependency-pack, dependency-pack-with-artifact */
protected def doFetchAlign(arg: TaskArgument, sourceObjects: Seq[(sbt.ModuleID, File)],
codeObjects: Seq[(sbt.ModuleID, File)]): Unit = codeObjects.foreach {
case (module, codeJar) =>
sourceObjects.find(source => source._1 == module) match {
case Some((_, sourceJar)) =>
if (arg.dependencyPack) {
align(arg, module.toString, codeJar, sourceJar, arg.pathDependency, resourceFilter, arg.streams, arg.packEntries, arg.packWithCode)
arg.packResources += codeJar.getAbsolutePath()
} else
align(arg, module.toString, codeJar, sourceJar, arg.pathDependency, resourceFilter, arg.streams)
case None =>
arg.streams.log.debug(logPrefix(arg.name) + "Skip align for dependency " + module + " - sources not found ")
if (arg.dependencyPack) {
arg.streams.log.info(logPrefix(arg.name) + "Add " + module + " to consolidated jar without source code")
copyToCodePack(arg, codeJar)
} else {
arg.streams.log.info(logPrefix(arg.name) + "Fetch " + module + " without source code")
val codeTarget = new File(arg.pathDependency, codeJar.getName())
arg.streams.log.debug(logPrefix(arg.name) + "Save result to " + codeTarget.getAbsolutePath())
sbt.IO.copyFile(codeJar, codeTarget, false)
}
}
}
/** Specific part for task dependency-fetch-with-sources */
protected def doFetchWithSources(arg: TaskArgument, sourceObjects: Seq[(sbt.ModuleID, File)],
codeObjects: Seq[(sbt.ModuleID, File)]): Unit = codeObjects.foreach {
case (module, codeJar) =>
sourceObjects.find(source => source._1 == module) match {
case Some((_, sourceJar)) =>
if (arg.dependencyPack) {
arg.streams.log.info(logPrefix(arg.name) + "Fetch with source code " + module + ", target: consolidated jar.")
copyToCodePack(arg, codeJar)
copyToSourcePack(arg, sourceJar)
arg.packResources += codeJar.getAbsolutePath()
} else {
val codeTarget = new File(arg.pathDependency, codeJar.getName())
val sourceTarget = new File(arg.pathDependency, sourceJar.getName())
arg.streams.log.info(logPrefix(arg.name) + "Fetch with source code " + module)
arg.streams.log.debug(logPrefix(arg.name) + "Save results to " + codeTarget.getParentFile.getAbsolutePath())
sbt.IO.copyFile(codeJar, codeTarget, false)
sbt.IO.copyFile(sourceJar, sourceTarget, false)
}
case None =>
if (arg.dependencyPack) {
arg.streams.log.info(logPrefix(arg.name) + "Fetch with source code " + module + ", target: consolidated jar.")
copyToCodePack(arg, codeJar)
} else {
arg.streams.log.info(logPrefix(arg.name) + "Fetch with source code " + module)
val codeTarget = new File(arg.pathDependency, codeJar.getName())
arg.streams.log.debug(logPrefix(arg.name) + "Save results to " + codeTarget.getParentFile.getAbsolutePath())
sbt.IO.copyFile(codeJar, codeTarget, false)
}
}
}
/** Specific part for task dependency-fetch */
protected def doFetch(arg: TaskArgument, sourceObjects: Seq[(sbt.ModuleID, File)],
codeObjects: Seq[(sbt.ModuleID, File)]): Unit = codeObjects.foreach {
case (module, codeJar) =>
sourceObjects.find(source => source._1 == module) match {
case Some((_, sourceJar)) =>
arg.streams.log.info(logPrefix(arg.name) + "Fetch " + module)
val codeTarget = new File(arg.pathDependency, codeJar.getName())
arg.streams.log.debug(logPrefix(arg.name) + "Save result to " + codeTarget.getAbsolutePath())
sbt.IO.copyFile(codeJar, codeTarget, false)
case None =>
arg.streams.log.debug(logPrefix(arg.name) + "Skip " + module)
}
}
/** Repack content of jar artifact */
private def alignScalaSource(arg: TaskArgument, alignEntries: HashSet[String], entry: ZipEntry, content: String, s: TaskStreams): Option[ZipEntry] = {
val searchFor = "/" + entry.getName.takeWhile(_ != '.')
val distance = alignEntries.toSeq.map(path => (path.indexOf(searchFor), path)).filter(_._1 > 1).sortBy(_._1).headOption
distance match {
case Some((idx, entryPath)) =>
val newEntry = new ZipEntry(entryPath.substring(0, idx) + searchFor + ".scala")
s.log.debug(logPrefix(arg.name) + "Align " + entry.getName + " to " + newEntry.getName())
newEntry.setComment(entry.getComment())
newEntry.setCompressedSize(entry.getCompressedSize())
newEntry.setCrc(entry.getCrc())
newEntry.setExtra(entry.getExtra())
newEntry.setMethod(entry.getMethod())
newEntry.setSize(entry.getSize())
newEntry.setTime(entry.getTime())
Some(newEntry)
case None =>
var path = Seq[String]()
val pattern = """\s*package\s+([a-z\\._$-]+).*""".r
content.split("\n").foreach {
case pattern(packageName) =>
path = path :+ packageName.replaceAll("\\.", "/")
case line =>
}
if (path.nonEmpty) {
val prefix = path.mkString("/") + "/"
alignEntries.toSeq.find(_.startsWith(prefix)) match {
case Some(path) =>
val newEntry = new ZipEntry(prefix + entry.getName())
s.log.debug(logPrefix(arg.name) + "Align " + entry.getName + " to " + newEntry.getName())
newEntry.setComment(entry.getComment())
newEntry.setCompressedSize(entry.getCompressedSize())
newEntry.setCrc(entry.getCrc())
newEntry.setExtra(entry.getExtra())
newEntry.setMethod(entry.getMethod())
newEntry.setSize(entry.getSize())
newEntry.setTime(entry.getTime())
Some(newEntry)
case None =>
s.log.warn(logPrefix(arg.name) + "Failed to align source " + entry.getName())
None
}
} else
None
}
}
/** Copy content of jar artifact */
private def copy(arg: TaskArgument, alignEntries: HashSet[String], in: JarInputStream, out: JarOutputStream, resourceFilter: ZipEntry => Boolean, s: TaskStreams) {
var entry: ZipEntry = null
// copy across all entries from the original code jar
var value: Int = 0
try {
val buffer = new Array[Byte](2048)
entry = in.getNextEntry()
while (entry != null) {
if (alignEntries(entry.getName))
s.log.debug(logPrefix(arg.name) + "Skip, entry already in jar: " + entry.getName())
else if (resourceFilter(entry)) {
s.log.debug(logPrefix(arg.name) + "Skip, filtered " + entry)
} else
try {
alignEntries(entry.getName) = true
val bos = new ByteArrayOutputStream()
value = in.read(buffer)
while (value > 0) {
bos.write(buffer, 0, value)
value = in.read(buffer)
}
val destEntry = new ZipEntry(entry.getName)
out.putNextEntry(destEntry)
out.write(bos.toByteArray())
// adjust root scala sources
if (entry.getName.endsWith(".scala") && entry.getName.indexOf("/") == -1)
alignScalaSource(arg, alignEntries, entry, bos.toString, s).foreach {
entry =>
if (alignEntries(entry.getName))
s.log.debug(logPrefix(arg.name) + "Skip, entry already in jar: " + entry.getName())
else {
out.putNextEntry(entry)
out.write(bos.toByteArray())
}
}
} catch {
case e: ZipException =>
s.log.error(logPrefix(arg.name) + "Zip failed: " + e.getMessage())
}
entry = in.getNextEntry()
}
} catch {
case e: Throwable =>
s.log.error(logPrefix(arg.name) + "Copy failed: " + e.getClass().getName() + " " + e.getMessage())
}
}
/** Copy content to consolidated jar */
private def copyToCodePack(arg: TaskArgument, codeJar: File) {
arg.streams.log.debug(logPrefix(arg.name) + "Append %s to consolidated jar with compiled classes.".format(codeJar.getName()))
// copy across all entries from the original code jar
val jarCode = new JarInputStream(new FileInputStream(codeJar))
try {
copy(arg, arg.packEntries, jarCode, arg.packWithCode, resourceFilter, arg.streams)
arg.packResources += codeJar.getAbsolutePath()
} catch {
case e: Throwable =>
arg.streams.log.error(logPrefix(arg.name) + "Unable to merge: " + e.getClass().getName() + " " + e.getMessage())
} finally {
if (jarCode != null)
jarCode.close()
}
}
/** Copy content to consolidated jar */
private def copyToSourcePack(arg: TaskArgument, sourceJar: File) {
arg.streams.log.debug("append %s to consolidated jar with source code.".format(sourceJar.getName()))
// copy across all entries from the original code jar
val jarSource = new JarInputStream(new FileInputStream(sourceJar))
try {
copy(arg, arg.packEntries, jarSource, arg.packWithSource, resourceFilter, arg.streams)
} catch {
case e: Throwable =>
arg.streams.log.error(logPrefix(arg.name) + "Unable to merge: " + e.getClass().getName() + " " + e.getMessage())
} finally {
if (jarSource != null)
jarSource.close()
}
}
private[this] def restrictedCopy(m: ModuleID, confs: Boolean) =
ModuleID(m.organization, m.name, m.revision, crossVersion = m.crossVersion, extraAttributes = m.extraAttributes, configurations = if (confs) m.configurations else None)
/** Consolidated argument with all required information. */
case class TaskArgument(
/** Application configuration that provides information about SBT process. */
appConfiguration: AppConfiguration,
/** The property representing Ivy process log level. */
ivyLogLevel: UpdateLogging.Value,
/** Ivy wrapper that contains org.apache.ivy.Ivy and org.apache.ivy.core.settings.IvySettings. */
ivySbt: IvySbt,
/** Ivy scala artifacts description. */
ivyScala: Option[IvyScala],
/** Original ModuleIDs from SBT project definition. */
libraryDependencies: Seq[ModuleID],
/** Current project name. */
name: String,
/** GetClassifiersModule. */
origClassifiersModule: GetClassifiersModule,
/** Update configuration. */
updateConfiguration: UpdateConfiguration,
/** Path to consolidated jar with file name. */
pathPack: java.io.File,
/** Path to Fetched artifacts. */
pathDependency: java.io.File,
/** Target path. */
pathTarget: java.io.File,
/** SBT task streams for logging. */
streams: TaskStreams,
/** Flag indicating whether custom libraries without ModuleID should be fetched. */
dependencyEnableCustom: Boolean,
/** The property representing artifact location. */
dependencyArtifact: Option[java.io.File],
/** Flag indicating whether plugin should create consolidated jar. */
dependencyPack: Boolean,
/** Classpath that is used to build dependency sequence. */
dependencyClasspath: Classpath,
/** Fetch filter. */
dependencyFilter: Option[ModuleFilter],
/** Flag indicating whether plugin should ignore a dependency configuration while lookup ('test' for example). */
dependencyIgnoreConfiguration: Boolean,
/** Function that filters jar content. */
dependencyResourceFilter: ZipEntry => Boolean,
/** Skip resolved dependencies with explicit artifacts which points to local resources. */
dependencySkipResolved: Boolean) {
/** Output stream for consolidated jar with compiled code. */
val packWithCode: JarOutputStream = if (dependencyPack) {
assert(pathPack.name endsWith ".jar", "incorrect dependency-pack-path, must be path to jar file")
pathPack.delete() // remove old pack
new JarOutputStream(new BufferedOutputStream(new FileOutputStream(pathPack, true)))
} else
null
/** Output stream for consolidated jar with source code. */
val packWithSource: JarOutputStream = if (dependencyPack) {
assert(pathPack.name endsWith ".jar", "incorrect dependency-pack-path, must be path to jar file")
val directory = pathPack.getParentFile()
val name = pathPack.getName
val pathSourcePack = new File(directory, name.replaceFirst(""".jar$""", """-sources.jar"""))
pathSourcePack.delete() // remove old pack
new JarOutputStream(new BufferedOutputStream(new FileOutputStream(pathSourcePack, true)))
} else
null
val packEntries = HashSet[String]()
val packResources = HashSet[String]()
}
class RichOption[T](option: Option[T]) {
def getOrThrow(onError: String) = option getOrElse { throw new NoSuchElementException(onError) }
}
}