diff --git a/.github/workflows/auto-check-workflow.yml b/.github/workflows/auto-check-workflow.yml deleted file mode 100644 index ab9dae1..0000000 --- a/.github/workflows/auto-check-workflow.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Initialise Repository - -on: - push: - branches: - - main # first push triggers bootstrap - -permissions: - contents: write - -jobs: - bootstrap: - # Only run for org repos and skip template repositories - if: ${{ github.repository_owner == 'snivilised' && !github.event.repository.is_template }} - runs-on: ubuntu-latest - - steps: - # Checkout repository using default github.token (always safe) - - name: Checkout repository - uses: actions/checkout@v4 - with: - ref: ${{ github.ref }} - - # Configure Git author - - name: Configure Git - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" - - # Install Go version specified in go.mod - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version-file: go.mod - - # Run automation script - - name: Run automation script - run: go run ./scripts/automate-checklist.go - - # Format code and tidy modules - - name: Format & tidy Go - run: | - go fmt ./... - go mod tidy - - # Remove bootstrap/template-only files - - name: Clean up template scripts - run: | - rm -fv .github/workflows/auto-check-workflow.yml - rm -fv ./scripts/automate-checklist.go - rm -fv ./scripts/automate-checklist.sh - - # Commit & push changes using org PAT (no linter warning) - - name: Commit & push changes - env: - ORG_PAT: ${{ secrets.AUTOMATION_PAT }} - run: | - git add . - if [ -n "$(git status --porcelain)" ]; then - git commit -m "chore(repo): bootstrap repository" - # Update remote URL to include PAT for push - git remote set-url origin https://x-access-token:${ORG_PAT}@github.com/${GITHUB_REPOSITORY}.git - git push origin HEAD:${{ github.event.repository.default_branch }} - else - echo "No changes to commit" - fi - diff --git a/scripts/automate-checklist.go b/scripts/automate-checklist.go deleted file mode 100644 index 6a2754d..0000000 --- a/scripts/automate-checklist.go +++ /dev/null @@ -1,215 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "io/fs" - "os" - "os/exec" - "path/filepath" - "strings" -) - -const ( - THIS_FILE = "automate-checklist.go" -) - -func main() { - owner := os.Getenv("GITHUB_REPOSITORY_OWNER") - if owner == "" { - owner = getGitOwner() - } - - repo := "" - githubRepo := os.Getenv("GITHUB_REPOSITORY") // e.g., "owner/repo" - if githubRepo != "" { - parts := strings.Split(githubRepo, "/") - if len(parts) == 2 { - repo = parts[1] - } - } - if repo == "" { - repo = getGitRepoName() - } - - if owner == "" || repo == "" { - fmt.Println("Could not determine owner or repo. Exiting.") - os.Exit(1) - } - - fmt.Printf("---> 😎 OWNER: %s\n", owner) - fmt.Printf("---> 🧰 REPO: %s\n\n", repo) - - lcRepo := strings.ToLower(repo) - if len(repo) == 0 { - fmt.Println("Repo name is empty. Exiting.") - os.Exit(1) - } - tcRepo := strings.ToUpper(repo[:1]) + strings.ToLower(repo[1:]) - ucRepo := strings.ToUpper(repo) - - replacements := map[string]string{ - "snivilised/astrolib": fmt.Sprintf("%s/%s", owner, lcRepo), - "snivilised/Astrolib": fmt.Sprintf("%s/%s", owner, tcRepo), - "snivilised/ASTROLIB": fmt.Sprintf("%s/%s", owner, ucRepo), - "astrolib": lcRepo, - "Astrolib": tcRepo, - "ASTROLIB": ucRepo, - } - - // 1. & 2. Global replace - doGlobalReplace(replacements) - - // 3. Rename files - renameTargetFiles("astrolib", lcRepo) - renameTargetFiles("Astrolib", tcRepo) - renameTargetFiles("ASTROLIB", ucRepo) - - // 4. Reset version - err := os.WriteFile("VERSION", []byte("v0.1.0\n"), 0644) //nolint:gosec // ok, not sensitive - if err != nil { - fmt.Printf("Error writing VERSION: %v\n", err) - } - - // 5. Create .env - f, err := os.OpenFile(".env", os.O_CREATE|os.O_RDWR, 0644) //nolint:gosec // ok, not sensitive - if err == nil { - _ = f.Close() - } - - fmt.Println("✔️ done") -} - -func getGitOwner() string { - cmd := exec.Command("git", "config", "--get", "remote.origin.url") - out, err := cmd.Output() - if err != nil { - return "" - } - url := strings.TrimSpace(string(out)) - // Parse URL: https://github.com/owner/repo.git or git@github.com:owner/repo.git - if strings.Contains(url, "git@") { - parts := strings.Split(url, ":") - if len(parts) > 1 { - path := parts[1] - pathParts := strings.Split(path, "/") - if len(pathParts) > 0 { - return pathParts[0] - } - } - } else if strings.Contains(url, "https://") { - parts := strings.Split(url, "/") - if len(parts) >= 4 { - return parts[3] - } - } - return "" -} - -func getGitRepoName() string { - cmd := exec.Command("git", "rev-parse", "--show-toplevel") - out, err := cmd.Output() - if err != nil { - return "" - } - return filepath.Base(strings.TrimSpace(string(out))) -} - -func isTargetFile(path string) bool { - if strings.Contains(path, "/.git/") || strings.Contains(path, "/vendor/") { - return false - } - ext := filepath.Ext(path) - base := filepath.Base(path) - if base == THIS_FILE { - return false - } - switch ext { - case ".go", ".md", ".yml", ".yaml", ".json": - return true - } - if base == "go.mod" || strings.HasPrefix(base, "Taskfile") { - return true - } - return false -} - -func doGlobalReplace(replacements map[string]string) { - _ = filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error { - if err != nil { - return nil - } - if d.IsDir() { - if d.Name() == ".git" || d.Name() == "vendor" { - return fs.SkipDir - } - return nil - } - if !isTargetFile(path) { - return nil - } - - content, err := os.ReadFile(path) //nolint:gosec // ok, not user-provided content - if err != nil { - return nil - } - - modified := false - newContent := content - - // Order of replacements matters - order := []string{ - "snivilised/astrolib", - "snivilised/Astrolib", - "snivilised/ASTROLIB", - "astrolib", - "Astrolib", - "ASTROLIB", - } - - for _, k := range order { - v := replacements[k] - if bytes.Contains(newContent, []byte(k)) { - newContent = bytes.ReplaceAll(newContent, []byte(k), []byte(v)) - modified = true - } - } - - if modified { - // Don't change file permissions - info, err := d.Info() - if err == nil { - _ = os.WriteFile(path, newContent, info.Mode()) - } - } - return nil - }) -} - -func renameTargetFiles(target, replacement string) { - var pathsToRename []string - _ = filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error { - if err != nil { - return nil - } - if d.IsDir() { - if d.Name() == ".git" || d.Name() == "vendor" { - return fs.SkipDir - } - return nil - } - base := filepath.Base(path) - if strings.Contains(base, target) { - pathsToRename = append(pathsToRename, path) - } - return nil - }) - - for _, path := range pathsToRename { - dir := filepath.Dir(path) - base := filepath.Base(path) - newBase := strings.ReplaceAll(base, target, replacement) - newPath := filepath.Join(dir, newBase) - _ = os.Rename(path, newPath) //nolint:gosec // ok, not user-provided content - } -} diff --git a/scripts/automate-checklist.sh b/scripts/automate-checklist.sh deleted file mode 100644 index c60f2c7..0000000 --- a/scripts/automate-checklist.sh +++ /dev/null @@ -1,243 +0,0 @@ -# shellcheck disable=SC2148 - -function auto-check() { - owner=$(git config --get remote.origin.url | cut -d '/' -f 4) - repo=$(git rev-parse --show-toplevel | xargs basename) - - echo "---> 😎 OWNER: $owner" - echo "---> 🧰 REPO: $repo" - echo "" - - if ! update-workflow-names "$repo" "$owner"; then - return 1 - fi - - if ! update-mod-file "$repo" "$owner"; then - return 1 - fi - - if ! update-source-id-variable-in-translate-defs "$repo" "$owner"; then - return 1 - fi - - - if ! update-astrolib-in-taskfile "$repo" "$owner"; then - return 1 - fi - - - if ! update-astrolib-in-goreleaser "$repo" "$owner"; then - return 1 - fi - - - if ! rename-templ-data-id "$repo" "$owner"; then - return 1 - fi - - - if ! update-import-statements "$repo" "$owner"; then - return 1 - fi - - - if ! update-readme "$repo" "$owner"; then - return 1 - fi - - - if ! rename-language-files "$repo" "$owner"; then - return 1 - fi - - - if ! reset-version; then - return 1 - fi - - touch ./.env - echo "✔️ done" - return 0 -} - -function update-all-generic() { - local title=$1 - local repo=$2 - local owner=$3 - local folder=$4 - local name=$5 - local target=$6 - local replacement=$7 - - echo " 🎯 ---> title: $title" - echo " ✅ ---> file pattern: $name" - echo " ✅ ---> folder: $folder" - echo " ✅ ---> target: $target" - echo " ✅ ---> replacement: $replacement" - - # !!!WARNING: sed - # does not work the same way between mac and linux - # - # find "$folder" -name "$name" -type f -print0: This part of the command uses the find utility to search for files within the specified folder ($folder) matching the given pattern ($name). Here's what each option does: - # -name "$name": Specifies the filename pattern to match. In this case, it matches the filenames with the pattern stored in the variable $name. - # -type f: Specifies that only regular files should be matched, excluding directories, symbolic links, etc. - # -print0: This option tells find to print the matched filenames separated by null characters (\0). This is essential for correctly handling filenames with spaces or special characters. - - # while IFS= read -r -d '' file; do: This initiates a while loop that reads each null-delimited filename (-d '') produced by the find command. Here's what each part does: - # IFS=: This sets the Internal Field Separator to nothing. This is to ensure that leading and trailing whitespace characters are not trimmed from each filename. - # read -r: This command reads input from the pipe (|) without interpreting backslashes (-r option). - # -d '': This option specifies the delimiter as null characters (\0), ensuring that filenames containing spaces or special characters are read correctly. - # file: This is the variable where each filename from the find command is stored during each iteration of the loop. - # - # trying to modify: - # - ci-workflow.yml - # - release-workflow.yml - find "$folder" -name "$name" -type f -print0 | while IFS= read -r -d '' file; do - echo "Processing file: $file" - uname_output=$(uname) - # sed help: - # '': no file backup needed (we modify the file in place without backing up original) - # but note that this is only required for mac. for linux, you dont need the ''. - # - # -i: The in-place edit flag, which tells sed to modify the original file inline. - # 's/search_pattern/replacement_text/g': - # - # s: Indicates that this is a substitution command. - # /search_pattern/: The pattern to search for. - # /replacement_text/: The text to replace the search pattern with. - # g: The global flag, which ensures that all occurrences of the - # search pattern are replaced, not just the first one. - if [[ "$uname_output" == *"Darwin"* ]]; then - if ! sed -i '' "s/${target}/${replacement}/g" "$file"; then - echo "!!! ⛔ Sed on mac failed for $file" - return 1 - fi - else - if ! sed -i "s/${target}/${replacement}/g" "$file"; then - echo "!!! ⛔ Sed on linux failed for $file" - return 1 - fi - fi - done - - echo " ✔️ ---> DONE" - echo "" - return 0 -} - -function update-mod-file() { - local repo=$1 - local owner=$2 - local folder=./ - local file_pattern=go.mod - local target="module github.com\/snivilised\/astrolib" - local replacement="module github.com\/$owner\/$repo" - update-all-generic "update-mod-file" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function update-source-id-variable-in-translate-defs() { - local repo=$1 - local owner=$2 - local folder=./i18n/ - local file_pattern=translate-defs.go - local target="AstrolibSourceID" - tc_repo=$(echo "${repo:0:1}" | tr '[:lower:]' '[:upper:]')${repo:1} - local replacement="${tc_repo}SourceID" - update-all-generic "update-source-id-variable-in-translate-defs" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function update-astrolib-in-taskfile() { - local repo=$1 - local owner=$2 - local folder=./ - local file_pattern=Taskfile.yml - local target=astrolib - local replacement=$repo - update-all-generic "update-astrolib-in-taskfile" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function update-workflow-names() { - local repo=$1 - local owner=$2 - local folder=.github/workflows - local file_pattern="*.yml" - local target="name: Astrolib" - tc_repo=$(echo "${repo:0:1}" | tr '[:lower:]' '[:upper:]')${repo:1} - local replacement="name: $tc_repo" - update-all-generic "💥 update-workflow-names" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function update-astrolib-in-goreleaser() { - local repo=$1 - local owner=$2 - local folder=./ - local file_pattern=.goreleaser.yaml - local target=astrolib - local replacement=$repo - update-all-generic "update-astrolib-in-goreleaser" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function rename-templ-data-id() { - local repo=$1 - local owner=$2 - local folder=./ - local file_pattern="*.go" - local target="astrolibTemplData" - local replacement="${repo}TemplData" - update-all-generic "rename-templ-data-id" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function update-readme() { - local repo=$1 - local owner=$2 - local folder=./ - local file_pattern=README.md - local target="astrolib: " - local replacement="${repo}: " - - - if ! update-all-generic "update-readme(astrolib:)" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement"; then - return 1 - fi - - target="snivilised\/astrolib" - replacement="$owner\/$repo" - - if ! update-all-generic "update-readme(snivilised/astrolib)" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement"; then - return 1 - fi - - target="astrolib Continuous Integration" - tc_repo=$(echo "${repo:0:1}" | tr '[:lower:]' '[:upper:]')${repo:1} - replacement="$tc_repo Continuous Integration" - - if ! update-all-generic "update-readme(astrolib Continuous Integration)" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement"; then - return 1 - fi - - return 0 -} - -function update-import-statements() { - local repo=$1 - local owner=$2 - local folder=./ - local file_pattern="*.go" - local target="snivilised\/astrolib" - local replacement="$owner\/$repo" - update-all-generic "update-import-statements" "$repo" "$owner" "$folder" "$file_pattern" "$target" "$replacement" -} - -function rename-language-files() { - local repo=$1 - find . -name 'astrolib*.json' -type f -print0 | - while IFS= read -r -d '' file; do - mv "$file" "$(dirname "$file")/$(basename "$file" | sed "s/^astrolib/$repo/")" - done - return $? -} - -function reset-version() { - echo "v0.1.0" > ./VERSION - return 0 -}