shopify-bundler 1.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +96 -0
  5. data/.rubocop_todo.yml +177 -0
  6. data/.travis.yml +104 -0
  7. data/CHANGELOG.md +2131 -0
  8. data/CODE_OF_CONDUCT.md +42 -0
  9. data/CONTRIBUTING.md +32 -0
  10. data/DEVELOPMENT.md +118 -0
  11. data/ISSUES.md +96 -0
  12. data/LICENSE.md +23 -0
  13. data/README.md +40 -0
  14. data/Rakefile +309 -0
  15. data/bin/rake +14 -0
  16. data/bin/rspec +10 -0
  17. data/bin/rubocop +11 -0
  18. data/bundler.gemspec +34 -0
  19. data/exe/bundle +21 -0
  20. data/exe/bundle_ruby +60 -0
  21. data/exe/bundler +21 -0
  22. data/lib/bundler.rb +499 -0
  23. data/lib/bundler/capistrano.rb +16 -0
  24. data/lib/bundler/cli.rb +435 -0
  25. data/lib/bundler/cli/binstubs.rb +37 -0
  26. data/lib/bundler/cli/cache.rb +34 -0
  27. data/lib/bundler/cli/check.rb +37 -0
  28. data/lib/bundler/cli/clean.rb +25 -0
  29. data/lib/bundler/cli/common.rb +56 -0
  30. data/lib/bundler/cli/config.rb +88 -0
  31. data/lib/bundler/cli/console.rb +37 -0
  32. data/lib/bundler/cli/exec.rb +51 -0
  33. data/lib/bundler/cli/gem.rb +208 -0
  34. data/lib/bundler/cli/init.rb +32 -0
  35. data/lib/bundler/cli/inject.rb +32 -0
  36. data/lib/bundler/cli/install.rb +188 -0
  37. data/lib/bundler/cli/lock.rb +35 -0
  38. data/lib/bundler/cli/open.rb +22 -0
  39. data/lib/bundler/cli/outdated.rb +86 -0
  40. data/lib/bundler/cli/package.rb +45 -0
  41. data/lib/bundler/cli/platform.rb +42 -0
  42. data/lib/bundler/cli/show.rb +74 -0
  43. data/lib/bundler/cli/update.rb +72 -0
  44. data/lib/bundler/cli/viz.rb +26 -0
  45. data/lib/bundler/constants.rb +5 -0
  46. data/lib/bundler/current_ruby.rb +218 -0
  47. data/lib/bundler/definition.rb +675 -0
  48. data/lib/bundler/dep_proxy.rb +45 -0
  49. data/lib/bundler/dependency.rb +119 -0
  50. data/lib/bundler/deployment.rb +62 -0
  51. data/lib/bundler/deprecate.rb +17 -0
  52. data/lib/bundler/dsl.rb +478 -0
  53. data/lib/bundler/endpoint_specification.rb +100 -0
  54. data/lib/bundler/env.rb +82 -0
  55. data/lib/bundler/environment.rb +41 -0
  56. data/lib/bundler/fetcher.rb +288 -0
  57. data/lib/bundler/fetcher/base.rb +26 -0
  58. data/lib/bundler/fetcher/dependency.rb +88 -0
  59. data/lib/bundler/fetcher/downloader.rb +60 -0
  60. data/lib/bundler/fetcher/index.rb +32 -0
  61. data/lib/bundler/friendly_errors.rb +92 -0
  62. data/lib/bundler/gem_helper.rb +191 -0
  63. data/lib/bundler/gem_helpers.rb +26 -0
  64. data/lib/bundler/gem_installer.rb +9 -0
  65. data/lib/bundler/gem_path_manipulation.rb +8 -0
  66. data/lib/bundler/gem_tasks.rb +5 -0
  67. data/lib/bundler/graph.rb +173 -0
  68. data/lib/bundler/index.rb +199 -0
  69. data/lib/bundler/injector.rb +62 -0
  70. data/lib/bundler/inline.rb +58 -0
  71. data/lib/bundler/installer.rb +242 -0
  72. data/lib/bundler/installer/parallel_installer.rb +122 -0
  73. data/lib/bundler/installer/standalone.rb +48 -0
  74. data/lib/bundler/lazy_specification.rb +82 -0
  75. data/lib/bundler/lockfile_parser.rb +199 -0
  76. data/lib/bundler/match_platform.rb +13 -0
  77. data/lib/bundler/psyched_yaml.rb +26 -0
  78. data/lib/bundler/remote_specification.rb +82 -0
  79. data/lib/bundler/resolver.rb +350 -0
  80. data/lib/bundler/retry.rb +60 -0
  81. data/lib/bundler/ruby_dsl.rb +11 -0
  82. data/lib/bundler/ruby_version.rb +116 -0
  83. data/lib/bundler/rubygems_ext.rb +175 -0
  84. data/lib/bundler/rubygems_integration.rb +674 -0
  85. data/lib/bundler/runtime.rb +285 -0
  86. data/lib/bundler/settings.rb +263 -0
  87. data/lib/bundler/setup.rb +26 -0
  88. data/lib/bundler/shared_helpers.rb +176 -0
  89. data/lib/bundler/similarity_detector.rb +61 -0
  90. data/lib/bundler/source.rb +35 -0
  91. data/lib/bundler/source/git.rb +286 -0
  92. data/lib/bundler/source/git/git_proxy.rb +190 -0
  93. data/lib/bundler/source/path.rb +224 -0
  94. data/lib/bundler/source/path/installer.rb +43 -0
  95. data/lib/bundler/source/rubygems.rb +436 -0
  96. data/lib/bundler/source/rubygems/remote.rb +38 -0
  97. data/lib/bundler/source_list.rb +101 -0
  98. data/lib/bundler/spec_set.rb +156 -0
  99. data/lib/bundler/ssl_certs/.document +1 -0
  100. data/lib/bundler/ssl_certs/AddTrustExternalCARoot-2048.pem +25 -0
  101. data/lib/bundler/ssl_certs/AddTrustExternalCARoot.pem +32 -0
  102. data/lib/bundler/ssl_certs/Class3PublicPrimaryCertificationAuthority.pem +14 -0
  103. data/lib/bundler/ssl_certs/DigiCertHighAssuranceEVRootCA.pem +23 -0
  104. data/lib/bundler/ssl_certs/EntrustnetSecureServerCertificationAuthority.pem +28 -0
  105. data/lib/bundler/ssl_certs/GeoTrustGlobalCA.pem +20 -0
  106. data/lib/bundler/ssl_certs/certificate_manager.rb +64 -0
  107. data/lib/bundler/stub_specification.rb +23 -0
  108. data/lib/bundler/templates/Executable +16 -0
  109. data/lib/bundler/templates/Executable.standalone +12 -0
  110. data/lib/bundler/templates/Gemfile +4 -0
  111. data/lib/bundler/templates/newgem/.travis.yml.tt +4 -0
  112. data/lib/bundler/templates/newgem/CODE_OF_CONDUCT.md.tt +22 -0
  113. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  114. data/lib/bundler/templates/newgem/LICENSE.txt.tt +21 -0
  115. data/lib/bundler/templates/newgem/README.md.tt +41 -0
  116. data/lib/bundler/templates/newgem/Rakefile.tt +29 -0
  117. data/lib/bundler/templates/newgem/bin/console.tt +14 -0
  118. data/lib/bundler/templates/newgem/bin/setup.tt +8 -0
  119. data/lib/bundler/templates/newgem/exe/newgem.tt +3 -0
  120. data/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt +3 -0
  121. data/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt +9 -0
  122. data/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt +6 -0
  123. data/lib/bundler/templates/newgem/gitignore.tt +16 -0
  124. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +12 -0
  125. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  126. data/lib/bundler/templates/newgem/newgem.gemspec.tt +43 -0
  127. data/lib/bundler/templates/newgem/rspec.tt +2 -0
  128. data/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt +11 -0
  129. data/lib/bundler/templates/newgem/spec/spec_helper.rb.tt +2 -0
  130. data/lib/bundler/templates/newgem/test/newgem_test.rb.tt +11 -0
  131. data/lib/bundler/templates/newgem/test/test_helper.rb.tt +4 -0
  132. data/lib/bundler/ui.rb +7 -0
  133. data/lib/bundler/ui/rg_proxy.rb +17 -0
  134. data/lib/bundler/ui/shell.rb +108 -0
  135. data/lib/bundler/ui/silent.rb +44 -0
  136. data/lib/bundler/vendor/molinillo/lib/molinillo.rb +5 -0
  137. data/lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb +277 -0
  138. data/lib/bundler/vendor/molinillo/lib/molinillo/errors.rb +69 -0
  139. data/lib/bundler/vendor/molinillo/lib/molinillo/gem_metadata.rb +3 -0
  140. data/lib/bundler/vendor/molinillo/lib/molinillo/modules/specification_provider.rb +99 -0
  141. data/lib/bundler/vendor/molinillo/lib/molinillo/modules/ui.rb +63 -0
  142. data/lib/bundler/vendor/molinillo/lib/molinillo/resolution.rb +434 -0
  143. data/lib/bundler/vendor/molinillo/lib/molinillo/resolver.rb +43 -0
  144. data/lib/bundler/vendor/molinillo/lib/molinillo/state.rb +51 -0
  145. data/lib/bundler/vendor/net/http/faster.rb +26 -0
  146. data/lib/bundler/vendor/net/http/persistent.rb +1230 -0
  147. data/lib/bundler/vendor/net/http/persistent/ssl_reuse.rb +128 -0
  148. data/lib/bundler/vendor/thor/lib/thor.rb +484 -0
  149. data/lib/bundler/vendor/thor/lib/thor/actions.rb +319 -0
  150. data/lib/bundler/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  151. data/lib/bundler/vendor/thor/lib/thor/actions/create_link.rb +59 -0
  152. data/lib/bundler/vendor/thor/lib/thor/actions/directory.rb +118 -0
  153. data/lib/bundler/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
  154. data/lib/bundler/vendor/thor/lib/thor/actions/file_manipulation.rb +316 -0
  155. data/lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb +107 -0
  156. data/lib/bundler/vendor/thor/lib/thor/base.rb +656 -0
  157. data/lib/bundler/vendor/thor/lib/thor/command.rb +133 -0
  158. data/lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +77 -0
  159. data/lib/bundler/vendor/thor/lib/thor/core_ext/io_binary_read.rb +10 -0
  160. data/lib/bundler/vendor/thor/lib/thor/core_ext/ordered_hash.rb +98 -0
  161. data/lib/bundler/vendor/thor/lib/thor/error.rb +32 -0
  162. data/lib/bundler/vendor/thor/lib/thor/group.rb +281 -0
  163. data/lib/bundler/vendor/thor/lib/thor/invocation.rb +178 -0
  164. data/lib/bundler/vendor/thor/lib/thor/line_editor.rb +17 -0
  165. data/lib/bundler/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
  166. data/lib/bundler/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
  167. data/lib/bundler/vendor/thor/lib/thor/parser.rb +4 -0
  168. data/lib/bundler/vendor/thor/lib/thor/parser/argument.rb +73 -0
  169. data/lib/bundler/vendor/thor/lib/thor/parser/arguments.rb +175 -0
  170. data/lib/bundler/vendor/thor/lib/thor/parser/option.rb +125 -0
  171. data/lib/bundler/vendor/thor/lib/thor/parser/options.rb +218 -0
  172. data/lib/bundler/vendor/thor/lib/thor/rake_compat.rb +71 -0
  173. data/lib/bundler/vendor/thor/lib/thor/runner.rb +322 -0
  174. data/lib/bundler/vendor/thor/lib/thor/shell.rb +81 -0
  175. data/lib/bundler/vendor/thor/lib/thor/shell/basic.rb +421 -0
  176. data/lib/bundler/vendor/thor/lib/thor/shell/color.rb +149 -0
  177. data/lib/bundler/vendor/thor/lib/thor/shell/html.rb +126 -0
  178. data/lib/bundler/vendor/thor/lib/thor/util.rb +267 -0
  179. data/lib/bundler/vendor/thor/lib/thor/version.rb +3 -0
  180. data/lib/bundler/vendored_molinillo.rb +2 -0
  181. data/lib/bundler/vendored_persistent.rb +11 -0
  182. data/lib/bundler/vendored_thor.rb +3 -0
  183. data/lib/bundler/version.rb +6 -0
  184. data/lib/bundler/vlad.rb +11 -0
  185. data/lib/bundler/worker.rb +72 -0
  186. data/man/bundle-config.ronn +187 -0
  187. data/man/bundle-exec.ronn +136 -0
  188. data/man/bundle-gem.ronn +77 -0
  189. data/man/bundle-install.ronn +398 -0
  190. data/man/bundle-package.ronn +66 -0
  191. data/man/bundle-platform.ronn +42 -0
  192. data/man/bundle-update.ronn +188 -0
  193. data/man/bundle.ronn +98 -0
  194. data/man/gemfile.5.ronn +495 -0
  195. data/man/index.txt +8 -0
  196. metadata +346 -0
