From c510ff5ba72f3abcbcc17c55c46767ad933d3472 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Tue, 26 Sep 2023 14:45:04 +0900 Subject: [PATCH] Rename project --- .github/workflows/ci-go.yml | 2 +- .github/workflows/dependency-review.yml | 2 +- .github/workflows/lint.yml | 4 ++-- .github/workflows/release.yml | 2 +- .gitignore | 2 +- .goreleaser.yaml | 2 +- CONTRIBUTING.md | 6 +++--- README.md | 24 ++++++++++++------------ Taskfile.yml | 4 ++-- cmd/{self-replacer => selfup}/main.go | 12 ++++++------ flake.nix | 12 ++++++------ go.mod | 2 +- replacer.go => selfup.go | 12 ++++++------ 13 files changed, 43 insertions(+), 43 deletions(-) rename cmd/{self-replacer => selfup}/main.go (75%) rename replacer.go => selfup.go (83%) diff --git a/.github/workflows/ci-go.yml b/.github/workflows/ci-go.yml index 0f89991..a209507 100644 --- a/.github/workflows/ci-go.yml +++ b/.github/workflows/ci-go.yml @@ -45,4 +45,4 @@ jobs: with: # Keep after `setup-go`, to use same go version install-go: false - version: '2023.1.6' # Replace as { "regex": "\\d{4}[^']+", "command": "staticcheck --version | cut -d ' ' -f 2" } + version: '2023.1.6' # selfup { "regex": "\\d{4}[^']+", "script": "staticcheck --version | cut -d ' ' -f 2" } diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 097c912..5005401 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -23,6 +23,6 @@ jobs: # https://spdx.org/licenses/ allow-licenses: MIT, BSD-3-Clause, BSD-2-Clause, 0BSD, Unlicense, ISC, Apache-2.0, CC-BY-4.0 # The PURL prefix is got from following command - # `gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/kachick/self-replacer/dependency-graph/compare/87d736b80a8a254bcac003d97e0b84712176b04f...5ba877ff1048d5fedcc6758484623eb67737f0fb` + # `gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/kachick/selfup/dependency-graph/compare/87d736b80a8a254bcac003d97e0b84712176b04f...5ba877ff1048d5fedcc6758484623eb67737f0fb` # Not found in https://github.com/package-url/packageurl-js allow-dependencies-licenses: pkg:githubactions/DeterminateSystems/nix-installer-action@4 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 554cafd..b8289d1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -14,14 +14,14 @@ jobs: - uses: actions/checkout@v4 - uses: dprint/check@v2.2 with: - dprint-version: '0.40.2' # Replace as { "regex": "\\d[^']+", "command": "dprint --version | cut -d ' ' -f 2" } + dprint-version: '0.40.2' # selfup { "regex": "\\d[^']+", "script": "dprint --version | cut -d ' ' -f 2" } typos: timeout-minutes: 15 runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: crate-ci/typos@v1.16.11 # Replace as { "regex": "\\d\\.\\d+\\.\\d+", "command": "typos --version | cut -d ' ' -f 2" } + - uses: crate-ci/typos@v1.16.11 # selfup { "regex": "\\d\\.\\d+\\.\\d+", "script": "typos --version | cut -d ' ' -f 2" } with: files: | . diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index df3d88a..c31d33a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,7 +34,7 @@ jobs: if: startsWith(github.ref, 'refs/tags/') with: distribution: goreleaser - version: 'v1.20.0' # Replace as { "regex": "\\d[^']+", "command": "goreleaser --version | grep 'GitVersion:' | tr -s ' ' | cut -d ' ' -f 2" } + version: 'v1.20.0' # selfup { "regex": "\\d[^']+", "script": "goreleaser --version | grep 'GitVersion:' | tr -s ' ' | cut -d ' ' -f 2" } args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index c10e1c4..6c3db86 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,7 @@ go.work # Binary when not used goreleaser -/self-replacer +/selfup # goreleaser generates to dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 901dd38..cc7bd0b 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -9,7 +9,7 @@ before: builds: - env: - CGO_ENABLED=0 - main: ./cmd/self-replacer + main: ./cmd/selfup goos: - linux # - windows diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 18d0bbf..808b3a9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -20,8 +20,8 @@ task: [test] go test task: [lint] dprint check task: [lint] go vet PASS -ok self-replacer 0.313s +ok selfup 0.313s -> ./dist/self-replacer --version -self-replacer 0.1.1-next (906924b) # 2023-06-19T09:33:14Z +> ./dist/selfup --version +selfup 0.1.1-next (906924b) # 2023-06-19T09:33:14Z ``` diff --git a/README.md b/README.md index a6c74ac..fbb022b 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ -# self-replacer +# selfup -[![CI - Go Status](https://github.com/kachick/self-replacer/actions/workflows/ci-go.yml/badge.svg?branch=main)](https://github.com/kachick/self-replacer/actions/workflows/ci-go.yml?query=branch%3Amain+) -[![CI - Nix Status](https://github.com/kachick/self-replacer/actions/workflows/ci-nix.yml/badge.svg?branch=main)](https://github.com/kachick/self-replacer/actions/workflows/ci-nix.yml?query=branch%3Amain+) +[![CI - Go Status](https://github.com/kachick/selfup/actions/workflows/ci-go.yml/badge.svg?branch=main)](https://github.com/kachick/selfup/actions/workflows/ci-go.yml?query=branch%3Amain+) +[![CI - Nix Status](https://github.com/kachick/selfup/actions/workflows/ci-nix.yml/badge.svg?branch=main)](https://github.com/kachick/selfup/actions/workflows/ci-nix.yml?query=branch%3Amain+) -Replace strings in files if the line contains the scripts to output new string +Replace strings if the line contains the definition of how to update itself. ## Installation In [Nix](https://nixos.org/), you can skip installation steps ```bash -nix run github:kachick/self-replacer -- --prefix='# Replace as ' .github/workflows/*.yml +nix run github:kachick/selfup -- --prefix='# selfup ' .github/workflows/*.yml ``` ## Usage @@ -20,28 +20,28 @@ Assume some GitHub actions workflow have lines like follows ```yaml - uses: dprint/check@v2.2 with: - dprint-version: '0.40.2' # Replace as { "regex": "\\d[^']+", "command": "dprint --version | cut -d ' ' -f 2" } + dprint-version: '0.40.2' # selfup { "regex": "\\d[^']+", "script": "dprint --version | cut -d ' ' -f 2" } ``` Then we can call this tool as follows ```bash -self-replacer --prefix='# Replace as ' .github/workflows/*.yml +selfup --prefix='# selfup ' .github/workflows/*.yml ``` ### JSON schema -| Field | Description | -| ------- | ----------------------------------------------------------------------------------------- | -| regex | [RE2](https://github.com/google/re2/wiki/Syntax), be careful to escape for JSON stringify | -| command | Bash scripts | +| Field | Description | +| ------ | ----------------------------------------------------------------------------------------- | +| regex | [RE2](https://github.com/google/re2/wiki/Syntax), be careful to escape for JSON stringify | +| script | Bash script | ### Mode `--list-targets` option prints extracted targets without side-effect ```console -> self-replacer --list-targets --prefix='# Replace as ' .github/workflows/*.yml +> selfup --list-targets --prefix='# selfup ' .github/workflows/*.yml .github/workflows/ci-go.yml:48: 2023.1.6 .github/workflows/lint.yml:17: 0.40.2 .github/workflows/lint.yml:24: 1.16.11 diff --git a/Taskfile.yml b/Taskfile.yml index 8d1874f..9f11dbd 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -13,10 +13,10 @@ tasks: - go install -ldflags "-X main.commit=$(git rev-parse HEAD)" build:preview: cmds: - - goreleaser build --snapshot --single-target --clean --output dist/self-replacer + - goreleaser build --snapshot --single-target --clean --output dist/selfup build: cmds: - - go build -o ./dist/self-replacer ./cmd/self-replacer + - go build -o ./dist/selfup ./cmd/selfup test: cmds: - go test ./... diff --git a/cmd/self-replacer/main.go b/cmd/selfup/main.go similarity index 75% rename from cmd/self-replacer/main.go rename to cmd/selfup/main.go index ebcc307..1ff0b06 100644 --- a/cmd/self-replacer/main.go +++ b/cmd/selfup/main.go @@ -7,7 +7,7 @@ import ( "os" "sync" - replacer "github.com/kachick/self-replacer" + updater "github.com/kachick/selfup" "golang.org/x/xerrors" ) @@ -24,10 +24,10 @@ func main() { listFlag := flag.Bool("list-targets", false, "print target lines without actual replacing") versionFlag := flag.Bool("version", false, "print the version of this program") - const usage = `Usage: self-replacer [OPTIONS] [PATH]... + const usage = `Usage: selfup [OPTIONS] [PATH]... -$ self-replacer --prefix='# Replace as ' .github/workflows/*.yml -$ self-replacer --prefix='# Replace as ' --list-targets .github/workflows/*.yml +$ selfup --prefix='# selfup ' .github/workflows/*.yml +$ selfup --prefix='# selfup ' --list-targets .github/workflows/*.yml ` flag.Usage = func() { @@ -40,7 +40,7 @@ $ self-replacer --prefix='# Replace as ' --list-targets .github/workflows/*.yml if len(commit) >= 7 { revision = commit[:7] } - version := fmt.Sprintf("%s\n", "self-replacer"+" "+version+" "+"("+revision+")") + version := fmt.Sprintf("%s\n", "selfup"+" "+version+" "+"("+revision+")") flag.Parse() if *versionFlag { @@ -61,7 +61,7 @@ $ self-replacer --prefix='# Replace as ' --list-targets .github/workflows/*.yml wg.Add(1) go func(path string) { defer wg.Done() - newBody, isDirty, err := replacer.Replace(path, prefix, isListMode) + newBody, isDirty, err := updater.Update(path, prefix, isListMode) if err != nil { log.Fatalf("%+v", err) diff --git a/flake.nix b/flake.nix index 967a5ac..213a11c 100644 --- a/flake.nix +++ b/flake.nix @@ -13,7 +13,7 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; - replacerVersion = + updaterVersion = if (self ? shortRev) then self.shortRev else "dev"; @@ -37,21 +37,21 @@ ]; }; - packages.self-replacer = pkgs.buildGo121Module { - pname = "self-replacer"; - version = replacerVersion; + packages.selfup = pkgs.buildGo121Module { + pname = "selfup"; + version = updaterVersion; src = pkgs.lib.cleanSource self; # When updating go.mod or go.sum, update this sha together vendorSha256 = "sha256-ot3JjQ49kLVG+l1EyLaxonDfgxTyFTmli3B8YDIjhyY="; }; - packages.default = packages.self-replacer; + packages.default = packages.selfup; # `nix run` apps.default = { type = "app"; - program = "${packages.self-replacer}/bin/self-replacer"; + program = "${packages.selfup}/bin/selfup"; }; } ); diff --git a/go.mod b/go.mod index c6eec4f..5cb15d5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kachick/self-replacer +module github.com/kachick/selfup go 1.21 diff --git a/replacer.go b/selfup.go similarity index 83% rename from replacer.go rename to selfup.go index 356003d..277f5f9 100644 --- a/replacer.go +++ b/selfup.go @@ -1,4 +1,4 @@ -package replacer +package updater import ( "bufio" @@ -14,12 +14,12 @@ import ( ) type Definition struct { - Regex string - Command string + Regex string + Script string } // Returns new body and true if it is changed -func Replace(path string, prefix string, isListMode bool) (string, bool, error) { +func Update(path string, prefix string, isListMode bool) (string, bool, error) { newLines := []string{} isChanged := false @@ -50,9 +50,9 @@ func Replace(path string, prefix string, isListMode bool) (string, bool, error) fmt.Printf("%s:%d: %s\n", path, lineNumber, re.FindString(head)) continue } - out, err := exec.Command("bash", "-c", definition.Command).Output() + out, err := exec.Command("bash", "-c", definition.Script).Output() if err != nil { - return "", false, xerrors.Errorf("%s:%d: Executing %s with bash has been failed: %w", path, lineNumber, definition.Command, err) + return "", false, xerrors.Errorf("%s:%d: Executing %s with bash has been failed: %w", path, lineNumber, definition.Script, err) } replaced := re.ReplaceAllString(head, strings.TrimSuffix(string(out), "\n")) if !isChanged {