-
Notifications
You must be signed in to change notification settings - Fork 44
/
GenerativePlugin.scala
905 lines (761 loc) · 29.4 KB
/
GenerativePlugin.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
* Copyright 2022 Typelevel
*
* 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 org.typelevel.sbt.gha
import sbt.Keys._
import sbt._
import java.nio.file.FileSystems
import scala.io.Source
object GenerativePlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements
object autoImport extends GenerativeKeys {
type WorkflowJob = org.typelevel.sbt.gha.WorkflowJob
val WorkflowJob = org.typelevel.sbt.gha.WorkflowJob
type JobContainer = org.typelevel.sbt.gha.JobContainer
val JobContainer = org.typelevel.sbt.gha.JobContainer
type WorkflowStep = org.typelevel.sbt.gha.WorkflowStep
val WorkflowStep = org.typelevel.sbt.gha.WorkflowStep
type RefPredicate = org.typelevel.sbt.gha.RefPredicate
val RefPredicate = org.typelevel.sbt.gha.RefPredicate
type Ref = org.typelevel.sbt.gha.Ref
val Ref = org.typelevel.sbt.gha.Ref
type UseRef = org.typelevel.sbt.gha.UseRef
val UseRef = org.typelevel.sbt.gha.UseRef
type PREventType = org.typelevel.sbt.gha.PREventType
val PREventType = org.typelevel.sbt.gha.PREventType
type MatrixInclude = org.typelevel.sbt.gha.MatrixInclude
val MatrixInclude = org.typelevel.sbt.gha.MatrixInclude
type MatrixExclude = org.typelevel.sbt.gha.MatrixExclude
val MatrixExclude = org.typelevel.sbt.gha.MatrixExclude
type Paths = org.typelevel.sbt.gha.Paths
val Paths = org.typelevel.sbt.gha.Paths
type JavaSpec = org.typelevel.sbt.gha.JavaSpec
val JavaSpec = org.typelevel.sbt.gha.JavaSpec
}
import autoImport._
private def indent(output: String, level: Int): String = {
val space = (0 until level * 2).map(_ => ' ').mkString
(space + output.replace("\n", s"\n$space")).replaceAll("""\n[ ]+\n""", "\n\n")
}
private def isSafeString(str: String): Boolean =
!(str.indexOf(':') >= 0 || // pretend colon is illegal everywhere for simplicity
str.indexOf('#') >= 0 || // same for comment
str.indexOf('!') == 0 ||
str.indexOf('*') == 0 ||
str.indexOf('-') == 0 ||
str.indexOf('?') == 0 ||
str.indexOf('{') == 0 ||
str.indexOf('}') == 0 ||
str.indexOf('[') == 0 ||
str.indexOf(']') == 0 ||
str.indexOf(',') == 0 ||
str.indexOf('|') == 0 ||
str.indexOf('>') == 0 ||
str.indexOf('@') == 0 ||
str.indexOf('`') == 0 ||
str.indexOf('"') == 0 ||
str.indexOf('\'') == 0 ||
str.indexOf('&') == 0)
private def wrap(str: String): String =
if (str.indexOf('\n') >= 0)
"|\n" + indent(str, 1)
else if (isSafeString(str))
str
else
s"'${str.replace("'", "''")}'"
def compileList(items: List[String], level: Int): String = {
val rendered = items.map(wrap)
if (rendered.map(_.length).sum < 40) // just arbitrarily...
rendered.mkString(" [", ", ", "]")
else
"\n" + indent(rendered.map("- " + _).mkString("\n"), level)
}
def compileListOfSimpleDicts(items: List[Map[String, String]]): String =
items map { dict =>
val rendered = dict map { case (key, value) => s"$key: $value" } mkString "\n"
"-" + indent(rendered, 1).substring(1)
} mkString "\n"
def compilePREventType(tpe: PREventType): String = {
import PREventType._
tpe match {
case Assigned => "assigned"
case Unassigned => "unassigned"
case Labeled => "labeled"
case Unlabeled => "unlabeled"
case Opened => "opened"
case Edited => "edited"
case Closed => "closed"
case Reopened => "reopened"
case Synchronize => "synchronize"
case ReadyForReview => "ready_for_review"
case Locked => "locked"
case Unlocked => "unlocked"
case ReviewRequested => "review_requested"
case ReviewRequestRemoved => "review_request_removed"
}
}
def compileRef(ref: Ref): String = ref match {
case Ref.Branch(name) => s"refs/heads/$name"
case Ref.Tag(name) => s"refs/tags/$name"
}
def compileBranchPredicate(target: String, pred: RefPredicate): String = pred match {
case RefPredicate.Equals(ref) =>
s"$target == '${compileRef(ref)}'"
case RefPredicate.Contains(Ref.Tag(name)) =>
s"(startsWith($target, 'refs/tags/') && contains($target, '$name'))"
case RefPredicate.Contains(Ref.Branch(name)) =>
s"(startsWith($target, 'refs/heads/') && contains($target, '$name'))"
case RefPredicate.StartsWith(ref) =>
s"startsWith($target, '${compileRef(ref)}')"
case RefPredicate.EndsWith(Ref.Tag(name)) =>
s"(startsWith($target, 'refs/tags/') && endsWith($target, '$name'))"
case RefPredicate.EndsWith(Ref.Branch(name)) =>
s"(startsWith($target, 'refs/heads/') && endsWith($target, '$name'))"
}
def compileEnvironment(environment: JobEnvironment): String =
environment.url match {
case Some(url) =>
val fields = s"""name: ${wrap(environment.name)}
|url: ${wrap(url.toString)}""".stripMargin
s"""environment:
|${indent(fields, 1)}""".stripMargin
case None =>
s"environment: ${wrap(environment.name)}"
}
def compileEnv(env: Map[String, String], prefix: String = "env"): String =
if (env.isEmpty) {
""
} else {
val rendered = env map {
case (key, value) =>
if (!isSafeString(key) || key.indexOf(' ') >= 0)
sys.error(s"'$key' is not a valid environment variable name")
s"""$key: ${wrap(value)}"""
}
s"""$prefix:
${indent(rendered.mkString("\n"), 1)}"""
}
def compileStep(
step: WorkflowStep,
sbt: String,
sbtStepPreamble: List[String],
declareShell: Boolean = false): String = {
import WorkflowStep._
val renderedName = step.name.map(wrap).map("name: " + _ + "\n").getOrElse("")
val renderedId = step.id.map(wrap).map("id: " + _ + "\n").getOrElse("")
val renderedCond = step.cond.map(wrap).map("if: " + _ + "\n").getOrElse("")
val renderedShell = if (declareShell) "shell: bash\n" else ""
val renderedEnvPre = compileEnv(step.env)
val renderedEnv =
if (renderedEnvPre.isEmpty)
""
else
renderedEnvPre + "\n"
val preamblePre = renderedName + renderedId + renderedCond + renderedEnv
val preamble =
if (preamblePre.isEmpty)
""
else
preamblePre
val body = step match {
case run: Run =>
renderRunBody(run.commands, run.params, renderedShell)
case sbtStep: Sbt =>
import sbtStep.commands
val sbtClientMode = sbt.matches("""sbt.* --client($| .*)""")
val safeCommands =
if (sbtClientMode)
s"'${(sbtStepPreamble ::: commands).mkString("; ")}'"
else
(sbtStepPreamble ::: commands)
.map { c =>
if (c.indexOf(' ') >= 0)
s"'$c'"
else
c
}
.mkString(" ")
renderRunBody(
commands = List(s"$sbt $safeCommands"),
params = sbtStep.params,
renderedShell = renderedShell
)
case use: Use =>
import use.{ref, params}
val decl = ref match {
case UseRef.Public(owner, repo, ref) =>
s"uses: $owner/$repo@$ref"
case UseRef.Local(path) =>
val cleaned =
if (path.startsWith("./"))
path
else
"./" + path
s"uses: $cleaned"
case UseRef.Docker(image, tag, Some(host)) =>
s"uses: docker://$host/$image:$tag"
case UseRef.Docker(image, tag, None) =>
s"uses: docker://$image:$tag"
}
decl + renderParams(params)
}
indent(preamble + body, 1).updated(0, '-')
}
def renderRunBody(
commands: List[String],
params: Map[String, String],
renderedShell: String) =
renderedShell + "run: " + wrap(commands.mkString("\n")) + renderParams(params)
def renderParams(params: Map[String, String]): String = {
val renderedParamsPre = compileEnv(params, prefix = "with")
val renderedParams =
if (renderedParamsPre.isEmpty)
""
else
"\n" + renderedParamsPre
renderedParams
}
def compileJob(job: WorkflowJob, sbt: String): String = {
val renderedNeeds =
if (job.needs.isEmpty)
""
else
s"\nneeds: [${job.needs.mkString(", ")}]"
val renderedEnvironment =
job.environment.map(compileEnvironment).map("\n" + _).getOrElse("")
val renderedCond = job.cond.map(wrap).map("\nif: " + _).getOrElse("")
val renderedContainer = job.container match {
case Some(JobContainer(image, credentials, env, volumes, ports, options)) =>
if (credentials.isEmpty && env.isEmpty && volumes.isEmpty && ports.isEmpty && options.isEmpty) {
"\n" + s"container: ${wrap(image)}"
} else {
val renderedImage = s"image: ${wrap(image)}"
val renderedCredentials = credentials match {
case Some((username, password)) =>
s"\ncredentials:\n${indent(s"username: ${wrap(username)}\npassword: ${wrap(password)}", 1)}"
case None =>
""
}
val renderedEnv =
if (!env.isEmpty)
"\n" + compileEnv(env)
else
""
val renderedVolumes =
if (!volumes.isEmpty)
s"\nvolumes:${compileList(volumes.toList map { case (l, r) => s"$l:$r" }, 1)}"
else
""
val renderedPorts =
if (!ports.isEmpty)
s"\nports:${compileList(ports.map(_.toString), 1)}"
else
""
val renderedOptions =
if (!options.isEmpty)
s"\noptions: ${wrap(options.mkString(" "))}"
else
""
s"\ncontainer:\n${indent(renderedImage + renderedCredentials + renderedEnv + renderedVolumes + renderedPorts + renderedOptions, 1)}"
}
case None =>
""
}
val renderedEnvPre = compileEnv(job.env)
val renderedEnv =
if (renderedEnvPre.isEmpty)
""
else
"\n" + renderedEnvPre
List("include", "exclude") foreach { key =>
if (job.matrixAdds.contains(key)) {
sys.error(s"key `$key` is reserved and cannot be used in an Actions matrix definition")
}
}
val renderedMatricesPre = job.matrixAdds map {
case (key, values) => s"$key: ${values.map(wrap).mkString("[", ", ", "]")}"
} mkString "\n"
// TODO refactor all of this stuff to use whitelist instead
val whitelist = Map(
"os" -> job.oses,
"scala" -> job.scalas,
"java" -> job.javas.map(_.render)) ++ job.matrixAdds
def checkMatching(matching: Map[String, String]): Unit = {
matching foreach {
case (key, value) =>
if (!whitelist.contains(key)) {
sys.error(s"inclusion key `$key` was not found in matrix")
}
if (!whitelist(key).contains(value)) {
sys.error(
s"inclusion key `$key` was present in matrix, but value `$value` was not in ${whitelist(key)}")
}
}
}
val renderedIncludesPre = if (job.matrixIncs.isEmpty) {
renderedMatricesPre
} else {
job.matrixIncs.foreach(inc => checkMatching(inc.matching))
val rendered = compileListOfSimpleDicts(
job.matrixIncs.map(i => i.matching ++ i.additions))
val renderedMatrices =
if (renderedMatricesPre.isEmpty)
""
else
renderedMatricesPre + "\n"
s"${renderedMatrices}include:\n${indent(rendered, 1)}"
}
val renderedExcludesPre = if (job.matrixExcs.isEmpty) {
renderedIncludesPre
} else {
job.matrixExcs.foreach(exc => checkMatching(exc.matching))
val rendered = compileListOfSimpleDicts(job.matrixExcs.map(_.matching))
val renderedIncludes =
if (renderedIncludesPre.isEmpty)
""
else
renderedIncludesPre + "\n"
s"${renderedIncludes}exclude:\n${indent(rendered, 1)}"
}
val renderedMatrices =
if (renderedExcludesPre.isEmpty)
""
else
"\n" + indent(renderedExcludesPre, 2)
val declareShell = job.oses.exists(_.contains("windows"))
val runsOn =
if (job.runsOnExtraLabels.isEmpty)
s"$${{ matrix.os }}"
else
job.runsOnExtraLabels.mkString(s"""[ "$${{ matrix.os }}", """, ", ", " ]")
val renderedFailFast = job.matrixFailFast.fold("")("\n fail-fast: " + _)
// format: off
val body = s"""name: ${wrap(job.name)}${renderedNeeds}${renderedCond}
strategy:${renderedFailFast}
matrix:
os:${compileList(job.oses, 3)}
scala:${compileList(job.scalas, 3)}
java:${compileList(job.javas.map(_.render), 3)}${renderedMatrices}
runs-on: ${runsOn}${renderedEnvironment}${renderedContainer}${renderedEnv}
steps:
${indent(job.steps.map(compileStep(_, sbt, job.sbtStepPreamble, declareShell = declareShell)).mkString("\n\n"), 1)}"""
// format: on
s"${job.id}:\n${indent(body, 1)}"
}
def compileWorkflow(
name: String,
branches: List[String],
tags: List[String],
paths: Paths,
prEventTypes: List[PREventType],
env: Map[String, String],
jobs: List[WorkflowJob],
sbt: String): String = {
val renderedEnvPre = compileEnv(env)
val renderedEnv =
if (renderedEnvPre.isEmpty)
""
else
renderedEnvPre + "\n\n"
val renderedTypesPre = prEventTypes.map(compilePREventType).mkString("[", ", ", "]")
val renderedTypes =
if (prEventTypes.sortBy(_.toString) == PREventType.Defaults)
""
else
"\n" + indent("types: " + renderedTypesPre, 2)
val renderedTags =
if (tags.isEmpty)
""
else
s"""
tags: [${tags.map(wrap).mkString(", ")}]"""
val renderedPaths = paths match {
case Paths.None =>
""
case Paths.Include(paths) =>
"\n" + indent(s"""paths: [${paths.map(wrap).mkString(", ")}]""", 2)
case Paths.Ignore(paths) =>
"\n" + indent(s"""paths-ignore: [${paths.map(wrap).mkString(", ")}]""", 2)
}
s"""# This file was automatically generated by sbt-github-actions using the
# githubWorkflowGenerate task. You should add and commit this file to
# your git repository. It goes without saying that you shouldn't edit
# this file by hand! Instead, if you wish to make changes, you should
# change your sbt build configuration to revise the workflow description
# to meet your needs, then regenerate this file.
name: ${wrap(name)}
on:
pull_request:
branches: [${branches.map(wrap).mkString(", ")}]$renderedTypes$renderedPaths
push:
branches: [${branches.map(wrap).mkString(", ")}]$renderedTags$renderedPaths
${renderedEnv}jobs:
${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
"""
}
val settingDefaults = Seq(
githubWorkflowSbtCommand := "sbt",
githubWorkflowIncludeClean := true,
// This is currently set to false because of https://github.com/sbt/sbt/issues/6468. When a new SBT version is
// released that fixes this issue then check for that SBT version (or higher) and set to true.
githubWorkflowUseSbtThinClient := false,
githubWorkflowBuildMatrixFailFast := None,
githubWorkflowBuildMatrixAdditions := Map(),
githubWorkflowBuildMatrixInclusions := Seq(),
githubWorkflowBuildMatrixExclusions := Seq(),
githubWorkflowBuildRunsOnExtraLabels := Seq(),
githubWorkflowBuildPreamble := Seq(),
githubWorkflowBuildPostamble := Seq(),
githubWorkflowBuildSbtStepPreamble := Seq(s"++$${{ matrix.scala }}"),
githubWorkflowBuild := Seq(WorkflowStep.Sbt(List("test"), name = Some("Build project"))),
githubWorkflowPublishPreamble := Seq(),
githubWorkflowPublishPostamble := Seq(),
githubWorkflowPublish := Seq(
WorkflowStep.Sbt(List("+publish"), name = Some("Publish project"))),
githubWorkflowPublishTargetBranches := Seq(RefPredicate.Equals(Ref.Branch("main"))),
githubWorkflowPublishCond := None,
githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")),
githubWorkflowScalaVersions := crossScalaVersions.value,
githubWorkflowOSes := Seq("ubuntu-latest"),
githubWorkflowDependencyPatterns := Seq("**/*.sbt", "project/build.properties"),
githubWorkflowTargetBranches := Seq("**"),
githubWorkflowTargetTags := Seq(),
githubWorkflowTargetPaths := Paths.None,
githubWorkflowEnv := Map("GITHUB_TOKEN" -> s"$${{ secrets.GITHUB_TOKEN }}"),
githubWorkflowAddedJobs := Seq()
)
private lazy val internalTargetAggregation =
settingKey[Seq[File]]("Aggregates target directories from all subprojects")
private val windowsGuard = Some("contains(runner.os, 'windows')")
private val PlatformSep = FileSystems.getDefault.getSeparator
private def normalizeSeparators(pathStr: String): String = {
pathStr.replace(PlatformSep, "/") // *force* unix separators
}
private val pathStrs = Def setting {
val base = (ThisBuild / baseDirectory).value.toPath
internalTargetAggregation.value map { file =>
val path = file.toPath
if (path.isAbsolute)
normalizeSeparators(base.relativize(path).toString)
else
normalizeSeparators(path.toString)
}
}
override def globalSettings =
Seq(internalTargetAggregation := Seq(), githubWorkflowArtifactUpload := true)
override def buildSettings = settingDefaults ++ Seq(
githubWorkflowPREventTypes := PREventType.Defaults,
githubWorkflowArtifactDownloadExtraKeys := Set.empty,
githubWorkflowGeneratedUploadSteps := {
val generate =
githubWorkflowArtifactUpload.value &&
githubWorkflowPublishTargetBranches.value.nonEmpty
if (generate) {
val sanitized = pathStrs.value map { str =>
if (str.indexOf(' ') >= 0) // TODO be less naive
s"'$str'"
else
str
}
val mkdir = WorkflowStep.Run(
List(s"mkdir -p ${sanitized.mkString(" ")} project/target"),
name = Some("Make target directories"),
cond = Some(publicationCond.value))
val tar = WorkflowStep.Run(
List(s"tar cf targets.tar ${sanitized.mkString(" ")} project/target"),
name = Some("Compress target directories"),
cond = Some(publicationCond.value))
val keys = githubWorkflowBuildMatrixAdditions.value.keys.toList.sorted
val artifactId =
(List("os", "java", "scala") ::: keys).map(k => s"$${{ matrix.$k }}").mkString("-")
val upload = WorkflowStep.Use(
UseRef.Public("actions", "upload-artifact", "v3"),
name = Some(s"Upload target directories"),
params = Map("name" -> s"target-$artifactId", "path" -> "targets.tar"),
cond = Some(publicationCond.value)
)
Seq(mkdir, tar, upload)
} else {
Seq()
}
},
githubWorkflowGeneratedDownloadSteps := {
val extraKeys = githubWorkflowArtifactDownloadExtraKeys.value
val additions = githubWorkflowBuildMatrixAdditions.value
val matrixAdds = additions.map {
case (key, values) =>
if (extraKeys(key))
key -> values // we want to iterate over all values
else
key -> values.take(1) // we only want the primary value
}
val oses = githubWorkflowOSes.value.toList.take(1)
val scalas = githubWorkflowScalaVersions.value.toList
val javas = githubWorkflowJavaVersions.value.toList.take(1)
val exclusions = githubWorkflowBuildMatrixExclusions.value.toList
// we build the list of artifacts, by iterating over all combinations of keys
val artifacts =
expandMatrix(
oses,
scalas,
javas,
matrixAdds,
Nil,
exclusions
).map {
case _ :: scala :: _ :: tail => scala :: tail
case _ => sys.error("Bug generating artifact download steps") // shouldn't happen
}
if (githubWorkflowArtifactUpload.value) {
artifacts flatMap { v =>
val pretty = v.mkString(", ")
val download = WorkflowStep.Use(
UseRef.Public("actions", "download-artifact", "v3"),
name = Some(s"Download target directories ($pretty)"),
params =
Map("name" -> s"target-$${{ matrix.os }}-$${{ matrix.java }}-${v.mkString("-")}")
)
val untar = WorkflowStep.Run(
List("tar xf targets.tar", "rm targets.tar"),
name = Some(s"Inflate target directories ($pretty)"))
Seq(download, untar)
}
} else {
Seq()
}
},
githubWorkflowGeneratedCacheSteps := Seq(),
githubWorkflowJobSetup := {
val autoCrlfOpt = if (githubWorkflowOSes.value.exists(_.contains("windows"))) {
List(
WorkflowStep.Run(
List("git config --global core.autocrlf false"),
name = Some("Ignore line ending differences in git"),
cond = windowsGuard))
} else {
Nil
}
autoCrlfOpt :::
List(WorkflowStep.CheckoutFull) :::
WorkflowStep.SetupJava(githubWorkflowJavaVersions.value.toList) :::
githubWorkflowGeneratedCacheSteps.value.toList
},
githubWorkflowGeneratedCI := {
val uploadStepsOpt =
if (githubWorkflowPublishTargetBranches
.value
.isEmpty && githubWorkflowAddedJobs.value.isEmpty)
Nil
else
githubWorkflowGeneratedUploadSteps.value.toList
val publishJobOpt = Seq(
WorkflowJob(
"publish",
"Publish Artifacts",
githubWorkflowJobSetup.value.toList :::
githubWorkflowGeneratedDownloadSteps.value.toList :::
githubWorkflowPublishPreamble.value.toList :::
githubWorkflowPublish.value.toList :::
githubWorkflowPublishPostamble.value.toList,
cond = Some(publicationCond.value),
oses = githubWorkflowOSes.value.toList.take(1),
scalas = List(scalaVersion.value),
javas = List(githubWorkflowJavaVersions.value.head),
needs = List("build")
)).filter(_ => !githubWorkflowPublishTargetBranches.value.isEmpty)
Seq(
WorkflowJob(
"build",
"Build and Test",
githubWorkflowJobSetup.value.toList :::
githubWorkflowBuildPreamble.value.toList :::
WorkflowStep.Run(
List(s"${sbt.value} githubWorkflowCheck"),
name = Some("Check that workflows are up to date")) ::
githubWorkflowBuild.value.toList :::
githubWorkflowBuildPostamble.value.toList :::
uploadStepsOpt,
sbtStepPreamble = githubWorkflowBuildSbtStepPreamble.value.toList,
oses = githubWorkflowOSes.value.toList,
scalas = githubWorkflowScalaVersions.value.toList,
javas = githubWorkflowJavaVersions.value.toList,
matrixFailFast = githubWorkflowBuildMatrixFailFast.value,
matrixAdds = githubWorkflowBuildMatrixAdditions.value,
matrixIncs = githubWorkflowBuildMatrixInclusions.value.toList,
matrixExcs = githubWorkflowBuildMatrixExclusions.value.toList,
runsOnExtraLabels = githubWorkflowBuildRunsOnExtraLabels.value.toList
)) ++ publishJobOpt ++ githubWorkflowAddedJobs.value
}
)
private val publicationCond = Def setting {
val publicationCondPre =
githubWorkflowPublishTargetBranches
.value
.map(compileBranchPredicate("github.ref", _))
.mkString("(", " || ", ")")
val publicationCond = githubWorkflowPublishCond.value match {
case Some(cond) => publicationCondPre + " && (" + cond + ")"
case None => publicationCondPre
}
s"github.event_name != 'pull_request' && $publicationCond"
}
private val sbt = Def.setting {
if (githubWorkflowUseSbtThinClient.value) {
githubWorkflowSbtCommand.value + " --client"
} else {
githubWorkflowSbtCommand.value
}
}
private val generateCiContents = Def task {
compileWorkflow(
"Continuous Integration",
githubWorkflowTargetBranches.value.toList,
githubWorkflowTargetTags.value.toList,
githubWorkflowTargetPaths.value,
githubWorkflowPREventTypes.value.toList,
githubWorkflowEnv.value,
githubWorkflowGeneratedCI.value.toList,
sbt.value
)
}
private val readCleanContents = Def task {
val src = Source.fromURL(getClass.getResource("/clean.yml"))
try {
src.mkString
} finally {
src.close()
}
}
private val workflowsDirTask = Def task {
val githubDir = baseDirectory.value / ".github"
val workflowsDir = githubDir / "workflows"
if (!githubDir.exists()) {
githubDir.mkdir()
}
if (!workflowsDir.exists()) {
workflowsDir.mkdir()
}
workflowsDir
}
private val ciYmlFile = Def task {
workflowsDirTask.value / "ci.yml"
}
private val cleanYmlFile = Def task {
workflowsDirTask.value / "clean.yml"
}
override def projectSettings = Seq(
Global / internalTargetAggregation ++= {
if (githubWorkflowArtifactUpload.value)
Seq(target.value)
else
Seq()
},
githubWorkflowGenerate / aggregate := false,
githubWorkflowCheck / aggregate := false,
githubWorkflowGenerate := {
val ciContents = generateCiContents.value
val includeClean = githubWorkflowIncludeClean.value
val cleanContents = readCleanContents.value
val ciYml = ciYmlFile.value
val cleanYml = cleanYmlFile.value
IO.write(ciYml, ciContents)
if (includeClean)
IO.write(cleanYml, cleanContents)
},
githubWorkflowCheck := {
val expectedCiContents = generateCiContents.value
val includeClean = githubWorkflowIncludeClean.value
val expectedCleanContents = readCleanContents.value
val ciYml = ciYmlFile.value
val cleanYml = cleanYmlFile.value
val log = state.value.log
def reportMismatch(file: File, expected: String, actual: String): Unit = {
log.error(s"Expected:\n$expected")
log.error(s"Actual:\n${diff(expected, actual)}")
sys.error(
s"${file.getName} does not contain contents that would have been generated by sbt-github-actions; try running githubWorkflowGenerate")
}
def compare(file: File, expected: String): Unit = {
val actual = IO.read(file)
if (expected != actual) {
reportMismatch(file, expected, actual)
}
}
compare(ciYml, expectedCiContents)
if (includeClean)
compare(cleanYml, expectedCleanContents)
}
)
private[sbt] def expandMatrix(
oses: List[String],
scalas: List[String],
javas: List[JavaSpec],
matrixAdds: Map[String, List[String]],
includes: List[MatrixInclude],
excludes: List[MatrixExclude]
): List[List[String]] = {
val keys = "os" :: "scala" :: "java" :: matrixAdds.keys.toList.sorted
val matrix =
matrixAdds + ("os" -> oses) + ("scala" -> scalas) + ("java" -> javas.map(_.render))
// expand the matrix
keys
.foldLeft(List(List.empty[String])) { (cells, key) =>
val values = matrix.getOrElse(key, Nil)
cells.flatMap { cell => values.map(v => cell ::: v :: Nil) }
}
.filterNot { cell => // remove the excludes
val job = keys.zip(cell).toMap
excludes.exists { // there is an exclude that matches the current job
case MatrixExclude(matching) => matching.toSet.subsetOf(job.toSet)
}
} ::: includes.map { // add the includes
case MatrixInclude(matching, additions) =>
// yoloing here, but let's wait for the bug report
keys.map(matching) ::: additions.values.toList
}
}
private[sbt] def diff(expected: String, actual: String): String = {
val expectedLines = expected.split("\n", -1)
val actualLines = actual.split("\n", -1)
val (lines, _) =
expectedLines.zipAll(actualLines, "", "").foldLeft((Vector.empty[String], false)) {
case ((acc, foundDifference), (expectedLine, actualLine))
if expectedLine == actualLine =>
(acc :+ actualLine, foundDifference)
case ((acc, false), ("", actualLine)) =>
val previousLineLength = acc.lastOption.map(_.length).getOrElse(0)
val padding = " " * previousLineLength
val highlight = s"$padding^ (additional lines)"
(acc :+ highlight :+ actualLine, true)
case ((acc, false), (_, "")) =>
val previousLineLength = acc.lastOption.map(_.length).getOrElse(0)
val padding = " " * previousLineLength
val highlight = s"$padding^ (missing lines)"
(acc :+ highlight, true)
case ((acc, false), (expectedLine, actualLine)) =>
val sameCount =
expectedLine.zip(actualLine).takeWhile { case (a, b) => a == b }.length
val padding = " " * sameCount
val highlight = s"$padding^ (different character)"
(acc :+ actualLine :+ highlight, true)
case ((acc, true), (_, "")) =>
(acc, true)
case ((acc, true), (_, actualLine)) =>
(acc :+ actualLine, true)
}
lines.mkString("\n")
}
}