@@ -0,0 +1,107 @@
1
+ require "bundler/vendor/thor/lib/thor/actions/empty_directory"
2
+
3
+ class Bundler::Thor
4
+ module Actions
5
+ # Injects the given content into a file. Different from gsub_file, this
6
+ # method is reversible.
7
+ #
8
+ # ==== Parameters
9
+ # destination<String>:: Relative path to the destination root
10
+ # data<String>:: Data to add to the file. Can be given as a block.
11
+ # config<Hash>:: give :verbose => false to not log the status and the flag
12
+ # for injection (:after or :before) or :force => true for
13
+ # insert two or more times the same content.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # insert_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n"
18
+ #
19
+ # insert_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do
20
+ # gems = ask "Which gems would you like to add?"
21
+ # gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
22
+ # end
23
+ #
24
+ def insert_into_file(destination, *args, &block)
25
+ if block_given?
26
+ data, config = block, args.shift
27
+ else
28
+ data, config = args.shift, args.shift
29
+ end
30
+ action InjectIntoFile.new(self, destination, data, config)
31
+ end
32
+ alias_method :inject_into_file, :insert_into_file
33
+
34
+ class InjectIntoFile < EmptyDirectory #:nodoc:
35
+ attr_reader :replacement, :flag, :behavior
36
+
37
+ def initialize(base, destination, data, config)
38
+ super(base, destination, {:verbose => true}.merge(config))
39
+
40
+ @behavior, @flag = if @config.key?(:after)
41
+ [:after, @config.delete(:after)]
42
+ else
43
+ [:before, @config.delete(:before)]
44
+ end
45
+
46
+ @replacement = data.is_a?(Proc) ? data.call : data
47
+ @flag = Regexp.escape(@flag) unless @flag.is_a?(Regexp)
48
+ end
49
+
50
+ def invoke!
51
+ say_status :invoke
52
+
53
+ content = if @behavior == :after
54
+ '\0' + replacement
55
+ else
56
+ replacement + '\0'
57
+ end
58
+
59
+ replace!(/#{flag}/, content, config[:force])
60
+ end
61
+
62
+ def revoke!
63
+ say_status :revoke
64
+
65
+ regexp = if @behavior == :after
66
+ content = '\1\2'
67
+ /(#{flag})(.*)(#{Regexp.escape(replacement)})/m
68
+ else
69
+ content = '\2\3'
70
+ /(#{Regexp.escape(replacement)})(.*)(#{flag})/m
71
+ end
72
+
73
+ replace!(regexp, content, true)
74
+ end
75
+
76
+ protected
77
+
78
+ def say_status(behavior)
79
+ status = if behavior == :invoke
80
+ if flag == /\A/
81
+ :prepend
82
+ elsif flag == /\z/
83
+ :append
84
+ else
85
+ :insert
86
+ end
87
+ else
88
+ :subtract
89
+ end
90
+
91
+ super(status, config[:verbose])
92
+ end
93
+
94
+ # Adds the content to the file.
95
+ #
96
+ def replace!(regexp, string, force)
97
+ unless base.options[:pretend]
98
+ content = File.binread(destination)
99
+ if force || !content.include?(replacement)
100
+ content.gsub!(regexp, string)
101
+ File.open(destination, "wb") { |file| file.write(content) }
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,656 @@
1
+ require "bundler/vendor/thor/lib/thor/command"
2
+ require "bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access"
3
+ require "bundler/vendor/thor/lib/thor/core_ext/ordered_hash"
4
+ require "bundler/vendor/thor/lib/thor/error"
5
+ require "bundler/vendor/thor/lib/thor/invocation"
6
+ require "bundler/vendor/thor/lib/thor/parser"
7
+ require "bundler/vendor/thor/lib/thor/shell"
8
+ require "bundler/vendor/thor/lib/thor/line_editor"
9
+ require "bundler/vendor/thor/lib/thor/util"
10
+
11
+ class Bundler::Thor
12
+ autoload :Actions, "bundler/vendor/thor/lib/thor/actions"
13
+ autoload :RakeCompat, "bundler/vendor/thor/lib/thor/rake_compat"
14
+ autoload :Group, "bundler/vendor/thor/lib/thor/group"
15
+
16
+ # Shortcuts for help.
17
+ HELP_MAPPINGS = %w[-h -? --help -D]
18
+
19
+ # Bundler::Thor methods that should not be overwritten by the user.
20
+ THOR_RESERVED_WORDS = %w[invoke shell options behavior root destination_root relative_root
21
+ action add_file create_file in_root inside run run_ruby_script]
22
+
23
+ TEMPLATE_EXTNAME = ".tt"
24
+
25
+ module Base
26
+ attr_accessor :options, :parent_options, :args
27
+
28
+ # It receives arguments in an Array and two hashes, one for options and
29
+ # other for configuration.
30
+ #
31
+ # Notice that it does not check if all required arguments were supplied.
32
+ # It should be done by the parser.
33
+ #
34
+ # ==== Parameters
35
+ # args<Array[Object]>:: An array of objects. The objects are applied to their
36
+ # respective accessors declared with <tt>argument</tt>.
37
+ #
38
+ # options<Hash>:: An options hash that will be available as self.options.
39
+ # The hash given is converted to a hash with indifferent
40
+ # access, magic predicates (options.skip?) and then frozen.
41
+ #
42
+ # config<Hash>:: Configuration for this Bundler::Thor class.
43
+ #
44
+ def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength
45
+ parse_options = self.class.class_options
46
+
47
+ # The start method splits inbound arguments at the first argument
48
+ # that looks like an option (starts with - or --). It then calls
49
+ # new, passing in the two halves of the arguments Array as the
50
+ # first two parameters.
51
+
52
+ command_options = config.delete(:command_options) # hook for start
53
+ parse_options = parse_options.merge(command_options) if command_options
54
+ if local_options.is_a?(Array)
55
+ array_options, hash_options = local_options, {}
56
+ else
57
+ # Handle the case where the class was explicitly instantiated
58
+ # with pre-parsed options.
59
+ array_options, hash_options = [], local_options
60
+ end
61
+
62
+ # Let Bundler::Thor::Options parse the options first, so it can remove
63
+ # declared options from the array. This will leave us with
64
+ # a list of arguments that weren't declared.
65
+ stop_on_unknown = self.class.stop_on_unknown_option? config[:current_command]
66
+ opts = Bundler::Thor::Options.new(parse_options, hash_options, stop_on_unknown)
67
+ self.options = opts.parse(array_options)
68
+ self.options = config[:class_options].merge(options) if config[:class_options]
69
+
70
+ # If unknown options are disallowed, make sure that none of the
71
+ # remaining arguments looks like an option.
72
+ opts.check_unknown! if self.class.check_unknown_options?(config)
73
+
74
+ # Add the remaining arguments from the options parser to the
75
+ # arguments passed in to initialize. Then remove any positional
76
+ # arguments declared using #argument (this is primarily used
77
+ # by Bundler::Thor::Group). Tis will leave us with the remaining
78
+ # positional arguments.
79
+ to_parse = args
80
+ to_parse += opts.remaining unless self.class.strict_args_position?(config)
81
+
82
+ thor_args = Bundler::Thor::Arguments.new(self.class.arguments)
83
+ thor_args.parse(to_parse).each { |k, v| __send__("#{k}=", v) }
84
+ @args = thor_args.remaining
85
+ end
86
+
87
+ class << self
88
+ def included(base) #:nodoc:
89
+ base.extend ClassMethods
90
+ base.send :include, Invocation
91
+ base.send :include, Shell
92
+ end
93
+
94
+ # Returns the classes that inherits from Bundler::Thor or Bundler::Thor::Group.
95
+ #
96
+ # ==== Returns
97
+ # Array[Class]
98
+ #
99
+ def subclasses
100
+ @subclasses ||= []
101
+ end
102
+
103
+ # Returns the files where the subclasses are kept.
104
+ #
105
+ # ==== Returns
106
+ # Hash[path<String> => Class]
107
+ #
108
+ def subclass_files
109
+ @subclass_files ||= Hash.new { |h, k| h[k] = [] }
110
+ end
111
+
112
+ # Whenever a class inherits from Bundler::Thor or Bundler::Thor::Group, we should track the
113
+ # class and the file on Bundler::Thor::Base. This is the method responsable for it.
114
+ #
115
+ def register_klass_file(klass) #:nodoc:
116
+ file = caller[1].match(/(.*):\d+/)[1]
117
+ Bundler::Thor::Base.subclasses << klass unless Bundler::Thor::Base.subclasses.include?(klass)
118
+
119
+ file_subclasses = Bundler::Thor::Base.subclass_files[File.expand_path(file)]
120
+ file_subclasses << klass unless file_subclasses.include?(klass)
121
+ end
122
+ end
123
+
124
+ module ClassMethods
125
+ def attr_reader(*) #:nodoc:
126
+ no_commands { super }
127
+ end
128
+
129
+ def attr_writer(*) #:nodoc:
130
+ no_commands { super }
131
+ end
132
+
133
+ def attr_accessor(*) #:nodoc:
134
+ no_commands { super }
135
+ end
136
+
137
+ # If you want to raise an error for unknown options, call check_unknown_options!
138
+ # This is disabled by default to allow dynamic invocations.
139
+ def check_unknown_options!
140
+ @check_unknown_options = true
141
+ end
142
+
143
+ def check_unknown_options #:nodoc:
144
+ @check_unknown_options ||= from_superclass(:check_unknown_options, false)
145
+ end
146
+
147
+ def check_unknown_options?(config) #:nodoc:
148
+ !!check_unknown_options
149
+ end
150
+
151
+ # If true, option parsing is suspended as soon as an unknown option or a
152
+ # regular argument is encountered. All remaining arguments are passed to
153
+ # the command as regular arguments.
154
+ def stop_on_unknown_option?(command_name) #:nodoc:
155
+ false
156
+ end
157
+
158
+ # If you want only strict string args (useful when cascading thor classes),
159
+ # call strict_args_position! This is disabled by default to allow dynamic
160
+ # invocations.
161
+ def strict_args_position!
162
+ @strict_args_position = true
163
+ end
164
+
165
+ def strict_args_position #:nodoc:
166
+ @strict_args_position ||= from_superclass(:strict_args_position, false)
167
+ end
168
+
169
+ def strict_args_position?(config) #:nodoc:
170
+ !!strict_args_position
171
+ end
172
+
173
+ # Adds an argument to the class and creates an attr_accessor for it.
174
+ #
175
+ # Arguments are different from options in several aspects. The first one
176
+ # is how they are parsed from the command line, arguments are retrieved
177
+ # from position:
178
+ #
179
+ # thor command NAME
180
+ #
181
+ # Instead of:
182
+ #
183
+ # thor command --name=NAME
184
+ #
185
+ # Besides, arguments are used inside your code as an accessor (self.argument),
186
+ # while options are all kept in a hash (self.options).
187
+ #
188
+ # Finally, arguments cannot have type :default or :boolean but can be
189
+ # optional (supplying :optional => :true or :required => false), although
190
+ # you cannot have a required argument after a non-required argument. If you
191
+ # try it, an error is raised.
192
+ #
193
+ # ==== Parameters
194
+ # name<Symbol>:: The name of the argument.
195
+ # options<Hash>:: Described below.
196
+ #
197
+ # ==== Options
198
+ # :desc - Description for the argument.
199
+ # :required - If the argument is required or not.
200
+ # :optional - If the argument is optional or not.
201
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric.
202
+ # :default - Default value for this argument. It cannot be required and have default values.
203
+ # :banner - String to show on usage notes.
204
+ #
205
+ # ==== Errors
206
+ # ArgumentError:: Raised if you supply a required argument after a non required one.
207
+ #
208
+ def argument(name, options = {}) # rubocop:disable MethodLength
209
+ is_thor_reserved_word?(name, :argument)
210
+ no_commands { attr_accessor name }
211
+
212
+ required = if options.key?(:optional)
213
+ !options[:optional]
214
+ elsif options.key?(:required)
215
+ options[:required]
216
+ else
217
+ options[:default].nil?
218
+ end
219
+
220
+ remove_argument name
221
+
222
+ arguments.each do |argument|
223
+ next if argument.required?
224
+ fail ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
225
+ "the non-required argument #{argument.human_name.inspect}."
226
+ end if required
227
+
228
+ options[:required] = required
229
+
230
+ arguments << Bundler::Thor::Argument.new(name, options)
231
+ end
232
+
233
+ # Returns this class arguments, looking up in the ancestors chain.
234
+ #
235
+ # ==== Returns
236
+ # Array[Bundler::Thor::Argument]
237
+ #
238
+ def arguments
239
+ @arguments ||= from_superclass(:arguments, [])
240
+ end
241
+
242
+ # Adds a bunch of options to the set of class options.
243
+ #
244
+ # class_options :foo => false, :bar => :required, :baz => :string
245
+ #
246
+ # If you prefer more detailed declaration, check class_option.
247
+ #
248
+ # ==== Parameters
249
+ # Hash[Symbol => Object]
250
+ #
251
+ def class_options(options = nil)
252
+ @class_options ||= from_superclass(:class_options, {})
253
+ build_options(options, @class_options) if options
254
+ @class_options
255
+ end
256
+
257
+ # Adds an option to the set of class options
258
+ #
259
+ # ==== Parameters
260
+ # name<Symbol>:: The name of the argument.
261
+ # options<Hash>:: Described below.
262
+ #
263
+ # ==== Options
264
+ # :desc:: -- Description for the argument.
265
+ # :required:: -- If the argument is required or not.
266
+ # :default:: -- Default value for this argument.
267
+ # :group:: -- The group for this options. Use by class options to output options in different levels.
268
+ # :aliases:: -- Aliases for this option. <b>Note:</b> Bundler::Thor follows a convention of one-dash-one-letter options. Thus aliases like "-something" wouldn't be parsed; use either "\--something" or "-s" instead.
269
+ # :type:: -- The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
270
+ # :banner:: -- String to show on usage notes.
271
+ # :hide:: -- If you want to hide this option from the help.
272
+ #
273
+ def class_option(name, options = {})
274
+ build_option(name, options, class_options)
275
+ end
276
+
277
+ # Removes a previous defined argument. If :undefine is given, undefine
278
+ # accessors as well.
279
+ #
280
+ # ==== Parameters
281
+ # names<Array>:: Arguments to be removed
282
+ #
283
+ # ==== Examples
284
+ #
285
+ # remove_argument :foo
286
+ # remove_argument :foo, :bar, :baz, :undefine => true
287
+ #
288
+ def remove_argument(*names)
289
+ options = names.last.is_a?(Hash) ? names.pop : {}
290
+
291
+ names.each do |name|
292
+ arguments.delete_if { |a| a.name == name.to_s }
293
+ undef_method name, "#{name}=" if options[:undefine]
294
+ end
295
+ end
296
+
297
+ # Removes a previous defined class option.
298
+ #
299
+ # ==== Parameters
300
+ # names<Array>:: Class options to be removed
301
+ #
302
+ # ==== Examples
303
+ #
304
+ # remove_class_option :foo
305
+ # remove_class_option :foo, :bar, :baz
306
+ #
307
+ def remove_class_option(*names)
308
+ names.each do |name|
309
+ class_options.delete(name)
310
+ end
311
+ end
312
+
313
+ # Defines the group. This is used when thor list is invoked so you can specify
314
+ # that only commands from a pre-defined group will be shown. Defaults to standard.
315
+ #
316
+ # ==== Parameters
317
+ # name<String|Symbol>
318
+ #
319
+ def group(name = nil)
320
+ if name
321
+ @group = name.to_s
322
+ else
323
+ @group ||= from_superclass(:group, "standard")
324
+ end
325
+ end
326
+
327
+ # Returns the commands for this Bundler::Thor class.
328
+ #
329
+ # ==== Returns
330
+ # OrderedHash:: An ordered hash with commands names as keys and Bundler::Thor::Command
331
+ # objects as values.
332
+ #
333
+ def commands
334
+ @commands ||= Bundler::Thor::CoreExt::OrderedHash.new
335
+ end
336
+ alias_method :tasks, :commands
337
+
338
+ # Returns the commands for this Bundler::Thor class and all subclasses.
339
+ #
340
+ # ==== Returns
341
+ # OrderedHash:: An ordered hash with commands names as keys and Bundler::Thor::Command
342
+ # objects as values.
343
+ #
344
+ def all_commands
345
+ @all_commands ||= from_superclass(:all_commands, Bundler::Thor::CoreExt::OrderedHash.new)
346
+ @all_commands.merge(commands)
347
+ end
348
+ alias_method :all_tasks, :all_commands
349
+
350
+ # Removes a given command from this Bundler::Thor class. This is usually done if you
351
+ # are inheriting from another class and don't want it to be available
352
+ # anymore.
353
+ #
354
+ # By default it only remove the mapping to the command. But you can supply
355
+ # :undefine => true to undefine the method from the class as well.
356
+ #
357
+ # ==== Parameters
358
+ # name<Symbol|String>:: The name of the command to be removed
359
+ # options<Hash>:: You can give :undefine => true if you want commands the method
360
+ # to be undefined from the class as well.
361
+ #
362
+ def remove_command(*names)
363
+ options = names.last.is_a?(Hash) ? names.pop : {}
364
+
365
+ names.each do |name|
366
+ commands.delete(name.to_s)
367
+ all_commands.delete(name.to_s)
368
+ undef_method name if options[:undefine]
369
+ end
370
+ end
371
+ alias_method :remove_task, :remove_command
372
+
373
+ # All methods defined inside the given block are not added as commands.
374
+ #
375
+ # So you can do:
376
+ #
377
+ # class MyScript < Bundler::Thor
378
+ # no_commands do
379
+ # def this_is_not_a_command
380
+ # end
381
+ # end
382
+ # end
383
+ #
384
+ # You can also add the method and remove it from the command list:
385
+ #
386
+ # class MyScript < Bundler::Thor
387
+ # def this_is_not_a_command
388
+ # end
389
+ # remove_command :this_is_not_a_command
390
+ # end
391
+ #
392
+ def no_commands
393
+ @no_commands = true
394
+ yield
395
+ ensure
396
+ @no_commands = false
397
+ end
398
+ alias_method :no_tasks, :no_commands
399
+
400
+ # Sets the namespace for the Bundler::Thor or Bundler::Thor::Group class. By default the
401
+ # namespace is retrieved from the class name. If your Bundler::Thor class is named
402
+ # Scripts::MyScript, the help method, for example, will be called as:
403
+ #
404
+ # thor scripts:my_script -h
405
+ #
406
+ # If you change the namespace:
407
+ #
408
+ # namespace :my_scripts
409
+ #
410
+ # You change how your commands are invoked:
411
+ #
412
+ # thor my_scripts -h
413
+ #
414
+ # Finally, if you change your namespace to default:
415
+ #
416
+ # namespace :default
417
+ #
418
+ # Your commands can be invoked with a shortcut. Instead of:
419
+ #
420
+ # thor :my_command
421
+ #
422
+ def namespace(name = nil)
423
+ if name
424
+ @namespace = name.to_s
425
+ else
426
+ @namespace ||= Bundler::Thor::Util.namespace_from_thor_class(self)
427
+ end
428
+ end
429
+
430
+ # Parses the command and options from the given args, instantiate the class
431
+ # and invoke the command. This method is used when the arguments must be parsed
432
+ # from an array. If you are inside Ruby and want to use a Bundler::Thor class, you
433
+ # can simply initialize it:
434
+ #
435
+ # script = MyScript.new(args, options, config)
436
+ # script.invoke(:command, first_arg, second_arg, third_arg)
437
+ #
438
+ def start(given_args = ARGV, config = {})
439
+ config[:shell] ||= Bundler::Thor::Base.shell.new
440
+ dispatch(nil, given_args.dup, nil, config)
441
+ rescue Bundler::Thor::Error => e
442
+ config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
443
+ exit(1) if exit_on_failure?
444
+ rescue Errno::EPIPE
445
+ # This happens if a thor command is piped to something like `head`,
446
+ # which closes the pipe when it's done reading. This will also
447
+ # mean that if the pipe is closed, further unnecessary
448
+ # computation will not occur.
449
+ exit(0)
450
+ end
451
+
452
+ # Allows to use private methods from parent in child classes as commands.
453
+ #
454
+ # ==== Parameters
455
+ # names<Array>:: Method names to be used as commands
456
+ #
457
+ # ==== Examples
458
+ #
459
+ # public_command :foo
460
+ # public_command :foo, :bar, :baz
461
+ #
462
+ def public_command(*names)
463
+ names.each do |name|
464
+ class_eval "def #{name}(*); super end"
465
+ end
466
+ end
467
+ alias_method :public_task, :public_command
468
+
469
+ def handle_no_command_error(command, has_namespace = $thor_runner) #:nodoc:
470
+ if has_namespace
471
+ fail UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace."
472
+ else
473
+ fail UndefinedCommandError, "Could not find command #{command.inspect}."
474
+ end
475
+ end
476
+ alias_method :handle_no_task_error, :handle_no_command_error
477
+
478
+ def handle_argument_error(command, error, args, arity) #:nodoc:
479
+ msg = "ERROR: \"#{basename} #{command.name}\" was called with "
480
+ msg << "no arguments" if args.empty?
481
+ msg << "arguments " << args.inspect unless args.empty?
482
+ msg << "\nUsage: #{banner(command).inspect}"
483
+ fail InvocationError, msg
484
+ end
485
+
486
+ protected
487
+
488
+ # Prints the class options per group. If an option does not belong to
489
+ # any group, it's printed as Class option.
490
+ #
491
+ def class_options_help(shell, groups = {}) #:nodoc:
492
+ # Group options by group
493
+ class_options.each do |_, value|
494
+ groups[value.group] ||= []
495
+ groups[value.group] << value
496
+ end
497
+
498
+ # Deal with default group
499
+ global_options = groups.delete(nil) || []
500
+ print_options(shell, global_options)
501
+
502
+ # Print all others
503
+ groups.each do |group_name, options|
504
+ print_options(shell, options, group_name)
505
+ end
506
+ end
507
+
508
+ # Receives a set of options and print them.
509
+ def print_options(shell, options, group_name = nil)
510
+ return if options.empty?
511
+
512
+ list = []
513
+ padding = options.map { |o| o.aliases.size }.max.to_i * 4
514
+
515
+ options.each do |option|
516
+ unless option.hide
517
+ item = [option.usage(padding)]
518
+ item.push(option.description ? "# #{option.description}" : "")
519
+
520
+ list << item
521
+ list << ["", "# Default: #{option.default}"] if option.show_default?
522
+ list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
523
+ end
524
+ end
525
+
526
+ shell.say(group_name ? "#{group_name} options:" : "Options:")
527
+ shell.print_table(list, :indent => 2)
528
+ shell.say ""
529
+ end
530
+
531
+ # Raises an error if the word given is a Bundler::Thor reserved word.
532
+ def is_thor_reserved_word?(word, type) #:nodoc:
533
+ return false unless THOR_RESERVED_WORDS.include?(word.to_s)
534
+ fail "#{word.inspect} is a Bundler::Thor reserved word and cannot be defined as #{type}"
535
+ end
536
+
537
+ # Build an option and adds it to the given scope.
538
+ #
539
+ # ==== Parameters
540
+ # name<Symbol>:: The name of the argument.
541
+ # options<Hash>:: Described in both class_option and method_option.
542
+ # scope<Hash>:: Options hash that is being built up
543
+ def build_option(name, options, scope) #:nodoc:
544
+ scope[name] = Bundler::Thor::Option.new(name, options)
545
+ end
546
+
547
+ # Receives a hash of options, parse them and add to the scope. This is a
548
+ # fast way to set a bunch of options:
549
+ #
550
+ # build_options :foo => true, :bar => :required, :baz => :string
551
+ #
552
+ # ==== Parameters
553
+ # Hash[Symbol => Object]
554
+ def build_options(options, scope) #:nodoc:
555
+ options.each do |key, value|
556
+ scope[key] = Bundler::Thor::Option.parse(key, value)
557
+ end
558
+ end
559
+
560
+ # Finds a command with the given name. If the command belongs to the current
561
+ # class, just return it, otherwise dup it and add the fresh copy to the
562
+ # current command hash.
563
+ def find_and_refresh_command(name) #:nodoc:
564
+ if commands[name.to_s]
565
+ commands[name.to_s]
566
+ elsif command = all_commands[name.to_s] # rubocop:disable AssignmentInCondition
567
+ commands[name.to_s] = command.clone
568
+ else
569
+ fail ArgumentError, "You supplied :for => #{name.inspect}, but the command #{name.inspect} could not be found."
570
+ end
571
+ end
572
+ alias_method :find_and_refresh_task, :find_and_refresh_command
573
+
574
+ # Everytime someone inherits from a Bundler::Thor class, register the klass
575
+ # and file into baseclass.
576
+ def inherited(klass)
577
+ Bundler::Thor::Base.register_klass_file(klass)
578
+ klass.instance_variable_set(:@no_commands, false)
579
+ end
580
+
581
+ # Fire this callback whenever a method is added. Added methods are
582
+ # tracked as commands by invoking the create_command method.
583
+ def method_added(meth)
584
+ meth = meth.to_s
585
+
586
+ if meth == "initialize"
587
+ initialize_added
588
+ return
589
+ end
590
+
591
+ # Return if it's not a public instance method
592
+ return unless public_method_defined?(meth.to_sym)
593
+
594
+ @no_commands ||= false
595
+ return if @no_commands || !create_command(meth)
596
+
597
+ is_thor_reserved_word?(meth, :command)
598
+ Bundler::Thor::Base.register_klass_file(self)
599
+ end
600
+
601
+ # Retrieves a value from superclass. If it reaches the baseclass,
602
+ # returns default.
603
+ def from_superclass(method, default = nil)
604
+ if self == baseclass || !superclass.respond_to?(method, true)
605
+ default
606
+ else
607
+ value = superclass.send(method)
608
+
609
+ # Ruby implements `dup` on Object, but raises a `TypeError`
610
+ # if the method is called on immediates. As a result, we
611
+ # don't have a good way to check whether dup will succeed
612
+ # without calling it and rescuing the TypeError.
613
+ begin
614
+ value.dup
615
+ rescue TypeError
616
+ value
617
+ end
618
+
619
+ end
620
+ end
621
+
622
+ # A flag that makes the process exit with status 1 if any error happens.
623
+ def exit_on_failure?
624
+ false
625
+ end
626
+
627
+ #
628
+ # The basename of the program invoking the thor class.
629
+ #
630
+ def basename
631
+ File.basename($PROGRAM_NAME).split(" ").first
632
+ end
633
+
634
+ # SIGNATURE: Sets the baseclass. This is where the superclass lookup
635
+ # finishes.
636
+ def baseclass #:nodoc:
637
+ end
638
+
639
+ # SIGNATURE: Creates a new command if valid_command? is true. This method is
640
+ # called when a new method is added to the class.
641
+ def create_command(meth) #:nodoc:
642
+ end
643
+ alias_method :create_task, :create_command
644
+
645
+ # SIGNATURE: Defines behavior when the initialize method is added to the
646
+ # class.
647
+ def initialize_added #:nodoc:
648
+ end
649
+
650
+ # SIGNATURE: The hook invoked by start.
651
+ def dispatch(command, given_args, given_opts, config) #:nodoc:
652
+ fail NotImplementedError
653
+ end
654
+ end
655
+ end
656
+ end