From 831f773e3a66d542ecd20bdff0baaa82483115ea Mon Sep 17 00:00:00 2001 From: Pat Garrity Date: Thu, 4 Jan 2024 22:58:03 -0600 Subject: [PATCH] (patch) Initial Blob Implementation --- .forgejo/workflows/pull_request.yaml | 66 ++++++++++++++++++++++ .forgejo/workflows/release.yaml | 83 ++++++++++++++++++++++++++++ .gitignore | 5 ++ .pre-commit-config.yaml | 17 ++++++ .scalafmt.conf | 72 ++++++++++++++++++++++++ README.md | 29 ++++++++++ build.sbt | 35 ++++++++++++ mit | 9 +++ project/build.properties | 1 + project/plugins.sbt | 28 ++++++++++ src/main/scala/gs/blob/v0/Blob.scala | 68 +++++++++++++++++++++++ 11 files changed, 413 insertions(+) create mode 100644 .forgejo/workflows/pull_request.yaml create mode 100644 .forgejo/workflows/release.yaml create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 .scalafmt.conf create mode 100644 README.md create mode 100644 build.sbt create mode 100644 mit create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 src/main/scala/gs/blob/v0/Blob.scala diff --git a/.forgejo/workflows/pull_request.yaml b/.forgejo/workflows/pull_request.yaml new file mode 100644 index 0000000..bba274a --- /dev/null +++ b/.forgejo/workflows/pull_request.yaml @@ -0,0 +1,66 @@ +on: + pull_request: + types: [opened, synchronize, reopened] + +defaults: + run: + shell: bash + +jobs: + library_snapshot: + runs-on: docker + container: + image: registry.garrity.co:8443/gs/ci-scala:latest + name: 'Build and Test Library Snapshot' + env: + GS_MAVEN_USER: ${{ vars.GS_MAVEN_USER }} + GS_MAVEN_TOKEN: ${{ secrets.GS_MAVEN_TOKEN }} + steps: + - uses: actions/checkout@v4 + name: 'Checkout Repository' + with: + fetch-depth: 0 + - name: 'Pre-Commit' + run: | + pre-commit install + pre-commit run --all-files + - name: 'Prepare Versioned Build' + run: | + latest_git_tag="$(git describe --tags --abbrev=0 || echo 'No Tags')" + latest_commit_message="$(git show -s --format=%s HEAD)" + if [[ "$latest_commit_message" == *"(major)"* ]]; then + export GS_RELEASE_TYPE="major" + elif [[ "$latest_commit_message" == *"(minor)"* ]]; then + export GS_RELEASE_TYPE="minor" + elif [[ "$latest_commit_message" == *"(patch)"* ]]; then + export GS_RELEASE_TYPE="patch" + elif [[ "$latest_commit_message" == *"(docs)"* ]]; then + export GS_RELEASE_TYPE="norelease" + elif [[ "$latest_commit_message" == *"(norelease)"* ]]; then + export GS_RELEASE_TYPE="norelease" + else + export GS_RELEASE_TYPE="norelease" + fi + echo "GS_RELEASE_TYPE=$GS_RELEASE_TYPE" >> $GITHUB_ENV + echo "Previous Git Tag: $latest_git_tag" + echo "Latest Commit: $latest_commit_message ($GS_RELEASE_TYPE) (SNAPSHOT)" + if [ "$GS_RELEASE_TYPE" = "norelease" ]; then + sbtn -Dsnapshot=true -Drelease="patch" semVerInfo + else + sbtn -Dsnapshot=true -Drelease="$GS_RELEASE_TYPE" semVerInfo + fi + - name: 'Unit Tests and Code Coverage' + run: | + sbtn clean + sbtn coverage + sbtn test + sbtn coverageReport + - name: 'Publish Snapshot' + run: | + echo "Testing env var propagation = ${{ env.GS_RELEASE_TYPE }}" + if [ "${{ env.GS_RELEASE_TYPE }}" = "norelease" ]; then + echo "Skipping publish due to GS_RELEASE_TYPE=norelease" + else + sbtn clean + sbtn publish + fi diff --git a/.forgejo/workflows/release.yaml b/.forgejo/workflows/release.yaml new file mode 100644 index 0000000..8351605 --- /dev/null +++ b/.forgejo/workflows/release.yaml @@ -0,0 +1,83 @@ +on: + push: + branches: + - main + +defaults: + run: + shell: bash + +jobs: + library_release: + runs-on: docker + container: + image: registry.garrity.co:8443/gs/ci-scala:latest + name: 'Build and Release Library' + env: + GS_MAVEN_USER: ${{ vars.GS_MAVEN_USER }} + GS_MAVEN_TOKEN: ${{ secrets.GS_MAVEN_TOKEN }} + steps: + - uses: actions/checkout@v4 + name: 'Checkout Repository' + with: + fetch-depth: 0 + - name: 'Pre-Commit' + run: | + pre-commit install + pre-commit run --all-files + - name: 'Prepare Versioned Build' + run: | + latest_git_tag="$(git describe --tags --abbrev=0 || echo 'No Tags')" + latest_commit_message="$(git show -s --format=%s HEAD)" + if [[ "$latest_commit_message" == *"(major)"* ]]; then + export GS_RELEASE_TYPE="major" + elif [[ "$latest_commit_message" == *"(minor)"* ]]; then + export GS_RELEASE_TYPE="minor" + elif [[ "$latest_commit_message" == *"(patch)"* ]]; then + export GS_RELEASE_TYPE="patch" + elif [[ "$latest_commit_message" == *"(docs)"* ]]; then + export GS_RELEASE_TYPE="norelease" + elif [[ "$latest_commit_message" == *"(norelease)"* ]]; then + export GS_RELEASE_TYPE="norelease" + else + export GS_RELEASE_TYPE="norelease" + fi + + echo "GS_RELEASE_TYPE=$GS_RELEASE_TYPE" >> $GITHUB_ENV + echo "Previous Git Tag: $latest_git_tag" + echo "Latest Commit: $latest_commit_message" + echo "Selected Release Type: '$GS_RELEASE_TYPE'" + + if [ "$GS_RELEASE_TYPE" = "norelease" ]; then + echo "Skipping all versioning for 'norelease' commit." + else + sbtn -Drelease="$GS_RELEASE_TYPE" semVerInfo + fi + - name: 'Unit Tests and Code Coverage' + run: | + if [ "${{ env.GS_RELEASE_TYPE }}" = "norelease" ]; then + echo "Skipping build/test for 'norelease' commit." + else + sbtn clean + sbtn coverage + sbtn test + sbtn coverageReport + fi + - name: 'Publish Release' + run: | + if [ "${{ env.GS_RELEASE_TYPE }}" = "norelease" ]; then + echo "Skipping publish for 'norelease' commit." + else + sbtn clean + sbtn semVerWriteVersionToFile + sbtn publish + fi + - name: 'Create Git Tag' + run: | + if [ "${{ env.GS_RELEASE_TYPE }}" = "norelease" ]; then + echo "Skipping Git tag for 'norelease' commit." + else + selected_version="$(cat .version)" + git tag "$selected_version" + git push origin "$selected_version" + fi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7af9867 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target/ +project/target/ +project/project/ +modules/core/target/ +.version diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..f2e8cfc --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + - id: fix-byte-order-marker + - id: mixed-line-ending + args: ['--fix=lf'] + description: Enforces using only 'LF' line endings. + - id: trailing-whitespace + - id: check-yaml + - repo: https://git.garrity.co/garrity-software/gs-pre-commit-scala + rev: v1.0.0 + hooks: + - id: scalafmt diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..80e6cc9 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,72 @@ +// See: https://github.com/scalameta/scalafmt/tags for the latest tags. +version = 3.7.17 +runner.dialect = scala3 +maxColumn = 80 + +rewrite { + rules = [RedundantBraces, RedundantParens, Imports, SortModifiers] + imports.expand = true + imports.sort = scalastyle + redundantBraces.ifElseExpressions = true + redundantBraces.stringInterpolation = true +} + +indent { + main = 2 + callSite = 2 + defnSite = 2 + extendSite = 4 + withSiteRelativeToExtends = 2 + commaSiteRelativeToExtends = 2 +} + +align { + preset = more + openParenCallSite = false + openParenDefnSite = false +} + +newlines { + implicitParamListModifierForce = [before,after] + topLevelStatementBlankLines = [ + { + blanks = 1 + } + ] + afterCurlyLambdaParams = squash +} + +danglingParentheses { + defnSite = true + callSite = true + ctrlSite = true + exclude = [] +} + +verticalMultiline { + atDefnSite = true + arityThreshold = 2 + newlineAfterOpenParen = true +} + +comments { + wrap = standalone +} + +docstrings { + style = "SpaceAsterisk" + oneline = unfold + wrap = yes + forceBlankLineBefore = true +} + +project { + excludePaths = [ + "glob:**target/**", + "glob:**.metals/**", + "glob:**.bloop/**", + "glob:**.bsp/**", + "glob:**metals.sbt", + "glob:**.git/**" + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..dfb93f9 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# gs-blob + +[GS Open Source](https://garrity.co/oss.html) | +[License (MIT)](./LICENSE) + +Blob data type for Scala 3. + +- [Usage](#usage) + - [Dependency](#dependency) +- [Donate](#donate) + +## Usage + +### Dependency + +This artifact is available in the Garrity Software Maven repository. + +```scala +externalResolvers += + "Garrity Software Releases" at "https://maven.garrity.co/releases" + +val GsBlob: ModuleID = + "gs" %% "gs-blob-v0" % "$VERSION" +``` + +## Donate + +Enjoy this project or want to help me achieve my [goals](https://garrity.co)? +Consider [Donating to Pat on Ko-fi](https://ko-fi.com/gspfm). diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..10dd0b8 --- /dev/null +++ b/build.sbt @@ -0,0 +1,35 @@ +val scala3: String = "3.4.1" + +externalResolvers := Seq( + "Garrity Software Mirror" at "https://maven.garrity.co/releases", + "Garrity Software Releases" at "https://maven.garrity.co/gs" +) + +ThisBuild / scalaVersion := scala3 +ThisBuild / versionScheme := Some("semver-spec") +ThisBuild / gsProjectName := "gs-blob" + +val sharedSettings = Seq( + scalaVersion := scala3, + version := semVerSelected.value, + coverageFailOnMinimum := true, + coverageMinimumStmtTotal := 100, + coverageMinimumBranchTotal := 100 +) + +lazy val testSettings = Seq( + libraryDependencies ++= Seq( + "org.scalameta" %% "munit" % "1.0.0-M10" % Test + ) +) + +lazy val `gs-blob` = project + .in(file(".")) + .settings(sharedSettings) + .settings(testSettings) + .settings(name := s"${gsProjectName.value}-v${semVerMajor.value}") + .settings( + libraryDependencies ++= Seq( + "gs" %% "gs-hex-v0" % "0.1.1" + ) + ) diff --git a/mit b/mit new file mode 100644 index 0000000..f2b2735 --- /dev/null +++ b/mit @@ -0,0 +1,9 @@ +MIT License + +Copyright Patrick Garrity + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..04267b1 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.9.9 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..3c02b6a --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,28 @@ +def selectCredentials(): Credentials = + if ((Path.userHome / ".sbt" / ".credentials").exists()) + Credentials(Path.userHome / ".sbt" / ".credentials") + else + Credentials.apply( + realm = "Reposilite", + host = "maven.garrity.co", + userName = sys.env + .get("GS_MAVEN_USER") + .getOrElse( + throw new RuntimeException( + "You must either provide ~/.sbt/.credentials or specify the GS_MAVEN_USER environment variable." + ) + ), + passwd = sys.env + .get("GS_MAVEN_TOKEN") + .getOrElse( + throw new RuntimeException( + "You must either provide ~/.sbt/.credentials or specify the GS_MAVEN_TOKEN environment variable." + ) + ) + ) + +credentials += selectCredentials() + +addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.11") +addSbtPlugin("gs" % "sbt-garrity-software" % "0.3.0") +addSbtPlugin("gs" % "sbt-gs-semver" % "0.3.0") diff --git a/src/main/scala/gs/blob/v0/Blob.scala b/src/main/scala/gs/blob/v0/Blob.scala new file mode 100644 index 0000000..ad14369 --- /dev/null +++ b/src/main/scala/gs/blob/v0/Blob.scala @@ -0,0 +1,68 @@ +package gs.blob.v0 + +import gs.hex.v0.Hex +import gs.hex.v0.HexDecode +import gs.hex.v0.HexEncode +import java.util.Base64 + +/** Opaque type that represents an array of bytes -- this type is intended to be + * used for arbitrary blobs of data. + */ +opaque type Blob = Array[Byte] + +object Blob: + + /** Instantiate a new [[Blob]]. + * + * @param data + * The raw data. + * @return + * The new blob. + */ + def apply(data: Array[Byte]): Blob = data + + given CanEqual[Blob, Blob] = CanEqual.derived + + given HexEncode[Blob] = new HexEncode[Blob] { + + /** @inheritDocs + */ + override def toHexString(data: Blob): String = Hex.toHexString(data) + } + + given HexDecode[Blob] = new HexDecode[Array[Byte]] { + + /** @inheritDocs + */ + override def fromHexString(data: String): Option[Array[Byte]] = + Hex.fromHexString(data) + + } + + /** Convert the given base64 string to a [[Blob]]. + * + * @param data + * The candidate data. + * @return + * The parsed [[Blob]], or `None` if the input is not valid base64. + */ + def fromBase64(data: String): Option[Blob] = + scala.util.Try(Base64.getDecoder().decode(data)).toOption + + extension (blob: Blob) + /** @return + * The underlying bytes of this [[Blob]]. + */ + def toByteArray(): Array[Byte] = blob + + /** @return + * The base64 representation of this [[Blob]]. + */ + def toBase64(): String = Base64.getEncoder().encodeToString(blob) + + /** @return + * The number of bytes contained in this [[Blob]]. + */ + def length(): Int = blob.length + +end Blob