-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ccdad1
commit bf278fe
Showing
4 changed files
with
94 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |