From 4635524bce0afbb4be8319f65b8aab9643e810dd Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Tue, 25 Jun 2024 11:43:21 +0300 Subject: [PATCH] ci: universal publisher with Ruby script (#756) * ci: universal builder with scripts * ci: implement clean and put_readme :) * fix: prepare all parts in the script * chore: use Ruby script * chore: rebase --- .github/workflows/release.yml | 4 +- .github/workflows/test.yml | 4 +- .gitignore | 10 ++-- Makefile | 4 +- packaging/Makefile | 52 ---------------- packaging/pack.rb | 108 ++++++++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 64 deletions(-) delete mode 100644 packaging/Makefile create mode 100755 packaging/pack.rb diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c4623610..18fefdbb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -59,8 +59,8 @@ jobs: EOF chmod 0600 ~/.gem/credentials cd packaging/ - make prepare - make publish + ruby pack.rb prepare + ruby pack.rb publish - name: Update Homebrew formula uses: dawidd6/action-homebrew-bump-formula@v3 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4e63f537..a80a7671 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -70,11 +70,11 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - name: Build binaries - uses: goreleaser/goreleaser-action@v5 + uses: goreleaser/goreleaser-action@v6 with: distribution: goreleaser version: latest - args: build --snapshot --skip-validate --clean + args: build --snapshot --skip=validate --clean - name: Tar binaries to preserve executable bit run: 'tar -cvf lefthook-binaries.tar --directory dist/ $(find dist/ -executable -type f -printf "%P\0" | xargs --null)' - name: Upload binaries as artifacts diff --git a/.gitignore b/.gitignore index 3fb90110..9d02edbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,17 @@ .vscode/ .idea/ -/lefthook-local.yml /lefthook +/lefthook-local.yml tmp/ dist/ + +# Packages packaging/rubygems/pkg/ packaging/rubygems/libexec/ packaging/npm-bundled/bin/ packaging/npm-*/README.md packaging/npm/*/bin/ -!packaging/npm/lefthook/bin/index.js packaging/npm/*/README.md -package.json !packaging/npm/*/package.json -node_modules/ -yarn.lock -package-lock.json +!packaging/npm/lefthook/bin/index.js diff --git a/Makefile b/Makefile index 3de1cbfc..e73c1409 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ lint: bin/golangci-lint version: @read -p "New version: " version sed -i "s/const version = .*/const version = \"$$version\"/" internal/version/version.go - sed -i "s/VERSION := .*/VERSION := $$version/" packaging/Makefile + sed -i "s/VERSION = .*/VERSION = \"$$version\"/" packaging/pack.rb sed -i "s/lefthook-plugin.git\", exact: \".*\"/lefthook-plugin.git\", exact: \"$$version\"/" docs/install.md - make -C packaging clean set-version + ruby packaging/pack.rb clean set_version git add internal/version/version.go packaging/* docs/install.md diff --git a/packaging/Makefile b/packaging/Makefile deleted file mode 100644 index 642dbe1b..00000000 --- a/packaging/Makefile +++ /dev/null @@ -1,52 +0,0 @@ -# Packages version to release -VERSION := 1.6.18 - -DIST_DIR := ../dist - -LINUX_AMD64_BIN=$(DIST_DIR)/lefthook_linux_amd64_v1/lefthook -LINUX_ARM64_BIN=$(DIST_DIR)/lefthook_linux_arm64/lefthook -FREEBSD_AMD64_BIN=$(DIST_DIR)/lefthook_freebsd_amd64_v1/lefthook -FREEBSD_ARM64_BIN=$(DIST_DIR)/lefthook_freebsd_arm64/lefthook -WINDOWS_AMD64_BIN=$(DIST_DIR)/lefthook_windows_amd64_v1/lefthook.exe -WINDOWS_ARM64_BIN=$(DIST_DIR)/lefthook_windows_arm64/lefthook.exe -DARWIN_AMD64_BIN=$(DIST_DIR)/lefthook_darwin_amd64_v1/lefthook -DARWIN_ARM64_BIN=$(DIST_DIR)/lefthook_darwin_arm64/lefthook - -prepare: clean set-version put-readme put-binaries - -publish: - cd npm; find . -type d -name 'lefthook*' -exec npm publish --access public \{} \; ; cd - - cd npm-bundled; npm publish --access public ; cd - - cd npm-installer; npm publish --access public ; cd - - cd rubygems; rake build; gem push pkg/*.gem ; cd - - -# Update versions of all packages -set-version: - find npm -name 'package.json' -type f -print0 | xargs -0 sed -E -i "s/\"version\": \".+\"/\"version\": \"$(VERSION)\"/" - sed -E -i "s/\"(lefthook-.+)\": \".+\"/\"\1\": \"$(VERSION)\"/g" npm/lefthook/package.json - sed -E -i "0,/version/{s/\"version\": \".+\"/\"version\": \"$(VERSION)\"/}" npm-bundled/package.json - sed -E -i "0,/version/{s/\"version\": \".+\"/\"version\": \"$(VERSION)\"/}" npm-installer/package.json - sed -E -i "s/(spec\.version\s+= ).*/\1\"$(VERSION)\"/" rubygems/lefthook.gemspec - -put-binaries: - install -D $(LINUX_AMD64_BIN) npm/lefthook-linux-x64/bin/lefthook - install -D $(LINUX_ARM64_BIN) npm/lefthook-linux-arm64/bin/lefthook - install -D $(FREEBSD_AMD64_BIN) npm/lefthook-freebsd-x64/bin/lefthook - install -D $(FREEBSD_ARM64_BIN) npm/lefthook-freebsd-arm64/bin/lefthook - install -D $(WINDOWS_AMD64_BIN) npm/lefthook-windows-x64/bin/lefthook.exe - install -D $(WINDOWS_ARM64_BIN) npm/lefthook-windows-arm64/bin/lefthook.exe - install -D $(DARWIN_AMD64_BIN) npm/lefthook-darwin-x64/bin/lefthook - install -D $(DARWIN_ARM64_BIN) npm/lefthook-darwin-arm64/bin/lefthook - cd npm-bundled; npm version $(VERSION) --allow-same-version; cd - - cd $(DIST_DIR); find . -maxdepth 2 -type f -exec cp --parents \{\} ../packaging/rubygems/libexec/ \; - -put-readme: - find npm/ -type d -name 'lefthook*' -exec cp -f ../README.md \{} \; - cp ../README.md npm-bundled/ - cp ../README.md npm-installer/ - -clean: - find npm/ -name 'README.md' -exec rm \{} \; - find npm/ -type f -name 'lefthook*' -exec rm \{} \; - git clean -fdX npm-installer/ npm-bundled/ npm-bundled/bin/ - git clean -fdX rubygems/libexec/ rubygems/pkg/ diff --git a/packaging/pack.rb b/packaging/pack.rb new file mode 100755 index 00000000..2009ca83 --- /dev/null +++ b/packaging/pack.rb @@ -0,0 +1,108 @@ +#!/usr/bin/env ruby + +require "fileutils" + +VERSION = "1.6.18" + +ROOT = File.join(__dir__, "..") +DIST = File.join(ROOT, "dist") + +module Pack + extend FileUtils + + module_function + + def prepare + clean + set_version + put_readme + put_binaries + end + + def clean + cd(__dir__) + puts "Cleaning... " + rm(Dir["npm/**/README.md"]) + rm(Dir["npm/**/lefthook*"].filter(&File.method(:file?))) + system("git clean -fdX npm-installer/ npm-bundled/ npm-bundled/bin/ rubygems/libexec/ rubygems/pkg/", exception: true) + puts "done" + end + + def set_version + cd(__dir__) + puts "Replacing version to #{VERSION} in packages" + (Dir["npm/**/package.json"] + ["npm-bundled/package.json", "npm-installer/package.json"]).each do |package_json| + replace_in_file(package_json, /"version": "[\d.]+"/, %{"version": "#{VERSION}"}) + end + + replace_in_file("npm/lefthook/package.json", /"(lefthook-.+)": "[\d.]+"/, %{"\\1": "#{VERSION}"}) + replace_in_file("rubygems/lefthook.gemspec", /(spec\.version\s+= ).*/, %{\\1"#{VERSION}"}) + end + + def put_readme + cd(__dir__) + puts "Putting READMEs... " + Dir["npm/*"].each do |npm_dir| + cp(File.join(ROOT, "README.md"), File.join(npm_dir, "README.md"), verbose: true) + end + cp(File.join(ROOT, "README.md"), "npm-bundled/", verbose: true) + cp(File.join(ROOT, "README.md"), "npm-installer/", verbose: true) + puts "done" + end + + def put_binaries + cd(__dir__) + puts "Putting binaries to packages..." + { + "#{DIST}/lefthook_linux_amd64_v1/lefthook" => "npm/lefthook-linux-x64/bin/lefthook", + "#{DIST}/lefthook_linux_arm64/lefthook" => "npm/lefthook-linux-arm64/bin/lefthook", + "#{DIST}/lefthook_freebsd_amd64_v1/lefthook" => "npm/lefthook-freebsd-x64/bin/lefthook", + "#{DIST}/lefthook_freebsd_arm64/lefthook" => "npm/lefthook-freebsd-arm64/bin/lefthook", + "#{DIST}/lefthook_windows_amd64_v1/lefthook.exe" => "npm/lefthook-windows-x64/bin/lefthook.exe", + "#{DIST}/lefthook_windows_arm64/lefthook.exe" => "npm/lefthook-windows-arm64/bin/lefthook.exe", + "#{DIST}/lefthook_darwin_amd64_v1/lefthook" => "npm/lefthook-darwin-x64/bin/lefthook", + "#{DIST}/lefthook_darwin_arm64/lefthook" => "npm/lefthook-darwin-arm64/bin/lefthook", + }.each do |(source, dest)| + mkdir_p(File.dirname(dest)) + cp(source, dest, verbose: true) + end + puts "done" + end + + def publish + puts "Publishing lefthook npm..." + cd(File.join(__dir__, "npm")) + Dir["lefthook*"].each do |package| + system("npm publish --access public #{package}", exception: true) + end + + puts "Publishing @evilmartians/lefthook npm..." + cd(File.join(__dir__, "npm-bundled")) + system("npm publish --access public", exception: true) + + puts "Publishing @evilmartians/lefthook-installer npm..." + cd(File.join(__dir__, "npm-installer")) + system("npm publish --access public", exception: true) + + puts "Publishing lefthook gem..." + cd(File.join(__dir__, "rubygems")) + system("rake build", exception: true) + system("gem push pkg/*.gem", exception: true) + + puts "done" + end + + def replace_in_file(filepath, regexp, value) + text = File.open(filepath, "r") do |f| + f.read + end + text.gsub!(regexp, value) + File.open(filepath, "w") do |f| + f.write(text) + end + end +end + +ARGV.each do |cmd| + Pack.public_send(cmd) +end