From d312d8b2f6a867ca8a3e4a103795b981a5217aa4 Mon Sep 17 00:00:00 2001 From: Ali Rashid Date: Wed, 1 Mar 2023 22:40:01 +0300 Subject: [PATCH] Initial implementation --- .github/dependabot.yml | 8 + .github/workflows/build.yml | 137 ++++++++ .gitignore | 10 + .scalafmt.conf | 25 ++ LICENSE | 202 ++++++++++++ README.md | 74 +++++ build.sbt | 62 ++++ modules/documentation/README.md | 74 +++++ .../scala/africa/shuwari/sbt/plugin.scala | 232 ++++++++++++++ .../main/scala/africa/shuwari/sbt/keys.scala | 167 ++++++++++ .../scala/africa/shuwari/sbt/plugin.scala | 32 ++ .../africa/shuwari/sbt/vite/defaults.scala | 301 ++++++++++++++++++ .../africa/shuwari/sbt/vite/settings.scala | 66 ++++ .../scala/africa/shuwari/sbt/vite/util.scala | 58 ++++ project/build.properties | 1 + project/plugins.sbt | 4 + 16 files changed, 1453 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 .scalafmt.conf create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.sbt create mode 100644 modules/documentation/README.md create mode 100644 modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala create mode 100644 modules/sbt-vite/src/main/scala/africa/shuwari/sbt/keys.scala create mode 100644 modules/sbt-vite/src/main/scala/africa/shuwari/sbt/plugin.scala create mode 100644 modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/defaults.scala create mode 100644 modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/settings.scala create mode 100644 modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/util.scala create mode 100644 project/build.properties create mode 100644 project/plugins.sbt diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f1b2422 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + open-pull-requests-limit: 10 + rebase-strategy: auto + schedule: + interval: daily \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..6a1da13 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,137 @@ +name: "CI Build" + +on: + workflow_dispatch: + pull_request: + branches: [main] + push: + branches: [main] + tags: [v*] + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + test: + name: Execute Tests (Java ${{ matrix.java }}) + strategy: + fail-fast: false + matrix: + java: [8, 17] + runs-on: ubuntu-22.04 + + steps: + - name: Checkout Source Code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Java + uses: actions/setup-java@v3 + with: + distribution: "temurin" + java-version: ${{ matrix.java }} + + - name: Execute Unit Tests + run: sbt test + + - name: Package Project Products + run: sbt package + + publish: + name: Publish Release + needs: [test] + if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-22.04 + + steps: + - name: Checkout Source Code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Java + uses: actions/setup-java@v3 + with: + distribution: "temurin" + java-version: 11 + + - name: Import Signing Key + uses: crazy-max/ghaction-import-gpg@v5 + with: + gpg_private_key: ${{ secrets.OSS_PUBLISH_USER_SIGNING_KEY }} + git_user_signingkey: true + git_commit_gpgsign: true + + - name: Update Signing Key Trust Level + run: echo -e "trust\n5\ny" | gpg --batch --no-tty --command-fd 0 --edit-key ${{ secrets.OSS_PUBLISH_USER_SIGNING_KEY_ID }} + + - name: Publish Projects + run: sbt publishSigned sonatypeBundleRelease + env: + PUBLISH_USER: ${{ secrets.OSS_PUBLISH_USER }} + PUBLISH_USER_PASSPHRASE: ${{ secrets.OSS_PUBLISH_USER_PASSPHRASE }} + SIGNING_KEY_ID: ${{ secrets.OSS_PUBLISH_USER_SIGNING_KEY_ID }} + + + publish-documentation: + name: Publish Documentation + needs: [publish] + if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-22.04 + steps: + - name: Checkout Source Code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Setup Java + uses: actions/setup-java@v3 + with: + distribution: "temurin" + java-version: 17 + + - name: Import Signing Key + uses: crazy-max/ghaction-import-gpg@v5 + with: + gpg_private_key: ${{ secrets.OSS_PUBLISH_USER_SIGNING_KEY }} + git_user_signingkey: true + git_commit_gpgsign: true + git_committer_name: Shuwari Africa Team + + - name: Update Signing Key Trust Level + run: echo -e "trust\n5\ny" | gpg --batch --no-tty --command-fd 0 --edit-key ${{ secrets.OSS_PUBLISH_USER_SIGNING_KEY_ID }} + + - name: Generate Documentation + run: sbt sbt-js-documentation/mdoc + + - name: Commit Documentation Changes + run: | + git add ./README.md && \ + git commit --gpg-sign --message "Update documentation for ${{ github.ref_name }}" + + - name: Push Documentation Changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.OSS_TEAM_TOKEN }} + branch: main + + + synchronise-repositories: + name: Synchronise Repositories + if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) + runs-on: ubuntu-22.04 + steps: + - name: Checkout Source Code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Synchronise Azure DevOps + uses: yesolutions/mirror-action@v0.6.0 + with: + REMOTE: "git@ssh.dev.azure.com:v3/shuwari/sbt-js/sbt-js" + GIT_SSH_PRIVATE_KEY: ${{ secrets.DEVOPS_SSH_PRIVATE_KEY }} + GIT_SSH_NO_VERIFY_HOST: "true" + PUSH_ALL_REFS: false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6c4648 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.bloop/ +.bsp/ +.mdoc/ +.metals/ +.sbt-shuwari/ +.vscode/ +metals.sbt +target/ + +.core-collection/ \ No newline at end of file diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..227840b --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,25 @@ +version = "3.7.2" +runner.dialect = scala213 + +maxColumn = 120 + +danglingParentheses.preset = true + +rewrite.rules = [AvoidInfix, Imports, PreferCurlyFors, RedundantBraces, RedundantParens, SortModifiers] +rewrite.imports.expand = false +rewrite.imports.sort = scalastyle +rewrite.imports.contiguousGroups = no + +indent.defnSite = 2 + +danglingParentheses.defnSite = false +danglingParentheses.callSite = false + +align.openParenCallSite = true +align.openParenDefnSite = true + +assumeStandardLibraryStripMargin = true +align.stripMargin = true + +docstrings.style = SpaceAsterisk +docstrings.oneline = keep \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..91e58cc --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# sbt-js + +Collection of [sbt](https://scala-sbt.org) plugins for uniform configuration of Shuwari Africa Ltd. sbt projects, as well +as CI and Release related functionality. + +_NB: Unless specified otherwise, all plugins listed below are sbt `AutoPlugins`, and will be enabled automatically upon enabling the required plugin dependencies for each._ + +## Core Plugins + +The following core plugins are available: + +__________________________________ + +### sbt-js-core + +```scala +addSbtPlugin("africa.shuwari.sbt" % "sbt-js" % "0.1.0-SNAPSHOT") +``` + +Preconfigures projects with opinionated project defaults for ScalaJS libraries and/or applications. Provides a foundation for incremental assembly using external Javascript ecosystem +tools. + +Introduces additional sbt `SettingKeys` and `TaskKeys`, specifically relevant: + +| Key | Description | Default | +|--------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------| +| `jsPrepare` | Task: Compiles, links, and prepares project for packaging and/or processing with external tools." | [Refer to implementation of `jsPrepare`](modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala) | +| `jsFullLink` | Setting: Defines whether _"fullLink"_ or _"fastLink\"_ ScalaJS Linker output is used. | `true` where `NODE_ENV` environment variable is defined with a value of `peoduction`. and `false` otherwise. | +| `js` | Task: Process and/or package assembled project with external tools. | Unimplemented. To be customised by end-user. | +| `js / sourceDirectory` | Setting: Default directory containing sources and resources to be copied as-is to `jsPrepare / target` during `jsPrepare` execution. | `(Compile / sourceDirectory) / js` | +| `js / sourceDirectories` | Setting: List of all directories containing sources and resources to be copied as-is to `jsPrepare / target` during `jsPrepare` execution. | `(Compile / sourceDirectory) / js` | +| `js / target` | Setting: Defines a target directory for the `js` task. Usable if required. | `(Compile / crossTarget) / (js.key.label.toLowerCase + "-" + normalizedName + "-" + (if (jsFullLink.value) "full" else "fast") + "-linked"` | +| `jsPrepare / target` | Setting: Defines a default target directory for the `jsPrepare` task. | `(Compile / crossTarget) / (jsPrepare.key.label.toLowerCase + "-" + normalizedName + "-" + (if (jsFullLink.value) "full" else "fast") + "-linked"` | +| `jsPrepare / fileInputIncludeFilter` | Setting: An sbt `sbt.nio.file.PathFilter` inclusion filter to apply to the input sources and resources copied to the prepared assembly by `jsPrepare`. | `RecursiveGlob` | +| `jsPrepare / fileInputExcludeFilter` | Setting: An sbt `sbt.nio.file.PathFilter` exclusion filter to apply to the input sources and resources copied to the prepared assembly by `jsPrepare`. | `HiddenFileFilter || DirectoryFilter` | +__________________________________ + +### sbt-vite + +```scala +addSbtPlugin("africa.shuwari.sbt" % "sbt-vite" % "0.1.0-SNAPSHOT") +``` + +|Depends On: | +|-----------------------------------| +|[sbt-js](#sbt-js-core) | + +Preconfigures projects with opinionated project defaults for ScalaJS libraries and/or applications. Uses [Vite](https://vitejs.dev/) for bundling, and postprocessing. + +Introduces additional sbt `SettingKeys` and `TaskKeys`, specifically: + +| Key | Description | Default | +|------------|---------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| +| `vite` | Task: Compiles, links, and prepares project for packaging and/or processing with external tools." | [Refer to implementation of `jsPrepare`](modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala) | +| `viteBuild`| Executes `vite build` using the options specified in _sbt-vite_ plugin settings. | _N/A_ | +| `viteStop` | Shuts down any running instances of Vite's development server. | _N/A_ | + +__________________________________ + +## License + +Copyright © Shuwari Africa Ltd. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this work except in compliance with the License. +You may obtain a copy of the License at: + + [`http://www.apache.org/licenses/LICENSE-2.0`](https://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. diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..397b065 --- /dev/null +++ b/build.sbt @@ -0,0 +1,62 @@ +/**************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd licenses this file to you under the terms * + * of 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: * + * * + * https://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. * + ****************************************************************/ + +name := "sbt-js-root" + +organization := "africa.shuwari.sbt" +shuwariProject +apacheLicensed +startYear := Some(2023) + +def commonSettings = List(publishMavenStyle := true) + +lazy val `sbt-js` = + project + .in(file("modules/sbt-js")) + .enablePlugins(SbtPlugin) + .settings(addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.12.0")) + +lazy val `sbt-vite` = + project + .in(file("modules/sbt-vite")) + .enablePlugins(SbtPlugin) + .dependsOn(`sbt-js`) + .settings( + Keys.run := { + (Compile / scalacOptions).value.foreach(println) + } + ) + +lazy val `sbt-js-documentation` = + project + .in(file(".sbt-js-doc")) + .dependsOn(`sbt-js`) + .enablePlugins(MdocPlugin) + .settings( + mdocIn := (LocalRootProject / baseDirectory).value / "modules" / "documentation", + mdocOut := (LocalRootProject / baseDirectory).value, + mdocVariables := Map( + "VERSION" -> version.value + ) + ) + +lazy val `sbt-js-root` = project + .in(file(".")) + .enablePlugins(SbtPlugin) + .aggregate(`sbt-js`, `sbt-vite`) + .notPublished diff --git a/modules/documentation/README.md b/modules/documentation/README.md new file mode 100644 index 0000000..b12c63b --- /dev/null +++ b/modules/documentation/README.md @@ -0,0 +1,74 @@ +# sbt-js + +Collection of [sbt](https://scala-sbt.org) plugins for uniform configuration of Shuwari Africa Ltd. sbt projects, as well +as CI and Release related functionality. + +_NB: Unless specified otherwise, all plugins listed below are sbt `AutoPlugins`, and will be enabled automatically upon enabling the required plugin dependencies for each._ + +## Core Plugins + +The following core plugins are available: + +__________________________________ + +### sbt-js-core + +```scala +addSbtPlugin("africa.shuwari.sbt" % "sbt-js" % "@VERSION@") +``` + +Preconfigures projects with opinionated project defaults for ScalaJS libraries and/or applications. Provides a foundation for incremental assembly using external Javascript ecosystem +tools. + +Introduces additional sbt `SettingKeys` and `TaskKeys`, specifically relevant: + +| Key | Description | Default | +|--------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------| +| `jsPrepare` | Task: Compiles, links, and prepares project for packaging and/or processing with external tools." | [Refer to implementation of `jsPrepare`](modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala) | +| `jsFullLink` | Setting: Defines whether _"fullLink"_ or _"fastLink\"_ ScalaJS Linker output is used. | `true` where `NODE_ENV` environment variable is defined with a value of `peoduction`. and `false` otherwise. | +| `js` | Task: Process and/or package assembled project with external tools. | Unimplemented. To be customised by end-user. | +| `js / sourceDirectory` | Setting: Default directory containing sources and resources to be copied as-is to `jsPrepare / target` during `jsPrepare` execution. | `(Compile / sourceDirectory) / js` | +| `js / sourceDirectories` | Setting: List of all directories containing sources and resources to be copied as-is to `jsPrepare / target` during `jsPrepare` execution. | `(Compile / sourceDirectory) / js` | +| `js / target` | Setting: Defines a target directory for the `js` task. Usable if required. | `(Compile / crossTarget) / (js.key.label.toLowerCase + "-" + normalizedName + "-" + (if (jsFullLink.value) "full" else "fast") + "-linked"` | +| `jsPrepare / target` | Setting: Defines a default target directory for the `jsPrepare` task. | `(Compile / crossTarget) / (jsPrepare.key.label.toLowerCase + "-" + normalizedName + "-" + (if (jsFullLink.value) "full" else "fast") + "-linked"` | +| `jsPrepare / fileInputIncludeFilter` | Setting: An sbt `sbt.nio.file.PathFilter` inclusion filter to apply to the input sources and resources copied to the prepared assembly by `jsPrepare`. | `RecursiveGlob` | +| `jsPrepare / fileInputExcludeFilter` | Setting: An sbt `sbt.nio.file.PathFilter` exclusion filter to apply to the input sources and resources copied to the prepared assembly by `jsPrepare`. | `HiddenFileFilter || DirectoryFilter` | +__________________________________ + +### sbt-vite + +```scala +addSbtPlugin("africa.shuwari.sbt" % "sbt-vite" % "@VERSION@") +``` + +|Depends On: | +|-----------------------------------| +|[sbt-js](#sbt-js-core) | + +Preconfigures projects with opinionated project defaults for ScalaJS libraries and/or applications. Uses [Vite](https://vitejs.dev/) for bundling, and postprocessing. + +Introduces additional sbt `SettingKeys` and `TaskKeys`, specifically: + +| Key | Description | Default | +|------------|---------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| +| `vite` | Task: Compiles, links, and prepares project for packaging and/or processing with external tools." | [Refer to implementation of `jsPrepare`](modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala) | +| `viteBuild`| Executes `vite build` using the options specified in _sbt-vite_ plugin settings. | _N/A_ | +| `viteStop` | Shuts down any running instances of Vite's development server. | _N/A_ | + +__________________________________ + +## License + +Copyright © Shuwari Africa Ltd. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this work except in compliance with the License. +You may obtain a copy of the License at: + + [`http://www.apache.org/licenses/LICENSE-2.0`](https://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. diff --git a/modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala b/modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala new file mode 100644 index 0000000..4d0718b --- /dev/null +++ b/modules/sbt-js/src/main/scala/africa/shuwari/sbt/plugin.scala @@ -0,0 +1,232 @@ +/***************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd. licenses this file to you under the terms * + * of 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: * + * * + * https://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 africa.shuwari.sbt + +import org.scalajs.sbtplugin.ScalaJSPlugin +import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._ +import sbt.Keys._ +import sbt._ +import sbt.Path._ +import sbt.nio.Keys._ + +import java.nio.file.Files +import java.nio.file.Path + +object JSBundlerPlugin extends AutoPlugin { + + override def requires: Plugins = ScalaJSPlugin + + override def trigger: PluginTrigger = allRequirements + + object autoImport { + + val jsPrepare = + taskKey[File]( + "Compile, link, and prepare project for packaging and/or processing with external tools." + ) + + val jsFullLink = + settingKey[Boolean]( + "Defines whether \"fullLink\" or \"fastLink\" ScalaJS Linker output is used." + ) + + val js = taskKey[File]( + "Process and/or package assembled project with external tools." + ) + + /** Alias for "[[js]] / [[sbt.Keys.target]]" */ + def jsTarget = js / target + + /** Alias for "[[jsPrepare]] / [[sbt.Keys.target]]" */ + def jsPrepareTarget = jsPrepare / target + + /** Alias for "[[jsPrepare]] / [[sbt.nio.Keys.fileInputIncludeFilter]]" */ + def jsPrepareIncludeFilter = jsPrepare / fileInputIncludeFilter + + /** Alias for "[[jsPrepare]] / [[sbt.nio.Keys.fileInputExcludeFilter]]" */ + def jsPrepareExcludeFilter = jsPrepare / fileInputExcludeFilter + + /** Alias for "[[js]] / [[sbt.Keys.sourceDirectory]]" */ + def jsSourceDirectory = js / sourceDirectory + + /** Alias for "[[js]] / [[sbt.Keys.sourceDirectories]]" */ + def jsSourceDirectories = js / sourceDirectories + + /** Alias for "[[jsPrepare]] / [[sbt.nio.Keys.fileInputs]]" */ + def jsPrepareFileInputs = jsPrepare / fileInputs + + } + + import autoImport._ + + override def projectSettings: Seq[Setting[_]] = List( + jsFullLink := sys.env + .get("NODE_ENV") + .map(_.toLowerCase.trim == "production") + .getOrElse(false), + jsSourceDirectory := (Compile / sourceDirectory).value / "js", // TODO: Allow for Test configuration + jsSourceDirectories := List(jsSourceDirectory.value), + jsTarget := (Compile / crossTarget).value / s"${js.key.label.toLowerCase}-${normalizedName.value}-assembly${pathSuffix.value}", + jsPrepareTarget := (Compile / crossTarget).value / s"${jsPrepare.key.label.toLowerCase}-${normalizedName.value}-target${pathSuffix.value}", + jsPrepareFileInputs := Glob( + (ThisProject / baseDirectory).value, + "package*.json" + ) +: jsSourceDirectories.value.map(allDescendants), + jsPrepareIncludeFilter := (jsPrepareIncludeFilter ?? PathFilter( + RecursiveGlob + )).value, + jsPrepareExcludeFilter := (jsPrepareExcludeFilter ?? (HiddenFileFilter || DirectoryFilter).toNio).value, + jsPrepare := Def.taskDyn { + val full = jsFullLink.value + Def.task { + val log = streams.value.log + def logf(file: File) = + "\"" + file + .relativeTo((LocalRootProject / baseDirectory).value) + .getOrElse("External File") + "\"" + def logfMaps(files: Iterable[(File, File)]) = + files + .map(p => s"${logf(p._1)} -> ${logf(p._2)}") + .mkString("\n\t\t", "\n\t\t", "\n") + + val target = jsPrepareTarget.value + log.debug( + s"Using ${jsPrepare.key.label} target directory: ${logf(target)}" + ) + if (!target.exists) { + Files.createDirectories(target.toPath) + log.debug( + s"Created ${jsPrepare.key.label} target directory: ${logf(target)}" + ) + } + + val jsInputFiles = jsPrepare.inputFiles.toSet + val jsInputFileDirectories = jsSourceDirectories.value.toSet + log.debug( + s"Discovered Javascript project files in ${jsInputFileDirectories + .mkString(", ")}:" + jsInputFiles + .map(p => logf(p.toFile)) + .mkString("\n\t\t", "\n\t\t", "\n") + ) + + val jsInputFileMappings = + jsInputFiles + .map(f => f.toAbsolutePath.normalize.toFile) + .pair(rebase(jsInputFileDirectories, target) | flat(target)) + .toSet + + val scalaJsOutputDir = (if (full) Compile / fullLinkJSOutput + else Compile / fastLinkJSOutput).value + + val scalaJsOutput = + fileTreeView.value.list(scalaJsOutputDir.toGlob / **) + def scalaJsOutputPaths = scalaJsOutput.map(_._1) + log.debug( + "Discovered ScalaJS Linker output files:" + scalaJsOutputPaths + .map(p => logf(p.toFile)) + .mkString("\n\t\t", "\n\t\t", "\n") + ) + + val scalaJsOutputMappings = scalaJsOutputPaths + .map(_.toAbsolutePath.normalize.toFile) + .pair(rebase(scalaJsOutputDir, target)) + .toSet + + val allMappings = jsInputFileMappings ++ scalaJsOutputMappings + + val existing = { + // import scala.jdk.CollectionConverters._ + + def nodeModules(path: Path) = + path.toAbsolutePath.normalize.toFile.getAbsolutePath + .contains("node_modules") + + fileTreeView.value + .list(allDescendants(target)) + .flatMap { case (path, _) => + if ( + !allMappings.exists( + _._2.toPath.toAbsolutePath.normalize == path.toAbsolutePath.normalize + ) && !nodeModules(path) + ) { + IO.delete(path.toFile) + log.info( + "Deleted obsolete Javascript project file:\n\t\t" + logf( + path.toFile + ) + ) + None + } else Some(path) + } + .toSet + } + + val jsSourceFileChanges = jsPrepare.inputFileChanges + + val jsUpdated = jsInputFileMappings.filter(p => + (jsSourceFileChanges.created ++ jsSourceFileChanges.modified) + .contains(p._1.toPath)) + if (jsUpdated.nonEmpty) + log.debug( + "Updated and/or new Javascript input files:" + logfMaps(jsUpdated) + ) + + val scalaJsUpdated = scalaJsOutputMappings.collect { + case (in, out) + if existing.contains(out.toPath) && FileInfo + .lastModified(existing.find(_ == out.toPath).get.toFile) + .lastModified != FileInfo.lastModified(in).lastModified => + log.info( + FileInfo.lastModified(in) + "\n" + FileInfo + .lastModified(out) + .lastModified + ) + ((in, out)) + } + if (scalaJsUpdated.nonEmpty) + log.debug( + "Replacing updated ScalaJS output files:" + logfMaps(scalaJsUpdated) + ) + + val mappings = jsUpdated ++ scalaJsUpdated ++ allMappings.filterNot(p => existing.contains(p._2.toPath)) + if (mappings.nonEmpty) + log.info( + "Creating and/or updating Javascript project with mappings:" + logfMaps( + mappings + ) + ) + + IO.copy( + mappings, + CopyOptions( + overwrite = true, + preserveLastModified = true, + preserveExecutable = true + ) + ) + target + } + }.value + ) + + private def allDescendants(base: File) = Glob(base, **) + + private def pathSuffix = + Def.setting(s"-${if (jsFullLink.value) "full" else "fast"}-linked") + +} diff --git a/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/keys.scala b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/keys.scala new file mode 100644 index 0000000..e30b00d --- /dev/null +++ b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/keys.scala @@ -0,0 +1,167 @@ +/***************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd. licenses this file to you under the terms * + * of 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: * + * * + * https://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 africa.shuwari.sbt + +import sbt._ +import java.io.File +import java.util.concurrent.atomic.AtomicReference + +object ViteKeys { + @inline private def doc(str: String) = + str + " See https://vitejs.dev/guide/cli.html." + + // Vite Common Keys + + val base = SettingKey[Option[String]]( + "viteBase", + doc("Option corresponding to Vite's \"--base\" setting.") + ) + + val config = SettingKey[Option[String]]( + "viteConfig", + doc("Option corresponding to Vite's \"--config\" setting.") + ) + + // val debug = SettingKey[Option[Boolean]]( + // "viteDebug", + // doc("Option corresponding to Vite's \"--debug\" setting.") + // ) + + val force = SettingKey[Option[Boolean]]( + "viteForce", + doc("Option corresponding to Vite's \"--force\" setting.") + ) + + val logLevel = SettingKey[Level.Value]( + "viteLogLevel", + doc("Option corresponding to Vite's \"--logLevel\" setting.") + ) + + val mode = SettingKey[Mode]( + "viteMode", + doc("Option corresponding to Vite's \"--mode\" setting.") + ) + + // Vite Development Server Keys + + val host = SettingKey[Option[String]]( + "viteHost", + doc("Option corresponding to Vite's \"--host\" setting.") + ) + + val port = SettingKey[Option[Int]]( + "vitePort", + doc("Option corresponding to Vite's \"--port\" setting.") + ) + + val strictPort = SettingKey[Option[Boolean]]( + "viteStrictPort", + doc("Option corresponding to Vite's \"--strictPort\" setting.") + ) + + val cors = SettingKey[Option[Boolean]]( + "viteCors", + doc("Option corresponding to Vite's \"--cors\" setting.") + ) + + // Vite Build Keys + val assetsDir = SettingKey[Option[String]]( + "viteAssetsDir", + doc("Option corresponding to Vite's \"--assetsDir\" setting.") + ) + + val assetsInlineLimit = SettingKey[Option[Int]]( + "viteAssetsInlineLimit", + doc("Option corresponding to Vite's \"--assetsInlineLimit\" setting.") + ) + + val ssr = SettingKey[Option[String]]( + "viteSsr", + doc("Option corresponding to Vite's \"--ssr\" setting.") + ) + + val sourcemap = SettingKey[Option[Boolean]]( + "viteSourcemap", + doc("Option corresponding to Vite's \"--sourcemap\" setting.") + ) + + val minify = SettingKey[Option[Minifier]]( + "viteMinify", + doc("Option corresponding to Vite's \"--minify\" setting.") + ) + + // Plugin Keys + + private[sbt] val viteProcessInstances = + settingKey[AtomicReference[List[scala.sys.process.Process]]]( + "Currently running Vite process instances." + ) + + val useNpx = SettingKey[Boolean]( + "viteUseNpx", + "Defines whether \"npx\" should be used to execute Vite." + ) + + val viteVersion = settingKey[Option[String]]( + "Defines the version of Vite executed if \"useNpx\" is specified as \"true\". Uses latest version of Vite if not specified." + ) + + val viteExecutable = taskKey[List[String]]( + "If specified, defines the command used to execute Vite. Will cause \"useNpx\" and \"viteVersion\" to be ignored." + ) + + val vite = taskKey[Unit](doc("Executes Vite's development server")) + val viteBuild = taskKey[File](doc("Executes Vite's \"build\" command.")) + val viteStop = taskKey[Unit]("Shuts down any running instances of Vite's development server.") + // val vitePreview = taskKey[File](doc("Executes Vite's \"preview\" command.")) + + sealed trait ConfigurationItemResolver[A] { + def resolve(param: A): String + } + + sealed trait Mode extends Product with Serializable + + object Mode extends ConfigurationItemResolver[Mode] { + case object Development extends Mode + case object Production extends Mode + + def resolve(param: Mode) = param match { + case Development => "development" + case Production => "production" + } + } + + sealed trait Minifier extends Product with Serializable + + object Minifier extends ConfigurationItemResolver[Minifier] { + + case object ESBuild extends Minifier + case object Terser extends Minifier + case object Default extends Minifier + case object Disabled extends Minifier + + def resolve(param: Minifier): String = param match { + case ESBuild => "esbuild" + case Terser => "terser" + case Default => "true" + case Disabled => "false" + } + + } + +} diff --git a/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/plugin.scala b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/plugin.scala new file mode 100644 index 0000000..4ce33e6 --- /dev/null +++ b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/plugin.scala @@ -0,0 +1,32 @@ +/***************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd. licenses this file to you under the terms * + * of 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: * + * * + * https://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 africa.shuwari.sbt + +import sbt._ + +object VitePlugin extends AutoPlugin { + object autoImport { + def vite = ViteKeys + } + override def requires: Plugins = JSBundlerPlugin + + override def trigger = allRequirements + + override def projectSettings: Seq[Setting[_]] = vite.DefaultSettings() + +} diff --git a/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/defaults.scala b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/defaults.scala new file mode 100644 index 0000000..cec7ff5 --- /dev/null +++ b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/defaults.scala @@ -0,0 +1,301 @@ +/***************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd. licenses this file to you under the terms * + * of 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: * + * * + * https://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 africa.shuwari.sbt +package vite + +import sbt.Keys._ +import sbt.{Util => _, _} +import java.io.File.pathSeparator + +import JSBundlerPlugin.{autoImport => js} +import scala.sys.process._ +import java.util.concurrent.atomic.AtomicReference + +object DefaultSettings { + + def apply() = plugin ++ common ++ run ++ build + + def pluginGlobal = List( + onLoad := ((s: State) => + s.addExitHook( + shutdownProcesses( + (ThisProject / ViteKeys.viteProcessInstances).value + .getAndSet(List.empty), + s.log + ) + )) + ) + + def plugin = List( + ViteKeys.viteExecutable := viteCommand( + ViteKeys.useNpx.value, + ViteKeys.viteVersion.value.getOrElse("latest") + ), + ViteKeys.useNpx := false, + ViteKeys.viteVersion := None, + ViteKeys.viteBuild := Def + .taskDyn { + val processTarget = (ViteKeys.viteBuild / target).value + val logger = streams.value.log + val parameters = CliParameter.buildParameters.value + Def.task { + val process = viteBuildProcess(parameters).value + logger.info(s"Started Vite Build process: ${process}") + process.exitValue + logger.info(s"Completed Vite Build process: ${process}") + processTarget + } + } + .dependsOn(JSBundlerPlugin.autoImport.jsPrepare) + .value, + ViteKeys.viteProcessInstances := new AtomicReference(List.empty), + // ViteKeys.viteBuild := viteBuildProcess(CliParameter.devServer.value), + ViteKeys.vite := Def.taskDyn { + val reference = ViteKeys.viteProcessInstances.value + val logger = streams.value.log + val parameters = + CliParameter.devServerParameters.value + Def.task { + val process = viteDevProcess(parameters).value + shutdownProcesses(reference.getAndSet(List.empty), logger) + logger.info(s"Started Vite process: ${process}") + reference.set(List(process)) + } + }.value, + ViteKeys.viteStop := shutdownProcesses( + ViteKeys.viteProcessInstances.value.getAndSet(List.empty), + streams.value.log + ) + ) + + def common = List( + ViteKeys.base := None, + ViteKeys.config := None, + ViteKeys.force := None, + ViteKeys.logLevel := Level.Info, + ViteKeys.mode := (if (js.jsFullLink.value) + ViteKeys.Mode.Production + else ViteKeys.Mode.Development) + ) + + def run = List( + ViteKeys.host := None, + ViteKeys.port := None, + ViteKeys.strictPort := None, + ViteKeys.cors := None + ) + + def build = List( + ViteKeys.viteBuild / target := js.jsTarget.value, + ViteKeys.assetsDir := None, + ViteKeys.assetsInlineLimit := None, + ViteKeys.ssr := None, + ViteKeys.sourcemap := None, + ViteKeys.minify := None + ) + + private def viteDevProcess(parameters: List[String]) = + viteProcess("", parameters) + + private def viteBuildProcess(parameters: List[String]) = + viteProcess("build", parameters) + + private def viteProcess(command: String, parameters: List[String]) = + Def.taskDyn { + val executable = ViteKeys.viteExecutable.value + val source = js.jsPrepareTarget.value + val logger = streams.value.log + val env = defaultEnv( + sys.env, + ViteKeys.mode.value, + ViteKeys.useNpx.value, + source, + (ThisProject / baseDirectory).value, + (LocalRootProject / baseDirectory).value + ) + Def.task( + process( + executable, + command, + parameters, + env, + source, + logger + ) + ) + } + + private def shutdownProcesses(processes: List[Process], logger: Logger) = + processes + .foreach { p => + logger.info(s"Stopping Vite process: $p") + p.destroy() + p.exitValue + logger.info(s"Stopped Vite process: $p") + } + + private def viteCommand(useNpx: Boolean, viteVersion: String): List[String] = + if (useNpx) List("npx", "--yes", s"vite@$viteVersion") else List("vite") + + private def defaultEnv( + env: Map[String, String], + mode: ViteKeys.Mode, + useNpx: Boolean, + preparedAppDirectory: File, + projectDirectory: File, + rootDirectory: File + ): Map[String, String] = { + def nodeEnv = if (mode == ViteKeys.Mode.Production) + env + ("NODE_ENV" -> "production") + else env + if (useNpx) nodeEnv + else { + def path = env.get("PATH").get + def binDir(base: File) = base / "node_modules" / ".bin" + def binPaths = List(preparedAppDirectory, projectDirectory, rootDirectory) + .map(binDir) + .mkString("", pathSeparator, pathSeparator) + nodeEnv + ("PATH" -> (binPaths + path)) + } + } + + private def process( + executable: List[String], + command: String, + parameters: List[String], + env: Map[String, String], + workingDirectory: File, + logger: Logger + ): Process = { + def processLogger(logger: Logger) = + ProcessLogger(fout => logger.info(fout), ferr => logger.error(ferr)) + + val commandList = + if (System.getProperty("os.name").toLowerCase.contains("win")) + List( + "powershell.exe", + "-Command" + ) ++ executable ++ (command +: parameters) + else List("sh", "-c") ++ (executable ++ (command +: parameters)) + + logger.info(env.mkString("\n")) + + logger.info(commandList.mkString("\n\n")) + + Process(commandList, workingDirectory, env.toList: _*) + .run(processLogger(logger)) + } + + sealed private trait ParameterDecoder[A] extends (A => String) + + private object ParameterDecoder { + + def apply[A](f: (A => String)) = new ParameterDecoder[A] { + def apply(v: A): String = f(v) + } + + implicit def boolean: ParameterDecoder[Boolean] = + ParameterDecoder(v => if (v == true) "true" else "false") + + implicit def string: ParameterDecoder[String] = + ParameterDecoder((str: String) => str) + + implicit def number: ParameterDecoder[Int] = ParameterDecoder(_.toString) + + implicit def logLevel: ParameterDecoder[Level.Value] = ParameterDecoder( + Util.viteLogLevel + ) + + implicit def minifier: ParameterDecoder[ViteKeys.Minifier] = + ParameterDecoder( + ViteKeys.Minifier.resolve + ) + + implicit def mode: ParameterDecoder[ViteKeys.Mode] = ParameterDecoder( + ViteKeys.Mode.resolve + ) + + implicit def file: ParameterDecoder[File] = ParameterDecoder( + _.getAbsolutePath + ) + + } + + private case class CliParameter[A: ParameterDecoder]( + key: String, + value: Option[A] + ) { + def resolve = + value + .map(v => s"$key ${implicitly[ParameterDecoder[A]].apply(v)}") + .getOrElse("") + } + + private object CliParameter { + + def apply[A: ParameterDecoder](pair: (String, Option[A])): CliParameter[A] = + CliParameter(pair._1, pair._2)(implicitly[ParameterDecoder[A]]) + + private def resolve(col: List[CliParameter[_]]) = + col.map(_.resolve).filter(_.nonEmpty) + + def commonCliParameters = Def.taskDyn { + val base = CliParameter("base" -> ViteKeys.base.value) + val config = CliParameter("config" -> ViteKeys.config.value) + val force = CliParameter("force" -> ViteKeys.force.value) + val logLevel = CliParameter("logLevel", Some(ViteKeys.logLevel.value)) + val mode = CliParameter("mode", Some(ViteKeys.mode.value)) + val clearScreen = CliParameter("clearScreen", Some(false)) + Def.task(List(base, config, force, logLevel, mode, clearScreen)) + } + + def devServerParameters = + Def.taskDyn { + val common = commonCliParameters.value + val host = CliParameter("host" -> ViteKeys.host.value) + val port = CliParameter("port" -> ViteKeys.port.value) + val strictPort = CliParameter("strictPort" -> ViteKeys.strictPort.value) + val cors = CliParameter("cors", ViteKeys.cors.value) + Def.task(resolve(common ++ List(host, port, strictPort, cors))) + } + + def buildParameters = + Def.taskDyn { + val common = commonCliParameters.value + + val assetsDir = CliParameter("assetsDir" -> ViteKeys.assetsDir.value) + val assetsInlineLimit = CliParameter( + "assetsInlineLimit" -> ViteKeys.assetsInlineLimit.value + ) + val ssr = CliParameter("ssr" -> ViteKeys.ssr.value) + val sourcemap = CliParameter("sourcemap" -> ViteKeys.sourcemap.value) + val target = CliParameter( + "target" -> Some((JSBundlerPlugin.autoImport.js / sbt.Keys.target).value) + ) + + Def.task( + resolve( + common ++ List(assetsDir, assetsInlineLimit, ssr, sourcemap, target) + ) + ) + + } + + } + +} diff --git a/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/settings.scala b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/settings.scala new file mode 100644 index 0000000..dcaa94d --- /dev/null +++ b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/settings.scala @@ -0,0 +1,66 @@ +/***************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd. licenses this file to you under the terms * + * of 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: * + * * + * https://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 africa.shuwari.sbt.vite + +import java.io.File +import sbt.util.Level +import africa.shuwari.sbt.ViteKeys + +sealed trait ViteConfiguration { + + /** Public base path */ + def base: Option[String] + + /** Use specified config file */ + def config: Option[File] + + /** Force the optimizer to ignore the cache and re-bundle. */ + def force: Option[Boolean] + + /** Use specified config file */ + def logLevel: Level.Value + + /** Use specified config file */ + def mode: ViteKeys.Mode + +} + +final case class BuildConfiguration( + base: Option[String], + config: Option[File], + force: Option[Boolean], + logLevel: Level.Value, + mode: ViteKeys.Mode, + target: Option[String], + assetsDir: Option[String], + assetsInlineLimit: Option[Int], + ssr: Option[String], + sourcemap: Option[Boolean], + minify: Option[ViteKeys.Minifier], + manifest: Option[String], + ssrManifest: Option[String], + emptyOutDir: Option[Boolean] +) extends ViteConfiguration + +final case class RunConfiguration( + base: Option[String], + config: Option[File], + force: Option[Boolean], + logLevel: Level.Value, + mode: ViteKeys.Mode +) extends ViteConfiguration diff --git a/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/util.scala b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/util.scala new file mode 100644 index 0000000..fee3fdf --- /dev/null +++ b/modules/sbt-vite/src/main/scala/africa/shuwari/sbt/vite/util.scala @@ -0,0 +1,58 @@ +/***************************************************************** + * Copyright © Shuwari Africa Ltd. All rights reserved. * + * * + * Shuwari Africa Ltd. licenses this file to you under the terms * + * of 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: * + * * + * https://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 africa.shuwari.sbt +package vite + +import sbt.util.Logger +import scala.sys.process._ +import java.io.File +import sbt.util.Level + +object Util { + + // final val windows = System.getProperty("os.name").toLowerCase.contains("win") + + def classNameToString(cls: Class[_]) = cls.getSimpleName + .filterNot(_ == '$') + .toLowerCase + + def viteLogLevel(level: Level.Value) = + level match { + case Level.Info => "info" + case Level.Warn => "warn" + case Level.Error => "error" + case _ => "info" + } + + def process( + commands: List[String], + env: Map[String, String], + workingDirectory: File, + logger: Logger + ): Process = { + val cmd = + if (System.getProperty("os.name").toLowerCase.contains("win")) + List("powershell.exe", "-Command") ++ commands + else commands + + scala.sys.process + .Process(cmd, workingDirectory, env.toList: _*) + .run(ProcessLogger(fout => logger.info(fout), ferr => logger.error(ferr))) + } + +} diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..46e43a9 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.8.2 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..89a7f52 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,4 @@ +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.7") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") +addSbtPlugin("africa.shuwari.sbt" % "sbt-shuwari" % "0.9.2") +