Skip to content

Commit

Permalink
Use dry-cli for release commands
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed May 29, 2024
1 parent 1ccdad1 commit bf278fe
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 144 deletions.
4 changes: 2 additions & 2 deletions .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"hooks": {
"before:init": ["npm install", "bundle install", "bin/test"],
"after:bump": ["npm run lint:fix", "npm run build"],
"after:version:bump": "bin/release bump_version ${version} && bundle",
"after:release": "bin/release package_and_publish"
"after:version:bump": "bin/release bump ${version} && bundle",
"after:release": "bin/release publish"
}
}
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ gem "view_component"
group :development, :test do
gem "better_errors"
gem "binding_of_caller"
gem "colorize"
gem "dry-cli"
gem "listen"
gem "rubocop-rails", require: false
gem "standard", require: false
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
cgi (0.4.1)
colorize (1.1.0)
combustion (1.4.0)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
Expand All @@ -124,6 +125,7 @@ GEM
date (3.3.4)
debug_inspector (1.2.0)
drb (2.2.1)
dry-cli (1.0.0)
erb (4.0.4)
cgi (>= 0.3.3)
erubi (1.12.0)
Expand Down Expand Up @@ -330,8 +332,10 @@ PLATFORMS
DEPENDENCIES
better_errors
binding_of_caller
colorize
combustion
cuprite
dry-cli
listen
lookbook!
minitest-hooks
Expand Down
228 changes: 86 additions & 142 deletions bin/release
Original file line number Diff line number Diff line change
@@ -1,172 +1,116 @@
#!/usr/bin/env ruby

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath)

bundle_binstub = File.expand_path("../bundle", __FILE__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"
require "dry/cli"
require "bundler/gem_tasks"

require "optparse"
require "active_support"
require "colorize"
require "lookbook"

class Task
def initialize(args, options = {})
@args = args
@options = options
end

def log(message)
puts message
end

alias_method :info, :log
alias_method :success, :log
alias_method :error, :log

class << self
def call(args, opts = nil)
if opts.nil?
parsed_options = {}
options.parse!(into: parsed_options)
else
parsed_options = opts
end

new(args, parsed_options).call
end

def options
OptionParser.new
end
end
def info(message)
puts "→ #{message}".colorize(:blue)
end

class BumpVersionTask < Task
def initialize(args, options)
@version = args.shift
raise ArgumentError, "No version number provided" unless @version.present?

super(args, options)
end

def new_version
@version.gsub("v", "").tr("-", ".")
end

def current_version
Lookbook::VERSION.to_s
end

def version_file
File.expand_path("#{File.dirname(__FILE__)}/../lib/lookbook/version.rb")
end

def call
info "Bumping Lookbook version from `#{current_version}` to `#{new_version}`"

unless @options[:"dry-run"]
file = File.open(version_file)
contents = file.read
File.write(version_file, contents.gsub(current_version, new_version))
end

success "Version bump complete"
end
def success(message)
puts "→ #{message}".colorize(color: :green, mode: :bold)
end

def self.options
OptionParser.new do |opts|
opts.on "--dry-run", "Perform a dry run"
end
end
def error(message)
puts "→ #{message}".colorize(color: :red, mode: :bold)
end

class PackageGemTask < Task
def call
info "Building Lookbook gem"
def debug(message)
puts "→ #{message}".colorize(:gray)
end

unless @options[:"dry-run"]
Rake::Task["build"].execute
end
module Lookbook
module Release
module Commands
extend Dry::CLI::Registry

success "Gem build complete"
end
class Bump < Dry::CLI::Command
desc "Bumps the Lookbook version"

def self.options
OptionParser.new do |opts|
opts.on "--dry-run", "Perform a dry run"
end
end
end
argument :version, type: :string, required: true, desc: "The version to bump to"
option :dry_run, type: :boolean, default: false, desc: "Perform a dry run (no actual file changes)"

class PublishGemTask < Task
def gem_version
Lookbook::VERSION.to_s
end
def current_version
Lookbook::VERSION.to_s
end

def pkg_dir
File.expand_path("#{File.dirname(__FILE__)}/../pkg")
end
def version_file
File.expand_path("#{File.dirname(__FILE__)}/../lib/lookbook/version.rb")
end

def call
info "Publishing Lookbook v#{gem_version} to RubyGems"
def call(version:, dry_run: false)
new_version = version.gsub("v", "").tr("-", ".")

# successful = @options[:"dry-run"] ? true : "gem push #{pkg_dir}/lookbook-#{gem_version}.gem"
successful = true
info "Bumping Lookbook version from #{current_version} to #{new_version}..."

successful ? success("Gem published") : error("Error publishing Gem")
end
if dry_run
debug "[dry-run] Version file #{version_file} not updated."
else
file = File.open(version_file)
contents = file.read
File.write(version_file, contents.gsub(current_version, new_version))
debug "Updated version file #{version_file}"
end

def self.options
OptionParser.new do |opts|
opts.on "--dry-run", "Perform a dry run"
end
end
end
success "Version bump to #{new_version} complete."
end
end

class PackageAndPublishTask < Task
def call
PackageGemTask.call(@args, @options)
PublishGemTask.call(@args, @options)
end
class Publish < Dry::CLI::Command
desc "Publishes the Lookbook gem to RubyGems"

option :dry_run, type: :boolean, default: false, desc: "Perform a dry run (no actual file changes)"

def pkg_dir
File.expand_path("#{File.dirname(__FILE__)}/../pkg")
end

def call(dry_run:)
version = Lookbook::VERSION

info "Packaging Lookbook #{version}..."
if dry_run
debug "[dry-run] Gem not packaged into #{pkg_dir}."
else
Rake::Task["build"].execute
end
success "Gem packaging complete."

info "Publishing Lookbook #{version} to RubyGems..."
if dry_run
debug "[dry-run] Gem not pushed to RubyGems."
success("Lookbook #{version} successfully published.")
elsif system("gem push #{pkg_dir}/lookbook-#{version}.gem")
success("Lookbook #{version} successfully published.")
else
error("Error pushing gem to RubyGems")
end
rescue
error "Error publishing gem."
end
end

def self.options
OptionParser.new do |opts|
opts.on "--dry-run", "Perform a dry run"
end
end
end
class Start < Dry::CLI::Command
desc "Starts the release process"

class ReleaseTask < Task
def call
info "Starting release process"
option :dry_run, type: :boolean, default: false, desc: "Perform a dry run (no actual file changes)"

system "npm run release#{" -- --dry-run" if @options[:"dry-run"]}"
end
def call(dry_run:)
system("npm run release #{" -- --dry-run" if dry_run}")
end
end

def self.options
OptionParser.new do |opts|
opts.on "--dry-run", "Perform a dry run"
register "start", Start
register "bump", Bump
# register "package", Package
register "publish", Publish
# register "packpub", PackageAndPublish
end
end
end

begin
args = ARGV.filter { !_1.start_with?("-") }
task_name = args.none? ? "release_task" : "#{args.shift}_task"
task_name.camelize.constantize.call(args)
rescue NameError
abort("Unknown task `#{task_name.sub("_task", "")}`")
end
Dry::CLI.new(Lookbook::Release::Commands).call

0 comments on commit bf278fe

Please sign in to comment.