diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ca5048..6e958e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,10 +11,12 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Enforced Rails version depending on the Solidus version in generated Gemfile - Made Git ignore `spec/examples.txt` in generated extensions +- It's now possible to update an existing extension with the command `bundle exec solidus extension .` from within the extension ### Changed - Removed the version constraint for Factory Bot from the Gemfile +- The `solidus` executable is now solely managed by Thor and is open to extension by other gems ### Fixed @@ -24,6 +26,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Removed - Removed RuboCop from the default Rake task +- Removed the `-v` option from the `solidus` executable ## [0.3.0] - 2019-01-10 @@ -37,6 +40,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Changed - Updated solidus_support to 0.4.0 for Zeitwerk and Rails 6 compatibility +- Updated the `solidus` executable to only rely on Thor and be open to extension by other gems ### Removed diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000..9275675 --- /dev/null +++ b/bin/rake @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rake' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +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" + +load Gem.bin_path("rake", "rake") diff --git a/exe/solidus b/exe/solidus index f5171b5..e6a9086 100755 --- a/exe/solidus +++ b/exe/solidus @@ -1,53 +1,6 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'optparse' +require 'solidus_dev_support/solidus_command' -require 'solidus_dev_support/version' - -Options = Struct.new(:name) - -class Parser - class << self - def parse(args) - options.parse!(args) - end - - def options - OptionParser.new do |opts| - opts.banner = 'Usage: solidus [[extension extension_name] | [-h] [-v]]' - - opts.on('-h', '--help', 'Prints this help') do - puts opts - end - - opts.on( - '-v', - '--version', - "Prints the current version: #{SolidusDevSupport::VERSION}" - ) do - puts SolidusDevSupport::VERSION - end - end - end - end -end - -# Print help if no options are supplied -ARGV << '--help' unless ARGV.first - -if ARGV.first == 'extension' || ARGV.first == 'e' - ARGV.shift - - unless ARGV.first - puts 'An extension must have a name!' - puts `solidus -h` - exit 1 - end - - require 'solidus_dev_support' - require 'solidus_dev_support/extension' - SolidusDevSupport::Extension.start -else - Parser.parse(ARGV) -end +SolidusDevSupport::SolidusCommand.start diff --git a/lib/solidus_dev_support/extension.rb b/lib/solidus_dev_support/extension.rb index 211ee21..a6d534a 100644 --- a/lib/solidus_dev_support/extension.rb +++ b/lib/solidus_dev_support/extension.rb @@ -1,54 +1,56 @@ # frozen_string_literal: true require 'thor' -require 'thor/group' require 'pathname' module SolidusDevSupport - class Extension < Thor::Group + class Extension < Thor include Thor::Actions + PREFIX = 'solidus_' - desc 'builds a solidus extension' - argument :file_name, type: :string, desc: 'rails app_path', default: '.' + default_command :generate - source_root File.expand_path('templates/extension', __dir__) + desc 'generate PATH', 'Generates a new Solidus extension' + def generate(raw_path = '.') + self.path = raw_path - def generate - use_prefix 'solidus_' + empty_directory path - empty_directory file_name - - directory 'app', "#{file_name}/app" - directory 'lib', "#{file_name}/lib" - directory 'bin', "#{file_name}/bin" - directory '.circleci', "#{file_name}/.circleci" - directory '.github', "#{file_name}/.github" + directory 'app', "#{path}/app" + directory 'lib', "#{path}/lib" + directory 'bin', "#{path}/bin" + directory '.circleci', "#{path}/.circleci" + directory '.github', "#{path}/.github" Dir["#{file_name}/bin/*"].each do |executable| make_executable executable end - template 'extension.gemspec.erb', "#{file_name}/#{file_name}.gemspec" - template 'Gemfile', "#{file_name}/Gemfile" - template 'gitignore', "#{file_name}/.gitignore" - template 'gem_release.yml.tt', "#{file_name}/.gem_release.yml" - template 'LICENSE', "#{file_name}/LICENSE" - template 'Rakefile', "#{file_name}/Rakefile" - template 'README.md', "#{file_name}/README.md" - template 'config/routes.rb', "#{file_name}/config/routes.rb" - template 'config/locales/en.yml', "#{file_name}/config/locales/en.yml" - template 'rspec', "#{file_name}/.rspec" - template 'spec/spec_helper.rb.tt', "#{file_name}/spec/spec_helper.rb" - template 'rubocop.yml', "#{file_name}/.rubocop.yml" + template 'extension.gemspec.erb', "#{path}/#{file_name}.gemspec" + template 'Gemfile', "#{path}/Gemfile" + template 'gitignore', "#{path}/.gitignore" + template 'gem_release.yml.tt', "#{path}/.gem_release.yml" + template 'LICENSE', "#{path}/LICENSE" + template 'Rakefile', "#{path}/Rakefile" + template 'README.md', "#{path}/README.md" + template 'config/routes.rb', "#{path}/config/routes.rb" + template 'config/locales/en.yml', "#{path}/config/locales/en.yml" + template 'rspec', "#{path}/.rspec" + template 'spec/spec_helper.rb.tt', "#{path}/spec/spec_helper.rb" + template 'rubocop.yml', "#{path}/.rubocop.yml" end no_tasks do - def class_name - Thor::Util.camel_case file_name - end + def path=(path) + path = File.expand_path(path) + + @file_name = Thor::Util.snake_case(File.basename(path)) + @file_name = PREFIX + @file_name unless @file_name.start_with?(PREFIX) + + @class_name = Thor::Util.camel_case @file_name - def use_prefix(prefix) - @file_name = prefix + Thor::Util.snake_case(file_name) unless file_name =~ /^#{prefix}/ + @root = File.dirname(path) + @path = File.join(@root, @file_name) end def make_executable(path) @@ -56,6 +58,12 @@ def make_executable(path) executable = (path.stat.mode | 0o111) path.chmod(executable) end + + attr_reader :root, :path, :file_name, :class_name + end + + def self.source_root + "#{__dir__}/templates/extension" end end end diff --git a/lib/solidus_dev_support/solidus_command.rb b/lib/solidus_dev_support/solidus_command.rb new file mode 100644 index 0000000..c1b0567 --- /dev/null +++ b/lib/solidus_dev_support/solidus_command.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'thor' +require 'solidus_dev_support/extension' + +module SolidusDevSupport + class SolidusCommand < Thor + namespace '' + + desc 'extension', 'Manage solidus extensions' + subcommand 'extension', Extension + + desc 'e', 'Manage solidus extensions (shortcut for "extension")' + subcommand 'e', Extension + + def self.exit_on_failure? + true + end + end +end + diff --git a/spec/features/create_extension_spec.rb b/spec/features/create_extension_spec.rb index b401d6d..711ccb3 100644 --- a/spec/features/create_extension_spec.rb +++ b/spec/features/create_extension_spec.rb @@ -27,7 +27,7 @@ def check_solidus_cmd cd(tmp_path) do output = `#{solidus_cmd} -h` expect($?).to be_success - expect(output).to include('Usage') + expect(output).to include('Commands:') end end