From 97f39009d9e456a539b774ce3fc151c9a2cc2c40 Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Fri, 25 Oct 2024 23:32:14 +0200 Subject: [PATCH] simplify --- .../main/scala/zio/sbt/ZioSbtCiPlugin.scala | 28 ++++----- .../scala/zio/sbt/githubactions/model.scala | 9 ++- .../githubactionsnative/ScalaWorkflow.scala | 2 +- .../zio/sbt/githubactionsnative/model.scala | 41 +++++++++---- .../zio/sbt/githubactionsnative/package.scala | 60 ------------------- 5 files changed, 50 insertions(+), 90 deletions(-) delete mode 100644 zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/package.scala diff --git a/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala b/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala index 5c30471d..1af4f868 100644 --- a/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala +++ b/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala @@ -100,7 +100,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val checkWebsiteBuildProcess = ciCheckWebsiteBuildProcess.value Seq( - "build" -> JobValue( + Job( name = "Build", continueOnError = true, steps = { @@ -127,7 +127,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val lint = Lint.value Seq( - "lint" -> JobValue( + Job( name = "Lint", steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ Seq(checkout, SetupLibuv, SetupJava(javaVersion), CacheDependencies) ++ checkGithubWorkflow.flatMap( @@ -156,7 +156,7 @@ object ZioSbtCiPlugin extends AutoPlugin { versions.contains(scalaVersion) }.map(e => e._1 + "/test").mkString(" ")}" - "test" -> JobValue( + Job( name = "Test", strategy = Some( Strategy( @@ -218,7 +218,7 @@ object ZioSbtCiPlugin extends AutoPlugin { } val FlattenTests = - "test" -> JobValue( + Job( name = "Test", strategy = Some( Strategy( @@ -290,7 +290,7 @@ object ZioSbtCiPlugin extends AutoPlugin { ) val DefaultTestStrategy = - "test" -> JobValue( + Job( name = "Test", strategy = Some( Strategy( @@ -322,7 +322,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val pullRequestApprovalJobs = ciPullRequestApprovalJobs.value Seq( - "ci" -> JobValue( + Job( name = "ci", needs = Some(pullRequestApprovalJobs), steps = Seq( @@ -344,7 +344,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val generateReadme = GenerateReadme.value Seq( - "update-readme" -> JobValue( + Job( name = "Update README", `if` = updateReadmeCondition orElse Some(Condition.Expression("github.event_name == 'push'")), steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ @@ -429,7 +429,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val jobs = ciReleaseApprovalJobs.value Seq( - "release" -> JobValue( + Job( name = "Release", needs = Some(jobs), `if` = Some(Condition.Expression("github.event_name != 'pull_request'")), @@ -453,7 +453,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val publishToNpmRegistry = PublishToNpmRegistry.value Seq( - "release-docs" -> JobValue( + Job( name = "Release Docs", needs = Some(Seq("release")), `if` = Some( @@ -472,7 +472,7 @@ object ZioSbtCiPlugin extends AutoPlugin { publishToNpmRegistry ) ), - "notify-docs-release" -> JobValue( + Job( name = "Notify Docs Release", needs = Some(Seq("release-docs")), `if` = Some( @@ -542,10 +542,10 @@ object ZioSbtCiPlugin extends AutoPlugin { pullRequest = Some(Trigger.PullRequest(branchesIgnore = Some(Seq(Branch.Named("gh-pages"))))) ) ), - jobs = ListMap.empty[ - String, - JobValue - ] ++ buildJobs ++ lintJobs ++ testJobs ++ updateReadmeJobs ++ reportSuccessful ++ releaseJobs ++ postReleaseJobs + jobs = ListMap( + (buildJobs ++ lintJobs ++ testJobs ++ updateReadmeJobs ++ reportSuccessful ++ releaseJobs ++ postReleaseJobs) + .map(job => job.id -> job): _* + ) ) val yaml: String = workflow.toJsonAST.flatMap(_.toYaml(yamlOptions).left.map(_.getMessage())) match { diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/model.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/model.scala index 7414d321..09357068 100644 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/model.scala +++ b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/model.scala @@ -281,9 +281,8 @@ case class Job( object Job { implicit class JobOps(private val job: Job) extends AnyVal { - def toNative: ghnative.Job = ( - job.id, - ghnative.JobValue( + def toNative: ghnative.Job = + ghnative.Job( name = job.name, runsOn = job.runsOn, timeoutMinutes = Some(job.timeoutMinutes), @@ -294,7 +293,7 @@ object Job { services = Some(job.services.map(_.toNative)).filter(_.nonEmpty), `if` = job.condition.map(_.toNative) ) - ) + } } @@ -335,7 +334,7 @@ object Workflow { ) ).filter(_ => triggers.nonEmpty) }, - jobs = ListMap(workflow.jobs.map(_.toNative): _*) + jobs = ListMap(workflow.jobs.map(_.toNative).map(job => job.id -> job): _*) ) } } diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/ScalaWorkflow.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/ScalaWorkflow.scala index 64865c93..8268b4d4 100644 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/ScalaWorkflow.scala +++ b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/ScalaWorkflow.scala @@ -250,7 +250,7 @@ object ScalaWorkflow { operatingSystems: Seq[OS] = Seq(OS.UbuntuLatest), javaVersions: Seq[JavaVersion] = Seq(JDK11) ): Job = - job._1 -> job._2.copy( + job.copy( strategy = Some( Strategy( matrix = Map( diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/model.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/model.scala index d7099eda..63f12a8a 100644 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/model.scala +++ b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/model.scala @@ -272,7 +272,7 @@ object Service { } @jsonMemberNames(KebabCase) -case class JobValue( +case class Job( name: String, runsOn: String = "ubuntu-latest", timeoutMinutes: Option[Int] = None, @@ -283,22 +283,43 @@ case class JobValue( `if`: Option[Condition] = None, steps: Seq[Step.SingleStep] = Seq.empty ) { - def withStrategy(strategy: Strategy): JobValue = + + def id: String = name.toLowerCase().replace(" ", "-") + + def withStrategy(strategy: Strategy): Job = copy(strategy = Some(strategy)) - def withSteps(steps: Step*): JobValue = steps match { + def withSteps(steps: Step*): Job = steps match { case steps: Step.StepSequence => copy(steps = steps.flatten) case step: Step.SingleStep => copy(steps = step :: Nil) } - def withServices(services: Service*): JobValue = + def withServices(services: Service*): Job = copy(services = Some(services)) + + def withRunsOn(runsOn: String): Job = + copy(runsOn = runsOn) + + def withName(name: String): Job = + copy(name = name) + + def withTimeoutMinutes(timeoutMinutes: Option[Int]): Job = + copy(timeoutMinutes = timeoutMinutes) + + def withContinueOnError(continueOnError: Boolean): Job = + copy(continueOnError = continueOnError) + + def withStrategy(strategy: Option[Strategy]): Job = + copy(strategy = strategy) + + def withNeeds(needs: Option[Seq[String]]): Job = + copy(needs = needs) } -object JobValue { - implicit val codec: JsonCodec[JobValue] = DeriveJsonCodec.gen[JobValue] +object Job { + implicit val codec: JsonCodec[Job] = DeriveJsonCodec.gen[Job] } @jsonMemberNames(KebabCase) @@ -318,18 +339,18 @@ case class Workflow( concurrency: Concurrency = Concurrency( "${{ github.workflow }}-${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.run_id || github.ref }}" ), - jobs: ListMap[String, JobValue] = ListMap.empty + jobs: ListMap[String, Job] = ListMap.empty ) { def withOn(on: Triggers): Workflow = copy(on = Some(on)) - def withJobs(jobs: (String, JobValue)*): Workflow = + def withJobs(jobs: (String, Job)*): Workflow = copy(jobs = ListMap(jobs: _*)) - def addJob(job: (String, JobValue)): Workflow = + def addJob(job: (String, Job)): Workflow = copy(jobs = jobs + job) - def addJobs(newJobs: (String, JobValue)*): Workflow = + def addJobs(newJobs: (String, Job)*): Workflow = copy(jobs = jobs ++ newJobs) } diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/package.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/package.scala deleted file mode 100644 index b9efa33e..00000000 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactionsnative/package.scala +++ /dev/null @@ -1,60 +0,0 @@ -package zio.sbt - -package object githubactionsnative { - type Job = (String, JobValue) - - object Job { - def apply( - id: String, - name: String, - runsOn: String = "ubuntu-latest", - timeoutMinutes: Option[Int] = None, - continueOnError: Boolean = false, - strategy: Option[Strategy] = None, - needs: Option[Seq[String]] = None, - services: Option[Seq[Service]] = None, - `if`: Option[Condition] = None, - steps: Seq[Step.SingleStep] = Seq.empty - ): Job = id -> JobValue( - name, - runsOn, - timeoutMinutes, - continueOnError, - strategy, - needs, - services, - `if`, - steps - ) - } - - implicit class JobOps(job: Job) { - - def withRunsOn(runsOn: String): Job = - job._1 -> job._2.copy(runsOn = runsOn) - - def withName(name: String): Job = - job._1 -> job._2.copy(name = name) - - def withTimeoutMinutes(timeoutMinutes: Option[Int]): Job = - job._1 -> job._2.copy(timeoutMinutes = timeoutMinutes) - - def withContinueOnError(continueOnError: Boolean): Job = - job._1 -> job._2.copy(continueOnError = continueOnError) - - def withStrategy(strategy: Option[Strategy]): Job = - job._1 -> job._2.copy(strategy = strategy) - - def withNeeds(needs: Option[Seq[String]]): Job = - job._1 -> job._2.copy(needs = needs) - - def withStrategy(strategy: Strategy): Job = - job._1 -> job._2.withStrategy(strategy) - - def withServices(services: Service*): Job = - job._1 -> job._2.withServices(services: _*) - - def withSteps(steps: Seq[Step.SingleStep]): Job = - job._1 -> job._2.withSteps(steps: _*) - } -}