Skip to content

Commit c9631cc

Browse files
committed
Cleaned up redundant empty string checks, added prefix, suffix to some compile
1 parent d7d764f commit c9631cc

File tree

2 files changed

+45
-90
lines changed

2 files changed

+45
-90
lines changed

github-actions/src/main/scala/org/typelevel/sbt/gha/Concurrency.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@ package org.typelevel.sbt.gha
1818

1919
sealed abstract class Concurrency {
2020
def group: String
21-
def cancelInProgress: Option[Boolean]
21+
def cancelInProgress: Option[String]
2222
}
2323

2424
object Concurrency {
25+
def apply(group: String): Concurrency =
26+
Impl(group, None)
2527

26-
def apply(group: String, cancelInProgress: Option[Boolean] = None): Concurrency =
28+
def apply(group: String, cancelInProgress: Boolean): Concurrency =
29+
apply(group, Some(cancelInProgress))
30+
31+
def apply(group: String, cancelInProgress: Option[Boolean]): Concurrency =
32+
Impl(group, cancelInProgress.map(_.toString))
33+
34+
def apply(group: String, cancelInProgress: Option[String])(
35+
implicit dummy: DummyImplicit): Concurrency =
2736
Impl(group, cancelInProgress)
2837

29-
private final case class Impl(group: String, cancelInProgress: Option[Boolean])
38+
private final case class Impl(group: String, cancelInProgress: Option[String])
3039
extends Concurrency {
3140
override def productPrefix = "Concurrency"
3241
}

github-actions/src/main/scala/org/typelevel/sbt/gha/GenerativePlugin.scala

Lines changed: 33 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ object GenerativePlugin extends AutoPlugin {
228228
concurrency.cancelInProgress match {
229229
case Some(value) =>
230230
val fields = s"""group: ${wrap(concurrency.group)}
231-
|cancel-in-progress: ${wrap(value.toString)}""".stripMargin
231+
|cancel-in-progress: ${wrap(value)}""".stripMargin
232232
s"""concurrency:
233233
|${indent(fields, 1)}""".stripMargin
234234

@@ -247,19 +247,22 @@ object GenerativePlugin extends AutoPlugin {
247247
s"environment: ${wrap(environment.name)}"
248248
}
249249

250-
def compileEnv(env: Map[String, String]): String = compileMap(env, prefix = "env")
251-
def compileMap(data: Map[String, String], prefix: String): String =
250+
def compileEnv(env: Map[String, String], prefix: String = "", suffix: String = ""): String =
251+
compileMap(env, prefix = s"${prefix}env", suffix = suffix)
252+
def compileMap(data: Map[String, String], prefix: String = "", suffix: String = ""): String =
252253
if (data.isEmpty) {
253254
""
254255
} else {
255-
val rendered = data.map {
256-
case (key, value) =>
257-
if (!isSafeString(key) || key.indexOf(' ') >= 0)
258-
sys.error(s"'$key' is not a valid environment variable name")
256+
val rendered = data
257+
.map {
258+
case (key, value) =>
259+
if (!isSafeString(key) || key.indexOf(' ') >= 0)
260+
sys.error(s"'$key' is not a valid environment variable name")
259261

260-
s"""$key: ${wrap(value)}"""
261-
}
262-
s"""$prefix:\n${indent(rendered.mkString("\n"), 1)}"""
262+
s"""$key: ${wrap(value)}"""
263+
}
264+
.mkString("\n")
265+
s"""$prefix:\n${indent(rendered, 1)}$suffix"""
263266
}
264267

265268
def compilePermissionScope(permissionScope: PermissionScope): String = permissionScope match {
@@ -285,7 +288,10 @@ object GenerativePlugin extends AutoPlugin {
285288
case PermissionValue.None => "none"
286289
}
287290

288-
def compilePermissions(permissions: Option[Permissions]): String = {
291+
def compilePermissions(
292+
permissions: Option[Permissions],
293+
prefix: String = "",
294+
suffix: String = ""): String = {
289295
permissions match {
290296
case Some(perms) =>
291297
val rendered = perms match {
@@ -299,7 +305,7 @@ object GenerativePlugin extends AutoPlugin {
299305
}
300306
"\n" + indent(map.mkString("\n"), 1)
301307
}
302-
s"permissions:$rendered"
308+
s"${prefix}permissions:$rendered$suffix"
303309

304310
case None => ""
305311
}
@@ -317,25 +323,14 @@ object GenerativePlugin extends AutoPlugin {
317323
val renderedCond = step.cond.map(wrap).map("if: " + _ + "\n").getOrElse("")
318324
val renderedShell = if (declareShell) "shell: bash\n" else ""
319325

320-
val renderedEnvPre = compileEnv(step.env)
321-
val renderedEnv =
322-
if (renderedEnvPre.isEmpty)
323-
""
324-
else
325-
renderedEnvPre + "\n"
326+
val renderedEnv = compileEnv(step.env, suffix = "\n")
326327

327328
val renderedTimeoutMinutes =
328329
step.timeoutMinutes.map("timeout-minutes: " + _ + "\n").getOrElse("")
329330

330-
val preamblePre =
331+
val preamble: String =
331332
renderedName + renderedId + renderedCond + renderedEnv + renderedTimeoutMinutes
332333

333-
val preamble =
334-
if (preamblePre.isEmpty)
335-
""
336-
else
337-
preamblePre
338-
339334
val body = step match {
340335
case run: Run =>
341336
val renderedWorkingDirectory =
@@ -404,16 +399,8 @@ object GenerativePlugin extends AutoPlugin {
404399
renderedShell + renderedWorkingDirectory + "run: " + wrap(
405400
commands.mkString("\n")) + renderParams(params)
406401

407-
def renderParams(params: Map[String, String]): String = {
408-
val renderedParamsPre = compileMap(params, prefix = "with")
409-
val renderedParams =
410-
if (renderedParamsPre.isEmpty)
411-
""
412-
else
413-
"\n" + renderedParamsPre
414-
415-
renderedParams
416-
}
402+
def renderParams(params: Map[String, String]): String =
403+
compileMap(params, prefix = "\nwith")
417404

418405
def compileJob(job: WorkflowJob, sbt: String): String = job match {
419406
case job: WorkflowJob.Run => compileRunJob(job, sbt)
@@ -428,21 +415,9 @@ object GenerativePlugin extends AutoPlugin {
428415

429416
val renderedSecrets = job.secrets.fold("")(compileSecrets)
430417

431-
val renderedOutputs = {
432-
val renderedOutputsPre = compileMap(job.outputs, prefix = "outputs")
433-
if (renderedOutputsPre.isEmpty)
434-
""
435-
else
436-
"\n" + renderedOutputsPre
437-
}
418+
val renderedOutputs = compileMap(job.outputs, prefix = "\noutputs")
438419

439-
val renderedInputs = {
440-
val renderedInputsPre = compileMap(job.params, prefix = "with")
441-
if (renderedInputsPre.isEmpty)
442-
""
443-
else
444-
"\n" + renderedInputsPre
445-
}
420+
val renderedInputs = compileMap(job.params, prefix = "\nwith")
446421

447422
// format: off
448423
val body = s"""name: ${wrap(job.name)}${renderedNeeds}
@@ -471,7 +446,7 @@ object GenerativePlugin extends AutoPlugin {
471446
val renderedContainer = job.container match {
472447
case Some(JobContainer(image, credentials, env, volumes, ports, options)) =>
473448
if (credentials.isEmpty && env.isEmpty && volumes.isEmpty && ports.isEmpty && options.isEmpty) {
474-
"\n" + s"container: ${wrap(image)}"
449+
s"\ncontainer: ${wrap(image)}"
475450
} else {
476451
val renderedImage = s"image: ${wrap(image)}"
477452

@@ -483,11 +458,7 @@ object GenerativePlugin extends AutoPlugin {
483458
""
484459
}
485460

486-
val renderedEnv =
487-
if (env.nonEmpty)
488-
"\n" + compileEnv(env)
489-
else
490-
""
461+
val renderedEnv = compileEnv(env, prefix = "\n")
491462

492463
val renderedVolumes =
493464
if (volumes.nonEmpty)
@@ -514,31 +485,16 @@ object GenerativePlugin extends AutoPlugin {
514485
""
515486
}
516487

517-
val renderedEnvPre = compileEnv(job.env)
518-
val renderedEnv =
519-
if (renderedEnvPre.isEmpty)
520-
""
521-
else
522-
"\n" + renderedEnvPre
488+
val renderedEnv = compileMap(job.env, "\nenv")
523489

524-
val renderedOutputsPre = compileMap(job.outputs, prefix = "outputs")
525-
val renderedOutputs =
526-
if (renderedOutputsPre.isEmpty)
527-
""
528-
else
529-
"\n" + renderedOutputsPre
490+
val renderedOutputs = compileMap(job.outputs, prefix = "\noutputs")
530491

531-
val renderedPermPre = compilePermissions(job.permissions)
532-
val renderedPerm =
533-
if (renderedPermPre.isEmpty)
534-
""
535-
else
536-
"\n" + renderedPermPre
492+
val renderedPerm = compilePermissions(job.permissions, prefix = "\n")
537493

538494
val renderedTimeoutMinutes =
539495
job.timeoutMinutes.map(timeout => s"\ntimeout-minutes: $timeout").getOrElse("")
540496

541-
List("include", "exclude") foreach { key =>
497+
List("include", "exclude").foreach { key =>
542498
if (job.matrixAdds.contains(key)) {
543499
sys.error(s"key `$key` is reserved and cannot be used in an Actions matrix definition")
544500
}
@@ -681,18 +637,8 @@ object GenerativePlugin extends AutoPlugin {
681637

682638
val renderedName = name.fold("") { name => s"name: ${wrap(name)}" }
683639

684-
val renderedPermissionsPre = compilePermissions(permissions)
685-
val renderedEnvPre = compileEnv(env)
686-
val renderedEnv =
687-
if (renderedEnvPre.isEmpty)
688-
""
689-
else
690-
renderedEnvPre + "\n\n"
691-
val renderedPerm =
692-
if (renderedPermissionsPre.isEmpty)
693-
""
694-
else
695-
renderedPermissionsPre + "\n\n"
640+
val renderedEnv = compileEnv(env, suffix = "\n\n")
641+
val renderedPerm = compilePermissions(permissions, suffix = "\n\n")
696642

697643
val renderedConcurrency =
698644
concurrency.map(compileConcurrency).map("\n" + _ + "\n\n").getOrElse("")
@@ -725,7 +671,7 @@ object GenerativePlugin extends AutoPlugin {
725671
githubWorkflowConcurrency := Some(
726672
Concurrency(
727673
group = s"$${{ github.workflow }} @ $${{ github.ref }}",
728-
cancelInProgress = Some(true))
674+
cancelInProgress = true)
729675
),
730676
githubWorkflowBuildMatrixFailFast := None,
731677
githubWorkflowBuildMatrixAdditions := Map(),

0 commit comments

Comments
 (0)