Class: TTY::Commands::Add
- Includes:
- PathHelpers
- Defined in:
- lib/tty/commands/add.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#cmd_name ⇒ Object
readonly
Returns the value of attribute cmd_name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#subcmd_name ⇒ Object
readonly
Returns the value of attribute subcmd_name.
Instance Method Summary collapse
- #cmd_exists?(content) ⇒ Boolean
-
#cmd_matches ⇒ Object
private
Matches for inlining command defition in template.
- #execute(input: $stdin, output: $stdout) ⇒ Object
- #file_options ⇒ Object
-
#initialize(cmd_names, options) ⇒ Add
constructor
A new instance of Add.
- #namespaced_path ⇒ Object
- #register_subcmd_matches ⇒ Object
- #subcmd_exists?(content) ⇒ Boolean
- #subcmd_matches ⇒ Object
- #subcmd_present? ⇒ Boolean
- #subcmd_registered?(content) ⇒ Boolean
- #template_context ⇒ Object
Methods included from PathHelpers
#name_from_path, #relative_path_from, #root_path, #within_root_path
Methods inherited from TTY::Cmd
#command, #constantinize, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which
Constructor Details
#initialize(cmd_names, options) ⇒ Add
Returns a new instance of Add.
22 23 24 25 26 27 28 29 30 |
# File 'lib/tty/commands/add.rb', line 22 def initialize(cmd_names, ) @cmd_name = cmd_names[0] @subcmd_name = cmd_names[1] @app_path = relative_path_from(root_path, root_path) @app_name = name_from_path(root_path) @options = @templater = Templater.new('add', @app_path) end |
Instance Attribute Details
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
14 15 16 |
# File 'lib/tty/commands/add.rb', line 14 def app_name @app_name end |
#cmd_name ⇒ Object (readonly)
Returns the value of attribute cmd_name.
16 17 18 |
# File 'lib/tty/commands/add.rb', line 16 def cmd_name @cmd_name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
20 21 22 |
# File 'lib/tty/commands/add.rb', line 20 def @options end |
#subcmd_name ⇒ Object (readonly)
Returns the value of attribute subcmd_name.
18 19 20 |
# File 'lib/tty/commands/add.rb', line 18 def subcmd_name @subcmd_name end |
Instance Method Details
#cmd_exists?(content) ⇒ Boolean
150 151 152 |
# File 'lib/tty/commands/add.rb', line 150 def cmd_exists?(content) content =~ %r{\s*def #{cmd_name_underscored}.*} end |
#cmd_matches ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Matches for inlining command defition in template
157 158 159 160 161 162 163 |
# File 'lib/tty/commands/add.rb', line 157 def cmd_matches [ %r{def version.*?:version\n}m, %r{def version.*?#{app_indent} end\n}m, %r{class CLI < Thor\n} ] end |
#execute(input: $stdin, output: $stdout) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/tty/commands/add.rb', line 65 def execute(input: $stdin, output: $stdout) validate_cmd_name(cmd_name) test_dir = (["test"] == 'rspec') || ::Dir.exist?('spec') ? 'spec' : 'test' cli_file = "lib/#{namespaced_path}/cli.rb" cli_content = ::File.read(cli_file) cmd_file = "lib/#{namespaced_path}/commands/#{cmd_name_path}.rb" cmd_template_path = "lib/#{namespaced_path}/templates/#{cmd_name_path}" cmd_integ_test_file = "#{test_dir}/integration/#{cmd_name_path}_#{test_dir}.rb" cmd_unit_test_file = "#{test_dir}/unit/#{cmd_name_path}_#{test_dir}.rb" if !subcmd_present? @templater.add_mapping( "#{test_dir}/integration/command_#{test_dir}.rb.tt", "#{test_dir}/integration/#{cmd_name_path}_#{test_dir}.rb") @templater.add_mapping("#{test_dir}/unit/command_#{test_dir}.rb.tt", "#{test_dir}/unit/#{cmd_name_path}_#{test_dir}.rb") @templater.add_mapping('command.rb.tt', cmd_file) @templater.add_mapping('gitkeep.tt', "#{cmd_template_path}/.gitkeep") @templater.generate(template_context, ) if !cmd_exists?(cli_content) match = cmd_matches.find { |m| cli_content =~ m } generator.inject_into_file( cli_file, "\n#{cmd_template}", {after: match}.merge()) end else subcmd_file = "lib/#{namespaced_path}/commands/#{cmd_name_path}/#{subcmd_name_path}.rb" subcmd_template_path = "lib/#{namespaced_path}/templates/#{cmd_name_path}/#{subcmd_name_path}" unless ::File.exists?(cmd_integ_test_file) @templater.add_mapping( "#{test_dir}/integration/command_#{test_dir}.rb.tt", cmd_integ_test_file) end unless ::File.exists?(cmd_unit_test_file) @templater.add_mapping( "#{test_dir}/unit/#{cmd_name_path}_#{test_dir}.rb", cmd_unit_test_file ) end @templater.add_mapping( "#{test_dir}/integration/sub_command_#{test_dir}.rb.tt", "#{test_dir}/integration/#{cmd_name_path}/#{subcmd_name_path}_#{test_dir}.rb") @templater.add_mapping( "#{test_dir}/unit/sub_command_#{test_dir}.rb.tt", "#{test_dir}/unit/#{cmd_name_path}/#{subcmd_name_path}_#{test_dir}.rb" ) unless ::File.exists?(cmd_file) # namespace already present @templater.add_mapping('namespace.rb.tt', cmd_file) end @templater.add_mapping('command.rb.tt', subcmd_file) @templater.add_mapping('gitkeep.tt', "#{subcmd_template_path}/.gitkeep") @templater.generate(template_context, ) if !subcmd_registered?(cli_content) match = register_subcmd_matches.find { |m| cli_content =~ m } generator.inject_into_file( cli_file, "\n#{register_subcmd_template}", {after: match}.merge()) end content = ::File.read(cmd_file) if !subcmd_exists?(content) match = subcmd_matches.find {|m| content =~ m } generator.inject_into_file( cmd_file, "\n#{subcmd_template}", {after: match}.merge()) end end end |
#file_options ⇒ Object
58 59 60 61 62 63 |
# File 'lib/tty/commands/add.rb', line 58 def opts = {} opts[:force] = true if ['force'] opts[:color] = false if ['no-color'] opts end |
#namespaced_path ⇒ Object
32 33 34 |
# File 'lib/tty/commands/add.rb', line 32 def namespaced_path app_name.tr('-', '/') end |
#register_subcmd_matches ⇒ Object
172 173 174 175 176 |
# File 'lib/tty/commands/add.rb', line 172 def register_subcmd_matches [ %r{require_relative .*?\nregister .*?\n}m ].concat(cmd_matches) end |
#subcmd_exists?(content) ⇒ Boolean
146 147 148 |
# File 'lib/tty/commands/add.rb', line 146 def subcmd_exists?(content) content =~ %r{\s*def #{subcmd_name_underscored}.*} end |
#subcmd_matches ⇒ Object
165 166 167 168 169 170 |
# File 'lib/tty/commands/add.rb', line 165 def subcmd_matches [ %r{namespace .*?\n}, %r{class .*? < Thor\n} ] end |
#subcmd_present? ⇒ Boolean
138 139 140 |
# File 'lib/tty/commands/add.rb', line 138 def subcmd_present? !subcmd_name.nil? end |
#subcmd_registered?(content) ⇒ Boolean
142 143 144 |
# File 'lib/tty/commands/add.rb', line 142 def subcmd_registered?(content) content =~%r{\s*require_relative 'commands/#{cmd_name_path}'} end |
#template_context ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tty/commands/add.rb', line 36 def template_context opts = OpenStruct.new opts[:cmd_options] = opts[:cmd_object_parts] = cmd_object_parts opts[:cmd_desc_args] = cmd_desc_args opts[:cmd_desc] = cmd_desc opts[:app_indent] = app_indent opts[:cmd_indent] = cmd_indent opts[:cmd_path] = "#{namespaced_path}/commands/#{cmd_name_path}" opts[:subcmd_path] = subcmd_name && "#{namespaced_path}/commands/#{cmd_name_path}/#{subcmd_name_path}" opts[:cmd_name_constantinized] = cmd_name_constantinized opts[:subcmd_name_constantinized] = subcmd_name && subcmd_name_constantinized opts[:app_name_underscored] = app_name_underscored opts[:cmd_name_underscored] = cmd_name_underscored opts[:subcmd_name_underscored] = subcmd_name && subcmd_name_underscored opts[:app_constantinized_parts] = app_name_constantinized.split('::') opts[:cmd_constantinized_parts] = cmd_constantinized_parts opts[:cmd_file_path] = cmd_file_path opts end |