lib/new/cli.rb in new-1.0.9 vs lib/new/cli.rb in new-1.0.10
- old
+ new
@@ -1,5 +1,8 @@
+#
+# New::Cli is a class built on Thor for interfacing with the user on the command line
+#
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/hash/keys'
require 'cli_miami'
require 'listen'
require 'pp'
@@ -194,10 +197,11 @@
S.ay "You are now ready to run `#{'new release'.green}` to release your software into the wild!"
S.ay
end
desc 'tasks', 'List all available tasks'
+ option :task, :aliases => ['-t'], :desc => 'Get details for a single task'
option :sources, :type => :boolean, :aliases => ['-s'], :default => false, :desc => 'Show/Hide task sources'
def tasks args = {}
# merge into default options
options = {
:show_source => (@options['sources'] || false),
@@ -205,75 +209,100 @@
:load_sources => true
}.merge(args)
New.load_newfiles if options[:load_newfiles]
if options[:load_sources]
- S.ay
S.ay 'Fetching sources...', :header
S.ay "Use the #{'green'.green} value for defining task sources in your Newfile", :indent => 2 if options[:show_source]
S.ay
New::Source.load_sources
end
- New::Source.sources.each do |source_name, source|
- # determine the widest task & add some padding
- longest_task_length = source.tasks.keys.map(&:length).max
+ if @options['task']
+ task = New::Source.find_task @options['task']
+ S.ay task.name.to_s.upcase, :preset => :header, :style => :underline, :padding => 30, :justify => :center
+ S.ay "source: #{task.source.name.to_s}", :color => :cyan, :padding => 30, :justify => :center
+ S.ay
- S.ay source_name.to_s, :indent => 2, :newline => false, :style => :underline
- S.ay source.path, :highlight_value
+ max_option_length = task.class_options.keys.map(&:length).max
+ task.class_options.each do |name, settings|
+ S.ay name.to_s, :preset => :highlight_key, :padding => max_option_length, :style => :underline
+ S.ay settings[:description], :highlight_value
- source.tasks.each do |task_name, task|
- if options[:show_source]
- padding = longest_task_length + source_name.to_s.length + 2
- S.ay "#{source_name}##{task_name}", :preset => :header, :newline => false, :indent => 2, :padding => padding, :justify => :ljust
- else
- padding = longest_task_length + 2
- S.ay task_name.to_s, :preset => :header, :newline => false, :indent => 2, :padding => padding, :justify => :ljust
+ S.ay 'Type:', :preset => :highlight_key, :padding => max_option_length + 13
+ S.ay "#{settings[:type] || 'String'}", :highlight_value
+
+ if settings[:required]
+ S.ay 'Required:', :preset => :highlight_key, :padding => max_option_length + 13
+ S.ay 'true', :highlight_value
+ elsif settings[:default]
+ S.ay 'Default:', :preset => :highlight_key, :padding => max_option_length + 13
+ S.ay settings[:default].to_s, :highlight_value
end
- S.ay task.description, :indent => 2
+
+ if settings[:validation]
+ S.ay 'Validation:', :preset => :highlight_key, :padding => max_option_length + 13
+ S.ay settings[:validation].to_s, :highlight_value
+ end
+
+ S.ay
end
+ else
+ New::Source.sources.each do |source_name, source|
+ # determine the widest task & add some padding
+ longest_task_length = source.tasks.keys.map(&:length).max
- S.ay
+ S.ay source_name.to_s, :indent => 2, :newline => false, :style => :underline
+ S.ay source.path, :highlight_value
+
+ source.tasks.each do |task_name, task|
+ if options[:show_source]
+ padding = longest_task_length + source_name.to_s.length + 2
+ S.ay "#{source_name}##{task_name}", :preset => :header, :newline => false, :indent => 2, :padding => padding, :justify => :ljust
+ else
+ padding = longest_task_length + 2
+ S.ay task_name.to_s, :preset => :header, :newline => false, :indent => 2, :padding => padding, :justify => :ljust
+ end
+ S.ay task.description, :indent => 2
+ end
+
+ S.ay
+ end
end
end
desc 'release', 'Release a new version of your project'
+ option :bump, :aliases => ['-b'], :enum => ['M', 'm', 'p'], :desc => 'Version part to bump'
option :verbose, :type => :boolean, :aliases => ['-v'], :default => false, :desc => 'Verbose mode'
option :skip, :type => :array, :aliases => ['-s'], :default => [], :desc => 'Tasks to skip for this release'
def release
- New.set_verbose if @options['verbose']
New.set_cli
+ New.set_verbose if @options['verbose']
New.load_newfiles
- # request the version to bump
S.ay
- S.ay 'Releasing a new version of: ', :highlight_key
- S.ay New.new_object[:name], :highlight_value
- S.ay 'What do you want to bump: ', :highlight_key
- S.ay "[#{'Mmp'.green}] (#{'M'.green}ajor / #{'m'.green}inor / #{'p'.green}atch)", :preset => :highlight_value, :color => :white
+ S.ay 'Releasing a new version of:', :highlight_key
+ S.ay New.new_object[:name].to_s, :highlight_value
+
version = Semantic::Version.new New.new_object[:version]
- version_bump_part = nil
- until version_bump_part
- S.ay 'Current Version:', :highlight_key
- A.sk version.to_s, :highlight_value do |response|
- version_bump_part = case response
- when 'M'
- version.major += 1
- version.minor = 0
- version.patch = 0
- when 'm'
- version.minor += 1
- version.patch = 0
- when 'p'
- version.patch += 1
- else
- S.ay 'You must choose from [Mmp]', :error
- nil
+ S.ay 'Current Version:', :highlight_key
+ S.ay version.to_s, :highlight_value
+
+ if @options['bump']
+ version = bump_version version, @options['bump']
+ else
+ S.ay 'What do you want to bump:', :highlight_key
+ S.ay "[#{'Mmp'.green}] (#{'M'.green}ajor / #{'m'.green}inor / #{'p'.green}atch)", :preset => :highlight_value, :color => :white
+ version_bump_part = nil
+ until version_bump_part
+ A.sk '', :prompt do |response|
+ version = version_bump_part = bump_version version, response
end
end
end
- S.ay 'New Version: ', :highlight_key
+
+ S.ay 'New Version:', :highlight_key
S.ay version.to_s, :highlight_value
S.ay
# collect a list of changes in this version
changelog = get_changelog_from_user
@@ -295,11 +324,11 @@
end
desc 'version', 'Show the current version'
def version
New.load_newfiles
- S.ay New.new_object[:name], :highlight_key
+ S.ay New.new_object[:name], :preset => :highlight_key, :padding => 0, :indent => 0
S.ay New.new_object[:version], :highlight_value
end
desc 'test', 'Run task tests from sources'
option :watch, :type => :boolean, :aliases => ['-w'], :desc => 'Watch local tasks for changes and run tests'
@@ -403,23 +432,19 @@
# start to build the array of user values
user_array = []
# convert array to hash of Strings
if validation.is_a? Array
- validation_hash = {}
- validation.each do |v|
- validation_hash[v.to_sym] = String
- end
- validation = validation_hash
+ validation = array_to_hash validation
end
if validation.is_a? Hash
S.ay 'We need to collect a list of complex objects', :header
user_response = nil
until user_response == 'n'
- S.ay "#{user_array.length} objects created: ", :indent => 2, :newline => false
+ S.ay "#{user_array.length} objects created:", :indent => 2, :newline => false
S.ay user_array.join(', '), :highlight_value
A.sk 'Press ENTER to create another object. Enter `n` to stop.', :prompt do |response|
if response == 'n'
user_response = 'n'
next
@@ -459,15 +484,11 @@
# start to build the hash of user values
user_hash = {}
# convert array to hash of Strings
if validation.is_a? Array
- validation_hash = {}
- validation.each do |v|
- validation_hash[v.to_sym] = String
- end
- validation = validation_hash
+ validation = array_to_hash validation
end
# get user values for required validation keys
validation.each do |key, klass|
S.ay 'We need to collect some required values', :header
@@ -532,9 +553,47 @@
end
end
end
return user_hash
+ end
+
+ # convert an array to a hash
+ # uses the array elements as hash keys, and sets the value to String
+ # @param array [Array] any array
+ # @return [Hash]
+ #
+ def array_to_hash array
+ array_hash = {}
+ array.each do |v|
+ array_hash[v.to_sym] = String
+ end
+
+ return array_hash
+ end
+
+ # bump a semantic version
+ # @param version [Semantic::Version] a Semantic::Version object
+ # @param part [String] a supported part character [Mmp]
+ # @return [Semantic::Version] a new Semantic::Version object
+ #
+ def bump_version version, part
+ case part
+ when 'M'
+ version.major += 1
+ version.minor = 0
+ version.patch = 0
+ when 'm'
+ version.minor += 1
+ version.patch = 0
+ when 'p'
+ version.patch += 1
+ else
+ S.ay 'You must choose from [Mmp]', :error
+ return nil
+ end
+
+ return version
end
end
default_task :release
end