From b2a7d1a85877c838457979bc72ed0b21cf1cef1c Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Thu, 17 Oct 2024 22:02:26 +0200 Subject: [PATCH 1/3] refactor: migrate to zio-json --- .github/workflows/ci.yml | 379 +++++++++--------- README.md | 14 +- docs/index.md | 14 +- project/Versions.scala | 8 +- project/build.properties | 2 +- project/plugins.sbt | 17 +- zio-sbt-ci/build.sbt | 5 +- .../main/scala/zio/sbt/ZioSbtCiPlugin.scala | 280 ++++++------- zio-sbt-ecosystem/build.sbt | 14 +- .../scala/zio/sbt/ScalaCompilerSettings.scala | 5 +- .../src/main/scala/zio/sbt/Versions.scala | 8 +- .../.github/workflows/ci.yml | 2 +- zio-sbt-githubactions/build.sbt | 5 +- .../zio/sbt/githubactions/ScalaWorkflow.scala | 81 ++-- .../scala/zio/sbt/githubactions/model.scala | 332 +++++++-------- .../scala/zio/sbt/githubactions/package.scala | 5 + zio-sbt-website/build.sbt | 7 +- 17 files changed, 609 insertions(+), 569 deletions(-) create mode 100644 zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b177da0f..3c69b756 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,16 +5,15 @@ name: CI env: JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags 'on': - workflow_dispatch: {} release: types: - - published - push: - branches: - - main + - published pull_request: branches-ignore: - - gh-pages + - gh-pages + push: + branches: + - main concurrency: group: ${{ github.workflow }}-${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.run_id || github.ref }} cancel-in-progress: true @@ -24,234 +23,234 @@ jobs: runs-on: ubuntu-latest continue-on-error: true steps: - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: Install libuv - run: sudo apt-get update && sudo apt-get install -y libuv1-dev - - name: Setup Scala - uses: actions/setup-java@v4 - with: - distribution: corretto - java-version: '17' - check-latest: true - - name: Cache Dependencies - uses: coursier/cache-action@v6 - - name: Check all code compiles - run: sbt +Test/compile - - name: Check artifacts build process - run: sbt +publishLocal - - name: Check website build process - run: sbt docs/clean; sbt docs/buildWebsite + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: '17' + check-latest: 'true' + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Check all code compiles + run: sbt +Test/compile + - name: Check artifacts build process + run: sbt +publishLocal + - name: Check website build process + run: sbt docs/clean; sbt docs/buildWebsite lint: name: Lint runs-on: ubuntu-latest continue-on-error: false steps: - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: Install libuv - run: sudo apt-get update && sudo apt-get install -y libuv1-dev - - name: Setup Scala - uses: actions/setup-java@v4 - with: - distribution: corretto - java-version: '17' - check-latest: true - - name: Cache Dependencies - uses: coursier/cache-action@v6 - - name: Check if the site workflow is up to date - run: sbt ciCheckGithubWorkflow - - name: Lint - run: sbt lint + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: '17' + check-latest: 'true' + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Check if the site workflow is up to date + run: sbt ciCheckGithubWorkflow + - name: Lint + run: sbt lint test: name: Test runs-on: ubuntu-latest continue-on-error: false strategy: - fail-fast: false matrix: java: - - '11' - - '17' - - '21' + - '11' + - '17' + - '21' + fail-fast: false steps: - - name: Install libuv - run: sudo apt-get update && sudo apt-get install -y libuv1-dev - - name: Setup Scala - uses: actions/setup-java@v4 - with: - distribution: corretto - java-version: ${{ matrix.java }} - check-latest: true - - name: Cache Dependencies - uses: coursier/cache-action@v6 - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: Test - run: sbt +test + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: ${{ matrix.java }} + check-latest: 'true' + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Test + run: sbt +test update-readme: name: Update README runs-on: ubuntu-latest continue-on-error: false if: ${{ github.event_name == 'push' }} steps: - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: Install libuv - run: sudo apt-get update && sudo apt-get install -y libuv1-dev - - name: Setup Scala - uses: actions/setup-java@v4 - with: - distribution: corretto - java-version: '17' - check-latest: true - - name: Cache Dependencies - uses: coursier/cache-action@v6 - - name: Generate Readme - run: sbt docs/generateReadme - - name: Commit Changes - run: | - git config --local user.email "zio-assistant[bot]@users.noreply.github.com" - git config --local user.name "ZIO Assistant" - git add README.md - git commit -m "Update README.md" || echo "No changes to commit" - - name: Generate Token - id: generate-token - uses: zio/generate-github-app-token@v1.0.0 - with: - app_id: ${{ secrets.APP_ID }} - app_private_key: ${{ secrets.APP_PRIVATE_KEY }} - - name: Create Pull Request - id: cpr - uses: peter-evans/create-pull-request@v6 - with: - body: |- - Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: '17' + check-latest: 'true' + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Generate Readme + run: sbt docs/generateReadme + - name: Commit Changes + run: | + git config --local user.email "zio-assistant[bot]@users.noreply.github.com" + git config --local user.name "ZIO Assistant" + git add README.md + git commit -m "Update README.md" || echo "No changes to commit" + - name: Generate Token + id: generate-token + uses: zio/generate-github-app-token@v1.0.0 + with: + app_id: ${{ secrets.APP_ID }} + app_private_key: ${{ secrets.APP_PRIVATE_KEY }} + - name: Create Pull Request + id: cpr + uses: peter-evans/create-pull-request@v6 + with: + body: |- + Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. - I will automatically update the README.md file whenever there is new change for README.md, e.g. - - After each release, I will update the version in the installation section. - - After any changes to the "docs/index.md" file, I will update the README.md file accordingly. - branch: zio-sbt-website/update-readme - commit-message: Update README.md - token: ${{ steps.generate-token.outputs.token }} - delete-branch: true - title: Update README.md - - name: Approve PR - if: ${{ steps.cpr.outputs.pull-request-number }} - run: gh pr review "$PR_URL" --approve - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_URL: ${{ steps.cpr.outputs.pull-request-url }} - - name: Enable Auto-Merge - if: ${{ steps.cpr.outputs.pull-request-number }} - run: gh pr merge --auto --squash "$PR_URL" || gh pr merge --squash "$PR_URL" - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_URL: ${{ steps.cpr.outputs.pull-request-url }} + I will automatically update the README.md file whenever there is new change for README.md, e.g. + - After each release, I will update the version in the installation section. + - After any changes to the "docs/index.md" file, I will update the README.md file accordingly. + branch: zio-sbt-website/update-readme + commit-message: Update README.md + token: ${{ steps.generate-token.outputs.token }} + delete-branch: 'true' + title: Update README.md + - name: Approve PR + if: ${{ steps.cpr.outputs.pull-request-number }} + run: gh pr review "$PR_URL" --approve + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_URL: ${{ steps.cpr.outputs.pull-request-url }} + - name: Enable Auto-Merge + if: ${{ steps.cpr.outputs.pull-request-number }} + run: gh pr merge --auto --squash "$PR_URL" || gh pr merge --squash "$PR_URL" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_URL: ${{ steps.cpr.outputs.pull-request-url }} ci: name: ci runs-on: ubuntu-latest continue-on-error: false needs: - - lint - - test - - build + - lint + - test + - build steps: - - name: Report Successful CI - run: echo "ci passed" + - name: Report Successful CI + run: echo "ci passed" release: name: Release runs-on: ubuntu-latest continue-on-error: false needs: - - ci + - ci if: ${{ github.event_name != 'pull_request' }} steps: - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: Install libuv - run: sudo apt-get update && sudo apt-get install -y libuv1-dev - - name: Setup Scala - uses: actions/setup-java@v4 - with: - distribution: corretto - java-version: '17' - check-latest: true - - name: Cache Dependencies - uses: coursier/cache-action@v6 - - name: Release - run: sbt ci-release - env: - PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} - PGP_SECRET: ${{ secrets.PGP_SECRET }} - SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} - SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: '17' + check-latest: 'true' + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Release + run: sbt ci-release + env: + PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} + PGP_SECRET: ${{ secrets.PGP_SECRET }} + SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} + SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} release-docs: name: Release Docs runs-on: ubuntu-latest continue-on-error: false needs: - - release + - release if: ${{ ((github.event_name == 'release') && (github.event.action == 'published')) || (github.event_name == 'workflow_dispatch') }} steps: - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: Install libuv - run: sudo apt-get update && sudo apt-get install -y libuv1-dev - - name: Setup Scala - uses: actions/setup-java@v4 - with: - distribution: corretto - java-version: '17' - check-latest: true - - name: Cache Dependencies - uses: coursier/cache-action@v6 - - name: Setup NodeJs - uses: actions/setup-node@v4 - with: - node-version: 16.x - registry-url: https://registry.npmjs.org - - name: Publish Docs to NPM Registry - run: sbt docs/publishToNpm - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: '17' + check-latest: 'true' + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Setup NodeJs + uses: actions/setup-node@v4 + with: + node-version: 16.x + registry-url: https://registry.npmjs.org + - name: Publish Docs to NPM Registry + run: sbt docs/publishToNpm + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} notify-docs-release: name: Notify Docs Release runs-on: ubuntu-latest continue-on-error: false needs: - - release-docs + - release-docs if: ${{ (github.event_name == 'release') && (github.event.action == 'published') }} steps: - - name: Git Checkout - uses: actions/checkout@v4 - with: - fetch-depth: '0' - - name: notify the main repo about the new release of docs package - run: | - PACKAGE_NAME=$(cat docs/package.json | grep '"name"' | awk -F'"' '{print $4}') - PACKAGE_VERSION=$(npm view $PACKAGE_NAME version) - curl -L \ - -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: token ${{ secrets.PAT_TOKEN }}"\ - https://api.github.com/repos/zio/zio/dispatches \ - -d '{ - "event_type":"update-docs", - "client_payload":{ - "package_name":"'"${PACKAGE_NAME}"'", - "package_version": "'"${PACKAGE_VERSION}"'" - } - }' + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: notify the main repo about the new release of docs package + run: | + PACKAGE_NAME=$(cat docs/package.json | grep '"name"' | awk -F'"' '{print $4}') + PACKAGE_VERSION=$(npm view $PACKAGE_NAME version) + curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${{ secrets.PAT_TOKEN }}"\ + https://api.github.com/repos/zio/zio/dispatches \ + -d '{ + "event_type":"update-docs", + "client_payload":{ + "package_name":"'"${PACKAGE_NAME}"'", + "package_version": "'"${PACKAGE_VERSION}"'" + } + }' diff --git a/README.md b/README.md index a619532d..7e49c0e9 100644 --- a/README.md +++ b/README.md @@ -168,12 +168,12 @@ In some cases, we may have multiple submodules in our project and we want to tes The `ciTargetScalaVersions` setting key is used to define a mapping of project names to the Scala versions that should be used for testing phase of continuous integration (CI). -For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.19`, and for the "submoduleB" we want to test it against Scala `2.12.19` and `2.13.13` and `3.3.3`, We can define the `ciTargetScalaVersions` setting as follows: +For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.20`, and for the "submoduleB" we want to test it against Scala `2.12.20` and `2.13.15` and `3.3.4`, We can define the `ciTargetScalaVersions` setting as follows: ```scala ThisBuild / ciTargetScalaVersions := Map( - "submoduleA" -> Seq("2.12.19"), - "submoduleB" -> Seq("2.12.19", "2.13.13", "3.3.3") + "submoduleA" -> Seq("2.12.20"), + "submoduleB" -> Seq("2.12.20", "2.13.15", "3.3.4") ) ``` @@ -206,10 +206,10 @@ test: matrix: java: ['11', '17', '21'] scala-project: - - ++2.12.19 submoduleA - - ++2.12.19 submoduleB - - ++2.13.13 submoduleB - - ++3.3.3 submoduleB + - ++2.12.20 submoduleA + - ++2.12.20 submoduleB + - ++2.13.15 submoduleB + - ++3.3.4 submoduleB steps: - name: Install libuv run: sudo apt-get update && sudo apt-get install -y libuv1-dev diff --git a/docs/index.md b/docs/index.md index 31d06d14..5489d349 100644 --- a/docs/index.md +++ b/docs/index.md @@ -167,12 +167,12 @@ In some cases, we may have multiple submodules in our project and we want to tes The `ciTargetScalaVersions` setting key is used to define a mapping of project names to the Scala versions that should be used for testing phase of continuous integration (CI). -For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.19`, and for the "submoduleB" we want to test it against Scala `2.12.19` and `2.13.13` and `3.3.3`, We can define the `ciTargetScalaVersions` setting as follows: +For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.20`, and for the "submoduleB" we want to test it against Scala `2.12.20` and `2.13.15` and `3.3.4`, We can define the `ciTargetScalaVersions` setting as follows: ```scala ThisBuild / ciTargetScalaVersions := Map( - "submoduleA" -> Seq("2.12.19"), - "submoduleB" -> Seq("2.12.19", "2.13.13", "3.3.3") + "submoduleA" -> Seq("2.12.20"), + "submoduleB" -> Seq("2.12.20", "2.13.15", "3.3.4") ) ``` @@ -205,10 +205,10 @@ test: matrix: java: ['11', '17', '21'] scala-project: - - ++2.12.19 submoduleA - - ++2.12.19 submoduleB - - ++2.13.13 submoduleB - - ++3.3.3 submoduleB + - ++2.12.20 submoduleA + - ++2.12.20 submoduleB + - ++2.13.15 submoduleB + - ++3.3.4 submoduleB steps: - name: Install libuv run: sudo apt-get update && sudo apt-get install -y libuv1-dev diff --git a/project/Versions.scala b/project/Versions.scala index 03da9412..7a8351bf 100644 --- a/project/Versions.scala +++ b/project/Versions.scala @@ -1,6 +1,6 @@ object Versions { - val Scala212 = "2.12.19" - val Scala213 = "2.13.13" - val Scala3 = "3.3.3" - val zio = "2.0.21" + val Scala212 = "2.12.20" + val Scala213 = "2.13.15" + val Scala3 = "3.3.4" + val zio = "2.1.9" } diff --git a/project/build.properties b/project/build.properties index ee4c672c..23f7d979 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.10.1 +sbt.version = 1.10.2 diff --git a/project/plugins.sbt b/project/plugins.sbt index b9292898..8260121f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,9 +1,9 @@ // Build Server Plugins -addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.6.0") +addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "2.0.3") // Linting Plugins addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") -addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.12.1") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0") addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1") // Versioning and Release Plugins @@ -11,20 +11,21 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") // Docs Plugins -addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.4") +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.6.1") addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0") // Cross-Compiler Plugins -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.17.0") addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2") -addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.4") +addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.5") addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2") addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.2") // Benchmarking Plugins addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7") -libraryDependencies += "org.snakeyaml" % "snakeyaml-engine" % "2.7" -libraryDependencies += "dev.zio" %% "zio" % "2.1.8" -libraryDependencies += "io.circe" %% "circe-yaml" % "0.16.0" +libraryDependencies += "org.snakeyaml" % "snakeyaml-engine" % "2.8" +libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" +libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" diff --git a/zio-sbt-ci/build.sbt b/zio-sbt-ci/build.sbt index b7d36a60..938d170c 100644 --- a/zio-sbt-ci/build.sbt +++ b/zio-sbt-ci/build.sbt @@ -1,2 +1,3 @@ -libraryDependencies += "dev.zio" %% "zio" % "2.1.8" -libraryDependencies += "io.circe" %% "circe-yaml" % "0.16.0" +libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" +libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" 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 70371ac6..b6d29217 100644 --- a/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala +++ b/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala @@ -15,14 +15,14 @@ */ package zio.sbt +import scala.collection.immutable.ListMap import scala.language.experimental.macros import scala.sys.process._ -import io.circe._ -import io.circe.syntax._ -import io.circe.yaml.Printer.{LineBreak, YamlVersion} import sbt.{Def, io => _, _} +import zio.json._ +import zio.json.yaml._ import zio.sbt.githubactions.Step.SingleStep import zio.sbt.githubactions.{Job, Step, _} @@ -100,8 +100,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val checkWebsiteBuildProcess = ciCheckWebsiteBuildProcess.value Seq( - Job( - id = "build", + "build" -> JobValue( name = "Build", continueOnError = true, steps = { @@ -111,7 +110,9 @@ object ZioSbtCiPlugin extends AutoPlugin { SetupLibuv, SetupJava(javaVersion), CacheDependencies - ) ++ checkAllCodeCompiles ++ checkArtifactBuildProcess ++ checkWebsiteBuildProcess + ) ++ checkAllCodeCompiles.flatMap(_.flatten) ++ checkArtifactBuildProcess.flatMap( + _.flatten + ) ++ checkWebsiteBuildProcess.flatMap(_.flatten) } ) ) @@ -126,11 +127,12 @@ object ZioSbtCiPlugin extends AutoPlugin { val lint = Lint.value Seq( - Job( - id = "lint", + "lint" -> JobValue( name = "Lint", steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ - Seq(checkout, SetupLibuv, SetupJava(javaVersion), CacheDependencies) ++ checkGithubWorkflow ++ Seq(lint) + Seq(checkout, SetupLibuv, SetupJava(javaVersion), CacheDependencies) ++ checkGithubWorkflow.flatMap( + _.flatten + ) ++ Seq(lint) ) ) } @@ -154,8 +156,7 @@ object ZioSbtCiPlugin extends AutoPlugin { versions.contains(scalaVersion) }.map(e => e._1 + "/test").mkString(" ")}" - Job( - id = "test", + "test" -> JobValue( name = "Test", strategy = Some( Strategy( @@ -177,7 +178,7 @@ object ZioSbtCiPlugin extends AutoPlugin { scalaVersionMatrix.values.toSeq.flatten.distinct.map { scalaVersion: String => Step.SingleStep( name = "Test", - condition = Some(Condition.Expression(s"matrix.scala == '$scalaVersion'")), + `if` = Some(Condition.Expression(s"matrix.scala == '$scalaVersion'")), run = Some( prefixJobs + "sbt ++${{ matrix.scala }}" + makeTests( scalaVersion @@ -200,7 +201,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Seq( Step.SingleStep( name = "Test", - condition = Some( + `if` = Some( Condition.Expression(s"matrix.java == '$javaPlatform'") && Condition.Expression( s"matrix.scala == '$scalaVersion'" ) @@ -217,8 +218,7 @@ object ZioSbtCiPlugin extends AutoPlugin { } val FlattenTests = - Job( - id = "test", + "test" -> JobValue( name = "Test", strategy = Some( Strategy( @@ -252,46 +252,45 @@ object ZioSbtCiPlugin extends AutoPlugin { SetupLibuv, SetupJava("${{ matrix.java }}"), CacheDependencies, - checkout, + checkout + ) ++ ( if (javaPlatformMatrix.values.toSet.isEmpty) { - Step.SingleStep( - name = "Test", - run = Some(prefixJobs + "sbt ${{ matrix.scala-project }}/test") + Seq( + Step.SingleStep( + name = "Test", + run = Some(prefixJobs + "sbt ${{ matrix.scala-project }}/test") + ) ) } else { - Step.StepSequence( - Seq( - Step.SingleStep( - name = "Java 11 Tests", - condition = Some(Condition.Expression("matrix.java == '11'")), - run = Some( - prefixJobs + "sbt ${{ matrix.scala-project-java11 }}/test" - ) - ), - Step.SingleStep( - name = "Java 17 Tests", - condition = Some(Condition.Expression("matrix.java == '17'")), - run = Some( - prefixJobs + "sbt ${{ matrix.scala-project-java17 }}/test" - ) - ), - Step.SingleStep( - name = "Java 21 Tests", - condition = Some(Condition.Expression("matrix.java == '21'")), - run = Some( - prefixJobs + "sbt ${{ matrix.scala-project-java21 }}/test" - ) + Seq( + Step.SingleStep( + name = "Java 11 Tests", + `if` = Some(Condition.Expression("matrix.java == '11'")), + run = Some( + prefixJobs + "sbt ${{ matrix.scala-project-java11 }}/test" + ) + ), + Step.SingleStep( + name = "Java 17 Tests", + `if` = Some(Condition.Expression("matrix.java == '17'")), + run = Some( + prefixJobs + "sbt ${{ matrix.scala-project-java17 }}/test" + ) + ), + Step.SingleStep( + name = "Java 21 Tests", + `if` = Some(Condition.Expression("matrix.java == '21'")), + run = Some( + prefixJobs + "sbt ${{ matrix.scala-project-java21 }}/test" ) ) ) - } ) ) val DefaultTestStrategy = - Job( - id = "test", + "test" -> JobValue( name = "Test", strategy = Some( Strategy( @@ -323,10 +322,9 @@ object ZioSbtCiPlugin extends AutoPlugin { val pullRequestApprovalJobs = ciPullRequestApprovalJobs.value Seq( - Job( - id = "ci", + "ci" -> JobValue( name = "ci", - need = pullRequestApprovalJobs, + needs = Some(pullRequestApprovalJobs), steps = Seq( SingleStep( name = "Report Successful CI", @@ -346,10 +344,9 @@ object ZioSbtCiPlugin extends AutoPlugin { val generateReadme = GenerateReadme.value Seq( - Job( - id = "update-readme", + "update-readme" -> JobValue( name = "Update README", - condition = updateReadmeCondition orElse Some(Condition.Expression("github.event_name == 'push'")), + `if` = updateReadmeCondition orElse Some(Condition.Expression("github.event_name == 'push'")), steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ Seq( checkout, @@ -369,44 +366,52 @@ object ZioSbtCiPlugin extends AutoPlugin { name = "Generate Token", id = Some("generate-token"), uses = Some(ActionRef(V("zio/generate-github-app-token"))), - parameters = Map( - "app_id" -> "${{ secrets.APP_ID }}".asJson, - "app_private_key" -> "${{ secrets.APP_PRIVATE_KEY }}".asJson + `with` = Some( + Map( + "app_id" -> "${{ secrets.APP_ID }}", + "app_private_key" -> "${{ secrets.APP_PRIVATE_KEY }}" + ) ) ), Step.SingleStep( name = "Create Pull Request", id = Some("cpr"), uses = Some(ActionRef(V("peter-evans/create-pull-request"))), - parameters = Map( - "title" -> "Update README.md".asJson, - "commit-message" -> "Update README.md".asJson, - "branch" -> "zio-sbt-website/update-readme".asJson, - "delete-branch" -> true.asJson, - "body" -> - """|Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. - | - |I will automatically update the README.md file whenever there is new change for README.md, e.g. - | - After each release, I will update the version in the installation section. - | - After any changes to the "docs/index.md" file, I will update the README.md file accordingly.""".stripMargin.asJson, - "token" -> "${{ steps.generate-token.outputs.token }}".asJson + `with` = Some( + Map( + "title" -> "Update README.md", + "commit-message" -> "Update README.md", + "branch" -> "zio-sbt-website/update-readme", + "delete-branch" -> "true", + "body" -> + """|Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. + | + |I will automatically update the README.md file whenever there is new change for README.md, e.g. + | - After each release, I will update the version in the installation section. + | - After any changes to the "docs/index.md" file, I will update the README.md file accordingly.""".stripMargin, + "token" -> "${{ steps.generate-token.outputs.token }}" + ) ) ), Step.SingleStep( name = "Approve PR", - condition = Some(Condition.Expression("steps.cpr.outputs.pull-request-number")), - env = Map( - "GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}", - "PR_URL" -> "${{ steps.cpr.outputs.pull-request-url }}" + `if` = Some(Condition.Expression("steps.cpr.outputs.pull-request-number")), + env = Some( + Map( + "GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}", + "PR_URL" -> "${{ steps.cpr.outputs.pull-request-url }}" + ) ), run = Some("gh pr review \"$PR_URL\" --approve") ), Step.SingleStep( name = "Enable Auto-Merge", - condition = Some(Condition.Expression("steps.cpr.outputs.pull-request-number")), - env = Map( - "GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}", - "PR_URL" -> "${{ steps.cpr.outputs.pull-request-url }}" + `if` = Some(Condition.Expression("steps.cpr.outputs.pull-request-number")), + env = Some( + Map( + "GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}", + "PR_URL" -> "${{ steps.cpr.outputs.pull-request-url }}" + ) ), run = Some("gh pr merge --auto --squash \"$PR_URL\" || gh pr merge --squash \"$PR_URL\"") ) @@ -424,11 +429,10 @@ object ZioSbtCiPlugin extends AutoPlugin { val jobs = ciReleaseApprovalJobs.value Seq( - Job( - id = "release", + "release" -> JobValue( name = "Release", - need = jobs, - condition = Some(Condition.Expression("github.event_name != 'pull_request'")), + needs = Some(jobs), + `if` = Some(Condition.Expression("github.event_name != 'pull_request'")), steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ Seq( checkout, @@ -449,11 +453,10 @@ object ZioSbtCiPlugin extends AutoPlugin { val publishToNpmRegistry = PublishToNpmRegistry.value Seq( - Job( - id = "release-docs", + "release-docs" -> JobValue( name = "Release Docs", - need = Seq("release"), - condition = Some( + needs = Some(Seq("release")), + `if` = Some( Condition.Expression("github.event_name == 'release'") && Condition.Expression("github.event.action == 'published'") || Condition.Expression( "github.event_name == 'workflow_dispatch'" @@ -461,23 +464,18 @@ object ZioSbtCiPlugin extends AutoPlugin { ), steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ Seq( - Step.StepSequence( - Seq( - checkout, - SetupLibuv, - SetupJava(javaVersion), - CacheDependencies, - SetupNodeJs, - publishToNpmRegistry - ) - ) + checkout, + SetupLibuv, + SetupJava(javaVersion), + CacheDependencies, + SetupNodeJs, + publishToNpmRegistry ) ), - Job( - id = "notify-docs-release", + "notify-docs-release" -> JobValue( name = "Notify Docs Release", - need = Seq("release-docs"), - condition = Some( + needs = Some(Seq("release-docs")), + `if` = Some( Condition.Expression("github.event_name == 'release'") && Condition.Expression("github.event.action == 'published'") ), @@ -520,40 +518,46 @@ object ZioSbtCiPlugin extends AutoPlugin { val jvmOptions = Seq("-XX:+PrintCommandLineFlags") ++ ciJvmOptions.value val nodeOptions = ciNodeOptions.value - val jvmMap = Map( + val jvmMap = ListMap( "JDK_JAVA_OPTIONS" -> jvmOptions.mkString(" ") ) - val nodeMap: Map[String, String] = - if (nodeOptions.nonEmpty) Map("NODE_OPTIONS" -> nodeOptions.mkString(" ")) else Map.empty - - val workflow = yaml - .Printer( - preserveOrder = true, - dropNullKeys = true, - splitLines = false, - lineBreak = LineBreak.Unix, - version = YamlVersion.Auto + val nodeMap: ListMap[String, String] = + if (nodeOptions.nonEmpty) ListMap("NODE_OPTIONS" -> nodeOptions.mkString(" ")) else ListMap.empty + + val yamlOptions = + YamlOptions.default.copy( + dropNulls = true, + lineBreak = org.yaml.snakeyaml.DumperOptions.LineBreak.UNIX, + maxScalarWidth = Some(1024) ) - .pretty( - Workflow( - name = workflowName, - env = jvmMap ++ nodeMap, - triggers = Seq( - Trigger.WorkflowDispatch(), - Trigger.Release(Seq("published")), - Trigger.Push(branches = enabledBranches.map(Branch.Named)), - Trigger.PullRequest(ignoredBranches = Seq(Branch.Named("gh-pages"))) - ), - jobs = - buildJobs ++ lintJobs ++ testJobs ++ updateReadmeJobs ++ reportSuccessful ++ releaseJobs ++ postReleaseJobs - ).asJson + + val workflow = + Workflow( + name = workflowName, + env = jvmMap ++ nodeMap, + on = Some( + Triggers( + release = Some(Triggers.Release(Seq(Triggers.ReleaseType.Published))), + push = Some(Triggers.Push(branches = Some(enabledBranches.map(Branch.Named)).filter(_.nonEmpty))), + pullRequest = Some(Triggers.PullRequest(branchesIgnore = Some(Seq(Branch.Named("gh-pages"))))) + ) + ), + jobs = ListMap.empty[ + String, + JobValue + ] ++ buildJobs ++ lintJobs ++ testJobs ++ updateReadmeJobs ++ reportSuccessful ++ releaseJobs ++ postReleaseJobs ) + val yaml: String = workflow.toJsonAST.flatMap(_.toYaml(yamlOptions).left.map(_.getMessage())) match { + case Right(value) => value + case Left(error) => sys.error(s"Error generating workflow yaml: $error") + } + val template = s"""|# This file was autogenerated using `zio-sbt-ci` plugin via `sbt ciGenerateGithubWorkflow` |# task and should be included in the git repository. Please do not edit it manually. | - |$workflow""".stripMargin + |$yaml""".stripMargin IO.write(new File(s".github/workflows/${ciWorkflowName.value.toLowerCase}.yml"), template) } @@ -641,7 +645,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Set Swap Space", uses = Some(ActionRef(V("pierotofy/set-swap-space"))), - parameters = Map("swap-size-gb" -> swapSizeGB.asJson) + `with` = Some(Map("swap-size-gb" -> swapSizeGB.toString)) ) } @@ -650,7 +654,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Git Checkout", uses = Some(ActionRef(V("actions/checkout"))), - parameters = Map("fetch-depth" -> "0".asJson) + `with` = Some(Map("fetch-depth" -> "0")) ) } @@ -662,10 +666,12 @@ object ZioSbtCiPlugin extends AutoPlugin { def SetupJava(version: String = "17"): Step.SingleStep = Step.SingleStep( name = "Setup Scala", uses = Some(ActionRef(V("actions/setup-java"))), - parameters = Map( - "distribution" -> "corretto".asJson, - "java-version" -> version.asJson, - "check-latest" -> true.asJson + `with` = Some( + Map( + "distribution" -> "corretto", + "java-version" -> version, + "check-latest" -> "true" + ) ) ) @@ -705,11 +711,13 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Release", run = Some(prefixJobs + "sbt ci-release"), - env = Map( - "PGP_PASSPHRASE" -> "${{ secrets.PGP_PASSPHRASE }}", - "PGP_SECRET" -> "${{ secrets.PGP_SECRET }}", - "SONATYPE_PASSWORD" -> "${{ secrets.SONATYPE_PASSWORD }}", - "SONATYPE_USERNAME" -> "${{ secrets.SONATYPE_USERNAME }}" + env = Some( + Map( + "PGP_PASSPHRASE" -> "${{ secrets.PGP_PASSPHRASE }}", + "PGP_SECRET" -> "${{ secrets.PGP_SECRET }}", + "SONATYPE_PASSWORD" -> "${{ secrets.SONATYPE_PASSWORD }}", + "SONATYPE_USERNAME" -> "${{ secrets.SONATYPE_USERNAME }}" + ) ) ) } @@ -717,9 +725,11 @@ object ZioSbtCiPlugin extends AutoPlugin { val SetupNodeJs: Step.SingleStep = Step.SingleStep( name = "Setup NodeJs", uses = Some(ActionRef(V("actions/setup-node"))), - parameters = Map( - "node-version" -> "16.x".asJson, - "registry-url" -> "https://registry.npmjs.org".asJson + `with` = Some( + Map( + "node-version" -> "16.x", + "registry-url" -> "https://registry.npmjs.org" + ) ) ) @@ -732,7 +742,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Publish Docs to NPM Registry", run = Some(prefixJobs + s"sbt docs/${docsVersioning.npmCommand}"), - env = Map("NODE_AUTH_TOKEN" -> "${{ secrets.NPM_TOKEN }}") + env = Some(Map("NODE_AUTH_TOKEN" -> "${{ secrets.NPM_TOKEN }}")) ) } diff --git a/zio-sbt-ecosystem/build.sbt b/zio-sbt-ecosystem/build.sbt index c673cdb8..83e9efb2 100644 --- a/zio-sbt-ecosystem/build.sbt +++ b/zio-sbt-ecosystem/build.sbt @@ -1,9 +1,9 @@ // Build Server Plugins -addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.6.0") +addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "2.0.2") // Linting Plugins addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") -addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.12.1") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0") addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1") // Versioning and Release Plugins @@ -11,12 +11,12 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") // Docs Plugins -addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.4") +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.6.1") addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0") // Cross-Compiler Plugins -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.17.0") addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2") addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.4") addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2") @@ -29,6 +29,6 @@ addSbtPlugin("pl.project13.scala" % "sbt-jcstress" % "0.2.0") // Binary Compatibility Plugin addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.3") -libraryDependencies += "org.snakeyaml" % "snakeyaml-engine" % "2.7" -libraryDependencies += "dev.zio" %% "zio" % "2.1.8" -libraryDependencies += "io.circe" %% "circe-yaml" % "0.16.0" +libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" +libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala index 1fc5b90a..43ba56c3 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala @@ -190,7 +190,7 @@ trait ScalaCompilerSettings { ) } else Seq.empty }, - Test / parallelExecution := scalaBinaryVersion.value != "3", + Test / parallelExecution := scalaBinaryVersion.value != "3", // why not parallel execution for Scala 3? incOptions ~= (_.withLogRecompileOnMacro(false)), autoAPIMappings := true, unusedCompileDependenciesFilter -= moduleFilter("org.scala-js", "scalajs-library") @@ -233,8 +233,7 @@ trait ScalaCompilerSettings { libraryDependencies ++= Seq( "dev.zio" %%% "zio-test" % ZioSbtEcosystemPlugin.autoImport.zioVersion.value % Test, "dev.zio" %%% "zio-test-sbt" % ZioSbtEcosystemPlugin.autoImport.zioVersion.value % Test - ), - testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework") + ) ) else Seq.empty) ++ { if (enableStreaming) diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala index 0a971aa1..815b9619 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala @@ -22,11 +22,11 @@ object Versions { val KindProjectorVersion = "0.13.3" val ScaluzziVersion = "0.1.23" - val scala3 = "3.3.3" - val scala212 = "2.12.19" - val scala213 = "2.13.13" + val scala3 = "3.3.4" + val scala212 = "2.12.20" + val scala213 = "2.13.15" - val zioVersion = "2.0.21" + val zioVersion = "2.1.9" lazy val betterMonadFor: ModuleID = "com.olegpy" %% "better-monadic-for" % "0.3.1" } diff --git a/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/.github/workflows/ci.yml b/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/.github/workflows/ci.yml index f861c9d0..b02f9e67 100644 --- a/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/.github/workflows/ci.yml +++ b/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: matrix: java: ['11', '17', '21'] # These version must be different than the versions in V.scala to verify that we are reading from the ci.yml file. - scala: ['2.12.19', '2.13.13', '3.3.3' ] + scala: ['2.12.20', '2.13.15', '3.3.4' ] steps: - uses: actions/checkout@v4.1.1 - uses: actions/setup-java@v3.13.0 diff --git a/zio-sbt-githubactions/build.sbt b/zio-sbt-githubactions/build.sbt index b7d36a60..938d170c 100644 --- a/zio-sbt-githubactions/build.sbt +++ b/zio-sbt-githubactions/build.sbt @@ -1,2 +1,3 @@ -libraryDependencies += "dev.zio" %% "zio" % "2.1.8" -libraryDependencies += "io.circe" %% "circe-yaml" % "0.16.0" +libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" +libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala index a9270149..f895e3cd 100644 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala +++ b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala @@ -16,8 +16,7 @@ package zio.sbt.githubactions -import io.circe.syntax._ - +import zio.json._ import zio.sbt.githubactions.ScalaWorkflow.JavaVersion.JDK11 // The original code of the githubactions package was originally copied from the zio-aws-codegen project: @@ -29,8 +28,10 @@ object ScalaWorkflow { SingleStep( name = "Checkout current branch", uses = Some(ActionRef("actions/checkout@v2")), - parameters = Map( - "fetch-depth" := fetchDepth + `with` = Some( + Map( + "fetch-depth" -> fetchDepth.toJson + ) ) ) @@ -38,11 +39,13 @@ object ScalaWorkflow { SingleStep( name = "Setup Java and Scala", uses = Some(ActionRef("olafurpg/setup-scala@v11")), - parameters = Map( - "java-version" := (javaVersion match { - case None => "${{ matrix.java }}" - case Some(version) => version.asString - }) + `with` = Some( + Map( + "java-version" -> (javaVersion match { + case None => "${{ matrix.java }}" + case Some(version) => version.asString + }) + ) ) ) @@ -50,12 +53,14 @@ object ScalaWorkflow { SingleStep( name = "Setup NodeJS", uses = Some(ActionRef("actions/setup-node@v3")), - parameters = Map( - "node-version" := (javaVersion match { - case None => "16.x" - case Some(version) => version.asString - }), - "registry-url" := "https://registry.npmjs.org" + `with` = Some( + Map( + "node-version" -> (javaVersion match { + case None => "16.x" + case Some(version) => version.asString + }), + "registry-url" -> "https://registry.npmjs.org" + ) ) ) @@ -75,14 +80,16 @@ object ScalaWorkflow { SingleStep( name = "Cache SBT", uses = Some(ActionRef("actions/cache@v2")), - parameters = Map( - "path" := Seq( - "~/.ivy2/cache", - "~/.sbt", - "~/.coursier/cache/v1", - "~/.cache/coursier/v1" - ).mkString("\n"), - "key" := s"$osS-sbt-$scalaS-$${{ hashFiles('**/*.sbt') }}-$${{ hashFiles('**/build.properties') }}" + `with` = Some( + Map( + "path" -> Seq( + "~/.ivy2/cache", + "~/.sbt", + "~/.coursier/cache/v1", + "~/.cache/coursier/v1" + ).mkString("\n"), + "key" -> s"$osS-sbt-$scalaS-$${{ hashFiles('**/*.sbt') }}-$${{ hashFiles('**/build.properties') }}" + ) ) ) } @@ -105,7 +112,7 @@ object ScalaWorkflow { run = Some( s"sbt -J-XX:+UseG1GC -J-Xmx${heapGb}g -J-Xms${heapGb}g -J-Xss${stackMb}m ${parameters.mkString(" ")}" ), - env = env + env = Some(env).filter(_.nonEmpty) ) def storeTargets( @@ -130,9 +137,11 @@ object ScalaWorkflow { SingleStep( s"Upload $id targets", uses = Some(ActionRef("actions/upload-artifact@v2")), - parameters = Map( - "name" := s"target-$id-$osS-$scalaS-$javaS", - "path" := "targets.tar" + `with` = Some( + Map( + "name" -> s"target-$id-$osS-$scalaS-$javaS", + "path" -> "targets.tar" + ) ) ) ) @@ -154,8 +163,10 @@ object ScalaWorkflow { SingleStep( s"Download stored $id targets", uses = Some(ActionRef("actions/download-artifact@v2")), - parameters = Map( - "name" := s"target-$id-$osS-$scalaS-$javaS" + `with` = Some( + Map( + "name" -> s"target-$id-$osS-$scalaS-$javaS" + ) ) ), SingleStep( @@ -182,15 +193,17 @@ object ScalaWorkflow { SingleStep( "Load PGP secret", run = Some(".github/import-key.sh"), - env = Map("PGP_SECRET" -> "${{ secrets.PGP_SECRET }}") + env = Some(Map("PGP_SECRET" -> "${{ secrets.PGP_SECRET }}")) ) def turnstyle(): Step = SingleStep( "Turnstyle", uses = Some(ActionRef("softprops/turnstyle@v1")), - env = Map( - "GITHUB_TOKEN" -> "${{ secrets.ADMIN_GITHUB_TOKEN }}" + env = Some( + Map( + "GITHUB_TOKEN" -> "${{ secrets.ADMIN_GITHUB_TOKEN }}" + ) ) ) @@ -231,12 +244,12 @@ object ScalaWorkflow { val JDK21: JavaVersion = CorrettoJDK("21") } - implicit class JobOps(job: Job) { + implicit class JobOps(job: JobValue) { def matrix( scalaVersions: Seq[ScalaVersion], operatingSystems: Seq[OS] = Seq(OS.UbuntuLatest), javaVersions: Seq[JavaVersion] = Seq(JDK11) - ): Job = + ): JobValue = job.copy( strategy = Some( Strategy( 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 a3a6d142..96d364be 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 @@ -16,10 +16,10 @@ package zio.sbt.githubactions -import io.circe._ -import io.circe.syntax._ +import scala.collection.immutable.ListMap +import scala.util.{Failure, Success, Try} -import zio.sbt.githubactions.Step.StepSequence +import zio.json._ sealed trait OS { val asString: String @@ -33,96 +33,119 @@ object Branch { case object All extends Branch case class Named(name: String) extends Branch - implicit val encoder: Encoder[Branch] = { - case All => Json.fromString("*") - case Named(name) => Json.fromString(name) - } + implicit val codec: JsonCodec[Branch] = JsonCodec.string.transform( + { + case "*" => All + case name => Named(name) + }, + { + case All => "*" + case Named(name) => name + } + ) } -sealed trait Trigger { - def toKeyValuePair: (String, Json) -} +@jsonMemberNames(SnakeCase) +case class Triggers( + workflowDispatch: Option[Triggers.WorkflowDispatch] = None, + release: Option[Triggers.Release] = None, + pullRequest: Option[Triggers.PullRequest] = None, + push: Option[Triggers.Push] = None, + create: Option[Triggers.Create] = None +) -case class Input(key: String, description: String, required: Boolean, defaultValue: String) +object Triggers { + case class InputValue(description: String, required: Boolean, default: String) + object InputValue { + implicit val jsonCodec: JsonCodec[InputValue] = DeriveJsonCodec.gen[InputValue] + } -object Trigger { case class WorkflowDispatch( - inputs: Seq[Input] = Seq.empty - ) extends Trigger { - override def toKeyValuePair: (String, Json) = - "workflow_dispatch" := inputs.map { i => - i.key -> - Json.obj( - ("description", i.description.asJson), - ("required", i.required.asJson), - ("default", i.defaultValue.asJson) - ) - }.toMap.asJson + inputs: ListMap[String, InputValue] = ListMap.empty + ) + + object WorkflowDispatch { + implicit def listMapCodec[K: JsonFieldDecoder: JsonFieldEncoder, V: JsonCodec]: JsonCodec[ListMap[K, V]] = + JsonCodec( + JsonEncoder.keyValueIterable[K, V, ListMap], + JsonDecoder.keyValueChunk[K, V].map(c => ListMap(c: _*)) + ) + + implicit val jsonCodec: JsonCodec[WorkflowDispatch] = DeriveJsonCodec.gen[WorkflowDispatch] } case class Release( - releaseTypes: Seq[String] = Seq.empty - ) extends Trigger { - override def toKeyValuePair: (String, Json) = - "release" := Json.obj("types" := releaseTypes) + types: Seq[ReleaseType] = Seq.empty + ) + + object Release { + implicit val jsonCodec: JsonCodec[Release] = DeriveJsonCodec.gen[Release] } + sealed trait ReleaseType + object ReleaseType { + case object Created extends ReleaseType + case object Published extends ReleaseType + case object Prereleased extends ReleaseType + + implicit val codec: JsonCodec[ReleaseType] = JsonCodec.string.transformOrFail( + { + case "created" => Right(Created) + case "published" => Right(Published) + case "prereleased" => Right(Prereleased) + case other => Left(s"Invalid release type: $other") + }, + { + case Created => "created" + case Published => "published" + case Prereleased => "prereleased" + } + ) + } + + @jsonMemberNames(KebabCase) case class PullRequest( - branches: Seq[Branch] = Seq.empty, - ignoredBranches: Seq[Branch] = Seq.empty - ) extends Trigger { - override def toKeyValuePair: (String, Json) = - "pull_request" := Json.obj( - Seq( - "branches" := branches, - "branches-ignore" := ignoredBranches - ).filter { case (_, data) => data.asArray.exists(_.nonEmpty) }: _* - ) + // types: Option[Seq[PullRequestType]] = None, + branches: Option[Seq[Branch]] = None, + branchesIgnore: Option[Seq[Branch]] = None, + paths: Option[Seq[String]] = None + ) + + object PullRequest { + implicit val jsonCodec: JsonCodec[PullRequest] = DeriveJsonCodec.gen[PullRequest] } case class Push( - branches: Seq[Branch] = Seq.empty, - ignoredBranches: Seq[Branch] = Seq.empty - ) extends Trigger { - override def toKeyValuePair: (String, Json) = - "push" := Json.obj( - Seq( - "branches" := branches, - "branches-ignore" := ignoredBranches - ).filter { case (_, data) => data.asArray.exists(_.nonEmpty) }: _* - ) + branches: Option[Seq[Branch]] = None, + branchesIgnore: Option[Seq[Branch]] = None + ) + + object Push { + implicit val jsonCodec: JsonCodec[Push] = DeriveJsonCodec.gen[Push] } case class Create( - branches: Seq[Branch] = Seq.empty, - ignoredBranches: Seq[Branch] = Seq.empty - ) extends Trigger { - override def toKeyValuePair: (String, Json) = - "create" := Json.obj( - Seq( - "branches" := branches, - "branches-ignore" := ignoredBranches - ).filter { case (_, data) => data.asArray.exists(_.nonEmpty) }: _* - ) + branches: Option[Seq[Branch]] = None, + branchesIgnore: Option[Seq[Branch]] = None + ) + + object Create { + implicit val jsonCodec: JsonCodec[Create] = DeriveJsonCodec.gen[Create] } + + implicit val codec: JsonCodec[Triggers] = DeriveJsonCodec.gen[Triggers] } +@jsonMemberNames(KebabCase) case class Strategy(matrix: Map[String, List[String]], maxParallel: Option[Int] = None, failFast: Boolean = true) object Strategy { - implicit val encoder: Encoder[Strategy] = - (s: Strategy) => - Json.obj( - "fail-fast" := s.failFast, - "max-parallel" := s.maxParallel, - "matrix" := s.matrix - ) + implicit val codec: JsonCodec[Strategy] = DeriveJsonCodec.gen[Strategy] } case class ActionRef(ref: String) object ActionRef { - implicit val encoder: Encoder[ActionRef] = - (action: ActionRef) => Json.fromString(action.ref) + implicit val codec: JsonCodec[ActionRef] = JsonCodec.string.transform(ActionRef(_), _.ref) } sealed trait Condition { @@ -152,6 +175,10 @@ object Condition { def asString: String = s"$${{ $expression }}" } + object Expression { + implicit val codec: JsonCodec[Expression] = JsonCodec.string.transform(Expression(_), _.asString) + } + case class Function(expression: String) extends Condition { def &&(other: Condition): Condition = throw new IllegalArgumentException("Not supported currently") @@ -162,8 +189,17 @@ object Condition { def asString: String = expression } - implicit val encoder: Encoder[Condition] = - (c: Condition) => Json.fromString(c.asString) + object Function { + implicit val codec: JsonCodec[Function] = JsonCodec.string.transform(Function(_), _.expression) + } + + implicit val codec: JsonCodec[Condition] = JsonCodec.string.transform( + { + case expression if expression.startsWith("${{") => Expression(expression) + case expression => Function(expression) + }, + _.asString + ) } sealed trait Step { @@ -175,17 +211,21 @@ object Step { name: String, id: Option[String] = None, uses: Option[ActionRef] = None, - condition: Option[Condition] = None, - parameters: Map[String, Json] = Map.empty, + `if`: Option[Condition] = None, + `with`: Option[Map[String, String]] = None, run: Option[String] = None, - env: Map[String, String] = Map.empty + env: Option[Map[String, String]] = None ) extends Step { override def when(condition: Condition): Step = - copy(condition = Some(condition)) + copy(`if` = Some(condition)) override def flatten: Seq[Step.SingleStep] = Seq(this) } + object SingleStep { + implicit val codec: JsonCodec[SingleStep] = DeriveJsonCodec.gen[SingleStep] + } + case class StepSequence(steps: Seq[Step]) extends Step { override def when(condition: Condition): Step = copy(steps = steps.map(_.when(condition))) @@ -194,31 +234,25 @@ object Step { steps.flatMap(_.flatten) } - implicit val encoder: Encoder[SingleStep] = - (s: SingleStep) => - Json - .obj( - "name" := s.name, - "id" := s.id, - "uses" := s.uses, - "if" := s.condition, - "with" := (if (s.parameters.nonEmpty) s.parameters.asJson - else Json.Null), - "run" := s.run, - "env" := (if (s.env.nonEmpty) s.env.asJson else Json.Null) - ) + implicit val codec: JsonCodec[Step] = DeriveJsonCodec.gen[Step] } case class ImageRef(ref: String) object ImageRef { - implicit val encoder: Encoder[ImageRef] = - (image: ImageRef) => Json.fromString(image.ref) + implicit val codec: JsonCodec[ImageRef] = JsonCodec.string.transform(ImageRef(_), _.ref) } case class ServicePort(inner: Int, outer: Int) object ServicePort { - implicit val encoder: Encoder[ServicePort] = - (sp: ServicePort) => Json.fromString(s"${sp.inner}:${sp.outer}") + implicit val codec: JsonCodec[ServicePort] = JsonCodec.string.transformOrFail( + v => + Try(v.split(":", 2).map(_.toInt).toList) match { + case Success(inner :: outer :: Nil) => Right(ServicePort(inner.toInt, outer.toInt)) + case Success(_) => Left("Invalid service port format: " + v) + case Failure(_) => Left("Invalid service port format: " + v) + }, + sp => s"${sp.inner}:${sp.outer}" + ) } case class Service( @@ -228,100 +262,76 @@ case class Service( ports: Seq[ServicePort] = Seq.empty ) object Service { - implicit val encoder: Encoder[Service] = - (s: Service) => - Json.obj( - "image" := s.image, - "env" := s.env, - "ports" := s.ports - ) + implicit val codec: JsonCodec[Service] = DeriveJsonCodec.gen[Service] } -case class Job( - id: String, +@jsonMemberNames(KebabCase) +case class JobValue( name: String, runsOn: String = "ubuntu-latest", - timeoutMinutes: Int = 30, + timeoutMinutes: Option[Int] = None, continueOnError: Boolean = false, strategy: Option[Strategy] = None, - steps: Seq[Step] = Seq.empty, - need: Seq[String] = Seq.empty, - services: Seq[Service] = Seq.empty, - condition: Option[Condition] = None + needs: Option[Seq[String]] = None, + services: Option[Seq[Service]] = None, + `if`: Option[Condition] = None, + steps: Seq[Step.SingleStep] = Seq.empty ) { - def withStrategy(strategy: Strategy): Job = + def withStrategy(strategy: Strategy): JobValue = copy(strategy = Some(strategy)) - def withSteps(steps: Step*): Job = - copy(steps = steps) + def withSteps(steps: Step*): JobValue = steps match { + case steps: Step.StepSequence => + copy(steps = steps.flatten) + case step: Step.SingleStep => + copy(steps = step :: Nil) + } - def withServices(services: Service*): Job = - copy(services = services) + def withServices(services: Service*): JobValue = + copy(services = Some(services)) } -object Job { - implicit val encoder: Encoder[Job] = - (job: Job) => - Json - .obj( - "name" := job.name, - "runs-on" := job.runsOn, - "continue-on-error" := job.continueOnError, - "strategy" := job.strategy, - "needs" := (if (job.need.nonEmpty) job.need.asJson - else Json.Null), - "services" := (if (job.services.nonEmpty) { - Json.obj( - job.services.map(svc => svc.name := svc): _* - ) - } else { - Json.Null - }), - "if" := job.condition, - "steps" := StepSequence(job.steps).flatten - ) +object JobValue { + implicit val codec: JsonCodec[JobValue] = DeriveJsonCodec.gen[JobValue] +} + +@jsonMemberNames(KebabCase) +case class Concurrency( + group: String, + cancelInProgress: Boolean = true +) + +object Concurrency { + implicit val codec: JsonCodec[Concurrency] = DeriveJsonCodec.gen[Concurrency] } case class Workflow( name: String, - env: Map[String, String] = Map.empty, - triggers: Seq[Trigger] = Seq.empty, - jobs: Seq[Job] = Seq.empty + env: ListMap[String, String] = ListMap.empty, + on: Option[Triggers] = None, + 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 ) { - def on(triggers: Trigger*): Workflow = - copy(triggers = triggers) + def withOn(on: Triggers): Workflow = + copy(on = Some(on)) - def withJobs(jobs: Job*): Workflow = - copy(jobs = jobs) + def withJobs(jobs: (String, JobValue)*): Workflow = + copy(jobs = ListMap(jobs: _*)) - def addJob(job: Job): Workflow = - copy(jobs = jobs :+ job) + def addJob(job: (String, JobValue)): Workflow = + copy(jobs = jobs + job) - def addJobs(newJobs: Seq[Job]): Workflow = + def addJobs(newJobs: (String, JobValue)*): Workflow = copy(jobs = jobs ++ newJobs) } object Workflow { - implicit val encoder: Encoder[Workflow] = - (wf: Workflow) => - Json - .obj( - "name" := wf.name, - "env" := wf.env, - "on" := (if (wf.triggers.isEmpty) - Json.Null - else { - Json.obj( - wf.triggers - .map(_.toKeyValuePair): _* - ) - }), - "concurrency" := Json.obj( - "group" := Json.fromString( - "${{ github.workflow }}-${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.run_id || github.ref }}" - ), - "cancel-in-progress" := true - ), - "jobs" := Json.obj(wf.jobs.map(job => job.id := job): _*) - ) + implicit def listMapCodec[K: JsonFieldDecoder: JsonFieldEncoder, V: JsonCodec]: JsonCodec[ListMap[K, V]] = + JsonCodec( + JsonEncoder.keyValueIterable[K, V, ListMap], + JsonDecoder.keyValueChunk[K, V].map(c => ListMap(c: _*)) + ) + implicit val codec: JsonCodec[Workflow] = DeriveJsonCodec.gen[Workflow] } diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala new file mode 100644 index 00000000..9f1e009c --- /dev/null +++ b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala @@ -0,0 +1,5 @@ +package zio.sbt + +package object githubactions { + type Job = (String, JobValue) +} diff --git a/zio-sbt-website/build.sbt b/zio-sbt-website/build.sbt index f45bd37d..f6ebeff3 100644 --- a/zio-sbt-website/build.sbt +++ b/zio-sbt-website/build.sbt @@ -1,6 +1,7 @@ -addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.4") +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.6.1") addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % "3.0.2") -libraryDependencies += "dev.zio" %% "zio" % "2.1.8" -libraryDependencies += "io.circe" %% "circe-yaml" % "0.16.0" +libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" +libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" From 3fe9f88f355fd463ccb2b4ed1da7ca83c853f7bc Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Wed, 27 Nov 2024 21:00:17 +0100 Subject: [PATCH 2/3] updates and dedup scalaworkflow --- .github/workflows/ci.yml | 21 +-- .sbtopts | 1 + project/Versions.scala | 2 +- project/build.properties | 2 +- project/plugins.sbt | 2 +- zio-sbt-ci/build.sbt | 2 +- zio-sbt-ci/src/main/scala/zio/sbt/V.scala | 2 +- .../main/scala/zio/sbt/ZioSbtCiPlugin.scala | 98 ++++++------ zio-sbt-ecosystem/build.sbt | 2 +- .../src/main/scala/zio/sbt/Versions.scala | 2 +- zio-sbt-githubactions/build.sbt | 2 +- .../zio/sbt/githubactions/ScalaWorkflow.scala | 71 +++++---- .../scala/zio/sbt/githubactions/model.scala | 143 +++++++++++------- .../scala/zio/sbt/githubactions/package.scala | 5 - zio-sbt-website/build.sbt | 2 +- 15 files changed, 202 insertions(+), 155 deletions(-) create mode 100644 .sbtopts delete mode 100644 zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c69b756..8cf9a0a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,7 @@ name: CI env: JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags 'on': + workflow_dispatch: {} release: types: - published @@ -34,7 +35,7 @@ jobs: with: distribution: corretto java-version: '17' - check-latest: 'true' + check-latest: true - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Check all code compiles @@ -59,7 +60,7 @@ jobs: with: distribution: corretto java-version: '17' - check-latest: 'true' + check-latest: true - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Check if the site workflow is up to date @@ -85,7 +86,7 @@ jobs: with: distribution: corretto java-version: ${{ matrix.java }} - check-latest: 'true' + check-latest: true - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Git Checkout @@ -111,7 +112,7 @@ jobs: with: distribution: corretto java-version: '17' - check-latest: 'true' + check-latest: true - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Generate Readme @@ -132,17 +133,17 @@ jobs: id: cpr uses: peter-evans/create-pull-request@v6 with: + title: Update README.md + commit-message: Update README.md + branch: zio-sbt-website/update-readme + delete-branch: true body: |- Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. I will automatically update the README.md file whenever there is new change for README.md, e.g. - After each release, I will update the version in the installation section. - After any changes to the "docs/index.md" file, I will update the README.md file accordingly. - branch: zio-sbt-website/update-readme - commit-message: Update README.md token: ${{ steps.generate-token.outputs.token }} - delete-branch: 'true' - title: Update README.md - name: Approve PR if: ${{ steps.cpr.outputs.pull-request-number }} run: gh pr review "$PR_URL" --approve @@ -185,7 +186,7 @@ jobs: with: distribution: corretto java-version: '17' - check-latest: 'true' + check-latest: true - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Release @@ -214,7 +215,7 @@ jobs: with: distribution: corretto java-version: '17' - check-latest: 'true' + check-latest: true - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Setup NodeJs diff --git a/.sbtopts b/.sbtopts new file mode 100644 index 00000000..3b630f6a --- /dev/null +++ b/.sbtopts @@ -0,0 +1 @@ +-Dsbt.io.implicit.relative.glob.conversion=allow diff --git a/project/Versions.scala b/project/Versions.scala index 7a8351bf..ff018dfb 100644 --- a/project/Versions.scala +++ b/project/Versions.scala @@ -2,5 +2,5 @@ object Versions { val Scala212 = "2.12.20" val Scala213 = "2.13.15" val Scala3 = "3.3.4" - val zio = "2.1.9" + val zio = "2.1.13" } diff --git a/project/build.properties b/project/build.properties index 23f7d979..1767a6f8 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version = 1.10.2 +sbt.version = 1.10.5 diff --git a/project/plugins.sbt b/project/plugins.sbt index 8260121f..1586863d 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,5 @@ // Build Server Plugins -addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "2.0.3") +addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "2.0.5") // Linting Plugins addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") diff --git a/zio-sbt-ci/build.sbt b/zio-sbt-ci/build.sbt index 938d170c..f959f826 100644 --- a/zio-sbt-ci/build.sbt +++ b/zio-sbt-ci/build.sbt @@ -1,3 +1,3 @@ -libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio" % "2.1.13" libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" diff --git a/zio-sbt-ci/src/main/scala/zio/sbt/V.scala b/zio-sbt-ci/src/main/scala/zio/sbt/V.scala index d123c12a..f71e1f18 100644 --- a/zio-sbt-ci/src/main/scala/zio/sbt/V.scala +++ b/zio-sbt-ci/src/main/scala/zio/sbt/V.scala @@ -5,7 +5,7 @@ object V { Map( "peter-evans/create-pull-request" -> "v6", "zio/generate-github-app-token" -> "v1.0.0", - "pierotofy/set-swap-space" -> "master", + "pierotofy/set-swap-space" -> "49819abfb41bd9b44fb781159c033dba90353a7c", // 1.0, "actions/checkout" -> "v4", "coursier/cache-action" -> "v6", "actions/setup-java" -> "v4", 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 b6d29217..562fb65e 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,13 +156,13 @@ object ZioSbtCiPlugin extends AutoPlugin { versions.contains(scalaVersion) }.map(e => e._1 + "/test").mkString(" ")}" - "test" -> JobValue( + Job( name = "Test", strategy = Some( Strategy( - matrix = Map( - "java" -> javaPlatforms.toList, - "scala" -> scalaVersionMatrix.values.flatten.toSet.toList + matrix = ListMap( + "java" -> javaPlatforms.toList.sorted, + "scala" -> scalaVersionMatrix.values.flatten.toList.distinct.sorted ), maxParallel = matrixMaxParallel, failFast = false @@ -218,15 +218,15 @@ object ZioSbtCiPlugin extends AutoPlugin { } val FlattenTests = - "test" -> JobValue( + Job( name = "Test", strategy = Some( Strategy( - matrix = Map( - "java" -> javaPlatforms.toList + matrix = ListMap( + "java" -> javaPlatforms.toList.sorted ) ++ (if (javaPlatformMatrix.isEmpty) { - Map("scala-project" -> scalaVersionMatrix.flatMap { case (moduleName, versions) => + ListMap("scala-project" -> scalaVersionMatrix.flatMap { case (moduleName, versions) => versions.map { version => s"++$version $moduleName" } @@ -290,11 +290,13 @@ object ZioSbtCiPlugin extends AutoPlugin { ) val DefaultTestStrategy = - "test" -> JobValue( + Job( name = "Test", strategy = Some( Strategy( - matrix = Map("java" -> javaPlatforms.toList), + matrix = ListMap( + "java" -> javaPlatforms.toList.sorted + ), maxParallel = matrixMaxParallel, failFast = false ) @@ -322,7 +324,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val pullRequestApprovalJobs = ciPullRequestApprovalJobs.value Seq( - "ci" -> JobValue( + Job( name = "ci", needs = Some(pullRequestApprovalJobs), steps = Seq( @@ -344,7 +346,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) ++ @@ -367,9 +369,9 @@ object ZioSbtCiPlugin extends AutoPlugin { id = Some("generate-token"), uses = Some(ActionRef(V("zio/generate-github-app-token"))), `with` = Some( - Map( - "app_id" -> "${{ secrets.APP_ID }}", - "app_private_key" -> "${{ secrets.APP_PRIVATE_KEY }}" + ListMap( + "app_id" -> "${{ secrets.APP_ID }}".toJsonAST.right.get, + "app_private_key" -> "${{ secrets.APP_PRIVATE_KEY }}".toJsonAST.right.get ) ) ), @@ -378,18 +380,18 @@ object ZioSbtCiPlugin extends AutoPlugin { id = Some("cpr"), uses = Some(ActionRef(V("peter-evans/create-pull-request"))), `with` = Some( - Map( - "title" -> "Update README.md", - "commit-message" -> "Update README.md", - "branch" -> "zio-sbt-website/update-readme", - "delete-branch" -> "true", + ListMap( + "title" -> "Update README.md".toJsonAST.right.get, + "commit-message" -> "Update README.md".toJsonAST.right.get, + "branch" -> "zio-sbt-website/update-readme".toJsonAST.right.get, + "delete-branch" -> true.toJsonAST.right.get, "body" -> """|Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. | |I will automatically update the README.md file whenever there is new change for README.md, e.g. | - After each release, I will update the version in the installation section. - | - After any changes to the "docs/index.md" file, I will update the README.md file accordingly.""".stripMargin, - "token" -> "${{ steps.generate-token.outputs.token }}" + | - After any changes to the "docs/index.md" file, I will update the README.md file accordingly.""".stripMargin.toJsonAST.right.get, + "token" -> "${{ steps.generate-token.outputs.token }}".toJsonAST.right.get ) ) ), @@ -397,7 +399,7 @@ object ZioSbtCiPlugin extends AutoPlugin { name = "Approve PR", `if` = Some(Condition.Expression("steps.cpr.outputs.pull-request-number")), env = Some( - Map( + ListMap( "GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}", "PR_URL" -> "${{ steps.cpr.outputs.pull-request-url }}" ) @@ -408,7 +410,7 @@ object ZioSbtCiPlugin extends AutoPlugin { name = "Enable Auto-Merge", `if` = Some(Condition.Expression("steps.cpr.outputs.pull-request-number")), env = Some( - Map( + ListMap( "GITHUB_TOKEN" -> "${{ secrets.GITHUB_TOKEN }}", "PR_URL" -> "${{ steps.cpr.outputs.pull-request-url }}" ) @@ -429,7 +431,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 +455,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 +474,7 @@ object ZioSbtCiPlugin extends AutoPlugin { publishToNpmRegistry ) ), - "notify-docs-release" -> JobValue( + Job( name = "Notify Docs Release", needs = Some(Seq("release-docs")), `if` = Some( @@ -534,18 +536,18 @@ object ZioSbtCiPlugin extends AutoPlugin { val workflow = Workflow( name = workflowName, - env = jvmMap ++ nodeMap, + env = Some(jvmMap ++ nodeMap), on = Some( Triggers( - release = Some(Triggers.Release(Seq(Triggers.ReleaseType.Published))), - push = Some(Triggers.Push(branches = Some(enabledBranches.map(Branch.Named)).filter(_.nonEmpty))), - pullRequest = Some(Triggers.PullRequest(branchesIgnore = Some(Seq(Branch.Named("gh-pages"))))) + release = Some(Trigger.Release(Seq(Trigger.ReleaseType.Published))), + push = Some(Trigger.Push(branches = Some(enabledBranches.map(Branch.Named)).filter(_.nonEmpty))), + 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 { @@ -645,7 +647,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Set Swap Space", uses = Some(ActionRef(V("pierotofy/set-swap-space"))), - `with` = Some(Map("swap-size-gb" -> swapSizeGB.toString)) + `with` = Some(ListMap("swap-size-gb" -> swapSizeGB.toString.toJsonAST.right.get)) ) } @@ -654,7 +656,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Git Checkout", uses = Some(ActionRef(V("actions/checkout"))), - `with` = Some(Map("fetch-depth" -> "0")) + `with` = Some(ListMap("fetch-depth" -> "0".toJsonAST.right.get)) ) } @@ -667,10 +669,10 @@ object ZioSbtCiPlugin extends AutoPlugin { name = "Setup Scala", uses = Some(ActionRef(V("actions/setup-java"))), `with` = Some( - Map( - "distribution" -> "corretto", - "java-version" -> version, - "check-latest" -> "true" + ListMap( + "distribution" -> "corretto".toJsonAST.right.get, + "java-version" -> version.toJsonAST.right.get, + "check-latest" -> true.toJsonAST.right.get ) ) ) @@ -712,7 +714,7 @@ object ZioSbtCiPlugin extends AutoPlugin { name = "Release", run = Some(prefixJobs + "sbt ci-release"), env = Some( - Map( + ListMap( "PGP_PASSPHRASE" -> "${{ secrets.PGP_PASSPHRASE }}", "PGP_SECRET" -> "${{ secrets.PGP_SECRET }}", "SONATYPE_PASSWORD" -> "${{ secrets.SONATYPE_PASSWORD }}", @@ -726,9 +728,9 @@ object ZioSbtCiPlugin extends AutoPlugin { name = "Setup NodeJs", uses = Some(ActionRef(V("actions/setup-node"))), `with` = Some( - Map( - "node-version" -> "16.x", - "registry-url" -> "https://registry.npmjs.org" + ListMap( + "node-version" -> "16.x".toJsonAST.right.get, + "registry-url" -> "https://registry.npmjs.org".toJsonAST.right.get ) ) ) @@ -742,7 +744,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Step.SingleStep( name = "Publish Docs to NPM Registry", run = Some(prefixJobs + s"sbt docs/${docsVersioning.npmCommand}"), - env = Some(Map("NODE_AUTH_TOKEN" -> "${{ secrets.NPM_TOKEN }}")) + env = Some(ListMap("NODE_AUTH_TOKEN" -> "${{ secrets.NPM_TOKEN }}")) ) } diff --git a/zio-sbt-ecosystem/build.sbt b/zio-sbt-ecosystem/build.sbt index 83e9efb2..3cf71721 100644 --- a/zio-sbt-ecosystem/build.sbt +++ b/zio-sbt-ecosystem/build.sbt @@ -27,7 +27,7 @@ addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7") addSbtPlugin("pl.project13.scala" % "sbt-jcstress" % "0.2.0") // Binary Compatibility Plugin -addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.3") +libraryDependencies += "dev.zio" %% "zio" % "2.1.13" libraryDependencies += "dev.zio" %% "zio" % "2.1.9" libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala index 815b9619..5da8b57a 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala @@ -26,7 +26,7 @@ object Versions { val scala212 = "2.12.20" val scala213 = "2.13.15" - val zioVersion = "2.1.9" + val zioVersion = "2.1.13" lazy val betterMonadFor: ModuleID = "com.olegpy" %% "better-monadic-for" % "0.3.1" } diff --git a/zio-sbt-githubactions/build.sbt b/zio-sbt-githubactions/build.sbt index 938d170c..f959f826 100644 --- a/zio-sbt-githubactions/build.sbt +++ b/zio-sbt-githubactions/build.sbt @@ -1,3 +1,3 @@ -libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio" % "2.1.13" libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala index f895e3cd..a8350785 100644 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala +++ b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/ScalaWorkflow.scala @@ -16,8 +16,9 @@ package zio.sbt.githubactions +import scala.collection.immutable.ListMap + import zio.json._ -import zio.sbt.githubactions.ScalaWorkflow.JavaVersion.JDK11 // The original code of the githubactions package was originally copied from the zio-aws-codegen project: // https://github.com/zio/zio-aws/tree/master/zio-aws-codegen/src/main/scala/zio/aws/codegen/githubactions @@ -29,8 +30,8 @@ object ScalaWorkflow { name = "Checkout current branch", uses = Some(ActionRef("actions/checkout@v2")), `with` = Some( - Map( - "fetch-depth" -> fetchDepth.toJson + ListMap( + "fetch-depth" -> fetchDepth.toJsonAST.right.get ) ) ) @@ -40,11 +41,11 @@ object ScalaWorkflow { name = "Setup Java and Scala", uses = Some(ActionRef("olafurpg/setup-scala@v11")), `with` = Some( - Map( + ListMap( "java-version" -> (javaVersion match { case None => "${{ matrix.java }}" case Some(version) => version.asString - }) + }).toJsonAST.right.get ) ) ) @@ -54,12 +55,12 @@ object ScalaWorkflow { name = "Setup NodeJS", uses = Some(ActionRef("actions/setup-node@v3")), `with` = Some( - Map( + ListMap( "node-version" -> (javaVersion match { case None => "16.x" case Some(version) => version.asString - }), - "registry-url" -> "https://registry.npmjs.org" + }).toJsonAST.right.get, + "registry-url" -> "https://registry.npmjs.org".toJsonAST.right.get ) ) ) @@ -81,14 +82,14 @@ object ScalaWorkflow { name = "Cache SBT", uses = Some(ActionRef("actions/cache@v2")), `with` = Some( - Map( + ListMap( "path" -> Seq( "~/.ivy2/cache", "~/.sbt", "~/.coursier/cache/v1", "~/.cache/coursier/v1" - ).mkString("\n"), - "key" -> s"$osS-sbt-$scalaS-$${{ hashFiles('**/*.sbt') }}-$${{ hashFiles('**/build.properties') }}" + ).mkString("\n").toJsonAST.right.get, + "key" -> s"$osS-sbt-$scalaS-$${{ hashFiles('**/*.sbt') }}-$${{ hashFiles('**/build.properties') }}".toJsonAST.right.get ) ) ) @@ -105,7 +106,7 @@ object ScalaWorkflow { parameters: List[String], heapGb: Int = 6, stackMb: Int = 16, - env: Map[String, String] = Map.empty + env: ListMap[String, String] = ListMap.empty ): Step = SingleStep( name, @@ -138,9 +139,9 @@ object ScalaWorkflow { s"Upload $id targets", uses = Some(ActionRef("actions/upload-artifact@v2")), `with` = Some( - Map( - "name" -> s"target-$id-$osS-$scalaS-$javaS", - "path" -> "targets.tar" + ListMap( + "name" -> s"target-$id-$osS-$scalaS-$javaS".toJsonAST.right.get, + "path" -> "targets.tar".toJsonAST.right.get ) ) ) @@ -164,8 +165,8 @@ object ScalaWorkflow { s"Download stored $id targets", uses = Some(ActionRef("actions/download-artifact@v2")), `with` = Some( - Map( - "name" -> s"target-$id-$osS-$scalaS-$javaS" + ListMap( + "name" -> s"target-$id-$osS-$scalaS-$javaS".toJsonAST.right.get ) ) ), @@ -193,7 +194,7 @@ object ScalaWorkflow { SingleStep( "Load PGP secret", run = Some(".github/import-key.sh"), - env = Some(Map("PGP_SECRET" -> "${{ secrets.PGP_SECRET }}")) + env = Some(ListMap("PGP_SECRET" -> "${{ secrets.PGP_SECRET }}")) ) def turnstyle(): Step = @@ -201,7 +202,7 @@ object ScalaWorkflow { "Turnstyle", uses = Some(ActionRef("softprops/turnstyle@v1")), env = Some( - Map( + ListMap( "GITHUB_TOKEN" -> "${{ secrets.ADMIN_GITHUB_TOKEN }}" ) ) @@ -230,30 +231,39 @@ object ScalaWorkflow { case class ScalaVersion(version: String) - trait JavaVersion { - val asString: String + sealed trait JavaVersion { + def distribution: String + def version: String + def asString: String = s"$distribution:$version" } + object JavaVersion { - case class CorrettoJDK(javaVersion: String) extends JavaVersion { - override val asString: String = s"corretto:$javaVersion" + def apply(distribution: String, version: String): JavaVersion = CustomJDK(distribution, version) + + case class CustomJDK(distribution: String, version: String) extends JavaVersion + + case class CorrettoJDK(version: String) extends JavaVersion { + override def distribution: String = "corretto" } - val JDK11: JavaVersion = CorrettoJDK("11") - val JDK17: JavaVersion = CorrettoJDK("17") - val JDK21: JavaVersion = CorrettoJDK("21") + object CorrettoJDK { + val `11`: JavaVersion = CorrettoJDK("11") + val `17`: JavaVersion = CorrettoJDK("17") + val `21`: JavaVersion = CorrettoJDK("21") + } } - implicit class JobOps(job: JobValue) { + implicit class JobOps(job: Job) { def matrix( scalaVersions: Seq[ScalaVersion], operatingSystems: Seq[OS] = Seq(OS.UbuntuLatest), - javaVersions: Seq[JavaVersion] = Seq(JDK11) - ): JobValue = + javaVersions: Seq[JavaVersion] = Seq(JavaVersion.CorrettoJDK.`11`) + ): Job = job.copy( strategy = Some( Strategy( - matrix = Map( + matrix = ListMap( "os" -> operatingSystems.map(_.asString).toList, "scala" -> scalaVersions.map(_.version).toList, "java" -> javaVersions.map(_.asString).toList @@ -263,5 +273,4 @@ object ScalaWorkflow { runsOn = "${{ matrix.os }}" ) } - } 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 96d364be..91738031 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 @@ -20,12 +20,20 @@ import scala.collection.immutable.ListMap import scala.util.{Failure, Success, Try} import zio.json._ +import zio.json.ast.Json -sealed trait OS { - val asString: String +abstract class OS(name: String) { + val asString: String = name } object OS { - case object UbuntuLatest extends OS { val asString = "ubuntu-latest" } + + def apply(name: String): OS = Custom(name) + + case class Custom(name: String) extends OS(name) + + case object UbuntuLatest extends OS("ubuntu-latest") + case object Ubuntu2404 extends OS("ubuntu-24.04") + case object Ubuntu2204 extends OS("ubuntu-22.04") } sealed trait Branch @@ -33,7 +41,7 @@ object Branch { case object All extends Branch case class Named(name: String) extends Branch - implicit val codec: JsonCodec[Branch] = JsonCodec.string.transform( + implicit lazy val codec: JsonCodec[Branch] = JsonCodec.string.transform( { case "*" => All case name => Named(name) @@ -47,22 +55,29 @@ object Branch { @jsonMemberNames(SnakeCase) case class Triggers( - workflowDispatch: Option[Triggers.WorkflowDispatch] = None, - release: Option[Triggers.Release] = None, - pullRequest: Option[Triggers.PullRequest] = None, - push: Option[Triggers.Push] = None, - create: Option[Triggers.Create] = None + workflowDispatch: Trigger.WorkflowDispatch = Trigger.WorkflowDispatch(), + release: Option[Trigger.Release] = None, + pullRequest: Option[Trigger.PullRequest] = None, + push: Option[Trigger.Push] = None, + create: Option[Trigger.Create] = None ) object Triggers { + + implicit lazy val codec: JsonCodec[Triggers] = DeriveJsonCodec.gen[Triggers] +} + +sealed trait Trigger + +object Trigger { case class InputValue(description: String, required: Boolean, default: String) object InputValue { - implicit val jsonCodec: JsonCodec[InputValue] = DeriveJsonCodec.gen[InputValue] + implicit lazy val jsonCodec: JsonCodec[InputValue] = DeriveJsonCodec.gen[InputValue] } case class WorkflowDispatch( - inputs: ListMap[String, InputValue] = ListMap.empty - ) + inputs: Option[ListMap[String, InputValue]] = None + ) extends Trigger object WorkflowDispatch { implicit def listMapCodec[K: JsonFieldDecoder: JsonFieldEncoder, V: JsonCodec]: JsonCodec[ListMap[K, V]] = @@ -71,15 +86,15 @@ object Triggers { JsonDecoder.keyValueChunk[K, V].map(c => ListMap(c: _*)) ) - implicit val jsonCodec: JsonCodec[WorkflowDispatch] = DeriveJsonCodec.gen[WorkflowDispatch] + implicit lazy val jsonCodec: JsonCodec[WorkflowDispatch] = DeriveJsonCodec.gen[WorkflowDispatch] } case class Release( types: Seq[ReleaseType] = Seq.empty - ) + ) extends Trigger object Release { - implicit val jsonCodec: JsonCodec[Release] = DeriveJsonCodec.gen[Release] + implicit lazy val jsonCodec: JsonCodec[Release] = DeriveJsonCodec.gen[Release] } sealed trait ReleaseType @@ -88,7 +103,7 @@ object Triggers { case object Published extends ReleaseType case object Prereleased extends ReleaseType - implicit val codec: JsonCodec[ReleaseType] = JsonCodec.string.transformOrFail( + implicit lazy val codec: JsonCodec[ReleaseType] = JsonCodec.string.transformOrFail( { case "created" => Right(Created) case "published" => Right(Published) @@ -109,43 +124,43 @@ object Triggers { branches: Option[Seq[Branch]] = None, branchesIgnore: Option[Seq[Branch]] = None, paths: Option[Seq[String]] = None - ) + ) extends Trigger object PullRequest { - implicit val jsonCodec: JsonCodec[PullRequest] = DeriveJsonCodec.gen[PullRequest] + implicit lazy val jsonCodec: JsonCodec[PullRequest] = DeriveJsonCodec.gen[PullRequest] } case class Push( branches: Option[Seq[Branch]] = None, branchesIgnore: Option[Seq[Branch]] = None - ) + ) extends Trigger object Push { - implicit val jsonCodec: JsonCodec[Push] = DeriveJsonCodec.gen[Push] + implicit lazy val jsonCodec: JsonCodec[Push] = DeriveJsonCodec.gen[Push] } case class Create( branches: Option[Seq[Branch]] = None, branchesIgnore: Option[Seq[Branch]] = None - ) + ) extends Trigger object Create { - implicit val jsonCodec: JsonCodec[Create] = DeriveJsonCodec.gen[Create] + implicit lazy val jsonCodec: JsonCodec[Create] = DeriveJsonCodec.gen[Create] } - - implicit val codec: JsonCodec[Triggers] = DeriveJsonCodec.gen[Triggers] } @jsonMemberNames(KebabCase) -case class Strategy(matrix: Map[String, List[String]], maxParallel: Option[Int] = None, failFast: Boolean = true) +case class Strategy(matrix: ListMap[String, List[String]], maxParallel: Option[Int] = None, failFast: Boolean = true) object Strategy { - implicit val codec: JsonCodec[Strategy] = DeriveJsonCodec.gen[Strategy] + import Workflow.listMapCodec + + implicit lazy val codec: JsonCodec[Strategy] = DeriveJsonCodec.gen[Strategy] } case class ActionRef(ref: String) object ActionRef { - implicit val codec: JsonCodec[ActionRef] = JsonCodec.string.transform(ActionRef(_), _.ref) + implicit lazy val codec: JsonCodec[ActionRef] = JsonCodec.string.transform(ActionRef(_), _.ref) } sealed trait Condition { @@ -176,7 +191,7 @@ object Condition { } object Expression { - implicit val codec: JsonCodec[Expression] = JsonCodec.string.transform(Expression(_), _.asString) + implicit lazy val codec: JsonCodec[Expression] = JsonCodec.string.transform(Expression(_), _.asString) } case class Function(expression: String) extends Condition { @@ -190,10 +205,10 @@ object Condition { } object Function { - implicit val codec: JsonCodec[Function] = JsonCodec.string.transform(Function(_), _.expression) + implicit lazy val codec: JsonCodec[Function] = JsonCodec.string.transform(Function(_), _.expression) } - implicit val codec: JsonCodec[Condition] = JsonCodec.string.transform( + implicit lazy val codec: JsonCodec[Condition] = JsonCodec.string.transform( { case expression if expression.startsWith("${{") => Expression(expression) case expression => Function(expression) @@ -212,9 +227,9 @@ object Step { id: Option[String] = None, uses: Option[ActionRef] = None, `if`: Option[Condition] = None, - `with`: Option[Map[String, String]] = None, + `with`: Option[ListMap[String, Json]] = None, run: Option[String] = None, - env: Option[Map[String, String]] = None + env: Option[ListMap[String, String]] = None ) extends Step { override def when(condition: Condition): Step = copy(`if` = Some(condition)) @@ -223,7 +238,9 @@ object Step { } object SingleStep { - implicit val codec: JsonCodec[SingleStep] = DeriveJsonCodec.gen[SingleStep] + import Workflow.listMapCodec + + implicit lazy val codec: JsonCodec[SingleStep] = DeriveJsonCodec.gen[SingleStep] } case class StepSequence(steps: Seq[Step]) extends Step { @@ -234,17 +251,17 @@ object Step { steps.flatMap(_.flatten) } - implicit val codec: JsonCodec[Step] = DeriveJsonCodec.gen[Step] + implicit lazy val codec: JsonCodec[Step] = DeriveJsonCodec.gen[Step] } case class ImageRef(ref: String) object ImageRef { - implicit val codec: JsonCodec[ImageRef] = JsonCodec.string.transform(ImageRef(_), _.ref) + implicit lazy val codec: JsonCodec[ImageRef] = JsonCodec.string.transform(ImageRef(_), _.ref) } case class ServicePort(inner: Int, outer: Int) object ServicePort { - implicit val codec: JsonCodec[ServicePort] = JsonCodec.string.transformOrFail( + implicit lazy val codec: JsonCodec[ServicePort] = JsonCodec.string.transformOrFail( v => Try(v.split(":", 2).map(_.toInt).toList) match { case Success(inner :: outer :: Nil) => Right(ServicePort(inner.toInt, outer.toInt)) @@ -258,15 +275,15 @@ object ServicePort { case class Service( name: String, image: ImageRef, - env: Map[String, String] = Map.empty, - ports: Seq[ServicePort] = Seq.empty + env: Option[Map[String, String]] = None, + ports: Option[Seq[ServicePort]] = None ) object Service { - implicit val codec: JsonCodec[Service] = DeriveJsonCodec.gen[Service] + implicit lazy val codec: JsonCodec[Service] = DeriveJsonCodec.gen[Service] } @jsonMemberNames(KebabCase) -case class JobValue( +case class Job( name: String, runsOn: String = "ubuntu-latest", timeoutMinutes: Option[Int] = None, @@ -277,22 +294,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 lazy val codec: JsonCodec[Job] = DeriveJsonCodec.gen[Job] } @jsonMemberNames(KebabCase) @@ -302,36 +340,37 @@ case class Concurrency( ) object Concurrency { - implicit val codec: JsonCodec[Concurrency] = DeriveJsonCodec.gen[Concurrency] + implicit lazy val codec: JsonCodec[Concurrency] = DeriveJsonCodec.gen[Concurrency] } case class Workflow( name: String, - env: ListMap[String, String] = ListMap.empty, + env: Option[ListMap[String, String]] = None, on: Option[Triggers] = None, 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) } object Workflow { + implicit def listMapCodec[K: JsonFieldDecoder: JsonFieldEncoder, V: JsonCodec]: JsonCodec[ListMap[K, V]] = JsonCodec( JsonEncoder.keyValueIterable[K, V, ListMap], JsonDecoder.keyValueChunk[K, V].map(c => ListMap(c: _*)) ) - implicit val codec: JsonCodec[Workflow] = DeriveJsonCodec.gen[Workflow] + implicit lazy val codec: JsonCodec[Workflow] = DeriveJsonCodec.gen[Workflow] } diff --git a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala b/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala deleted file mode 100644 index 9f1e009c..00000000 --- a/zio-sbt-githubactions/src/main/scala/zio/sbt/githubactions/package.scala +++ /dev/null @@ -1,5 +0,0 @@ -package zio.sbt - -package object githubactions { - type Job = (String, JobValue) -} diff --git a/zio-sbt-website/build.sbt b/zio-sbt-website/build.sbt index f6ebeff3..2bc7157b 100644 --- a/zio-sbt-website/build.sbt +++ b/zio-sbt-website/build.sbt @@ -2,6 +2,6 @@ addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.6.1") addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % "3.0.2") -libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio" % "2.1.13" libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" From cc8e5fc68c100e406aa268cdfe55e52897ac6da8 Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Wed, 27 Nov 2024 21:09:48 +0100 Subject: [PATCH 3/3] feat: support projectmatrix and support better crossbuilding extend welcome message based on build settings add checkMima task and add to lint command support custom jdk distributions in ci job --- .github/workflows/ci.yml | 8 +- README.md | 236 +++++++- build.sbt | 42 +- docs/index.md | 236 +++++++- project/build.sbt | 6 + project/plugins.sbt | 11 +- .../src/main/scala/zio/sbt/CiVersions.scala | 8 + .../main/scala/zio/sbt/ZioSbtCiPlugin.scala | 502 ++++++++++-------- zio-sbt-ecosystem/build.sbt | 24 +- .../src/main/scala/zio/sbt/Commands.scala | 5 + .../src/main/scala/zio/sbt/MimaSettings.scala | 46 ++ .../scala/zio/sbt/ScalaCompilerSettings.scala | 131 +++-- .../src/main/scala/zio/sbt/Versions.scala | 8 +- .../scala/zio/sbt/ZioSbtEcosystemPlugin.scala | 86 +-- .../verifySettings/build.sbt | 2 + .../verifySettingsWithCI/build.sbt | 2 + zio-sbt-project/build.sbt | 7 + .../main/scala/zio/sbt/BuildAssertions.scala | 344 ++++++++++++ .../src/main/scala/zio/sbt/CompileTasks.scala | 204 +++++++ .../src/main/scala/zio/sbt/JavaVersions.scala | 43 ++ .../main/scala/zio/sbt/ScalaPlatform.scala | 53 ++ .../main/scala/zio/sbt/ScalaVersions.scala | 79 +++ .../src/main/scala/zio/sbt/TestTasks.scala | 210 ++++++++ .../zio/sbt/ZioSbtCrossbuildPlugin.scala | 68 +++ .../src/main/scala/zio/sbt/SharedTasks.scala | 12 + .../zio/sbt/SharedTasksAndSettings.scala | 100 ++++ .../zio-sbt-website/compileDocs/build.sbt | 4 +- .../zio-sbt-website/installWebsite/build.sbt | 4 +- 28 files changed, 2082 insertions(+), 399 deletions(-) create mode 100644 zio-sbt-ci/src/main/scala/zio/sbt/CiVersions.scala create mode 100644 zio-sbt-ecosystem/src/main/scala/zio/sbt/MimaSettings.scala create mode 100644 zio-sbt-project/build.sbt create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/BuildAssertions.scala create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/CompileTasks.scala create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/JavaVersions.scala create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/ScalaPlatform.scala create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/ScalaVersions.scala create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/TestTasks.scala create mode 100644 zio-sbt-project/src/main/scala/zio/sbt/ZioSbtCrossbuildPlugin.scala create mode 100644 zio-sbt-shared/src/main/scala/zio/sbt/SharedTasks.scala create mode 100644 zio-sbt-shared/src/main/scala/zio/sbt/SharedTasksAndSettings.scala diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8cf9a0a4..997814eb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,9 +39,9 @@ jobs: - name: Cache Dependencies uses: coursier/cache-action@v6 - name: Check all code compiles - run: sbt +Test/compile + run: sbt Test/compile - name: Check artifacts build process - run: sbt +publishLocal + run: sbt publishLocal - name: Check website build process run: sbt docs/clean; sbt docs/buildWebsite lint: @@ -94,7 +94,7 @@ jobs: with: fetch-depth: '0' - name: Test - run: sbt +test + run: sbt test update-readme: name: Update README runs-on: ubuntu-latest @@ -196,6 +196,8 @@ jobs: PGP_SECRET: ${{ secrets.PGP_SECRET }} SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} + CI_RELEASE: publishSigned + CI_SNAPSHOT_RELEASE: publish release-docs: name: Release Docs runs-on: ubuntu-latest diff --git a/README.md b/README.md index 7e49c0e9..4c0559d2 100644 --- a/README.md +++ b/README.md @@ -28,36 +28,190 @@ enablePlugins( ) ``` -## ZIO SBT Ecosystem +## ZIO SBT Shared + +`ZioSbtSharedPlugin` is a plugin that provides common settings and tasks which can be shared across multiple ZIO projects. This plugin includes the following settings: + +- `welcomeBannerEnabled`: A boolean setting that indicates whether or not to enable the welcome banner. +- `banners`: A sequence of strings representing banners that will be displayed as part of the welcome message. +- `usefulTasksAndSettings`: A sequence of tuples where each tuple contains a task or setting name and its description. +- `welcomeMessage`: A setting that generates the welcome message based on the `banners` and `usefulTasksAndSettings` settings. + +These settings can be used to customize the welcome message displayed when sbt starts. The welcome message can include useful tasks and settings that are relevant to the project. + +The plugin also provides the following tasks: + +- `allBanners`: A task that aggregates all banners defined in the project. +- `allUsefulTasksAndSettings`: A task that aggregates all `usefulTasksAndSettings` defined in the project. + +These tasks can be used to display a consolidated list of banners and useful tasks/settings in the welcome message. + +To use the ZIO SBT Shared plugin, add the following lines to your `plugins.sbt` file: + +```scala +addSbtPlugin("dev.zio" % "zio-sbt-shared" % "0.4.0-alpha.28") +``` + +Then in your `build.sbt` file, you can configure the settings and tasks as needed: + +```scala +welcomeBannerEnabled := true +banners := Seq("Welcome to the ZIO project!") +usefulTasksAndSettings := Seq( + "compile" -> "Compiles the source code.", + "test" -> "Runs the tests." +) +``` + +To enabled the welcome message for your project, add the following line to the root of your `build.sbt` file: + +```scala +welcomeMessage +``` + +## ZIO SBT Project + +depends on: ZioSbtShared + +`ZioSbtProjectPlugin` is an sbt plugin that provides a set of common settings and tasks for compiling and testing ZIO projects. It is designed to simplify the process of setting up and configuring ZIO projects by providing a set of default settings that can be easily customized. + +This modules provides settings and tasks for Scala versions, Scala platforms, Java versions and assertions to check what config the project is using. + +### Quickstart SBT Project + +To use the ZIO SBT Project plugin, add the following lines to your `plugins.sbt` file: + +```scala +addSbtPlugin("dev.zio" % "zio-sbt-project" % "0.4.0-alpha.28") +``` + +Then in your `build.sbt` file, enable the plugin by adding the following line: + +```scala +enablePlugins(ZioSbtProjectPlugin) +``` + +and add some config to `ThisBuild` scope (these are the default values, if you omit them they plugin defaults are used): + +```scala +inThisBuild( + List( + scala212 := ScalaVersion.scala212, + scala213 := ScalaVersion.scala213, + scala3 := ScalaVersion.scala3, + scalaVersion := scala213.value, + defaultCrossScalaVersions := Seq(scala212.value, scala213.value, scala3.value) + ) +) +``` + +### Compiling + +| Platform | Scala 2.12 | Scala 2.13 | Scala 3 | +|-------------|------------|------------|-----------| +| JVM | `compileJVM2_12` | `compileJVM2_13` | `compileJVM3` | +| Scala.js | `compileJS2_12` | `compileJS2_13` | `compileJS3` | +| Scala Native| `compileNative2_12` | `compileNative2_13` | `compileNative3` | -ZIO SBT Ecosystem plugin is an sbt plugin that provides a set of sbt settings and tasks that are very common and useful for configuring and managing ZIO projects. It is designed help developers to quickly set up a new ZIO project with a minimal amount of effort. +### Testing -This pluging provides the following settings with default values: +| Platform | Scala 2.12 | Scala 2.13 | Scala 3 | +|-------------|------------|------------|-----------| +| JVM | `testJVM2_12` | `testJVM2_13` | `testJVM3` | +| Scala.js | `testJS2_12` | `testJS2_13` | `testJS3` | +| Scala Native| `testNative2_12` | `testNative2_13` | `testNative3` | -- scala212 -- scala213 -- scala3 +### Scala versions +The plugin provides the following settings for Scala versions. Use these settings to set your Scala versions. The default values are the latest stable versions of Scala 2.12, 2.13, and Scala 3. All of these settings are of type `String` and can be overridden by the user. -By having these settings, then we can use them in other sbt settings. For example, we can use them to define the `crossScalaVersions` setting: +- `scala212` - The default Scala 2.12 version +- `scala213` - The default Scala 2.13 version +- `scala3` - The default Scala 3 version +- `defaultCrossScalaVersions` - An optional setting to define the cross Scala versions +- `allScalaVersions` - This setting lists all Scala versions used by the project + +By having these settings, we can use them in other sbt settings. For example, we can use them to define the `defaultCrossScalaVersions` setting: ```scala -crossScalaVersions := Seq(scala212.value, scala213.value, scala3.value) +ThisBuild / defaultCrossScalaVersions := Seq(scala212.value, scala213.value, scala3.value) ``` -There are also some other settings that are useful for configuring the projects: +### Scala platforms + +- `allScalaPlatforms` - This setting lists all Scala platforms used by the project + +### Java versions + +The plugin provides the following settings for Java versions. Use these settings to set your Java versions. + +- `javaPlatform` - java target platform to release for, default is 11 +- `currentJDK` - current (detected) JDK version` + +## ZIO SBT Ecosystem + +ZIO SBT Ecosystem plugin is an sbt plugin that provides a set of sbt settings and tasks that are very common and useful for configuring and managing ZIO projects. It is designed to help developers quickly set up a new ZIO project with minimal effort. + +### Quickstart SBT Ecosystem + +depends on: ZioSbtProjectPlugin + +To use the ZIO SBT Ecosystem plugin, add the following lines to your `plugins.sbt` file: + +```scala +addSbtPlugin("dev.zio" % "zio-sbt-ecosystem" % "0.4.0-alpha.28") +``` + +Then in your `build.sbt` file, enable the plugin by adding the following line: + +```scala +enablePlugins(ZioSbtEcosystemPlugin) +``` + +use the predefined settings to configure your project: + +```scala +lazy val zio-awesome = crossProject(JSPlatform, JVMPlatform, NativePlatform) + .in(file("zio-something")) + .settings(stdSettings(enableCrossProject = true)) + .settings(enableZIO) + .jsSettings(jsSettings) + .jvmSettings(jvmSettings) + .nativeSettings(nativeSettings) +``` + +or for an sbt-projectmatrix project: + +```scala +lazy val zio-awesome = projectMatrix + .crossScalaVersions("2.12.20", "2.13.15", "3.3.4") + .crossPlatforms(JSPlatform, JVMPlatform, NativePlatform) + .settings(stdSettings()) // enableCrossProject is not required, sbt-projectmatrix brings in default source directories for all scala versions and platforms + .settings(enableZIO) + .jvmPlatform(scalaVersions = Seq(Scala212, Scala213, Scala3), settings = jvmSettings) + .jsPlatform(scalaVersions = Seq(Scala212, Scala213, Scala3), settings = jsSettings) + .nativePlatform(scalaVersions = Seq(Scala212, Scala213, Scala3), settings = nativeSettings) +``` + +### Settings + +There are also some other (optional) settings that are useful for configuring the projects: - `stdSettings`— a set of standard settings which are common for every ZIO project, which includes configuring: - silencer plugin - kind projector plugin - - cross project plugin + - cross project source directories (custom source directories for different scala versions) - scalafix plugin - java target platform -- `enableZIO`- a set of ZIO related settings such as enabling zio streams and ZIO test framework. -- `jsSettings`, `nativeSettings`- common platform specific settings for Scala.js and Scala Native. +- `enableZIO`— a set of ZIO related settings such as enabling ZIO streams and ZIO test framework. +- `jvmSettings`— common platform-specific settings for JVM. +- `jsSettings` — common platform-specific settings for Scala.js. +- `nativeSettings`— common platform-specific settings for Scala Native. -It also provides some helper methods that are useful for configuring a compiler option for a specific Scala version: +### Roll your own settings + +It is still possible to configure the project as you like. The plugin provides some helper methods that are useful for configuring a compiler option for a specific Scala version: - `optionsOn` - `optionsOnExcept` @@ -79,7 +233,7 @@ And the same for adding a dependency for a specific Scala version: ZIO SBT Website is an SBT plugin that has the following tasks: -- `sbt compileDocs`— compile documentation inside `docs` directory. The compilation result will be inside `website/docs` directory. +- `sbt compileDocs`— compile documentation inside the `docs` directory. The compilation result will be inside the `website/docs` directory. - `sbt installWebsite`— creates a website for the project inside the `website` directory. - `sbt previewWebsite`— runs a local webserver that serves documentation locally on http://localhost:3000. By changing the documentation inside the `docs` directory, the website will be reloaded with new content. - `sbt publishToNpm`— publishes documentation inside the `docs` directory to the npm registry. @@ -87,15 +241,51 @@ ZIO SBT Website is an SBT plugin that has the following tasks: ## ZIO SBT CI Plugin +depends on: ZioSbtProjectPlugin + ZIO SBT CI is an sbt plugin which generates a GitHub workflow for a project, making it easier to set up continuous integration (CI) pipelines for Scala projects. With this plugin, developers can streamline their development workflow by automating the testing and deployment process, reducing manual effort and errors. The plugin is designed to work seamlessly with sbt, the popular build tool for Scala projects, and integrates smoothly with GitHub Actions, the CI/CD platform provided by GitHub. ZIO SBT CI provides a simple and efficient way to configure, manage, and run CI pipelines, helping teams to deliver high-quality software faster and with greater confidence. -ZIO SBT CI plugin generates a default GitHub workflow that includes common CI tasks such as building, testing, and publishing artifacts. However, users can also manually customize the workflow. This plugin is designed to be flexible and extensible, making it easy for users to tailor the workflow to their specific needs. Additionally, the plugin also provides tons of optional sbt settings that users can modify to change various aspects of the generated workflow. Overall, ZIO SBT CI plugin strikes a balance between automation and flexibility, allowing users to automate their CI process while still giving them control over how the workflow is generated. +ZIO SBT CI plugin generates a default GitHub workflow that includes common CI tasks such as building, testing, and publishing artifacts. However, users can also manually customize the workflow. This plugin is designed to be flexible and extensible, making it easy for users to tailor the workflow to their specific needs. Additionally, the plugin also provides numerous optional sbt settings that users can modify to change various aspects of the generated workflow. Overall, the ZIO SBT CI plugin strikes a balance between automation and flexibility, allowing users to automate their CI process while still giving them control over how the workflow is generated. + +### Settings + +The ZIO SBT CI plugin provides the following settings: + +- `ciTargetScalaVersions` - A mapping of project names to the Scala versions that should be used for the testing phase of continuous integration (CI). This setting is used to define the Scala versions that should be tested for each project in the build. +- `ciDefaultJavaVersion` - The default Java version to use in the CI workflow. The default value is `17`. +- `ciDefaultJavaDistribution` - The default Java distribution to use in the CI workflow. The default value is `corretto`. +- `ciDefaultNodeJSVersion` - The default Node.js version to use in the CI workflow. The default value is `16.x`. +- `ciJvmOptions` - The JVM options to use in the CI workflow. +- `ciNodeOptions` - The Node.js options to use in the CI workflow. + +### Tasks + +The ZIO SBT CI plugin provides the following tasks (steps can be overridden or modified): + +- `ciCheckGithubWorkflow` - Checks if the GitHub workflow file exists in the `.github/workflows` directory. +- `ciGenerateGithubWorkflow` - Generates a GitHub workflow file in the `.github/workflows` directory. +- `ciJobPerScalaPlatform` - Whether to generate a separate job for each Scala platform, when not defined `allScalaPlatforms.value > 1` is used +- `ciJobPerScalaVersion` - Whether to generate a separate job for each Scala version, when not defined `allScalaVersions.value > 1` is used +- `ciCheckGithubWorkflowSteps` - Steps for the GitHub workflow, for no steps use `Nil`. +- `ciCheckArtifactsCompilationSteps` - Compilation steps for the artifacts job in the GitHub workflow. +- `ciCheckArtifactsBuildSteps` - Build steps for the artifacts job in the GitHub workflow. +- `ciCheckWebsiteBuildProcess` - Build process for the website job in the GitHub workflow. +- `ciPullRequestApprovalJobs` - Jobs for the pull request approval workflow. +- `ciReleaseApprovalJobs` - Jobs for the release approval workflow. +- `ciWorkflowName` - The name of the GitHub workflow file. +- `ciEnabledBranches` - The pull request target branches on which the GitHub workflow should run. +- `ciBuildJobs` - The build jobs for the GitHub workflow. +- `ciLintJobs` - The lint jobs for the GitHub workflow. +- `ciTestJobs` - The test jobs for the GitHub workflow. +- `ciUpdateReadmeJobs` - The update readme jobs for the GitHub workflow. +- `ciReleaseJobs` - The release jobs for the GitHub workflow. +- `ciPostReleaseJobs` - The post-release jobs for the GitHub workflow. ### Getting Started -To use ZIO SBT CI plugin, add the following lines to your `plugins.sbt` file: +To use the ZIO SBT CI plugin, add the following lines to your `plugins.sbt` file: ```scala addSbtPlugin("dev.zio" % "zio-sbt-ci" % "0.4.0-alpha.28") @@ -109,13 +299,13 @@ Then in your `build.sbt` file, enable the plugin by adding the following line: enablePlugins(ZioSbtCiPlugin) ``` -Now you can generate a Github workflow by running the following command: +Now you can generate a GitHub workflow by running the following command: ```bash sbt ciGenerateGithubWorkflow ``` -This will generate a GitHub workflow file inside the `.github/workflows` directory, named `ci.yml`. The workflow file contains following default Jobs: +This will generate a GitHub workflow file inside the `.github/workflows` directory, named `ci.yml`. The workflow file contains the following default jobs: - Build - Lint @@ -130,7 +320,7 @@ This will generate a GitHub workflow file inside the `.github/workflows` directo ### Default Testing Strategy -The default testing strategy for ZIO SBT CI plugin is to run `sbt +test` on Corretto Java 11, 17 and 21. So this will generate the following job: +The default testing strategy for the ZIO SBT CI plugin is to run `sbt +test` on Corretto Java 11, 17, and 21. This will generate the following job: ```yaml test: @@ -166,9 +356,9 @@ The `sbt +test` command will run the `test` task for all submodules in the proje In some cases, we may have multiple submodules in our project and we want to test them concurrently using GitHub Actions matrix strategy. -The `ciTargetScalaVersions` setting key is used to define a mapping of project names to the Scala versions that should be used for testing phase of continuous integration (CI). +The `ciTargetScalaVersions` setting key is used to define a mapping of project names to the Scala versions that should be used for the testing phase of continuous integration (CI). -For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.20`, and for the "submoduleB" we want to test it against Scala `2.12.20` and `2.13.15` and `3.3.4`, We can define the `ciTargetScalaVersions` setting as follows: +For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.20`, and for "submoduleB" we want to test it against Scala `2.12.20`, `2.13.15`, and `3.3.4`. We can define the `ciTargetScalaVersions` setting as follows: ```scala ThisBuild / ciTargetScalaVersions := Map( @@ -179,7 +369,7 @@ ThisBuild / ciTargetScalaVersions := Map( In the example provided, `ciTargetScalaVersions` is defined at the `ThisBuild` level, meaning that the setting will apply to all projects within the build. The setting defines a Map where the key is the name of the current project, obtained by calling the `id` method on the `thisProject` setting, and the value is a sequence of Scala versions obtained from the `crossScalaVersions` of each submodule setting. -To simplify this process, we can populate the versions using each submodule's crossScalaVersions setting as follows: +To simplify this process, we can populate the versions using each submodule's `crossScalaVersions` setting as follows: ```scala ThisBuild / ciTargetScalaVersions := Map( @@ -188,7 +378,7 @@ ThisBuild / ciTargetScalaVersions := Map( ) ``` -The above code can be simplified further by using `targetScalaVersionsFor` helper method, it takes a list of submodules and returns a Map of project names to their `crossScalaVersions`: +The above code can be simplified further by using the `targetScalaVersionsFor` helper method, which takes a list of submodules and returns a Map of project names to their `crossScalaVersions`: ```scala ThisBuild / ciTargetScalaVersions := targetScalaVersionsFor(submoduleA, submoduleB).value diff --git a/build.sbt b/build.sbt index a941f5ea..2b55e4f8 100644 --- a/build.sbt +++ b/build.sbt @@ -9,13 +9,14 @@ addCommandAlias("test", "scripted") inThisBuild( List( - name := "ZIO SBT", - startYear := Some(2022), - scalaVersion := Scala212, - crossScalaVersions := Seq(scalaVersion.value), + name := "ZIO SBT", + startYear := Some(2022), + scalaVersion := Scala212, + defaultCrossScalaVersions := Seq(Scala212), developers := List( Developer("khajavi", "Milad Khajavi", "khajavi@gmail.com", url("https://github.com/khajavi")) ), + checkMima / skip := true, ciEnabledBranches := Seq("main") ) ) @@ -27,13 +28,27 @@ lazy val root = project publish / skip := true ) .aggregate( + `zio-sbt-shared`, `zio-sbt-githubactions`, `zio-sbt-website`, + `zio-sbt-project`, `zio-sbt-ecosystem`, `zio-sbt-ci`, `zio-sbt-tests` ) - .enablePlugins(ZioSbtCiPlugin) + +lazy val `zio-sbt-shared` = + project + .settings(stdSettings()) + .settings( + headerEndYear := Some(2024), + scriptedLaunchOpts := { + scriptedLaunchOpts.value ++ + Seq("-Xmx1024M", "-Dplugin.version=" + version.value) + }, + scriptedBufferLog := false + ) + .enablePlugins(SbtPlugin) lazy val `zio-sbt-tests` = project @@ -56,6 +71,20 @@ lazy val `zio-sbt-website` = ) .enablePlugins(SbtPlugin) +lazy val `zio-sbt-project` = + project + .settings(stdSettings()) + .settings( + headerEndYear := Some(2024), + scriptedLaunchOpts := { + scriptedLaunchOpts.value ++ + Seq("-Xmx1024M", "-Dplugin.version=" + version.value) + }, + scriptedBufferLog := false + ) + .enablePlugins(SbtPlugin) + .dependsOn(`zio-sbt-shared`) + lazy val `zio-sbt-ecosystem` = project .settings(stdSettings()) @@ -68,6 +97,7 @@ lazy val `zio-sbt-ecosystem` = scriptedBufferLog := false ) .enablePlugins(SbtPlugin) + .dependsOn(`zio-sbt-project`) lazy val `zio-sbt-ci` = project @@ -81,7 +111,7 @@ lazy val `zio-sbt-ci` = scriptedBufferLog := false ) .enablePlugins(SbtPlugin) - .dependsOn(`zio-sbt-githubactions`) + .dependsOn(`zio-sbt-githubactions`, `zio-sbt-project`) lazy val `zio-sbt-githubactions` = project diff --git a/docs/index.md b/docs/index.md index 5489d349..d25ab37a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -27,36 +27,190 @@ enablePlugins( ) ``` -## ZIO SBT Ecosystem +## ZIO SBT Shared + +`ZioSbtSharedPlugin` is a plugin that provides common settings and tasks which can be shared across multiple ZIO projects. This plugin includes the following settings: + +- `welcomeBannerEnabled`: A boolean setting that indicates whether or not to enable the welcome banner. +- `banners`: A sequence of strings representing banners that will be displayed as part of the welcome message. +- `usefulTasksAndSettings`: A sequence of tuples where each tuple contains a task or setting name and its description. +- `welcomeMessage`: A setting that generates the welcome message based on the `banners` and `usefulTasksAndSettings` settings. + +These settings can be used to customize the welcome message displayed when sbt starts. The welcome message can include useful tasks and settings that are relevant to the project. + +The plugin also provides the following tasks: + +- `allBanners`: A task that aggregates all banners defined in the project. +- `allUsefulTasksAndSettings`: A task that aggregates all `usefulTasksAndSettings` defined in the project. + +These tasks can be used to display a consolidated list of banners and useful tasks/settings in the welcome message. + +To use the ZIO SBT Shared plugin, add the following lines to your `plugins.sbt` file: + +```scala +addSbtPlugin("dev.zio" % "zio-sbt-shared" % "@VERSION@") +``` + +Then in your `build.sbt` file, you can configure the settings and tasks as needed: + +```scala +welcomeBannerEnabled := true +banners := Seq("Welcome to the ZIO project!") +usefulTasksAndSettings := Seq( + "compile" -> "Compiles the source code.", + "test" -> "Runs the tests." +) +``` + +To enabled the welcome message for your project, add the following line to the root of your `build.sbt` file: + +```scala +welcomeMessage +``` + +## ZIO SBT Project + +depends on: ZioSbtShared + +`ZioSbtProjectPlugin` is an sbt plugin that provides a set of common settings and tasks for compiling and testing ZIO projects. It is designed to simplify the process of setting up and configuring ZIO projects by providing a set of default settings that can be easily customized. + +This modules provides settings and tasks for Scala versions, Scala platforms, Java versions and assertions to check what config the project is using. + +### Quickstart SBT Project + +To use the ZIO SBT Project plugin, add the following lines to your `plugins.sbt` file: + +```scala +addSbtPlugin("dev.zio" % "zio-sbt-project" % "@VERSION@") +``` + +Then in your `build.sbt` file, enable the plugin by adding the following line: + +```scala +enablePlugins(ZioSbtProjectPlugin) +``` + +and add some config to `ThisBuild` scope (these are the default values, if you omit them they plugin defaults are used): + +```scala +inThisBuild( + List( + scala212 := ScalaVersion.scala212, + scala213 := ScalaVersion.scala213, + scala3 := ScalaVersion.scala3, + scalaVersion := scala213.value, + defaultCrossScalaVersions := Seq(scala212.value, scala213.value, scala3.value) + ) +) +``` + +### Compiling + +| Platform | Scala 2.12 | Scala 2.13 | Scala 3 | +|-------------|------------|------------|-----------| +| JVM | `compileJVM2_12` | `compileJVM2_13` | `compileJVM3` | +| Scala.js | `compileJS2_12` | `compileJS2_13` | `compileJS3` | +| Scala Native| `compileNative2_12` | `compileNative2_13` | `compileNative3` | -ZIO SBT Ecosystem plugin is an sbt plugin that provides a set of sbt settings and tasks that are very common and useful for configuring and managing ZIO projects. It is designed help developers to quickly set up a new ZIO project with a minimal amount of effort. +### Testing -This pluging provides the following settings with default values: +| Platform | Scala 2.12 | Scala 2.13 | Scala 3 | +|-------------|------------|------------|-----------| +| JVM | `testJVM2_12` | `testJVM2_13` | `testJVM3` | +| Scala.js | `testJS2_12` | `testJS2_13` | `testJS3` | +| Scala Native| `testNative2_12` | `testNative2_13` | `testNative3` | -- scala212 -- scala213 -- scala3 +### Scala versions +The plugin provides the following settings for Scala versions. Use these settings to set your Scala versions. The default values are the latest stable versions of Scala 2.12, 2.13, and Scala 3. All of these settings are of type `String` and can be overridden by the user. -By having these settings, then we can use them in other sbt settings. For example, we can use them to define the `crossScalaVersions` setting: +- `scala212` - The default Scala 2.12 version +- `scala213` - The default Scala 2.13 version +- `scala3` - The default Scala 3 version +- `defaultCrossScalaVersions` - An optional setting to define the cross Scala versions +- `allScalaVersions` - This setting lists all Scala versions used by the project + +By having these settings, we can use them in other sbt settings. For example, we can use them to define the `defaultCrossScalaVersions` setting: ```scala -crossScalaVersions := Seq(scala212.value, scala213.value, scala3.value) +ThisBuild / defaultCrossScalaVersions := Seq(scala212.value, scala213.value, scala3.value) ``` -There are also some other settings that are useful for configuring the projects: +### Scala platforms + +- `allScalaPlatforms` - This setting lists all Scala platforms used by the project + +### Java versions + +The plugin provides the following settings for Java versions. Use these settings to set your Java versions. + +- `javaPlatform` - java target platform to release for, default is 11 +- `currentJDK` - current (detected) JDK version` + +## ZIO SBT Ecosystem + +ZIO SBT Ecosystem plugin is an sbt plugin that provides a set of sbt settings and tasks that are very common and useful for configuring and managing ZIO projects. It is designed to help developers quickly set up a new ZIO project with minimal effort. + +### Quickstart SBT Ecosystem + +depends on: ZioSbtProjectPlugin + +To use the ZIO SBT Ecosystem plugin, add the following lines to your `plugins.sbt` file: + +```scala +addSbtPlugin("dev.zio" % "zio-sbt-ecosystem" % "@VERSION@") +``` + +Then in your `build.sbt` file, enable the plugin by adding the following line: + +```scala +enablePlugins(ZioSbtEcosystemPlugin) +``` + +use the predefined settings to configure your project: + +```scala +lazy val zio-awesome = crossProject(JSPlatform, JVMPlatform, NativePlatform) + .in(file("zio-something")) + .settings(stdSettings(enableCrossProject = true)) + .settings(enableZIO) + .jsSettings(jsSettings) + .jvmSettings(jvmSettings) + .nativeSettings(nativeSettings) +``` + +or for an sbt-projectmatrix project: + +```scala +lazy val zio-awesome = projectMatrix + .crossScalaVersions("2.12.20", "2.13.15", "3.3.4") + .crossPlatforms(JSPlatform, JVMPlatform, NativePlatform) + .settings(stdSettings()) // enableCrossProject is not required, sbt-projectmatrix brings in default source directories for all scala versions and platforms + .settings(enableZIO) + .jvmPlatform(scalaVersions = Seq(Scala212, Scala213, Scala3), settings = jvmSettings) + .jsPlatform(scalaVersions = Seq(Scala212, Scala213, Scala3), settings = jsSettings) + .nativePlatform(scalaVersions = Seq(Scala212, Scala213, Scala3), settings = nativeSettings) +``` + +### Settings + +There are also some other (optional) settings that are useful for configuring the projects: - `stdSettings`— a set of standard settings which are common for every ZIO project, which includes configuring: - silencer plugin - kind projector plugin - - cross project plugin + - cross project source directories (custom source directories for different scala versions) - scalafix plugin - java target platform -- `enableZIO`- a set of ZIO related settings such as enabling zio streams and ZIO test framework. -- `jsSettings`, `nativeSettings`- common platform specific settings for Scala.js and Scala Native. +- `enableZIO`— a set of ZIO related settings such as enabling ZIO streams and ZIO test framework. +- `jvmSettings`— common platform-specific settings for JVM. +- `jsSettings` — common platform-specific settings for Scala.js. +- `nativeSettings`— common platform-specific settings for Scala Native. -It also provides some helper methods that are useful for configuring a compiler option for a specific Scala version: +### Roll your own settings + +It is still possible to configure the project as you like. The plugin provides some helper methods that are useful for configuring a compiler option for a specific Scala version: - `optionsOn` - `optionsOnExcept` @@ -78,7 +232,7 @@ And the same for adding a dependency for a specific Scala version: ZIO SBT Website is an SBT plugin that has the following tasks: -- `sbt compileDocs`— compile documentation inside `docs` directory. The compilation result will be inside `website/docs` directory. +- `sbt compileDocs`— compile documentation inside the `docs` directory. The compilation result will be inside the `website/docs` directory. - `sbt installWebsite`— creates a website for the project inside the `website` directory. - `sbt previewWebsite`— runs a local webserver that serves documentation locally on http://localhost:3000. By changing the documentation inside the `docs` directory, the website will be reloaded with new content. - `sbt publishToNpm`— publishes documentation inside the `docs` directory to the npm registry. @@ -86,15 +240,51 @@ ZIO SBT Website is an SBT plugin that has the following tasks: ## ZIO SBT CI Plugin +depends on: ZioSbtProjectPlugin + ZIO SBT CI is an sbt plugin which generates a GitHub workflow for a project, making it easier to set up continuous integration (CI) pipelines for Scala projects. With this plugin, developers can streamline their development workflow by automating the testing and deployment process, reducing manual effort and errors. The plugin is designed to work seamlessly with sbt, the popular build tool for Scala projects, and integrates smoothly with GitHub Actions, the CI/CD platform provided by GitHub. ZIO SBT CI provides a simple and efficient way to configure, manage, and run CI pipelines, helping teams to deliver high-quality software faster and with greater confidence. -ZIO SBT CI plugin generates a default GitHub workflow that includes common CI tasks such as building, testing, and publishing artifacts. However, users can also manually customize the workflow. This plugin is designed to be flexible and extensible, making it easy for users to tailor the workflow to their specific needs. Additionally, the plugin also provides tons of optional sbt settings that users can modify to change various aspects of the generated workflow. Overall, ZIO SBT CI plugin strikes a balance between automation and flexibility, allowing users to automate their CI process while still giving them control over how the workflow is generated. +ZIO SBT CI plugin generates a default GitHub workflow that includes common CI tasks such as building, testing, and publishing artifacts. However, users can also manually customize the workflow. This plugin is designed to be flexible and extensible, making it easy for users to tailor the workflow to their specific needs. Additionally, the plugin also provides numerous optional sbt settings that users can modify to change various aspects of the generated workflow. Overall, the ZIO SBT CI plugin strikes a balance between automation and flexibility, allowing users to automate their CI process while still giving them control over how the workflow is generated. + +### Settings + +The ZIO SBT CI plugin provides the following settings: + +- `ciTargetScalaVersions` - A mapping of project names to the Scala versions that should be used for the testing phase of continuous integration (CI). This setting is used to define the Scala versions that should be tested for each project in the build. +- `ciDefaultJavaVersion` - The default Java version to use in the CI workflow. The default value is `17`. +- `ciDefaultJavaDistribution` - The default Java distribution to use in the CI workflow. The default value is `corretto`. +- `ciDefaultNodeJSVersion` - The default Node.js version to use in the CI workflow. The default value is `16.x`. +- `ciJvmOptions` - The JVM options to use in the CI workflow. +- `ciNodeOptions` - The Node.js options to use in the CI workflow. + +### Tasks + +The ZIO SBT CI plugin provides the following tasks (steps can be overridden or modified): + +- `ciCheckGithubWorkflow` - Checks if the GitHub workflow file exists in the `.github/workflows` directory. +- `ciGenerateGithubWorkflow` - Generates a GitHub workflow file in the `.github/workflows` directory. +- `ciJobPerScalaPlatform` - Whether to generate a separate job for each Scala platform, when not defined `allScalaPlatforms.value > 1` is used +- `ciJobPerScalaVersion` - Whether to generate a separate job for each Scala version, when not defined `allScalaVersions.value > 1` is used +- `ciCheckGithubWorkflowSteps` - Steps for the GitHub workflow, for no steps use `Nil`. +- `ciCheckArtifactsCompilationSteps` - Compilation steps for the artifacts job in the GitHub workflow. +- `ciCheckArtifactsBuildSteps` - Build steps for the artifacts job in the GitHub workflow. +- `ciCheckWebsiteBuildProcess` - Build process for the website job in the GitHub workflow. +- `ciPullRequestApprovalJobs` - Jobs for the pull request approval workflow. +- `ciReleaseApprovalJobs` - Jobs for the release approval workflow. +- `ciWorkflowName` - The name of the GitHub workflow file. +- `ciEnabledBranches` - The pull request target branches on which the GitHub workflow should run. +- `ciBuildJobs` - The build jobs for the GitHub workflow. +- `ciLintJobs` - The lint jobs for the GitHub workflow. +- `ciTestJobs` - The test jobs for the GitHub workflow. +- `ciUpdateReadmeJobs` - The update readme jobs for the GitHub workflow. +- `ciReleaseJobs` - The release jobs for the GitHub workflow. +- `ciPostReleaseJobs` - The post-release jobs for the GitHub workflow. ### Getting Started -To use ZIO SBT CI plugin, add the following lines to your `plugins.sbt` file: +To use the ZIO SBT CI plugin, add the following lines to your `plugins.sbt` file: ```scala addSbtPlugin("dev.zio" % "zio-sbt-ci" % "@VERSION@") @@ -108,13 +298,13 @@ Then in your `build.sbt` file, enable the plugin by adding the following line: enablePlugins(ZioSbtCiPlugin) ``` -Now you can generate a Github workflow by running the following command: +Now you can generate a GitHub workflow by running the following command: ```bash sbt ciGenerateGithubWorkflow ``` -This will generate a GitHub workflow file inside the `.github/workflows` directory, named `ci.yml`. The workflow file contains following default Jobs: +This will generate a GitHub workflow file inside the `.github/workflows` directory, named `ci.yml`. The workflow file contains the following default jobs: - Build - Lint @@ -129,7 +319,7 @@ This will generate a GitHub workflow file inside the `.github/workflows` directo ### Default Testing Strategy -The default testing strategy for ZIO SBT CI plugin is to run `sbt +test` on Corretto Java 11, 17 and 21. So this will generate the following job: +The default testing strategy for the ZIO SBT CI plugin is to run `sbt +test` on Corretto Java 11, 17, and 21. This will generate the following job: ```yaml test: @@ -165,9 +355,9 @@ The `sbt +test` command will run the `test` task for all submodules in the proje In some cases, we may have multiple submodules in our project and we want to test them concurrently using GitHub Actions matrix strategy. -The `ciTargetScalaVersions` setting key is used to define a mapping of project names to the Scala versions that should be used for testing phase of continuous integration (CI). +The `ciTargetScalaVersions` setting key is used to define a mapping of project names to the Scala versions that should be used for the testing phase of continuous integration (CI). -For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.20`, and for the "submoduleB" we want to test it against Scala `2.12.20` and `2.13.15` and `3.3.4`, We can define the `ciTargetScalaVersions` setting as follows: +For example, suppose we have a project with the name "submoduleA" and we want to test it against Scala `2.12.20`, and for "submoduleB" we want to test it against Scala `2.12.20`, `2.13.15`, and `3.3.4`. We can define the `ciTargetScalaVersions` setting as follows: ```scala ThisBuild / ciTargetScalaVersions := Map( @@ -178,7 +368,7 @@ ThisBuild / ciTargetScalaVersions := Map( In the example provided, `ciTargetScalaVersions` is defined at the `ThisBuild` level, meaning that the setting will apply to all projects within the build. The setting defines a Map where the key is the name of the current project, obtained by calling the `id` method on the `thisProject` setting, and the value is a sequence of Scala versions obtained from the `crossScalaVersions` of each submodule setting. -To simplify this process, we can populate the versions using each submodule's crossScalaVersions setting as follows: +To simplify this process, we can populate the versions using each submodule's `crossScalaVersions` setting as follows: ```scala ThisBuild / ciTargetScalaVersions := Map( @@ -187,7 +377,7 @@ ThisBuild / ciTargetScalaVersions := Map( ) ``` -The above code can be simplified further by using `targetScalaVersionsFor` helper method, it takes a list of submodules and returns a Map of project names to their `crossScalaVersions`: +The above code can be simplified further by using the `targetScalaVersionsFor` helper method, which takes a list of submodules and returns a Map of project names to their `crossScalaVersions`: ```scala ThisBuild / ciTargetScalaVersions := targetScalaVersionsFor(submoduleA, submoduleB).value diff --git a/project/build.sbt b/project/build.sbt index 523fb5f7..e4cbfb04 100644 --- a/project/build.sbt +++ b/project/build.sbt @@ -1,3 +1,9 @@ +Compile / unmanagedSourceDirectories += file("zio-sbt-shared/src/main/scala") +Compile / unmanagedResourceDirectories += file("zio-sbt-shared/src/main/resources") + +Compile / unmanagedSourceDirectories += file("zio-sbt-project/src/main/scala") +Compile / unmanagedResourceDirectories += file("zio-sbt-project/src/main/resources") + Compile / unmanagedSourceDirectories += file("zio-sbt-ecosystem/src/main/scala") Compile / unmanagedResourceDirectories += file("zio-sbt-ecosystem/src/main/resources") diff --git a/project/plugins.sbt b/project/plugins.sbt index 1586863d..4ebe17f8 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -5,10 +5,11 @@ addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "2.0.5") addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0") addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1") +addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.4") // Versioning and Release Plugins -addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") -addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") +addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.13.1") +addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.9.0") // Docs Plugins addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.6.1") @@ -21,11 +22,9 @@ addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2") addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.5") addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2") addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.2") - -// Benchmarking Plugins -addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7") +addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.10.0") libraryDependencies += "org.snakeyaml" % "snakeyaml-engine" % "2.8" -libraryDependencies += "dev.zio" %% "zio" % "2.1.9" +libraryDependencies += "dev.zio" %% "zio" % "2.1.13" libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" diff --git a/zio-sbt-ci/src/main/scala/zio/sbt/CiVersions.scala b/zio-sbt-ci/src/main/scala/zio/sbt/CiVersions.scala new file mode 100644 index 00000000..2d1428c7 --- /dev/null +++ b/zio-sbt-ci/src/main/scala/zio/sbt/CiVersions.scala @@ -0,0 +1,8 @@ +package zio.sbt + +object CiVersions { + + val TargetJavaVersions: Seq[String] = Seq("11", "17", "21") + val JavaDistribution = "corretto" + val NodeJS = "16.x" +} 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 562fb65e..8faddadc 100644 --- a/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala +++ b/zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala @@ -27,12 +27,12 @@ import zio.sbt.githubactions.Step.SingleStep import zio.sbt.githubactions.{Job, Step, _} object ZioSbtCiPlugin extends AutoPlugin { - override def requires = plugins.CorePlugin - override def trigger = allRequirements + override def requires: Plugins = plugins.CorePlugin && ZioSbtCrossbuildPlugin + override def trigger = allRequirements object autoImport { val ciDocsVersioningScheme: SettingKey[DocsVersioning] = settingKey[DocsVersioning]("Docs versioning style") - val ciEnabledBranches: SettingKey[Seq[String]] = settingKey[Seq[String]]("Publish branch for documentation") + val ciEnabledBranches: SettingKey[Seq[String]] = settingKey[Seq[String]]("Branches to trigger CI on") val ciGroupSimilarTests: SettingKey[Boolean] = settingKey[Boolean]("Group similar test by their Java and Scala versions, default is false") val ciMatrixMaxParallel: SettingKey[Option[Int]] = @@ -46,6 +46,14 @@ object ZioSbtCiPlugin extends AutoPlugin { settingKey[Option[Condition]]("condition to update readme") val ciTargetJavaVersions: SettingKey[Seq[String]] = settingKey[Seq[String]]("The default target Java versions for all modules, default is 11, 17, 21") + + val ciJobPerScalaPlatform: SettingKey[Boolean] = + settingKey[Boolean]( + "Create a job for each Scala platform, when not defined `allScalaPlatforms.value > 1` is used" + ) + val ciJobPerScalaVersion: SettingKey[Boolean] = + settingKey[Boolean]("Create a job for each Scala version, when not defined `allScalaVersions.value > 1` is used") + val ciTargetMinJavaVersions: SettingKey[Map[String, String]] = SettingKey[Map[String, String]]( "minimum target Java version for each module, default is an empty map which makes CI to use `ciAllTargetJavaVersions` to determine the minimum target Java version for all modules" @@ -58,6 +66,10 @@ object ZioSbtCiPlugin extends AutoPlugin { settingKey[String]( "The default Java version which is used in CI, especially for releasing artifacts, defaults to 17. Note that this is just JDK version used for compilation. Artefact will be compiled with -target and -source flags specified by 'javaPlatform' setting or 'javaPlatform' parameter in 'stdSettings'" ) + val ciDefaultJavaDistribution: SettingKey[String] = + settingKey[String]("The default Java distribution, default is Corretto") + val ciDefaultNodeJSVersion: SettingKey[String] = + settingKey[String]("The default NodeJS version, default is 16.x") val ciCheckGithubWorkflow: TaskKey[Unit] = taskKey[Unit]("Make sure if the ci.yml file is up-to-date") val ciCheckArtifactsBuildSteps: SettingKey[Seq[Step]] = settingKey[Seq[Step]]("Workflow steps for checking artifact build process") @@ -90,11 +102,15 @@ object ZioSbtCiPlugin extends AutoPlugin { import autoImport.* + implicit class CommandOps(command: String) { + def onlyForCrossbuild(crossbuild: Boolean): String = + if (crossbuild) command else "" + } + lazy val buildJobs: Def.Initialize[Seq[Job]] = Def.setting { val swapSizeGB = ciSwapSizeGB.value val setSwapSpace = SetSwapSpace.value val checkout = Checkout.value - val javaVersion = ciDefaultJavaVersion.value val checkAllCodeCompiles = ciCheckArtifactsCompilationSteps.value val checkArtifactBuildProcess = ciCheckArtifactsBuildSteps.value val checkWebsiteBuildProcess = ciCheckWebsiteBuildProcess.value @@ -108,7 +124,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Seq( checkout, SetupLibuv, - SetupJava(javaVersion), + SetupJava(ciDefaultJavaVersion.value, ciDefaultJavaDistribution.value), CacheDependencies ) ++ checkAllCodeCompiles.flatMap(_.flatten) ++ checkArtifactBuildProcess.flatMap( _.flatten @@ -122,7 +138,6 @@ object ZioSbtCiPlugin extends AutoPlugin { val checkout = Checkout.value val swapSizeGB = ciSwapSizeGB.value val setSwapSpace = SetSwapSpace.value - val javaVersion = ciDefaultJavaVersion.value val checkGithubWorkflow = ciCheckGithubWorkflowSteps.value val lint = Lint.value @@ -130,194 +145,247 @@ object ZioSbtCiPlugin extends AutoPlugin { Job( name = "Lint", steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ - Seq(checkout, SetupLibuv, SetupJava(javaVersion), CacheDependencies) ++ checkGithubWorkflow.flatMap( + Seq( + checkout, + SetupLibuv, + SetupJava(ciDefaultJavaVersion.value, ciDefaultJavaDistribution.value), + CacheDependencies + ) ++ checkGithubWorkflow.flatMap( _.flatten ) ++ Seq(lint) ) ) } - lazy val testJobs: Def.Initialize[Seq[Job]] = Def.setting { - val groupSimilarTests = ciGroupSimilarTests.value - val scalaVersionMatrix = ciTargetScalaVersions.value - val javaPlatforms = autoImport.ciTargetJavaVersions.value - val javaPlatformMatrix = ciTargetMinJavaVersions.value - val matrixMaxParallel = ciMatrixMaxParallel.value - val swapSizeGB = ciSwapSizeGB.value - val setSwapSpace = SetSwapSpace.value - val checkout = Checkout.value - val backgroundJobs = ciBackgroundJobs.value - - val prefixJobs = makePrefixJobs(backgroundJobs) - - val GroupTests = { - def makeTests(scalaVersion: String) = - s" ${scalaVersionMatrix.filter { case (_, versions) => - versions.contains(scalaVersion) - }.map(e => e._1 + "/test").mkString(" ")}" - - Job( - name = "Test", - strategy = Some( - Strategy( - matrix = ListMap( - "java" -> javaPlatforms.toList.sorted, - "scala" -> scalaVersionMatrix.values.flatten.toList.distinct.sorted - ), - maxParallel = matrixMaxParallel, - failFast = false - ) - ), - steps = { - (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ Seq( - SetupLibuv, - SetupJava("${{ matrix.java }}"), - CacheDependencies, - checkout - ) ++ (if (javaPlatformMatrix.values.toSet.isEmpty) { - scalaVersionMatrix.values.toSeq.flatten.distinct.map { scalaVersion: String => - Step.SingleStep( - name = "Test", - `if` = Some(Condition.Expression(s"matrix.scala == '$scalaVersion'")), - run = Some( - prefixJobs + "sbt ++${{ matrix.scala }}" + makeTests( - scalaVersion + lazy val testJobs: Def.Initialize[Seq[Job]] = Def.settingDyn { + Def.setting { + val groupSimilarTests = ciGroupSimilarTests.value + val scalaVersionMatrix = ciTargetScalaVersions.value + val javaVersions = autoImport.ciTargetJavaVersions.value + val scalaVersions = ScalaVersions.Keys.allScalaVersions.value + val scalaPlatforms = ScalaPlatforms.Keys.allScalaPlatforms.value + val javaPlatformMatrix = ciTargetMinJavaVersions.value + val matrixMaxParallel = ciMatrixMaxParallel.value + val swapSizeGB = ciSwapSizeGB.value + val setSwapSpace = SetSwapSpace.value + val checkout = Checkout.value + val backgroundJobs = ciBackgroundJobs.value + + val prefixJobs = makePrefixJobs(backgroundJobs) + + val isSingleBuild = IsSingleBuild.value + val crossbuildOrNot = CrossbuildOrNot.value + + val jobPerScalaPlatform = ciJobPerScalaPlatform.?.value.getOrElse(scalaPlatforms.size > 1) + val jobPerScalaVersion = ciJobPerScalaVersion.?.value.getOrElse(scalaVersions.size > 1) + + def setScalaVersionOrNot(scalaVersion: String) = if (isSingleBuild) "" else s"++$scalaVersion" + + val testTask: String => String = + if (isSingleBuild) + (scalaVersion: String) => "test" + TestTasks.scalaVersionToSuffix(scalaVersion) + else + _ => "test" + + val GroupTests = { + def makeTests(scalaVersion: String) = + s"${scalaVersionMatrix.filter { case (_, versions) => + versions.contains(scalaVersion) + }.map(e => e._1 + "/" + testTask(scalaVersion)).mkString(" ")}" + + Job( + name = "Test", + strategy = Some( + Strategy( + matrix = ListMap( + "java" -> javaVersions.toList.sorted, + "scala" -> scalaVersionMatrix.values.flatten.toList.distinct.sorted + ), + maxParallel = matrixMaxParallel, + failFast = false + ) + ), + steps = { + (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ Seq( + SetupLibuv, + SetupJava("${{ matrix.java }}", ciDefaultJavaDistribution.value), + CacheDependencies, + checkout + ) ++ (if (javaPlatformMatrix.values.toSet.isEmpty) { + scalaVersionMatrix.values.toSeq.flatten.distinct.map { scalaVersion: String => + Step.SingleStep( + name = "Test", + `if` = Some(Condition.Expression(s"matrix.scala == '$scalaVersion'")), + run = Some( + Seq(prefixJobs, "sbt", setScalaVersionOrNot("${{ matrix.scala }}"), makeTests(scalaVersion)) + .filterNot(_.isBlank()) + .mkString(" ") ) ) - ) - } - } else { - (for { - javaPlatform: String <- Set("11", "17", "21") - scalaVersion: String <- scalaVersionMatrix.values.toSeq.flatten.toSet - projects = - scalaVersionMatrix.filterKeys { p => - javaPlatformMatrix.getOrElse(p, javaPlatform).toInt <= javaPlatform.toInt - }.filter { case (_, versions) => - versions.contains(scalaVersion) - }.keys - } yield - if (projects.nonEmpty) - Seq( - Step.SingleStep( - name = "Test", - `if` = Some( - Condition.Expression(s"matrix.java == '$javaPlatform'") && Condition.Expression( - s"matrix.scala == '$scalaVersion'" + } + } else { + (for { + javaPlatform: String <- Set("11", "17", "21") + scalaVersion: String <- scalaVersionMatrix.values.toSeq.flatten.toSet + projects = + scalaVersionMatrix.filterKeys { p => + javaPlatformMatrix.getOrElse(p, javaPlatform).toInt <= javaPlatform.toInt + }.filter { case (_, versions) => + versions.contains(scalaVersion) + }.keys + } yield + if (projects.nonEmpty) + Seq( + Step.SingleStep( + name = "Test", + `if` = Some( + Condition.Expression(s"matrix.java == '$javaPlatform'") && Condition.Expression( + s"matrix.scala == '$scalaVersion'" + ) + ), + run = Some( + Seq( + prefixJobs, + "sbt", + setScalaVersionOrNot("${{ matrix.scala }}"), + projects.map(_ + "/" + testTask(scalaVersion)).mkString(" ") + ).filterNot(_.isBlank()).mkString(" ") ) - ), - run = Some( - prefixJobs + "sbt ++${{ matrix.scala }}" ++ s" ${projects.map(_ + "/test ").mkString(" ")}" ) ) - ) - else Seq.empty).flatten.toSeq - }) - } - ) - } + else Seq.empty).flatten.toSeq + }) + } + ) + } - val FlattenTests = - Job( - name = "Test", - strategy = Some( - Strategy( - matrix = ListMap( - "java" -> javaPlatforms.toList.sorted - ) ++ - (if (javaPlatformMatrix.isEmpty) { - ListMap("scala-project" -> scalaVersionMatrix.flatMap { case (moduleName, versions) => - versions.map { version => - s"++$version $moduleName" - } - }.toList) - } else { - def generateScalaProjectJavaPlatform(javaPlatform: String) = - s"scala-project-java$javaPlatform" -> scalaVersionMatrix.filterKeys { p => - javaPlatformMatrix.getOrElse(p, javaPlatform).toInt <= javaPlatform.toInt - }.flatMap { case (moduleName, versions) => + val FlattenTests = + Job( + name = "Test", + strategy = Some( + Strategy( + matrix = ListMap( + "java" -> javaVersions.toList.sorted + ) ++ + (if (javaPlatformMatrix.isEmpty) { + ListMap("scala-project" -> scalaVersionMatrix.flatMap { case (moduleName, versions) => versions.map { version => - s"++$version $moduleName" + Seq(setScalaVersionOrNot(version), moduleName + "/" + testTask(version)) + .filterNot(_.isBlank()) + .mkString(" ") } - }.toList - - javaPlatforms.map(jp => generateScalaProjectJavaPlatform(jp)) - }), - maxParallel = matrixMaxParallel, - failFast = false - ) - ), - steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ - Seq( - SetupLibuv, - SetupJava("${{ matrix.java }}"), - CacheDependencies, - checkout - ) ++ ( - if (javaPlatformMatrix.values.toSet.isEmpty) { - Seq( - Step.SingleStep( - name = "Test", - run = Some(prefixJobs + "sbt ${{ matrix.scala-project }}/test") - ) - ) - } else { - Seq( - Step.SingleStep( - name = "Java 11 Tests", - `if` = Some(Condition.Expression("matrix.java == '11'")), - run = Some( - prefixJobs + "sbt ${{ matrix.scala-project-java11 }}/test" - ) - ), - Step.SingleStep( - name = "Java 17 Tests", - `if` = Some(Condition.Expression("matrix.java == '17'")), - run = Some( - prefixJobs + "sbt ${{ matrix.scala-project-java17 }}/test" + }.toList) + } else { + def generateScalaProjectJavaPlatform(javaPlatform: String) = + s"scala-project-java$javaPlatform" -> scalaVersionMatrix.filterKeys { p => + javaPlatformMatrix.getOrElse(p, javaPlatform).toInt <= javaPlatform.toInt + }.flatMap { case (moduleName, versions) => + versions.map { version => + Seq(setScalaVersionOrNot(version), moduleName + "/" + testTask(version)) + .filterNot(_.isBlank()) + .mkString(" ") + } + }.toList + + javaVersions.map(jp => generateScalaProjectJavaPlatform(jp)) + }), + maxParallel = matrixMaxParallel, + failFast = false + ) + ), + steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ + Seq( + SetupLibuv, + SetupJava("${{ matrix.java }}", ciDefaultJavaDistribution.value), + CacheDependencies, + checkout + ) ++ ( + if (javaPlatformMatrix.values.toSet.isEmpty) { + Seq( + Step.SingleStep( + name = "Test", + run = Some(prefixJobs + "sbt ${{ matrix.scala-project }}") ) - ), - Step.SingleStep( - name = "Java 21 Tests", - `if` = Some(Condition.Expression("matrix.java == '21'")), - run = Some( - prefixJobs + "sbt ${{ matrix.scala-project-java21 }}/test" + ) + } else { + Seq( + Step.SingleStep( + name = "Java 11 Tests", + `if` = Some(Condition.Expression("matrix.java == '11'")), + run = Some( + prefixJobs + "sbt ${{ matrix.scala-project-java11 }}" + ) + ), + Step.SingleStep( + name = "Java 17 Tests", + `if` = Some(Condition.Expression("matrix.java == '17'")), + run = Some( + prefixJobs + "sbt ${{ matrix.scala-project-java17 }}" + ) + ), + Step.SingleStep( + name = "Java 21 Tests", + `if` = Some(Condition.Expression("matrix.java == '21'")), + run = Some( + prefixJobs + "sbt ${{ matrix.scala-project-java21 }}" + ) ) ) - ) - } - ) - ) + } + ) + ) - val DefaultTestStrategy = - Job( - name = "Test", - strategy = Some( - Strategy( - matrix = ListMap( - "java" -> javaPlatforms.toList.sorted - ), - maxParallel = matrixMaxParallel, - failFast = false - ) - ), - steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ - Seq( - SetupLibuv, - SetupJava("${{ matrix.java }}"), - CacheDependencies, - checkout, - Step.SingleStep( - name = "Test", - run = Some(prefixJobs + "sbt +test") + val DefaultTestStrategy = { + val ((testMatrix: ListMap[String, List[String]]), (testTaskSuffix: String)) = + if (jobPerScalaPlatform && jobPerScalaVersion) { + ListMap( + "scalaVersion" -> scalaVersions.toList.sorted, + "scalaPlatform" -> scalaPlatforms.toList.map(_.asString).sorted + ) -> + "${{ matrix.scalaPlatform }}${{ startsWith(matrix.scalaVersion, '2.12') && '2_12' || (startsWith(matrix.scalaVersion, '2.13') && '2_13' || (startsWith(matrix.scalaVersion, '3') && '3' || '')) }}" + } else if (jobPerScalaPlatform) { + ListMap( + "scalaPlatform" -> scalaPlatforms.toList.map(_.asString).sorted + ) -> "${{ matrix.scalaPlatform }}" + } else if (jobPerScalaVersion) { + ListMap( + "scalaVersion" -> scalaVersions.toList.sorted + ) -> "${{ startsWith(matrix.scalaVersion, '2.12') && '2_12' || (startsWith(matrix.scalaVersion, '2.13') && '2_13' || (startsWith(matrix.scalaVersion, '3') && '3' || '')) }}" + } else { + ListMap.empty -> "" + } + Job( + name = "Test", + strategy = Some( + Strategy( + matrix = ListMap( + "java" -> javaVersions.toList.sorted + ) ++ testMatrix, + maxParallel = matrixMaxParallel, + failFast = false ) - ) - ) + ), + steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++ + Seq( + SetupLibuv, + SetupJava("${{ matrix.java }}", ciDefaultJavaDistribution.value), + CacheDependencies, + checkout, + Step.SingleStep( + name = "Test", + run = Some( + prefixJobs + s"sbt ${crossbuildOrNot}test" ++ testTaskSuffix + ) + ) + ) + ) + } - if (javaPlatformMatrix.isEmpty && scalaVersionMatrix.isEmpty) - Seq(DefaultTestStrategy) - else - Seq(if (groupSimilarTests) GroupTests else FlattenTests) + if (javaPlatformMatrix.isEmpty && scalaVersionMatrix.isEmpty) + Seq(DefaultTestStrategy) + else + Seq(if (groupSimilarTests) GroupTests else FlattenTests) + } } lazy val reportSuccessfulJobs: Def.Initialize[Seq[Job]] = Def.setting { @@ -341,7 +409,6 @@ object ZioSbtCiPlugin extends AutoPlugin { val swapSizeGB = ciSwapSizeGB.value val setSwapSpace = SetSwapSpace.value val checkout = Checkout.value - val javaVersion = ciDefaultJavaVersion.value val updateReadmeCondition = autoImport.ciUpdateReadmeCondition.value val generateReadme = GenerateReadme.value @@ -353,7 +420,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Seq( checkout, SetupLibuv, - SetupJava(javaVersion), + SetupJava(ciDefaultJavaVersion.value, ciDefaultJavaDistribution.value), CacheDependencies, generateReadme, Step.SingleStep( @@ -426,7 +493,6 @@ object ZioSbtCiPlugin extends AutoPlugin { val swapSizeGB = ciSwapSizeGB.value val setSwapSpace = SetSwapSpace.value val checkout = Checkout.value - val javaVersion = ciDefaultJavaVersion.value val release = Release.value val jobs = ciReleaseApprovalJobs.value @@ -439,7 +505,7 @@ object ZioSbtCiPlugin extends AutoPlugin { Seq( checkout, SetupLibuv, - SetupJava(javaVersion), + SetupJava(ciDefaultJavaVersion.value, ciDefaultJavaDistribution.value), CacheDependencies, release ) @@ -451,7 +517,7 @@ object ZioSbtCiPlugin extends AutoPlugin { val swapSizeGB = ciSwapSizeGB.value val setSwapSpace = SetSwapSpace.value val checkout = Checkout.value - val javaVersion = ciDefaultJavaVersion.value + val nodeJsVersion = ciDefaultNodeJSVersion.value val publishToNpmRegistry = PublishToNpmRegistry.value Seq( @@ -468,9 +534,9 @@ object ZioSbtCiPlugin extends AutoPlugin { Seq( checkout, SetupLibuv, - SetupJava(javaVersion), + SetupJava(ciDefaultJavaVersion.value, ciDefaultJavaDistribution.value), CacheDependencies, - SetupNodeJs, + SetupNodeJs(nodeJsVersion), publishToNpmRegistry ) ), @@ -564,34 +630,48 @@ object ZioSbtCiPlugin extends AutoPlugin { IO.write(new File(s".github/workflows/${ciWorkflowName.value.toLowerCase}.yml"), template) } + val IsSingleBuild: Def.Initialize[Boolean] = BuildAssertions.scalaCrossVersionsCountsOnePerProject + val CrossbuildOrNot: Def.Initialize[String] = Def.setting { + if (IsSingleBuild.value) "" else "+" + } + override lazy val buildSettings: Seq[Setting[_]] = Seq( - ciWorkflowName := "CI", - ciEnabledBranches := Seq.empty, - ciGenerateGithubWorkflow := generateGithubWorkflowTask.value, - ciDocsVersioningScheme := DocsVersioning.SemanticVersioning, - ciCheckGithubWorkflow := checkGithubWorkflowTask.value, - ciTargetScalaVersions := Map.empty, - ciTargetMinJavaVersions := Map.empty, - ciJvmOptions := Seq.empty, - ciNodeOptions := Seq.empty, - ciUpdateReadmeCondition := None, - ciGroupSimilarTests := false, - ciSwapSizeGB := 0, - ciTargetJavaVersions := Seq("11", "17", "21"), + ciWorkflowName := "CI", + ciEnabledBranches := Seq.empty, + ciGenerateGithubWorkflow := generateGithubWorkflowTask.value, + ciDocsVersioningScheme := DocsVersioning.SemanticVersioning, + ciCheckGithubWorkflow := checkGithubWorkflowTask.value, + ciTargetScalaVersions := Map.empty, + ciTargetMinJavaVersions := Map.empty, + ciJvmOptions := Seq.empty, + ciNodeOptions := Seq.empty, + ciUpdateReadmeCondition := None, + ciGroupSimilarTests := false, + ciSwapSizeGB := 0, + ciTargetJavaVersions := CiVersions.TargetJavaVersions, + ciDefaultJavaDistribution := CiVersions.JavaDistribution, + ciDefaultNodeJSVersion := CiVersions.NodeJS, ciCheckArtifactsBuildSteps := Seq( Step.SingleStep( name = "Check artifacts build process", - run = Some("sbt +publishLocal") + run = Some(s"sbt ${CrossbuildOrNot.value}publishLocal") ) ), ciCheckWebsiteBuildProcess := CheckWebsiteBuildProcess.value, - ciCheckArtifactsCompilationSteps := Seq( - Step.SingleStep( - name = "Check all code compiles", - run = Some(makePrefixJobs(ciBackgroundJobs.value) + "sbt +Test/compile") - ) + ciCheckArtifactsCompilationSteps := ( + Def.settingDyn { + val compileCommand = s"sbt ${CrossbuildOrNot.value}Test/compile" + Def.setting( + Seq( + Step.SingleStep( + name = "Check all code compiles", + run = Some(makePrefixJobs(ciBackgroundJobs.value) + compileCommand) + ) + ) + ) + }.value ), ciCheckGithubWorkflowSteps := Seq( Step.SingleStep( @@ -603,7 +683,7 @@ object ZioSbtCiPlugin extends AutoPlugin { ), ciBackgroundJobs := Seq.empty, ciMatrixMaxParallel := None, - ciDefaultJavaVersion := "17", + ciDefaultJavaVersion := zio.sbt.JavaVersion.`17`, ciBuildJobs := buildJobs.value, ciLintJobs := lintJobs.value, ciTestJobs := testJobs.value, @@ -665,12 +745,12 @@ object ZioSbtCiPlugin extends AutoPlugin { run = Some("sudo apt-get update && sudo apt-get install -y libuv1-dev") ) - def SetupJava(version: String = "17"): Step.SingleStep = Step.SingleStep( + def SetupJava(version: String = "17", distribution: String = "corretto"): Step.SingleStep = Step.SingleStep( name = "Setup Scala", uses = Some(ActionRef(V("actions/setup-java"))), `with` = Some( ListMap( - "distribution" -> "corretto".toJsonAST.right.get, + "distribution" -> distribution.toJsonAST.right.get, "java-version" -> version.toJsonAST.right.get, "check-latest" -> true.toJsonAST.right.get ) @@ -710,26 +790,30 @@ object ZioSbtCiPlugin extends AutoPlugin { val prefixJobs = makePrefixJobs(backgroundJobs) + val isSingleBuild = IsSingleBuild.value + Step.SingleStep( name = "Release", run = Some(prefixJobs + "sbt ci-release"), env = Some( ListMap( - "PGP_PASSPHRASE" -> "${{ secrets.PGP_PASSPHRASE }}", - "PGP_SECRET" -> "${{ secrets.PGP_SECRET }}", - "SONATYPE_PASSWORD" -> "${{ secrets.SONATYPE_PASSWORD }}", - "SONATYPE_USERNAME" -> "${{ secrets.SONATYPE_USERNAME }}" + "PGP_PASSPHRASE" -> "${{ secrets.PGP_PASSPHRASE }}", + "PGP_SECRET" -> "${{ secrets.PGP_SECRET }}", + "SONATYPE_PASSWORD" -> "${{ secrets.SONATYPE_PASSWORD }}", + "SONATYPE_USERNAME" -> "${{ secrets.SONATYPE_USERNAME }}", + "CI_RELEASE" -> (if (isSingleBuild) "publishSigned" else "+publishSigned"), + "CI_SNAPSHOT_RELEASE" -> (if (isSingleBuild) "publish" else "+publish") ) ) ) } - val SetupNodeJs: Step.SingleStep = Step.SingleStep( + def SetupNodeJs(version: String = CiVersions.NodeJS): Step.SingleStep = Step.SingleStep( name = "Setup NodeJs", uses = Some(ActionRef(V("actions/setup-node"))), `with` = Some( ListMap( - "node-version" -> "16.x".toJsonAST.right.get, + "node-version" -> version.toJsonAST.right.get, "registry-url" -> "https://registry.npmjs.org".toJsonAST.right.get ) ) diff --git a/zio-sbt-ecosystem/build.sbt b/zio-sbt-ecosystem/build.sbt index 3cf71721..de210a9d 100644 --- a/zio-sbt-ecosystem/build.sbt +++ b/zio-sbt-ecosystem/build.sbt @@ -1,34 +1,18 @@ -// Build Server Plugins -addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "2.0.2") - // Linting Plugins addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0") addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1") // Versioning and Release Plugins -addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") -addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") +addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.13.1") +addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.9.0") // Docs Plugins addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.6.1") addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0") -// Cross-Compiler Plugins -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.17.0") -addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2") -addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.4") -addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2") -addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.2") - -// Benchmarking Plugins -addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7") -addSbtPlugin("pl.project13.scala" % "sbt-jcstress" % "0.2.0") - // Binary Compatibility Plugin -libraryDependencies += "dev.zio" %% "zio" % "2.1.13" +addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.4") -libraryDependencies += "dev.zio" %% "zio" % "2.1.9" -libraryDependencies += "dev.zio" %% "zio-json" % "0.7.3" -libraryDependencies += "dev.zio" %% "zio-json-yaml" % "0.7.3" +libraryDependencies += "dev.zio" %% "zio" % "2.1.13" diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Commands.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Commands.scala index f34c3e2a..29c7bc43 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Commands.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Commands.scala @@ -88,6 +88,9 @@ object Commands { val fmt: ComposableCommand = (quietOn >> "scalafmtSbt" >> "+scalafmt" >> "+Test / scalafmt") ?? ("fmt", "Formats source files using scalafmt.") + val checkMima: ComposableCommand = + (quietOn >> "+checkMima") ?? ("checkMima", "Checks binary compatibility against previous versions.") + // TODO: have the correct license headers val lint: ComposableCommand = { quietOn >> @@ -96,6 +99,7 @@ object Commands { "+scalafmtCheckAll" >> // "+headerCheckAll" >> fixLint >> + checkMima >> "disableStrictCompile" } ?? ( "lint", @@ -125,6 +129,7 @@ object Commands { lint.toItem, fmt.toItem, fix.toItem, + checkMima.toItem, prepare.toItem, // buildAll.toItem, prepare.toItem, diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/MimaSettings.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/MimaSettings.scala new file mode 100644 index 00000000..6d162270 --- /dev/null +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/MimaSettings.scala @@ -0,0 +1,46 @@ +/* + * Copyright 2022-2023 dev.zio + * + * 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 zio.sbt +import com.typesafe.tools.mima.plugin.MimaKeys._ +import sbt.Keys._ +import sbt._ +import sbtdynver.DynVerPlugin.autoImport.previousStableVersion + +import zio.sbt.BuildAssertions.Keys.isScalaJVM + +trait MimaSettings { + + lazy val checkMima: TaskKey[Unit] = taskKey[Unit]("Checks binary compatibility against previous versions.") +} + +object MimaSettings extends MimaSettings { + + def projectSettings: Seq[Setting[_]] = + Def.settings( + checkMima := { + Def.taskIf { + if (isScalaJVM.value && !((checkMima / skip).value || (publish / skip).value)) mimaReportBinaryIssues.value + else () + }.value + }, + mimaFailOnProblem := true, + mimaPreviousArtifacts := (ThisBuild / previousStableVersion).value + .map(organization.value %% moduleName.value % _) + .fold(Set.empty[ModuleID])(Set(_)) + ) + +} diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala index 43ba56c3..fc5b5184 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/ScalaCompilerSettings.scala @@ -19,12 +19,14 @@ package zio.sbt import explicitdeps.ExplicitDepsPlugin.autoImport._ import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._ import sbt.Keys._ -import sbt.{Def, _} +import sbt.{JavaVersion => _, _} import sbtbuildinfo.BuildInfoPlugin.autoImport.{BuildInfoKey, buildInfoKeys, buildInfoPackage} -import sbtcrossproject.CrossPlugin.autoImport.{JVMPlatform, crossProjectPlatform} +import sbtcrossproject.CrossPlugin.autoImport.crossProjectPlatform import scalafix.sbt.ScalafixPlugin.autoImport.{scalafixDependencies, scalafixSemanticdb} +import zio.sbt.ScalaVersions.Keys.{scala212, scala213, scala3} import zio.sbt.Versions._ +import zio.sbt.ZioSbtCrossbuildPlugin.autoImport.javaPlatform trait ScalaCompilerSettings { @@ -79,19 +81,19 @@ trait ScalaCompilerSettings { } ) - def extraOptions(scalaVersion: String, javaPlatform: String, optimize: Boolean): Seq[String] = - CrossVersion.partialVersion(scalaVersion) match { + def extraOptions(scalaVersion: String, optimize: Boolean): Seq[String] = + (CrossVersion.partialVersion(scalaVersion) match { case Some((3, _)) => Seq( "-language:implicitConversions", "-Xignore-scala2-macros", - "-noindent", - s"-release:$javaPlatform" + "-Xmax-inlines:64", + "-Wunused:imports" ) case Some((2, 13)) => Seq( "-Ywarn-unused:params,-implicits", - s"-release:$javaPlatform" + "-Wunused:imports" ) ++ std2xOptions ++ optimizerOptions(optimize) case Some((2, 12)) => Seq( @@ -112,7 +114,7 @@ trait ScalaCompilerSettings { "242" ) ++ std2xOptions ++ optimizerOptions(optimize) case _ => Seq.empty - } + }) def platformSpecificSources(platform: String, conf: String, baseDirectory: File)(versions: String*): List[File] = for { @@ -136,29 +138,42 @@ trait ScalaCompilerSettings { platformSpecificSources(platform, conf, baseDir)(versions: _*) } - lazy val crossProjectSettings: Seq[Setting[Seq[File]]] = Seq( - Compile / unmanagedSourceDirectories ++= { - crossPlatformSources( - scalaVersion.value, - crossProjectPlatform.value.identifier, - "main", - baseDirectory.value - ) - }, - Test / unmanagedSourceDirectories ++= { - crossPlatformSources( - scalaVersion.value, - crossProjectPlatform.value.identifier, - "test", - baseDirectory.value - ) - } + private lazy val nonProjectMatrixSourceDirectories = Seq( + Compile / unmanagedSourceDirectories ++= Def.settingDyn { + if (crossProjectPlatform.?.value.isDefined) { + Def.setting { + crossPlatformSources( + scalaVersion.value, + crossProjectPlatform.value.identifier, + "main", + baseDirectory.value + ) + } + } else { + Def.setting(List.empty[File]) + } + }.value, + Test / unmanagedSourceDirectories ++= Def.settingDyn { + if (crossProjectPlatform.?.value.isDefined) { + Def.setting { + crossPlatformSources( + scalaVersion.value, + crossProjectPlatform.value.identifier, + "test", + baseDirectory.value + ) + } + } else { + Def.setting(List.empty[File]) + } + }.value ) + def crossProjectSettings: Seq[Setting[_]] = nonProjectMatrixSourceDirectories + def stdSettings( name: Option[String] = None, packageName: Option[String] = None, - javaPlatform: String = "11", enableKindProjector: Boolean = true, enableCrossProject: Boolean = false, enableScalafix: Boolean = true, @@ -169,17 +184,23 @@ trait ScalaCompilerSettings { case None => Seq.empty }) ++ Seq( - ZioSbtEcosystemPlugin.autoImport.javaPlatform := javaPlatform, + crossScalaVersions := { + if (BuildAssertions.Keys.isProjectMatrix.?.value.contains(true)) + Seq.empty + else + ScalaVersions.Keys.defaultCrossScalaVersions.?.value + .getOrElse(Seq(scala212.value, scala213.value, scala3.value)) + }, scalacOptions := ( scalacOptions.value ++ stdOptions ++ - extraOptions(Keys.scalaVersion.value, javaPlatform, optimize = !isSnapshot.value) ++ + extraOptions(Keys.scalaVersion.value, optimize = !isSnapshot.value) ++ ( if (turnCompilerWarningIntoErrors && sys.env.contains("CI")) Seq("-Xfatal-warnings") else Nil // to enable Scalafix locally - ) + ) ++ + Seq(s"-release:${javaPlatform.value}") ).distinct, - javacOptions := Seq("-source", javaPlatform, "-target", javaPlatform), // Compile / console / scalacOptions ~= { // _.filterNot(Set("-Xfatal-warnings")) // }, @@ -192,8 +213,7 @@ trait ScalaCompilerSettings { }, Test / parallelExecution := scalaBinaryVersion.value != "3", // why not parallel execution for Scala 3? incOptions ~= (_.withLogRecompileOnMacro(false)), - autoAPIMappings := true, - unusedCompileDependenciesFilter -= moduleFilter("org.scala-js", "scalajs-library") + autoAPIMappings := true ) ++ (if (enableCrossProject) crossProjectSettings else Seq.empty) ++ { packageName match { case Some(name) => buildInfoSettings(name) @@ -205,8 +225,16 @@ trait ScalaCompilerSettings { lazy val scalafixSettings: Seq[Def.Setting[_]] = Seq( - semanticdbEnabled := Keys.scalaBinaryVersion.value != "3", - semanticdbOptions += "-P:semanticdb:synthetics:on", + semanticdbEnabled := { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((3, 3)) => true + case Some((2, _)) => true + case _ => false + } + }, + semanticdbOptions ++= { + if (BuildAssertions.Keys.isScala3.value) Seq.empty else Seq("-P:semanticdb:synthetics:on") + }, semanticdbVersion := scalafixSemanticdb.revision, // use Scalafix compatible version ThisBuild / scalafixDependencies ++= List( "com.github.vovapolu" %% "scaluzzi" % ScaluzziVersion @@ -217,7 +245,7 @@ trait ScalaCompilerSettings { def scalaReflectTestSettings: List[Setting[_]] = List( libraryDependencies ++= { if (scalaBinaryVersion.value == "3") - Seq("org.scala-lang" % "scala-reflect" % ZioSbtEcosystemPlugin.autoImport.scala213.value % Test) + Seq("org.scala-lang" % "scala-reflect" % ZioSbtCrossbuildPlugin.autoImport.scala213.value % Test) else Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value % Test) } @@ -243,7 +271,7 @@ trait ScalaCompilerSettings { def buildInfoSettings(packageName: String): Seq[Setting[_]] = Seq( - buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion, isSnapshot), + buildInfoKeys := Seq[BuildInfoKey](organization, moduleName, name, version, scalaVersion, sbtVersion, isSnapshot), buildInfoPackage := packageName ) @@ -268,19 +296,32 @@ trait ScalaCompilerSettings { ) def jsSettings: Seq[Setting[_]] = Seq( - Test / fork := crossProjectPlatform.value == JVMPlatform // set fork to `true` on JVM to improve log readability, JS and Native need `false` + Test / fork := BuildAssertions + .requireJS( + false + ) + .value, + unusedCompileDependenciesFilter -= moduleFilter("org.scala-js") ) -// def jsSettings_ = Seq( -// libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % "2.2.2", -// libraryDependencies += "io.github.cquiroz" %%% "scala-java-time-tzdb" % "2.2.2" -// ) + def jvmSettings: Seq[Setting[_]] = Seq( + Test / fork := BuildAssertions + .requireJVM( + true + ) + .value // set fork to `true` on JVM to improve log readability, JS and Native need `false` + ) def nativeSettings: Seq[Setting[_]] = Seq( - doc / skip := true, - Compile / doc / sources := Seq.empty, - Test / test := { val _ = (Test / compile).value; () }, - Test / fork := crossProjectPlatform.value == JVMPlatform // set fork to `true` on JVM to improve log readability, JS and Native need `false` + Test / fork := BuildAssertions + .requireNative( + false + ) + .value, + doc / skip := BuildAssertions.requireNative(true).value, + Compile / doc / sources := BuildAssertions.requireNative(Seq.empty).value, + unusedCompileDependenciesFilter -= moduleFilter("org.scala-native") + // Test / test := { val _ = (Test / compile).value; () } // ?? ) lazy val scalajs: Seq[Setting[_]] = diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala index 5da8b57a..8decbc07 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/Versions.scala @@ -16,15 +16,15 @@ package zio.sbt -import sbt._ +import sbt.{ScalaVersion => _, _} object Versions { val KindProjectorVersion = "0.13.3" val ScaluzziVersion = "0.1.23" - val scala3 = "3.3.4" - val scala212 = "2.12.20" - val scala213 = "2.13.15" + val Scala3 = ScalaVersion.Scala3 + val Scala212 = ScalaVersion.Scala212 + val Scala213 = ScalaVersion.Scala213 val zioVersion = "2.1.13" diff --git a/zio-sbt-ecosystem/src/main/scala/zio/sbt/ZioSbtEcosystemPlugin.scala b/zio-sbt-ecosystem/src/main/scala/zio/sbt/ZioSbtEcosystemPlugin.scala index 0d9e444c..6bbe6a0e 100644 --- a/zio-sbt-ecosystem/src/main/scala/zio/sbt/ZioSbtEcosystemPlugin.scala +++ b/zio-sbt-ecosystem/src/main/scala/zio/sbt/ZioSbtEcosystemPlugin.scala @@ -19,22 +19,25 @@ package zio.sbt import scala.collection.immutable.ListMap import com.jsuereth.sbtpgp.SbtPgp.autoImport._ +import com.typesafe.tools.mima.plugin.MimaPlugin import de.heikoseeberger.sbtheader.HeaderPlugin import org.scalafmt.sbt.ScalafmtPlugin import sbt.Keys._ import sbt.nio.Keys.{ReloadOnSourceChanges, onChangedBuildSource} import sbt.{Def, _} -import sbtbuildinfo.BuildInfoPlugin +import sbtdynver.DynVerPlugin import scalafix.sbt.ScalafixPlugin +import zio.sbt.ZioSbtShared.autoImport.{banners, usefulTasksAndSettings, welcomeMessage} + object ZioSbtEcosystemPlugin extends AutoPlugin { override def trigger = allRequirements override def requires: Plugins = - super.requires && HeaderPlugin && ScalafixPlugin && ScalafmtPlugin && BuildInfoPlugin + super.requires && HeaderPlugin && ScalafixPlugin && ScalafmtPlugin && MimaPlugin && DynVerPlugin && ZioSbtCrossbuildPlugin - object autoImport extends ScalaCompilerSettings { + object autoImport extends ScalaCompilerSettings with MimaSettings { def addCommand(commandString: List[String], name: String, description: String): Seq[Setting[_]] = { val cCommand = Commands.ComposableCommand(commandString, name, description) @@ -47,69 +50,35 @@ object ZioSbtEcosystemPlugin extends AutoPlugin { usefulTasksAndSettings += command.toItem ) - lazy val scala3: SettingKey[String] = settingKey[String]("Scala 3 version") - lazy val scala212: SettingKey[String] = settingKey[String]("Scala 2.12 version") - lazy val scala213: SettingKey[String] = settingKey[String]("Scala 2.13 version") lazy val zioVersion: SettingKey[String] = settingKey[String]("ZIO version") - lazy val javaPlatform: SettingKey[String] = settingKey[String]("java target platform, default is 11") - - val welcomeBannerEnabled: SettingKey[Boolean] = - settingKey[Boolean]("Indicates whether or not to enable the welcome banner.") - - val usefulTasksAndSettings: SettingKey[Map[String, String]] = settingKey[Map[String, String]]( - "A map of useful tasks and settings that will be displayed as part of the welcome banner." - ) - } import autoImport.* - private val defaultTasksAndSettings: Map[String, String] = Commands.ComposableCommand.makeHelp ++ ListMap( + private val defaultTasksAndSettings: ListMap[String, String] = Commands.ComposableCommand.makeHelp ++ ListMap( // "build" -> "Lints source files then strictly compiles and runs tests.", - "enableStrictCompile" -> "Enables strict compilation e.g. warnings become errors.", - "disableStrictCompile" -> "Disables strict compilation e.g. warnings are no longer treated as errors.", - "~compile" -> "Compiles all modules (file-watch enabled)", - "test" -> "Runs all tests", - """testOnly *.YourSpec -- -t \"YourLabel\"""" -> "Only runs tests with matching term e.g." + "enableStrictCompile" -> "Enables strict compilation e.g. warnings become errors.", + "disableStrictCompile" -> "Disables strict compilation e.g. warnings are no longer treated as errors.", + "~compile" -> "Compiles all modules (file-watch enabled)" ) - def welcomeMessage: Setting[String] = - onLoadMessage := { - if (welcomeBannerEnabled.value) { - import scala.Console - - val maxLen = usefulTasksAndSettings.value.keys.map(_.length).max - - def normalizedPadding(s: String) = " " * (maxLen - s.length) - - def item(text: String): String = s"${Console.GREEN}> ${Console.CYAN}$text${Console.RESET}" - - s"""|${Banner.trueColor(s"${name.value} v.${version.value}")} - |Useful sbt tasks: - |${usefulTasksAndSettings.value.map { case (task, description) => - s"${item(task)} ${normalizedPadding(task)}${description}" - } - .mkString("\n")} - """.stripMargin - - } else "" - } - override def projectSettings: Seq[Setting[_]] = - Commands.settings ++ welcomeMessage ++ Seq( - usefulTasksAndSettings := defaultTasksAndSettings, - welcomeBannerEnabled := true - ) ++ Tasks.settings // ++ Tasks.settings + Commands.settings ++ Tasks.settings ++ Seq( + banners ++= { + if (SharedTasks.isRoot.value) Seq(Banner.trueColor(s"${(ThisBuild / name).value} v.${version.value}")) + else Seq.empty + }, + usefulTasksAndSettings ++= { + if (SharedTasks.isRoot.value) ZioSbtEcosystemPlugin.defaultTasksAndSettings.toSeq else Seq.empty + }, + onLoadMessage := { + if (SharedTasks.isRoot.value) welcomeMessage.init.value else "" + } + ) ++ MimaSettings.projectSettings override def buildSettings: Seq[Def.Setting[_]] = super.buildSettings ++ Seq( - scala3 := Versions.scala3, - scala212 := Versions.scala212, - scala213 := Versions.scala213, - scalaVersion := scala213.value, - crossScalaVersions := Seq(scala212.value, scala213.value, scala3.value), - zioVersion := Versions.zioVersion, - javaPlatform := "11" + zioVersion := zioVersion.?.value.getOrElse(Versions.zioVersion) ) override def globalSettings: Seq[Def.Setting[_]] = @@ -124,9 +93,10 @@ object ZioSbtEcosystemPlugin extends AutoPlugin { s"scm:git:git@github.com:zio/${normalizedName}.git" ) ), - pgpPassphrase := sys.env.get("PGP_PASSPHRASE").map(_.toArray), - pgpPublicRing := file("/tmp/public.asc"), - pgpSecretRing := file("/tmp/secret.asc"), - onChangedBuildSource := ReloadOnSourceChanges + pgpPassphrase := sys.env.get("PGP_PASSPHRASE").map(_.toArray), + pgpPublicRing := file("/tmp/public.asc"), + pgpSecretRing := file("/tmp/secret.asc"), + onChangedBuildSource := ReloadOnSourceChanges, + usefulTasksAndSettings / aggregate := false ) } diff --git a/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettings/build.sbt b/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettings/build.sbt index 9ca9f65f..f523b7f0 100644 --- a/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettings/build.sbt +++ b/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettings/build.sbt @@ -1,3 +1,5 @@ +ThisBuild / name := "zio-sbt-ecosystem-test" + lazy val root = (project in file(".")) .settings( version := "0.1", diff --git a/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/build.sbt b/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/build.sbt index 9ca9f65f..f523b7f0 100644 --- a/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/build.sbt +++ b/zio-sbt-ecosystem/src/sbt-test/zio-sbt-ecosystem/verifySettingsWithCI/build.sbt @@ -1,3 +1,5 @@ +ThisBuild / name := "zio-sbt-ecosystem-test" + lazy val root = (project in file(".")) .settings( version := "0.1", diff --git a/zio-sbt-project/build.sbt b/zio-sbt-project/build.sbt new file mode 100644 index 00000000..b2cadd2c --- /dev/null +++ b/zio-sbt-project/build.sbt @@ -0,0 +1,7 @@ +// Cross-Compiler Plugins +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.17.0") +addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2") +addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.5") +addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2") +addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.2") +addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.10.0") diff --git a/zio-sbt-project/src/main/scala/zio/sbt/BuildAssertions.scala b/zio-sbt-project/src/main/scala/zio/sbt/BuildAssertions.scala new file mode 100644 index 00000000..92f86048 --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/BuildAssertions.scala @@ -0,0 +1,344 @@ +/* + * Copyright 2022-2023 dev.zio + * + * 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 zio.sbt + +import sbt.Keys._ +import sbt._ +import sbtprojectmatrix.ProjectMatrixKeys.virtualAxes + +object BuildAssertions { + + trait Keys { + + lazy val isProjectMatrix: SettingKey[Boolean] = settingKey[Boolean]("Use sbt-projectmatrix") + lazy val isScalaJS: SettingKey[Boolean] = settingKey[Boolean]("Is Scala.js project") + lazy val isScalaJVM: SettingKey[Boolean] = settingKey[Boolean]("Is JVM project") + lazy val isScalaNative: SettingKey[Boolean] = settingKey[Boolean]("Is Scala Native project") + lazy val isScala2_12: SettingKey[Boolean] = settingKey[Boolean]("Is Scala 2.12 project") + lazy val isScala2_13: SettingKey[Boolean] = settingKey[Boolean]("Is Scala 2.13 project") + lazy val isScala3: SettingKey[Boolean] = settingKey[Boolean]("Is Scala 3 project") + + lazy val isCrossbuildProject: SettingKey[Boolean] = + settingKey[Boolean]("Check if the project is a crossbuild project (crossScalaVersions.size > 1)") + + lazy val hasCrossbuildProjects: SettingKey[Boolean] = + settingKey[Boolean]("Check if the project has crossbuild projects") + } + + object Keys extends Keys + + import Keys._ + + def isProjectMatrixSetting: Setting[Boolean] = isProjectMatrix := virtualAxes.?.value.isDefined + + def requireJS[T](setting: => T): Def.Initialize[T] = + Def.settingDyn { + if (isScalaJS.value) Def.setting(setting) + else sys.error("This setting requires Scala.js") + } + + def isScalaJSSetting: Setting[Boolean] = isScalaJS := { + thisProject.?.value.map(_.autoPlugins.exists(_.label == "org.scalajs.sbtplugin.ScalaJSPlugin")).getOrElse(false) + } + + def requireJVM[T](setting: => T): Def.Initialize[T] = + Def.settingDyn { + if (isScalaJVM.value) Def.setting(setting) + else sys.error("This setting requires JVM") + } + + def isScalaJVMSetting: Setting[Boolean] = isScalaJVM := { + !isScalaJS.value && !isScalaNative.value + } + + def requireNative[T](setting: => T): Def.Initialize[T] = + Def.settingDyn { + if (isScalaNative.value) Def.setting(setting) + else sys.error("This setting requires Scala Native") + } + + def isScalaNativeSetting: Setting[Boolean] = isScalaNative := { + thisProject.?.value + .map(_.autoPlugins.exists(_.label == "scala.scalanative.sbtplugin.ScalaNativePlugin")) + .getOrElse(false) + } + + def isScala2_12Setting: Setting[Boolean] = isScala2_12 := Def.setting { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((2, 12)) => true + case _ => false + } + }.value + + def isScala2_13Setting: Setting[Boolean] = isScala2_13 := Def.setting { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((2, 13)) => true + case _ => false + } + }.value + + def isScala3Setting: Setting[Boolean] = isScala3 := Def.setting { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((3, _)) => true + case _ => false + } + }.value + + def isCrossbuildProjectSetting: Def.Setting[Boolean] = isCrossbuildProject := crossScalaVersions.value.size > 1 + + private val allAggregates = + ScopeFilter(inAggregates(ThisProject)) + + def hasCrossbuildProjectsSetting: Def.Setting[Boolean] = hasCrossbuildProjects := { + val current = isCrossbuildProject.value + current || isCrossbuildProject + .all(allAggregates) + .value + .reduce(_ || _) + } + + def contains2_12: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScala2_12.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def contains2_13: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScala2_13.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def contains3: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScala3.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJS: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJS.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJS2_12: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJS.value && isScala2_12.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJS2_13: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJS.value && isScala2_13.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJS3: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJS.value && isScala3.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJVM: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJVM.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJVM2_12: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJVM.value && isScala2_12.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJVM2_13: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJVM.value && isScala2_13.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsJVM3: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaJVM.value && isScala3.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsNative: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaNative.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsNative2_12: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaNative.value && isScala2_12.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsNative2_13: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaNative.value && isScala2_13.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def containsNative3: Def.Initialize[Boolean] = Def.settingDyn { + Def.setting { + Def.setting { + isScalaNative.value && isScala3.value + }.all(allAggregates) + .value + .reduce(_ || _) + } + } + + def scalaCrossVersionsCountsOnePerProject: Def.Initialize[Boolean] = Def.settingDyn { + val result = crossScalaVersions.?.all( + ScopeFilter( + inAggregates(LocalRootProject), + inConfigurations(Compile) + ) + ).value + Def.setting(result.map(_.forall(_.size <= 1)).reduce(_ && _)) + } + + def settings: Seq[Setting[_]] = Seq( + isScalaJSSetting, + isScalaJVMSetting, + isScalaNativeSetting, + isScala2_12Setting, + isScala2_13Setting, + isScala3Setting, + isProjectMatrixSetting, + isCrossbuildProjectSetting, + hasCrossbuildProjectsSetting, + hasCrossbuildProjects / aggregate := false + ) + + val docs: Def.Initialize[Seq[String]] = Def.settingDyn { + val containsJS2_12 = BuildAssertions.containsJS2_12.value + val containsJS2_13 = BuildAssertions.containsJS2_13.value + val containsJS3 = BuildAssertions.containsJS3.value + val containsJVM2_12 = BuildAssertions.containsJVM2_12.value + val containsJVM2_13 = BuildAssertions.containsJVM2_13.value + val containsJVM3 = BuildAssertions.containsJVM3.value + val containsNative2_12 = BuildAssertions.containsNative2_12.value + val containsNative2_13 = BuildAssertions.containsNative2_13.value + val containsNative3 = BuildAssertions.containsNative3.value + val allScalaCrossVersions = ScalaVersions.allScalaCrossVersions.value.filter(_.size > 1).flatten + val scalaCrossVersionsCountsOnePerProject = BuildAssertions.scalaCrossVersionsCountsOnePerProject.value + + val activeScala212 = + ScalaVersions.allCurrentScalaVersions.value + .filter(_.startsWith("2.12")) + .toList + .sorted + .lastOption + .getOrElse("2.12.x ") + val activeScala213 = + ScalaVersions.allCurrentScalaVersions.value + .filter(_.startsWith("2.13")) + .toList + .sorted + .lastOption + .getOrElse("2.13.x ") + val activeScala3 = + ScalaVersions.allCurrentScalaVersions.value.filter(_.startsWith("3.")).toList.sorted.lastOption.getOrElse("3.x ") + + Def.setting( + Seq( + s""" + | \u001b[33m### This build contains crossbuild projects (multiple scala versions defined in crossScalaVersions)\u001b[0m + | \u001b[33mVersions found: ${ScalaVersions.Keys.allScalaVersions.value.toSeq.sorted.mkString(", ")}\u001b + | \u001b[33mNote: having only one scala version per project makes it easier to compile and test all without `++` switching, sbt-projectmatrix can help with that.\u001b + | \u001b[33mSee: \u001b[36mhttps://github.com/sbt/sbt-projectmatrix?tab=readme-ov-file#sbt-projectmatrix\u001b + | \u001b[33mthis is feature in sbt 2.x, see: \u001b[36mhttps://www.scala-sbt.org/2.x/docs/en/reference/cross-building-setup.html#project-matrix + """.stripMargin.stripLeading() + ).filter(_ => !scalaCrossVersionsCountsOnePerProject) ++ + Seq( + // format: off + s""" + | ### Detected current Platforms, JDKs and Scala Versions ${if (!scalaCrossVersionsCountsOnePerProject) "( other builds " + (allScalaCrossVersions - scalaVersion.value).mkString(", ") + " )" else ""} + | Current JDK ${if (JavaVersions.Keys.currentJDK.value < JavaVersions.Keys.javaPlatform.value) "\u001b[31m" else ""}${JavaVersions.Keys.currentJDK.value}\u001b[0m + | Target JDK ${JavaVersions.Keys.javaPlatform.value} + | Scala | ${activeScala212} | ${activeScala213} | ${activeScala3} | + |---------|---------|---------|--------| + | JS | ${if (containsJS2_12) "✅" else "❌"} | ${if (containsJS2_13) "✅" else "❌"} | ${if (containsJS3) "✅" else "❌"} | + | JVM | ${if (containsJVM2_12) "✅" else "❌"} | ${if (containsJVM2_13) "✅" else "❌"} | ${if (containsJVM3) "✅" else "❌"} | + | Native | ${if (containsNative2_12) "✅" else "❌"} | ${if (containsNative2_13) "✅" else "❌"} | ${if (containsNative3) "✅" else "❌"} | + """.stripMargin.stripLeading() + // format: on + ) + ) + } + +} diff --git a/zio-sbt-project/src/main/scala/zio/sbt/CompileTasks.scala b/zio-sbt-project/src/main/scala/zio/sbt/CompileTasks.scala new file mode 100644 index 00000000..798e749c --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/CompileTasks.scala @@ -0,0 +1,204 @@ +/* + * Copyright 2022-2023 dev.zio + * + * 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 zio.sbt + +import sbt.Keys._ +import sbt._ + +object CompileTasks { + + import BuildAssertions.Keys._ + + trait Keys { + + lazy val compile2_12: TaskKey[Unit] = taskKey[Unit]("Compile all Scala 2.12") + lazy val compile2_13: TaskKey[Unit] = taskKey[Unit]("Compile all Scala 2.13") + lazy val compile3: TaskKey[Unit] = taskKey[Unit]("Compile all Scala 3") + + lazy val compileJS: TaskKey[Unit] = taskKey[Unit]("Compile all JS projects") + lazy val compileJS2_12: TaskKey[Unit] = taskKey[Unit]("Compile all JS 2.12 projects") + lazy val compileJS2_13: TaskKey[Unit] = taskKey[Unit]("Compile all JS 2.13 projects") + lazy val compileJS3: TaskKey[Unit] = taskKey[Unit]("Compile all JS 3 projects") + + lazy val compileJVM: TaskKey[Unit] = taskKey[Unit]("Compile all the JVM projects") + lazy val compileJVM2_12: TaskKey[Unit] = taskKey[Unit]("Compile all JVM 2.12 projects") + lazy val compileJVM2_13: TaskKey[Unit] = taskKey[Unit]("Compile all JVM 2.13 projects") + lazy val compileJVM3: TaskKey[Unit] = taskKey[Unit]("Compile all JVM 3 projects") + + lazy val compileNative: TaskKey[Unit] = taskKey[Unit]("Compile all Native projects") + lazy val compileNative2_12: TaskKey[Unit] = taskKey[Unit]("Compile all Native 2.12 projects") + lazy val compileNative2_13: TaskKey[Unit] = taskKey[Unit]("Compile all Native 2.13 projects") + lazy val compileNative3: TaskKey[Unit] = taskKey[Unit]("Compile all Native 3 projects") + + } + + object Keys extends Keys + + import Keys._ + + def compileOn( + condition: Def.Initialize[Boolean], + label: Option[Def.Initialize[Task[String]]] = None + ): Def.Initialize[Task[Unit]] = + Def.taskIf { + if (condition.value) { + val name = label.getOrElse(Def.task("")).value + if (name.nonEmpty) sLog.value.info("Compiling " + name) + val _ = (Scope.ThisScope / compile).value + } else () + } + + def compile2_12Task: Def.Setting[Task[Unit]] = compile2_12 := compileOn( + isScala2_12, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def compile2_13Task: Def.Setting[Task[Unit]] = compile2_13 := compileOn( + isScala2_13, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def compile3Task: Def.Setting[Task[Unit]] = compile3 := compileOn( + isScala3, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def compileJSTask: Def.Setting[Task[Unit]] = + compileJS := compileOn(isScalaJS, Some(thisProject.?.map(_.map(_.id).getOrElse("")))).value + def compileJS2_12Task: Def.Setting[Task[Unit]] = compileJS2_12 := compileOn( + isScalaJS.zip(isScala2_12) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def compileJS2_13Task: Def.Setting[Task[Unit]] = compileJS2_13 := compileOn( + isScalaJS.zip(isScala2_13) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def compileJS3Task: Def.Setting[Task[Unit]] = compileJS3 := compileOn( + isScalaJS.zip(isScala3) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def compileJVMTask: Def.Setting[Task[Unit]] = + compileJVM := compileOn(isScalaJVM, Some(thisProject.?.map(_.map(_.id).getOrElse("")))).value + def compileJVM2_12Task: Def.Setting[Task[Unit]] = compileJVM2_12 := compileOn( + isScalaJVM.zip(isScala2_12) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def compileJVM2_13Task: Def.Setting[Task[Unit]] = compileJVM2_13 := compileOn( + isScalaJVM.zip(isScala2_13) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def compileJVM3Task: Def.Setting[Task[Unit]] = compileJVM3 := compileOn( + isScalaJVM.zip(isScala3) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def compileNativeTask: Def.Setting[Task[Unit]] = + compileNative := compileOn(isScalaNative, Some(thisProject.?.map(_.map(_.id).getOrElse("")))).value + def compileNative2_12Task: Def.Setting[Task[Unit]] = compileNative2_12 := compileOn( + isScalaNative.zip(isScala2_12) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def compileNative2_13Task: Def.Setting[Task[Unit]] = compileNative2_13 := compileOn( + isScalaNative.zip(isScala2_13) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def compileNative3Task: Def.Setting[Task[Unit]] = compileNative3 := compileOn( + isScalaNative.zip(isScala3) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def settings: Seq[Setting[Task[Unit]]] = Seq( + compile2_12Task, + compile2_13Task, + compile3Task, + compileJSTask, + compileJS2_12Task, + compileJS2_13Task, + compileJS3Task, + compileJVMTask, + compileJVM2_12Task, + compileJVM2_13Task, + compileJVM3Task, + compileNativeTask, + compileNative2_12Task, + compileNative2_13Task, + compileNative3Task + ) + + val docs: Def.Initialize[Seq[(String, String)]] = Def.settingDyn { + val contains2_12 = BuildAssertions.contains2_12.value + val contains2_13 = BuildAssertions.contains2_13.value + val contains3 = BuildAssertions.contains3.value + val containsJS = BuildAssertions.containsJS.value + val containsJS2_12 = BuildAssertions.containsJS2_12.value + val containsJS2_13 = BuildAssertions.containsJS2_13.value + val containsJS3 = BuildAssertions.containsJS3.value + val containsJVM = BuildAssertions.containsJVM.value + val containsJVM2_12 = BuildAssertions.containsJVM2_12.value + val containsJVM2_13 = BuildAssertions.containsJVM2_13.value + val containsJVM3 = BuildAssertions.containsJVM3.value + val containsNative = BuildAssertions.containsNative.value + val containsNative2_12 = BuildAssertions.containsNative2_12.value + val containsNative2_13 = BuildAssertions.containsNative2_13.value + val containsNative3 = BuildAssertions.containsNative3.value + + Def.setting( + Seq( + Seq("compile" -> "Compile all projects"), + if (contains2_12) Seq(compile2_12Task.key.key.label -> compile2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (contains2_13) Seq(compile2_13Task.key.key.label -> compile2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (contains3) Seq(compile3Task.key.key.label -> compile3Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJS) Seq(compileJSTask.key.key.label -> compileJSTask.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJS2_12) Seq(compileJS2_12Task.key.key.label -> compileJS2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJS2_13) Seq(compileJS2_13Task.key.key.label -> compileJS2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJS3) Seq(compileJS3Task.key.key.label -> compileJS3Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM) + Seq(compileJVMTask.key.key.label -> compileJVMTask.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM2_12) + Seq(compileJVM2_12Task.key.key.label -> compileJVM2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM2_13) + Seq(compileJVM2_13Task.key.key.label -> compileJVM2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM3) Seq(compileJVM3Task.key.key.label -> compileJVM3Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative) + Seq(compileNativeTask.key.key.label -> compileNativeTask.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative2_12) + Seq(compileNative2_12Task.key.key.label -> compileNative2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative2_13) + Seq(compileNative2_13Task.key.key.label -> compileNative2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative3) + Seq(compileNative3Task.key.key.label -> compileNative3Task.key.key.description.getOrElse("")) + else Seq.empty + ).flatten + ) + } + +} diff --git a/zio-sbt-project/src/main/scala/zio/sbt/JavaVersions.scala b/zio-sbt-project/src/main/scala/zio/sbt/JavaVersions.scala new file mode 100644 index 00000000..e06fea38 --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/JavaVersions.scala @@ -0,0 +1,43 @@ +package zio.sbt + +import sbt.Keys._ +import sbt._ + +object JavaVersion { + + val `11` = "11" + val `17` = "17" + val `21` = "21" +} + +object JavaVersions { + + trait Keys { + + lazy val javaPlatform: SettingKey[String] = settingKey[String]("java target platform, default is 11") + lazy val currentJDK: SettingKey[String] = settingKey[String]("current JDK version") + + } + + object Keys extends Keys + + import Keys._ + + def buildSettings: Seq[Setting[_]] = Seq( + javaPlatform := { + val targetJVM = javaPlatform.?.value.getOrElse(JavaVersion.`11`) + val jdkNeedsUpgrade = currentJDK.value.toInt < targetJVM.toInt + if (jdkNeedsUpgrade) + sLog.value.warn( + s"\u001b[33mJDK upgrade is required, target ($targetJVM) is higher than the current JDK version (${currentJDK.value}), compilation will fail!\u001b" + ) + targetJVM + }, + scalacOptions ++= Seq(s"-release:${javaPlatform.value}"), + javacOptions ++= Seq("-source", javaPlatform.value, "-target", javaPlatform.value) + ) + + def globalSettings: Seq[Setting[_]] = Seq( + currentJDK := sys.props("java.specification.version") + ) +} diff --git a/zio-sbt-project/src/main/scala/zio/sbt/ScalaPlatform.scala b/zio-sbt-project/src/main/scala/zio/sbt/ScalaPlatform.scala new file mode 100644 index 00000000..8ccc9e2c --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/ScalaPlatform.scala @@ -0,0 +1,53 @@ +package zio.sbt + +import sbt._ + +sealed trait ScalaPlatform { + def asString: String = this match { + case ScalaPlatform.JS => "JS" + case ScalaPlatform.JVM => "JVM" + case ScalaPlatform.Native => "Native" + } +} +object ScalaPlatform { + case object JS extends ScalaPlatform + case object JVM extends ScalaPlatform + case object Native extends ScalaPlatform +} + +object ScalaPlatforms { + + trait Keys { + + lazy val allScalaPlatforms: SettingKey[Set[ScalaPlatform]] = + settingKey[Set[ScalaPlatform]]("All Scala platforms used in the whole project") + + } + + object Keys extends Keys + + import Keys._ + + def allScalaPlatformsSetting: Def.Setting[Set[ScalaPlatform]] = allScalaPlatforms := { + Def.setting { + Set( + BuildAssertions.Keys.isScalaJS.value -> ScalaPlatform.JS, + BuildAssertions.Keys.isScalaJVM.value -> ScalaPlatform.JVM, + BuildAssertions.Keys.isScalaNative.value -> ScalaPlatform.Native + ).collect { case (true, p) => + p + } + }.all( + ScopeFilter( + inAggregates(LocalRootProject), + inConfigurations(Compile) + ) + ).value + .flatten + .toSet + } + + def globalSettings: Seq[Setting[_]] = Seq( + allScalaPlatformsSetting + ) +} diff --git a/zio-sbt-project/src/main/scala/zio/sbt/ScalaVersions.scala b/zio-sbt-project/src/main/scala/zio/sbt/ScalaVersions.scala new file mode 100644 index 00000000..608cdbbd --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/ScalaVersions.scala @@ -0,0 +1,79 @@ +/* + * Copyright 2022-2023 dev.zio + * + * 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 zio.sbt + +import sbt.Keys._ +import sbt._ + +object ScalaVersion { + val Scala3 = "3.3.4" + val Scala212 = "2.12.20" + val Scala213 = "2.13.15" +} + +object ScalaVersions { + + trait Keys { + + lazy val scala212: SettingKey[String] = settingKey[String]("Scala 2.12 version") + lazy val scala213: SettingKey[String] = settingKey[String]("Scala 2.13 version") + lazy val scala3: SettingKey[String] = settingKey[String]("Scala 3 version") + + lazy val defaultCrossScalaVersions: SettingKey[Seq[String]] = + settingKey[Seq[String]]("Optional default cross scala versions") + lazy val allScalaVersions: SettingKey[Set[String]] = + settingKey[Set[String]]("All scala versions used in the whole project") + + } + + object Keys extends Keys + + import Keys._ + + def allCurrentScalaVersions: Def.Initialize[Set[String]] = Def.setting { + scalaVersion.?.all( + ScopeFilter( + inAggregates(LocalRootProject), + inConfigurations(Compile) + ) + ).value.flatten.toSet + } + + def allScalaVersionsSetting: Def.Setting[Set[String]] = allScalaVersions := { + allScalaCrossVersions.value.flatten ++ allCurrentScalaVersions.value + } + + def allScalaCrossVersions: Def.Initialize[Set[Set[String]]] = Def.setting { + crossScalaVersions.?.all( + ScopeFilter( + inAggregates(LocalRootProject), + inConfigurations(Compile) + ) + ).value.map(_.map(_.toSet)).flatten.toSet + } + + def buildSettings: Seq[Setting[_]] = Seq( + scala212 := ScalaVersion.Scala212, + scala213 := ScalaVersion.Scala213, + scala3 := ScalaVersion.Scala3 + ) + + def globalSettings: Seq[Setting[_]] = Seq( + allScalaVersionsSetting + ) + +} diff --git a/zio-sbt-project/src/main/scala/zio/sbt/TestTasks.scala b/zio-sbt-project/src/main/scala/zio/sbt/TestTasks.scala new file mode 100644 index 00000000..44a739b7 --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/TestTasks.scala @@ -0,0 +1,210 @@ +/* + * Copyright 2022-2023 dev.zio + * + * 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 zio.sbt + +import sbt.Keys._ +import sbt._ + +object TestTasks { + + import BuildAssertions.Keys._ + + trait Keys { + + lazy val test2_12: TaskKey[Unit] = taskKey[Unit]("Test all Scala 2.12") + lazy val test2_13: TaskKey[Unit] = taskKey[Unit]("Test all Scala 2.13") + lazy val test3: TaskKey[Unit] = taskKey[Unit]("Test all Scala 3") + + lazy val testJS: TaskKey[Unit] = taskKey[Unit]("Test all JS") + lazy val testJS2_12: TaskKey[Unit] = taskKey[Unit]("Test all JS 2.12") + lazy val testJS2_13: TaskKey[Unit] = taskKey[Unit]("Test all JS 2.13") + lazy val testJS3: TaskKey[Unit] = taskKey[Unit]("Test all JS 3") + + lazy val testJVM: TaskKey[Unit] = taskKey[Unit]("Test all the JVM") + lazy val testJVM2_12: TaskKey[Unit] = taskKey[Unit]("Test all JVM 2.12") + lazy val testJVM2_13: TaskKey[Unit] = taskKey[Unit]("Test all JVM 2.13") + lazy val testJVM3: TaskKey[Unit] = taskKey[Unit]("Test all JVM 3") + + lazy val testNative: TaskKey[Unit] = taskKey[Unit]("Test all Native") + lazy val testNative2_12: TaskKey[Unit] = taskKey[Unit]("Test all Native 2.12") + lazy val testNative2_13: TaskKey[Unit] = taskKey[Unit]("Test all Native 2.13") + lazy val testNative3: TaskKey[Unit] = taskKey[Unit]("Test all Native 3") + + } + + object Keys extends Keys + + def scalaVersionToSuffix(scalaVersion: String): String = + CrossVersion.partialVersion(scalaVersion) match { + case Some((3, _)) => "3" + case Some((2, 13)) => "2_13" + case Some((2, 12)) => "2_12" + case _ => throw new Exception(s"Unsupported Scala version: $scalaVersion") + } + + import Keys._ + + def testOn( + condition: Def.Initialize[Boolean], + label: Option[Def.Initialize[Task[String]]] = None + ): Def.Initialize[Task[Unit]] = + Def.taskIf { + if (condition.value) { + val name = label.getOrElse(Def.task("")).value + + if (name.nonEmpty) sLog.value.info("Running tests for " + name) + + test.in(Scope.ThisScope).value + } else () + } + + def test2_12Task: Def.Setting[Task[Unit]] = test2_12 := testOn( + isScala2_12, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def test2_13Task: Def.Setting[Task[Unit]] = test2_13 := testOn( + isScala2_13, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def test3Task: Def.Setting[Task[Unit]] = test3 := testOn( + isScala3, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def testJSTask: Def.Setting[Task[Unit]] = + testJS := testOn(isScalaJS, Some(thisProject.?.map(_.map(_.id).getOrElse("")))).value + def testJS2_12Task: Def.Setting[Task[Unit]] = testJS2_12 := testOn( + isScalaJS.zip(isScala2_12) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def testJS2_13Task: Def.Setting[Task[Unit]] = testJS2_13 := testOn( + isScalaJS.zip(isScala2_13) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def testJS3Task: Def.Setting[Task[Unit]] = testJS3 := testOn( + isScalaJS.zip(isScala3) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def testJVMTask: Def.Setting[Task[Unit]] = + testJVM := testOn(isScalaJVM, Some(thisProject.?.map(_.map(_.id).getOrElse("")))).value + def testJVM2_12Task: Def.Setting[Task[Unit]] = testJVM2_12 := testOn( + isScalaJVM.zip(isScala2_12) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def testJVM2_13Task: Def.Setting[Task[Unit]] = testJVM2_13 := testOn( + isScalaJVM.zip(isScala2_13) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def testJVM3Task: Def.Setting[Task[Unit]] = testJVM3 := testOn( + isScalaJVM.zip(isScala3) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def testNativeTask: Def.Setting[Task[Unit]] = + testNative := testOn(isScalaNative, Some(thisProject.?.map(_.map(_.id).getOrElse("")))).value + def testNative2_12Task: Def.Setting[Task[Unit]] = testNative2_12 := testOn( + isScalaNative.zip(isScala2_12) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def testNative2_13Task: Def.Setting[Task[Unit]] = testNative2_13 := testOn( + isScalaNative.zip(isScala2_13) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + def testNative3Task: Def.Setting[Task[Unit]] = testNative3 := testOn( + isScalaNative.zip(isScala3) { case (a, b) => a && b }, + Some(thisProject.?.map(_.map(_.id).getOrElse(""))) + ).value + + def settings: Seq[Setting[Task[Unit]]] = Seq( + test2_12Task, + test2_13Task, + test3Task, + testJSTask, + testJS2_12Task, + testJS2_13Task, + testJS3Task, + testJVMTask, + testJVM2_12Task, + testJVM2_13Task, + testJVM3Task, + testNativeTask, + testNative2_12Task, + testNative2_13Task, + testNative3Task + ) + + val docs: Def.Initialize[Seq[(String, String)]] = Def.settingDyn { + + val contains2_12 = BuildAssertions.contains2_12.value + val contains2_13 = BuildAssertions.contains2_13.value + val contains3 = BuildAssertions.contains3.value + val containsJS = BuildAssertions.containsJS.value + val containsJS2_12 = BuildAssertions.containsJS2_12.value + val containsJS2_13 = BuildAssertions.containsJS2_13.value + val containsJS3 = BuildAssertions.containsJS3.value + val containsJVM = BuildAssertions.containsJVM.value + val containsJVM2_12 = BuildAssertions.containsJVM2_12.value + val containsJVM2_13 = BuildAssertions.containsJVM2_13.value + val containsJVM3 = BuildAssertions.containsJVM3.value + val containsNative = BuildAssertions.containsNative.value + val containsNative2_12 = BuildAssertions.containsNative2_12.value + val containsNative2_13 = BuildAssertions.containsNative2_13.value + val containsNative3 = BuildAssertions.containsNative3.value + + Def.setting( + Seq("test" -> "Test all projects") ++ + Seq( + if (contains2_12) Seq(test2_12Task.key.key.label -> test2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (contains2_13) Seq(test2_13Task.key.key.label -> test2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (contains3) Seq(test3Task.key.key.label -> test3Task.key.key.description.getOrElse("")) else Seq.empty, + if (containsJS) Seq(testJSTask.key.key.label -> testJSTask.key.key.description.getOrElse("")) else Seq.empty, + if (containsJS2_12) Seq(testJS2_12Task.key.key.label -> testJS2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJS2_13) Seq(testJS2_13Task.key.key.label -> testJS2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJS3) Seq(testJS3Task.key.key.label -> testJS3Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM) Seq(testJVMTask.key.key.label -> testJVMTask.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM2_12) Seq(testJVM2_12Task.key.key.label -> testJVM2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM2_13) Seq(testJVM2_13Task.key.key.label -> testJVM2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsJVM3) Seq(testJVM3Task.key.key.label -> testJVM3Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative) Seq(testNativeTask.key.key.label -> testNativeTask.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative2_12) + Seq(testNative2_12Task.key.key.label -> testNative2_12Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative2_13) + Seq(testNative2_13Task.key.key.label -> testNative2_13Task.key.key.description.getOrElse("")) + else Seq.empty, + if (containsNative3) Seq(testNative3Task.key.key.label -> testNative3Task.key.key.description.getOrElse("")) + else Seq.empty + ).flatten + ++ Seq( + """testOnly *.YourSpec -- -t \"YourLabel\"""" -> "Only runs tests with matching term e.g." + ) + ) + } +} diff --git a/zio-sbt-project/src/main/scala/zio/sbt/ZioSbtCrossbuildPlugin.scala b/zio-sbt-project/src/main/scala/zio/sbt/ZioSbtCrossbuildPlugin.scala new file mode 100644 index 00000000..9d1935ca --- /dev/null +++ b/zio-sbt-project/src/main/scala/zio/sbt/ZioSbtCrossbuildPlugin.scala @@ -0,0 +1,68 @@ +/* + * Copyright 2022-2023 dev.zio + * + * 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 zio.sbt + +import sbt.{JavaVersion => _, _} + +import zio.sbt.ZioSbtShared.autoImport.{banners, usefulTasksAndSettings} + +object ZioSbtCrossbuildPlugin extends AutoPlugin { + + override def trigger = allRequirements + + override def requires: Plugins = super.requires && ZioSbtShared + + object autoImport + extends BuildAssertions.Keys + with CompileTasks.Keys + with TestTasks.Keys + with ScalaVersions.Keys + with ScalaPlatforms.Keys + with JavaVersions.Keys { + + def zioSbtCrossbuildSettings: Seq[Setting[_]] = + BuildAssertions.settings ++ + inConfig(Compile)(CompileTasks.settings) ++ + inConfig(Test)(CompileTasks.settings) ++ + inConfig(Test)(TestTasks.settings) + } + + import autoImport.* + + override def projectSettings: Seq[Setting[_]] = zioSbtCrossbuildSettings ++ Seq( + banners ++= { + if (SharedTasks.isRoot.value) BuildAssertions.docs.value + else Seq.empty + }, + usefulTasksAndSettings ++= { + if (SharedTasks.isRoot.value) { + CompileTasks.docs.value ++ TestTasks.docs.value + } else Seq.empty + } + ) + + override def buildSettings: Seq[Def.Setting[_]] = + super.buildSettings ++ + ScalaVersions.buildSettings ++ + JavaVersions.buildSettings + + override def globalSettings: Seq[Def.Setting[_]] = + super.globalSettings ++ + ScalaVersions.globalSettings ++ + ScalaPlatforms.globalSettings ++ + JavaVersions.globalSettings +} diff --git a/zio-sbt-shared/src/main/scala/zio/sbt/SharedTasks.scala b/zio-sbt-shared/src/main/scala/zio/sbt/SharedTasks.scala new file mode 100644 index 00000000..d8bf3e51 --- /dev/null +++ b/zio-sbt-shared/src/main/scala/zio/sbt/SharedTasks.scala @@ -0,0 +1,12 @@ +package zio.sbt + +import sbt.Keys._ +import sbt._ + +object SharedTasks { + + val isRoot: Def.Initialize[Boolean] = Def.settingDyn { + val r = thisProjectRef.?.value.fold(true)(_.project == loadedBuild.value.units(loadedBuild.value.root).root) + Def.setting(r) + } +} diff --git a/zio-sbt-shared/src/main/scala/zio/sbt/SharedTasksAndSettings.scala b/zio-sbt-shared/src/main/scala/zio/sbt/SharedTasksAndSettings.scala new file mode 100644 index 00000000..1d142175 --- /dev/null +++ b/zio-sbt-shared/src/main/scala/zio/sbt/SharedTasksAndSettings.scala @@ -0,0 +1,100 @@ +package zio.sbt + +import sbt.Keys._ +import sbt._ + +object ZioSbtShared extends AutoPlugin { + + override def trigger = allRequirements + + override def requires: Plugins = super.requires + + object autoImport { + + val welcomeBannerEnabled: SettingKey[Boolean] = + settingKey[Boolean]("Indicates whether or not to enable the welcome banner.") + + val banners: SettingKey[Seq[String]] = settingKey[Seq[String]]( + "A list of banners that will be displayed as part of the welcome message." + ) + + val welcomeTaskAndSettingsEnabled: SettingKey[Boolean] = + settingKey[Boolean]("Indicates whether or not to enable the welcome task and settings.") + + val usefulTasksAndSettings: SettingKey[Seq[(String, String)]] = settingKey[Seq[(String, String)]]( + "A map of useful tasks and settings that will be displayed as part of the welcome banner." + ) + + val welcomeMessage: Setting[String] = ZioSbtShared.welcomeMessageSetting + } + + import autoImport.* + + private val allAggregates = + ScopeFilter(inAggregates(ThisProject)) + + val allBanners: Def.Initialize[Seq[String]] = Def.settingDyn { + Def.setting( + banners + .all(allAggregates) + .value + .foldLeft(Seq.empty[String])(_ ++ _) + ) + } + + val allUsefulTasksAndSettings: Def.Initialize[Seq[(String, String)]] = + Def.settingDyn { + Def.setting( + usefulTasksAndSettings + .all(allAggregates) + .value + .foldLeft(Seq.empty[(String, String)])(_ ++ _) + ) + } + + def welcomeMessageSetting: Setting[String] = + onLoadMessage := { + if (welcomeBannerEnabled.value) { + import scala.Console + + val allBanners = ZioSbtShared.allBanners.value + val allUsefulTasksAndSettings = ZioSbtShared.allUsefulTasksAndSettings.value + val maxLen = (allUsefulTasksAndSettings.map(_._1.length) ++ Seq(1)).max + + def normalizedPadding(s: String) = " " * (maxLen - s.length) + + def item(text: String): String = s"${Console.GREEN}> ${Console.CYAN}$text${Console.RESET}" + + val renderedBanners = allBanners.mkString("\n") + val renderedTasksAndSettings = + if (welcomeTaskAndSettingsEnabled.value) { + s"""|Useful sbt tasks: + |${allUsefulTasksAndSettings.map { case (task, description) => + s"${item(task)} ${normalizedPadding(task)}${description}" + } + .mkString("\n")} + """.stripMargin + } else "" + + s"""| + |$renderedBanners + |$renderedTasksAndSettings + |""".stripMargin + } else "" + } + + override def projectSettings: Seq[Setting[_]] = Seq( + banners := Seq.empty, + usefulTasksAndSettings := Seq.empty + ) + + override def globalSettings: Seq[Def.Setting[_]] = super.globalSettings ++ + Seq( + welcomeBannerEnabled := true, + welcomeTaskAndSettingsEnabled := true, + banners := Seq.empty, + usefulTasksAndSettings := Seq.empty, + Global / excludeLintKeys += usefulTasksAndSettings + ) + +} diff --git a/zio-sbt-website/src/sbt-test/zio-sbt-website/compileDocs/build.sbt b/zio-sbt-website/src/sbt-test/zio-sbt-website/compileDocs/build.sbt index b7765627..52ed94a2 100644 --- a/zio-sbt-website/src/sbt-test/zio-sbt-website/compileDocs/build.sbt +++ b/zio-sbt-website/src/sbt-test/zio-sbt-website/compileDocs/build.sbt @@ -3,6 +3,8 @@ lazy val root = (project in file(".")) version := "0.1", projectName := "ZIO SBT", mainModuleName := "test-project", - projectStage := ProjectStage.ProductionReady + projectStage := ProjectStage.ProductionReady, + docsVersion := version.value, + publishToNpm := {} ) .enablePlugins(WebsitePlugin) diff --git a/zio-sbt-website/src/sbt-test/zio-sbt-website/installWebsite/build.sbt b/zio-sbt-website/src/sbt-test/zio-sbt-website/installWebsite/build.sbt index b7765627..52ed94a2 100644 --- a/zio-sbt-website/src/sbt-test/zio-sbt-website/installWebsite/build.sbt +++ b/zio-sbt-website/src/sbt-test/zio-sbt-website/installWebsite/build.sbt @@ -3,6 +3,8 @@ lazy val root = (project in file(".")) version := "0.1", projectName := "ZIO SBT", mainModuleName := "test-project", - projectStage := ProjectStage.ProductionReady + projectStage := ProjectStage.ProductionReady, + docsVersion := version.value, + publishToNpm := {} ) .enablePlugins(WebsitePlugin)