From af890f4df18107005bbb218211fbe4effbe6d3a5 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Sat, 16 Jul 2022 23:22:18 +0200 Subject: [PATCH] Redo how the distribution artifact is created - Add version information to the stitching tool --- .github/workflows/build-release.yml | 29 ++++++++--- .github/workflows/build-test.yml | 13 ++--- .gitignore | 3 +- .vscode/settings.json | 3 ++ bin/stitch/stitch.go | 2 + bin/stitch/version.go | 29 +++++++++++ go.mod | 3 ++ go.sum | 7 +++ scripts/dist/compress.go | 79 +++++++++++++++++++++++++++++ scripts/dist/main.go | 70 +++++++++++++++++++++++++ 10 files changed, 222 insertions(+), 16 deletions(-) create mode 100644 bin/stitch/version.go create mode 100644 scripts/dist/compress.go create mode 100644 scripts/dist/main.go diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index bfceb8f..11e4652 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -12,15 +12,28 @@ jobs: strategy: matrix: goos: [windows] - goarch: [amd64] + goarch: ["386"] steps: - - uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ^1.18 - - uses: wangyoucao577/go-release-action@master - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - goos: ${{ matrix.goos }} - goarch: ${{ matrix.goarch }} - extra_files: init.lua LICENSE compatibility.xml mod.xml README.md AREAS.md images/coordinates.png images/example1.png images/example2.png images/scale32_base-layout.png images/scale32_main-world.png images/scale32_extended.png data files capture-b/capture.dll capture-b/README.md stitch/stitch.exe stitch/README.md \ No newline at end of file + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Build stitch tool + run: go build -v -ldflags="-X 'main.versionString=${{ github.event.release.tag_name }}'" . + working-directory: ./bin/stitch + + - name: Create distribution archive + run: go run -v ./scripts/dist + + - name: Upload binary to release + uses: svenstaro/upload-release-action@v2 + with: + file: dist/dist.zip + asset_name: noita-mapcap-${{ matrix.goos }}-${{ matrix.goarch }}.zip + overwrite: true diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 9055adc..526b34b 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -9,17 +9,18 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.x + - name: Set up Go uses: actions/setup-go@v2 with: go-version: ^1.18 - id: go - name: Check out code into the Go module directory uses: actions/checkout@v2 - - name: Build - run: go build -v ./bin/stitch + - name: Build stitch tool + run: go build -v . + working-directory: ./bin/stitch - - name: Test - run: go test -v ./bin/stitch + - name: Test stitch tool + run: go test -v . + working-directory: ./bin/stitch diff --git a/.gitignore b/.gitignore index 72b42a6..aa73400 100644 --- a/.gitignore +++ b/.gitignore @@ -103,7 +103,6 @@ $RECYCLE.BIN/ # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) -/libs/ /output/ -/distribution/ +/dist/ /bin/stitch/output.png \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 1db118e..1241f06 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,11 +7,13 @@ "downscaling", "executables", "Fullscreen", + "goarch", "gridify", "hacky", "hilbertify", "kbinani", "Lanczos", + "ldflags", "lowram", "manifoldco", "mapcap", @@ -20,6 +22,7 @@ "prerender", "promptui", "schollz", + "svenstaro", "tcnksm", "Vogel", "xmax", diff --git a/bin/stitch/stitch.go b/bin/stitch/stitch.go index 46e5a90..f2c92a3 100644 --- a/bin/stitch/stitch.go +++ b/bin/stitch/stitch.go @@ -31,6 +31,8 @@ var flagPrerender = flag.Bool("prerender", false, "Pre renders the image in RAM var flagCleanupThreshold = flag.Float64("cleanup", 0, "Enable cleanup mode with the given threshold. This will DELETE images from the input folder, no stitching will be done in this mode. A good value to start with is 0.999, which deletes images where the sum of the min-max difference of each sub-pixel overlapping with other images is less than 99.9%% of the maximum possible sum of pixel differences.") func main() { + log.Printf("Noita MapCapture stitching tool v%s", version) + flag.Parse() // Query the user, if there were no cmd arguments given diff --git a/bin/stitch/version.go b/bin/stitch/version.go new file mode 100644 index 0000000..6b3e24e --- /dev/null +++ b/bin/stitch/version.go @@ -0,0 +1,29 @@ +// Copyright (c) 2022 David Vogel +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +package main + +import ( + "strings" + + "github.com/coreos/go-semver/semver" +) + +// versionString contains the semantic version of the software as a string. +// +// This variable is only used to transfer the correct version information into the build. +// Don't use this variable in the software, use `version` instead. +// +// When building the software, the default `v0.0.0-development` is used. +// To compile the program with the correct version information, compile the following way: +// +// `go build -ldflags="-X 'main.versionString=x.y.z'"`, where `x.y.z` is the correct and valid version from the git tag. +// This variable may or may not contain the prefix v. +var versionString = "0.0.0-development" + +// version of the program. +// +// When converted into a string, this will not contain the v prefix. +var version = semver.Must(semver.NewVersion(strings.TrimPrefix(versionString, "v"))) diff --git a/go.mod b/go.mod index 1140c1d..4c6f252 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,12 @@ go 1.18 require ( github.com/cheggaaa/pb/v3 v3.1.0 + github.com/coreos/go-semver v0.3.0 github.com/google/hilbert v0.0.0-20181122061418-320f2e35a565 github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329 github.com/manifoldco/promptui v0.9.0 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75 golang.org/x/image v0.0.0-20220617043117-41969df76e82 ) @@ -23,4 +25,5 @@ require ( github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 227e9b2..4c9595d 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObk github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04= github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8= +github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= @@ -42,6 +44,8 @@ github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75 h1:x03zeu7B2B11ySp+daztnwM5oBJ/8wGUSqrwcw9L0RA= +golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/image v0.0.0-20220617043117-41969df76e82 h1:KpZB5pUSBvrHltNEdK/tw0xlPeD13M6M6aGP32gKqiw= golang.org/x/image v0.0.0-20220617043117-41969df76e82/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -56,3 +60,6 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JC golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/scripts/dist/compress.go b/scripts/dist/compress.go new file mode 100644 index 0000000..123cd7b --- /dev/null +++ b/scripts/dist/compress.go @@ -0,0 +1,79 @@ +// Copyright (c) 2022 David Vogel +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +package main + +import ( + "archive/zip" + "io" + "io/fs" + "log" + "os" + "path/filepath" + + "golang.org/x/exp/slices" +) + +// addPathToZip adds the given file or directory at srcPath to the zipWriter. +// +// The ignorePaths list is compared to the archive path (archive base path + relative path). +func addPathToZip(zipWriter *zip.Writer, srcPath, archiveBasePath string, ignorePaths []string) error { + return filepath.WalkDir(srcPath, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + relPath, err := filepath.Rel(srcPath, path) + if err != nil { + return err + } + archivePath := filepath.Join(archiveBasePath, relPath) + + // Skip if path is in ignore list. + // This applies to directories or files. + if slices.Contains(ignorePaths, archivePath) { + log.Printf("Skipped %q", archivePath) + if d.IsDir() { + return fs.SkipDir + } + return nil + } + + // Ignore directories. + if d.IsDir() { + return nil + } + + fileToZip, err := os.Open(path) + if err != nil { + return err + } + defer fileToZip.Close() + + info, err := fileToZip.Stat() + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + + header.Name = archivePath + header.Method = zip.Deflate + + writer, err := zipWriter.CreateHeader(header) + if err != nil { + return err + } + + if _, err = io.Copy(writer, fileToZip); err != nil { + return err + } + + return nil + }) +} diff --git a/scripts/dist/main.go b/scripts/dist/main.go new file mode 100644 index 0000000..59eb1ca --- /dev/null +++ b/scripts/dist/main.go @@ -0,0 +1,70 @@ +// Copyright (c) 2022 David Vogel +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +package main + +import ( + "archive/zip" + "compress/flate" + "flag" + "io" + "log" + "os" + "path/filepath" + "time" +) + +func main() { + clean := flag.Bool("clean", true, "Remove distribution dir before starting") + dist := flag.String("dist", "dist", "Directory to put distribution files in") + flag.Parse() + + start := time.Now() + + if *clean { + os.RemoveAll(*dist) + } + + // Create dist directory tree. + os.MkdirAll(filepath.Join(*dist), 0755) + + toCopy := []string{ + "AREAS.md", "compatibility.xml", "init.lua", "LICENSE", "mod.xml", "README.md", + + filepath.Join("bin", "capture-b", "capture.dll"), filepath.Join("bin", "capture-b", "README.md"), + filepath.Join("bin", "stitch", "stitch.exe"), filepath.Join("bin", "stitch", "README.md"), + filepath.Join("data"), + filepath.Join("files"), + filepath.Join("images"), + } + + toIgnore := []string{ + filepath.Join("noita-mapcap", "images", "coordinates.pdn"), + } + + // Create distribution archive. + newZipFile, err := os.Create(filepath.Join("dist", "dist.zip")) + if err != nil { + log.Panicf("Couldn't create output archive: %v", err) + } + defer newZipFile.Close() + + zipWriter := zip.NewWriter(newZipFile) + defer zipWriter.Close() + + zipWriter.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) { + return flate.NewWriter(out, flate.BestCompression) + }) + + for _, v := range toCopy { + srcPath, archivePath := filepath.Join(".", v), filepath.Join("noita-mapcap", v) + if err := addPathToZip(zipWriter, srcPath, archivePath, toIgnore); err != nil { + log.Panicf("Failed to copy %q into distribution directory: %v", v, err) + } + log.Printf("Copied %q", v) + } + + log.Printf("Distribution script complete in %v", time.Since(start)) +}