bin/katapult in katapult-0.2.0 vs bin/katapult in katapult-0.3.0

- old
+ new

@@ -1,72 +1,106 @@ #!/usr/bin/env ruby # This script simplifies the usage of `katapult` by grouping relevant actions # that the user else had to perform manually. -# See bottom for USAGE. +usage = 'Usage: katapult new APP_NAME | fire [path/to/model] | version' + +require_relative '../lib/katapult/version' require_relative '../lib/katapult/binary_util' util = Katapult::BinaryUtil -case (transform_command = ARGV.shift) +require 'optparse' +options = {} +OptionParser.new do |opts| + opts.on '-u', '--db-user USER', 'Database user name' do |name| + options[:dbuser] = name + end + opts.on '-p', '--db-password PASS', 'Database password' do |password| + options[:dbpass] = password + end + opts.on '-v', '--verbose', 'Verbose output' do |verbose| + options[:verbose] = verbose + end +end.parse! + +# Check Ruby version ########################################################### +supported_ruby = Katapult::RUBY_VERSION +current_ruby = `ruby -v`.chomp # Long version string à la "ruby 2.5.0p0 ..." + +current_ruby.include?(supported_ruby) or util.fail <<-MSG +Ruby version error + +Your current Ruby (#{current_ruby}) +is not supported by this version of katapult. +Please switch to Ruby #{supported_ruby} and run again. +MSG + + +case ARGV.shift when 'new' - interactive = !ARGV.delete('--non-interactive') - app_name = ARGV.shift - basics_command = 'bundle exec rails generate katapult:basics' + app_name = ARGV.shift || util.ask('Please enter the application name:') + app_name = util.snake_case(app_name) + puts "Normalized application name: #{app_name}" if options[:verbose] - if interactive - util.puts 'Please enter your database user: ' - basics_command << ' --db-user ' << gets.chomp + # Any options that haven't be passed via command line should be asked for + options[:dbuser] ||= util.ask 'Please enter the database user:' + options[:dbpass] ||= util.ask 'Please enter the database password:' - util.puts 'Please enter your database password: ' - basics_command << ' --db-password ' << gets.chomp - end + basics_command = 'bundle exec rails generate katapult:basics' + basics_command << ' --db-user ' << options[:dbuser] + basics_command << ' --db-password ' << options[:dbpass] - util.puts 'Creating new Rails application ...' + util.pink "Creating new Rails application in #{app_name} ..." util.create_rails_app app_name - Dir.chdir app_name - util.puts 'Initializing git repository ...' + util.pink 'Initializing git repository ...' util.run 'git init --quiet' util.git_commit "rails new #{ app_name }", '--quiet' - util.puts 'Installing katapult ...' + util.pink 'Installing katapult ...' File.open('Gemfile', 'a') do |file| file.puts "gem 'katapult'#{ ENV['KATAPULT_GEMFILE_OPTIONS'] }, group: :development" end util.run 'bundle install --quiet' - util.run 'bundle exec rails generate katapult:install' - util.git_commit 'rails generate katapult:install', '--quiet' + util.run 'bundle exec rails generate katapult:app_model' + util.git_commit 'rails generate katapult:app_model', '--quiet' - util.puts 'Generating katapult basics ...' + util.pink 'Generating katapult basics ...' util.run basics_command + # Do not use `basics_command` as commit message, as it contains the password! util.git_commit 'rails generate katapult:basics', '--quiet' - util.puts <<-INSTRUCTIONS + util.pink <<-INSTRUCTIONS Application initialization done. Next step: Model your application in lib/katapult/application_model.rb and transform it into code by running `katapult fire`. INSTRUCTIONS when 'fire' app_model_path = ARGV.shift || 'lib/katapult/application_model.rb' transform_command = 'bin/rails generate katapult:transform ' + app_model_path - util.puts 'Loading katapult ...' + util.pink 'Loading katapult ...' util.run transform_command + + util.pink 'Committing result ...' util.git_commit transform_command - util.puts <<-INSTRUCTIONS + util.pink <<-INSTRUCTIONS Model transformation done. Now boot up your development server (e.g. with `rails server`) and try your kickstarted application in the browser! INSTRUCTIONS +when 'version' + puts <<-VERSION +Katapult #{Katapult::VERSION} +Generating a Rails #{Katapult::RAILS_VERSION} app on Ruby #{Katapult::RUBY_VERSION}. + VERSION + else - puts <<-USAGE -Usage: katapult [new APP_NAME | fire [path/to/model] ] -Suppress database credentials prompt with `--non-interactive` - USAGE + puts usage end