Skip to content

Commit

Permalink
ci: universal publisher with Ruby script (#756)
Browse files Browse the repository at this point in the history
* ci: universal builder with scripts

* ci: implement clean and put_readme :)

* fix: prepare all parts in the script

* chore: use Ruby script

* chore: rebase
  • Loading branch information
mrexox authored Jun 25, 2024
1 parent 4ea200b commit 4635524
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 64 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 0 additions & 52 deletions packaging/Makefile

This file was deleted.

108 changes: 108 additions & 0 deletions packaging/pack.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4635524

Please sign in to comment.