From 9bab3c4b8be7cf802cad7bc4071afb7eae089ad4 Mon Sep 17 00:00:00 2001 From: trobro Date: Sat, 23 Jul 2022 19:28:55 +0200 Subject: [PATCH] Tool build (#45) --- .github/workflows/build.yml | 47 ++++++++++++++----------------------- README.md | 2 ++ build_release.sh | 23 +++++++++--------- hjson-cli/main.go | 12 ++++++++-- 4 files changed, 41 insertions(+), 43 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4d84c6f..dd6cadf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,43 +6,30 @@ on: - 'v[0-9]+.[0-9]+**' jobs: - + build_hjson-cli_releases: - runs-on: ${{ matrix.os }} - + runs-on: ubuntu-latest + strategy: fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - + steps: - name: Checkout repo - uses: actions/checkout@v2 - - - name: Build the ${{ runner.os }} hjson-cli binary - run: | - mkdir binaries - cd hjson-cli - go build - - - name: Rename hjson-cli binary to reflect ${{ runner.os }} - run: | - mv hjson-cli/hjson-cli.exe binaries/hjson-cli_${{ github.ref_name }}_${{ runner.os }}.exe - if: ${{ contains(matrix.os, 'windows') }} - - - name: Rename hjson-cli binary to reflect ${{ runner.os }} - run: | - mv hjson-cli/hjson-cli binaries/hjson-cli_${{ github.ref_name }}_${{ runner.os }} - if: runner.os != 'Windows' - + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Build the hjson-cli binaries + run: ./build_release.sh + - name: Upload hjson-cli artifacts uses: actions/upload-artifact@v2 with: name: output path: binaries/* if-no-files-found: error - - + + release_artifacts: needs: [build_hjson-cli_releases] runs-on: ubuntu-latest @@ -51,21 +38,21 @@ jobs: uses: actions/download-artifact@v2 with: path: artifacts - + - name: Show the downloaded artifacts run: | pwd ls -laR * - + - name: Release binaries uses: ncipollo/release-action@v1 with: # ncipollo/release-action needs a tag! Either a usual "GIT TAG" or an dedicated TAG, see below! # set a TAG if you want to build a release, i.e. via "workflow_dispatch" on GitHub _AND_ do not push a regular GIT TAG # and the other way around, if you want to build releases based on pushed GIT TAGs, make sure you un-comment the "tag:" line below! - tag: ${{ github.ref_name }} + tag: ${{ github.ref_name }} draft: true artifactErrorsFailBuild: true artifacts: "artifacts/output/*" token: ${{ secrets.GITHUB_TOKEN }} - + diff --git a/README.md b/README.md index 0b32dd6..65c8aff 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ Options: Omit braces at the root. -quoteAlways Always quote string values. + -v + Show version. ``` Sample: diff --git a/build_release.sh b/build_release.sh index 9c5e309..bab1bca 100755 --- a/build_release.sh +++ b/build_release.sh @@ -1,21 +1,23 @@ #!/bin/bash cd `dirname $0` -ROOT=$PWD/_dist +ROOT=$PWD +BINARIES=$ROOT/binaries -if [ -d "$ROOT" ]; then rm -rf $ROOT; fi +if [ -d "$BINARIES" ]; then rm -rf $BINARIES; fi -mkdir $ROOT +mkdir $BINARIES function build() { export GOOS=$1 export GOARCH=$2 echo build $GOOS $GOARCH - OUT=$ROOT/${GOOS}_${GOARCH} + VERSION=`cd $ROOT && git describe --tags` + OUT=${BINARIES}/hjson_${VERSION}_${GOOS}_${GOARCH} mkdir $OUT cd $OUT - go build github.com/hjson/hjson-go/hjson-cli + go build -ldflags "-w -s -X main.Version=${VERSION}" github.com/hjson/hjson-go/v4/hjson-cli if [[ $3 == "zip" ]]; then mv $OUT/hjson-cli.exe $OUT/hjson.exe zip -j ${OUT}.zip $OUT/* @@ -23,16 +25,13 @@ function build() { mv $OUT/hjson-cli $OUT/hjson tar -czf ${OUT}.tar.gz -C $OUT . fi + rm -r $OUT } -# not all targets can be built on travis - -# build android arm -build darwin 386 +build android arm64 build darwin amd64 -# build darwin arm -# build darwin arm64 +build darwin arm64 build dragonfly amd64 build freebsd 386 build freebsd amd64 @@ -56,3 +55,5 @@ build plan9 amd64 build solaris amd64 build windows 386 zip build windows amd64 zip +build windows arm zip +build windows arm64 zip diff --git a/hjson-cli/main.go b/hjson-cli/main.go index af94aa7..89fb789 100644 --- a/hjson-cli/main.go +++ b/hjson-cli/main.go @@ -11,6 +11,10 @@ import ( "github.com/hjson/hjson-go/v4" ) +// Can be set when building for example like this: +// go build -ldflags "-X main.Version=v3.0" +var Version string + func fixJSON(data []byte) []byte { data = bytes.Replace(data, []byte("\\u003c"), []byte("<"), -1) data = bytes.Replace(data, []byte("\\u003e"), []byte(">"), -1) @@ -40,8 +44,7 @@ func main() { var bracesSameLine = flag.Bool("bracesSameLine", false, "Print braces on the same line.") var omitRootBraces = flag.Bool("omitRootBraces", false, "Omit braces at the root.") var quoteAlways = flag.Bool("quoteAlways", false, "Always quote string values.") - - // var showVersion = flag.Bool("V", false, "Show version.") + var showVersion = flag.Bool("v", false, "Show version.") flag.Parse() if *help || flag.NArg() > 1 { @@ -49,6 +52,11 @@ func main() { os.Exit(1) } + if *showVersion { + fmt.Println(Version) + os.Exit(0) + } + var err error var data []byte if flag.NArg() == 1 {