bundler-prehistoric 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (284) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +100 -0
  5. data/CHANGELOG.md +1640 -0
  6. data/CONTRIBUTING.md +32 -0
  7. data/DEVELOPMENT.md +117 -0
  8. data/Gemfile +3 -0
  9. data/ISSUES.md +96 -0
  10. data/LICENSE.md +23 -0
  11. data/README.md +46 -0
  12. data/Rakefile +256 -0
  13. data/UPGRADING.md +103 -0
  14. data/bin/bundle +21 -0
  15. data/bin/bundle_ruby +56 -0
  16. data/bin/bundler +21 -0
  17. data/bundler.gemspec +33 -0
  18. data/lib/bundler.rb +442 -0
  19. data/lib/bundler/backports/time.rb +7 -0
  20. data/lib/bundler/capistrano.rb +16 -0
  21. data/lib/bundler/cli.rb +395 -0
  22. data/lib/bundler/cli/binstubs.rb +38 -0
  23. data/lib/bundler/cli/cache.rb +34 -0
  24. data/lib/bundler/cli/check.rb +35 -0
  25. data/lib/bundler/cli/clean.rb +19 -0
  26. data/lib/bundler/cli/common.rb +54 -0
  27. data/lib/bundler/cli/config.rb +84 -0
  28. data/lib/bundler/cli/console.rb +39 -0
  29. data/lib/bundler/cli/exec.rb +37 -0
  30. data/lib/bundler/cli/gem.rb +101 -0
  31. data/lib/bundler/cli/init.rb +33 -0
  32. data/lib/bundler/cli/inject.rb +33 -0
  33. data/lib/bundler/cli/install.rb +133 -0
  34. data/lib/bundler/cli/open.rb +23 -0
  35. data/lib/bundler/cli/outdated.rb +80 -0
  36. data/lib/bundler/cli/package.rb +36 -0
  37. data/lib/bundler/cli/platform.rb +43 -0
  38. data/lib/bundler/cli/show.rb +48 -0
  39. data/lib/bundler/cli/update.rb +73 -0
  40. data/lib/bundler/cli/viz.rb +27 -0
  41. data/lib/bundler/constants.rb +5 -0
  42. data/lib/bundler/current_ruby.rb +155 -0
  43. data/lib/bundler/definition.rb +604 -0
  44. data/lib/bundler/dep_proxy.rb +43 -0
  45. data/lib/bundler/dependency.rb +106 -0
  46. data/lib/bundler/deployment.rb +59 -0
  47. data/lib/bundler/deprecate.rb +15 -0
  48. data/lib/bundler/dsl.rb +305 -0
  49. data/lib/bundler/endpoint_specification.rb +76 -0
  50. data/lib/bundler/env.rb +56 -0
  51. data/lib/bundler/environment.rb +42 -0
  52. data/lib/bundler/fetcher.rb +396 -0
  53. data/lib/bundler/friendly_errors.rb +42 -0
  54. data/lib/bundler/gem_helper.rb +169 -0
  55. data/lib/bundler/gem_helpers.rb +25 -0
  56. data/lib/bundler/gem_installer.rb +9 -0
  57. data/lib/bundler/gem_path_manipulation.rb +8 -0
  58. data/lib/bundler/gem_tasks.rb +2 -0
  59. data/lib/bundler/graph.rb +169 -0
  60. data/lib/bundler/index.rb +184 -0
  61. data/lib/bundler/injector.rb +64 -0
  62. data/lib/bundler/installer.rb +332 -0
  63. data/lib/bundler/lazy_specification.rb +83 -0
  64. data/lib/bundler/lockfile_parser.rb +146 -0
  65. data/lib/bundler/match_platform.rb +13 -0
  66. data/lib/bundler/parallel_workers.rb +18 -0
  67. data/lib/bundler/parallel_workers/thread_worker.rb +30 -0
  68. data/lib/bundler/parallel_workers/unix_worker.rb +101 -0
  69. data/lib/bundler/parallel_workers/worker.rb +69 -0
  70. data/lib/bundler/psyched_yaml.rb +26 -0
  71. data/lib/bundler/remote_specification.rb +57 -0
  72. data/lib/bundler/resolver.rb +534 -0
  73. data/lib/bundler/retry.rb +60 -0
  74. data/lib/bundler/ruby_dsl.rb +11 -0
  75. data/lib/bundler/ruby_version.rb +117 -0
  76. data/lib/bundler/rubygems_ext.rb +171 -0
  77. data/lib/bundler/rubygems_integration.rb +578 -0
  78. data/lib/bundler/runtime.rb +310 -0
  79. data/lib/bundler/settings.rb +176 -0
  80. data/lib/bundler/setup.rb +23 -0
  81. data/lib/bundler/shared_helpers.rb +110 -0
  82. data/lib/bundler/similarity_detector.rb +63 -0
  83. data/lib/bundler/source.rb +29 -0
  84. data/lib/bundler/source/git.rb +289 -0
  85. data/lib/bundler/source/git/git_proxy.rb +158 -0
  86. data/lib/bundler/source/path.rb +225 -0
  87. data/lib/bundler/source/path/installer.rb +38 -0
  88. data/lib/bundler/source/rubygems.rb +334 -0
  89. data/lib/bundler/source/svn.rb +260 -0
  90. data/lib/bundler/source/svn/svn_proxy.rb +110 -0
  91. data/lib/bundler/spec_set.rb +154 -0
  92. data/lib/bundler/ssl_certs/.document +1 -0
  93. data/lib/bundler/ssl_certs/Class3PublicPrimaryCertificationAuthority.pem +14 -0
  94. data/lib/bundler/ssl_certs/DigiCertHighAssuranceEVRootCA.pem +23 -0
  95. data/lib/bundler/ssl_certs/EntrustnetSecureServerCertificationAuthority.pem +28 -0
  96. data/lib/bundler/ssl_certs/GeoTrustGlobalCA.pem +20 -0
  97. data/lib/bundler/ssl_certs/certificate_manager.rb +41 -0
  98. data/lib/bundler/templates/Executable +16 -0
  99. data/lib/bundler/templates/Executable.standalone +12 -0
  100. data/lib/bundler/templates/Gemfile +4 -0
  101. data/lib/bundler/templates/newgem/.travis.yml.tt +3 -0
  102. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  103. data/lib/bundler/templates/newgem/LICENSE.txt.tt +22 -0
  104. data/lib/bundler/templates/newgem/README.md.tt +31 -0
  105. data/lib/bundler/templates/newgem/Rakefile.tt +26 -0
  106. data/lib/bundler/templates/newgem/bin/newgem.tt +3 -0
  107. data/lib/bundler/templates/newgem/consolerc.tt +3 -0
  108. data/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt +3 -0
  109. data/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt +9 -0
  110. data/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt +6 -0
  111. data/lib/bundler/templates/newgem/gitignore.tt +16 -0
  112. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +12 -0
  113. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  114. data/lib/bundler/templates/newgem/newgem.gemspec.tt +38 -0
  115. data/lib/bundler/templates/newgem/rspec.tt +2 -0
  116. data/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt +11 -0
  117. data/lib/bundler/templates/newgem/spec/spec_helper.rb.tt +2 -0
  118. data/lib/bundler/templates/newgem/test/minitest_helper.rb.tt +4 -0
  119. data/lib/bundler/templates/newgem/test/test_newgem.rb.tt +11 -0
  120. data/lib/bundler/ui.rb +7 -0
  121. data/lib/bundler/ui/rg_proxy.rb +21 -0
  122. data/lib/bundler/ui/shell.rb +98 -0
  123. data/lib/bundler/ui/silent.rb +44 -0
  124. data/lib/bundler/vendor/.document +0 -0
  125. data/lib/bundler/vendor/net/http/faster.rb +26 -0
  126. data/lib/bundler/vendor/net/http/persistent.rb +1230 -0
  127. data/lib/bundler/vendor/net/http/persistent/ssl_reuse.rb +128 -0
  128. data/lib/bundler/vendor/thor.rb +480 -0
  129. data/lib/bundler/vendor/thor/actions.rb +319 -0
  130. data/lib/bundler/vendor/thor/actions/create_file.rb +103 -0
  131. data/lib/bundler/vendor/thor/actions/create_link.rb +59 -0
  132. data/lib/bundler/vendor/thor/actions/directory.rb +118 -0
  133. data/lib/bundler/vendor/thor/actions/empty_directory.rb +135 -0
  134. data/lib/bundler/vendor/thor/actions/file_manipulation.rb +316 -0
  135. data/lib/bundler/vendor/thor/actions/inject_into_file.rb +107 -0
  136. data/lib/bundler/vendor/thor/base.rb +656 -0
  137. data/lib/bundler/vendor/thor/command.rb +133 -0
  138. data/lib/bundler/vendor/thor/core_ext/hash_with_indifferent_access.rb +77 -0
  139. data/lib/bundler/vendor/thor/core_ext/io_binary_read.rb +10 -0
  140. data/lib/bundler/vendor/thor/core_ext/ordered_hash.rb +98 -0
  141. data/lib/bundler/vendor/thor/error.rb +32 -0
  142. data/lib/bundler/vendor/thor/group.rb +281 -0
  143. data/lib/bundler/vendor/thor/invocation.rb +173 -0
  144. data/lib/bundler/vendor/thor/line_editor.rb +17 -0
  145. data/lib/bundler/vendor/thor/line_editor/basic.rb +35 -0
  146. data/lib/bundler/vendor/thor/line_editor/readline.rb +88 -0
  147. data/lib/bundler/vendor/thor/parser.rb +4 -0
  148. data/lib/bundler/vendor/thor/parser/argument.rb +73 -0
  149. data/lib/bundler/vendor/thor/parser/arguments.rb +175 -0
  150. data/lib/bundler/vendor/thor/parser/option.rb +121 -0
  151. data/lib/bundler/vendor/thor/parser/options.rb +218 -0
  152. data/lib/bundler/vendor/thor/rake_compat.rb +71 -0
  153. data/lib/bundler/vendor/thor/runner.rb +322 -0
  154. data/lib/bundler/vendor/thor/shell.rb +81 -0
  155. data/lib/bundler/vendor/thor/shell/basic.rb +420 -0
  156. data/lib/bundler/vendor/thor/shell/color.rb +149 -0
  157. data/lib/bundler/vendor/thor/shell/html.rb +126 -0
  158. data/lib/bundler/vendor/thor/util.rb +267 -0
  159. data/lib/bundler/vendor/thor/version.rb +3 -0
  160. data/lib/bundler/vendored_persistent.rb +3 -0
  161. data/lib/bundler/vendored_thor.rb +8 -0
  162. data/lib/bundler/version.rb +6 -0
  163. data/lib/bundler/vlad.rb +11 -0
  164. data/man/bundle-config.ronn +155 -0
  165. data/man/bundle-exec.ronn +136 -0
  166. data/man/bundle-install.ronn +372 -0
  167. data/man/bundle-package.ronn +59 -0
  168. data/man/bundle-platform.ronn +42 -0
  169. data/man/bundle-update.ronn +179 -0
  170. data/man/bundle.ronn +92 -0
  171. data/man/gemfile.5.ronn +369 -0
  172. data/man/index.txt +7 -0
  173. data/spec/bundler/bundler_spec.rb +74 -0
  174. data/spec/bundler/cli_spec.rb +16 -0
  175. data/spec/bundler/definition_spec.rb +21 -0
  176. data/spec/bundler/dsl_spec.rb +108 -0
  177. data/spec/bundler/friendly_errors_spec.rb +13 -0
  178. data/spec/bundler/gem_helper_spec.rb +225 -0
  179. data/spec/bundler/psyched_yaml_spec.rb +8 -0
  180. data/spec/bundler/retry_spec.rb +59 -0
  181. data/spec/bundler/settings_spec.rb +13 -0
  182. data/spec/bundler/source_spec.rb +25 -0
  183. data/spec/cache/gems_spec.rb +316 -0
  184. data/spec/cache/git_spec.rb +188 -0
  185. data/spec/cache/path_spec.rb +121 -0
  186. data/spec/cache/platform_spec.rb +53 -0
  187. data/spec/cache/svn_spec.rb +82 -0
  188. data/spec/commands/binstubs_spec.rb +246 -0
  189. data/spec/commands/check_spec.rb +278 -0
  190. data/spec/commands/clean_spec.rb +652 -0
  191. data/spec/commands/config_spec.rb +227 -0
  192. data/spec/commands/console_spec.rb +102 -0
  193. data/spec/commands/exec_spec.rb +367 -0
  194. data/spec/commands/help_spec.rb +39 -0
  195. data/spec/commands/init_spec.rb +39 -0
  196. data/spec/commands/inject_spec.rb +78 -0
  197. data/spec/commands/licenses_spec.rb +31 -0
  198. data/spec/commands/newgem_spec.rb +451 -0
  199. data/spec/commands/open_spec.rb +80 -0
  200. data/spec/commands/outdated_spec.rb +168 -0
  201. data/spec/commands/package_spec.rb +128 -0
  202. data/spec/commands/show_spec.rb +152 -0
  203. data/spec/install/binstubs_spec.rb +24 -0
  204. data/spec/install/bundler_spec.rb +146 -0
  205. data/spec/install/deploy_spec.rb +237 -0
  206. data/spec/install/gemfile/gemspec_spec.rb +170 -0
  207. data/spec/install/gemfile/git_spec.rb +967 -0
  208. data/spec/install/gemfile/path_spec.rb +480 -0
  209. data/spec/install/gemfile/svn_spec.rb +582 -0
  210. data/spec/install/gemfile_spec.rb +44 -0
  211. data/spec/install/gems/c_ext_spec.rb +48 -0
  212. data/spec/install/gems/dependency_api_spec.rb +580 -0
  213. data/spec/install/gems/env_spec.rb +107 -0
  214. data/spec/install/gems/flex_spec.rb +314 -0
  215. data/spec/install/gems/groups_spec.rb +308 -0
  216. data/spec/install/gems/mirror_spec.rb +39 -0
  217. data/spec/install/gems/platform_spec.rb +221 -0
  218. data/spec/install/gems/post_install_spec.rb +121 -0
  219. data/spec/install/gems/resolving_spec.rb +124 -0
  220. data/spec/install/gems/simple_case_spec.rb +386 -0
  221. data/spec/install/gems/standalone_spec.rb +260 -0
  222. data/spec/install/gems/sudo_spec.rb +136 -0
  223. data/spec/install/gems/win32_spec.rb +26 -0
  224. data/spec/install/gemspecs_spec.rb +50 -0
  225. data/spec/install/path_spec.rb +150 -0
  226. data/spec/install/post_bundle_message_spec.rb +142 -0
  227. data/spec/install/prereleases_spec.rb +43 -0
  228. data/spec/install/security_policy_spec.rb +77 -0
  229. data/spec/install/upgrade_spec.rb +26 -0
  230. data/spec/lock/git_spec.rb +34 -0
  231. data/spec/lock/lockfile_spec.rb +912 -0
  232. data/spec/lock/svn_spec.rb +35 -0
  233. data/spec/other/bundle_ruby_spec.rb +142 -0
  234. data/spec/other/cli_dispatch_spec.rb +21 -0
  235. data/spec/other/ext_spec.rb +60 -0
  236. data/spec/other/platform_spec.rb +1285 -0
  237. data/spec/other/ssl_cert_spec.rb +10 -0
  238. data/spec/quality_spec.rb +88 -0
  239. data/spec/realworld/dependency_api_spec.rb +60 -0
  240. data/spec/realworld/edgecases_spec.rb +177 -0
  241. data/spec/realworld/parallel_spec.rb +69 -0
  242. data/spec/resolver/basic_spec.rb +66 -0
  243. data/spec/resolver/platform_spec.rb +88 -0
  244. data/spec/runtime/executable_spec.rb +149 -0
  245. data/spec/runtime/load_spec.rb +107 -0
  246. data/spec/runtime/platform_spec.rb +90 -0
  247. data/spec/runtime/require_spec.rb +332 -0
  248. data/spec/runtime/setup_spec.rb +856 -0
  249. data/spec/runtime/with_clean_env_spec.rb +91 -0
  250. data/spec/spec_helper.rb +114 -0
  251. data/spec/support/artifice/endopint_marshal_fail_basic_authentication.rb +13 -0
  252. data/spec/support/artifice/endpoint.rb +71 -0
  253. data/spec/support/artifice/endpoint_500.rb +37 -0
  254. data/spec/support/artifice/endpoint_api_missing.rb +16 -0
  255. data/spec/support/artifice/endpoint_basic_authentication.rb +13 -0
  256. data/spec/support/artifice/endpoint_creds_diff_host.rb +38 -0
  257. data/spec/support/artifice/endpoint_extra.rb +31 -0
  258. data/spec/support/artifice/endpoint_extra_missing.rb +15 -0
  259. data/spec/support/artifice/endpoint_fallback.rb +17 -0
  260. data/spec/support/artifice/endpoint_host_redirect.rb +15 -0
  261. data/spec/support/artifice/endpoint_marshal_fail.rb +11 -0
  262. data/spec/support/artifice/endpoint_redirect.rb +15 -0
  263. data/spec/support/artifice/endpoint_strict_basic_authentication.rb +18 -0
  264. data/spec/support/artifice/endpoint_timeout.rb +13 -0
  265. data/spec/support/builders.rb +748 -0
  266. data/spec/support/fakeweb/rack-1.0.0.marshal +2 -0
  267. data/spec/support/fakeweb/windows.rb +23 -0
  268. data/spec/support/hax.rb +22 -0
  269. data/spec/support/helpers.rb +348 -0
  270. data/spec/support/indexes.rb +280 -0
  271. data/spec/support/less_than_proc.rb +14 -0
  272. data/spec/support/matchers.rb +77 -0
  273. data/spec/support/path.rb +81 -0
  274. data/spec/support/permissions.rb +10 -0
  275. data/spec/support/platforms.rb +94 -0
  276. data/spec/support/ruby_ext.rb +20 -0
  277. data/spec/support/rubygems_ext.rb +39 -0
  278. data/spec/support/streams.rb +13 -0
  279. data/spec/support/sudo.rb +16 -0
  280. data/spec/update/gems_spec.rb +201 -0
  281. data/spec/update/git_spec.rb +236 -0
  282. data/spec/update/source_spec.rb +63 -0
  283. data/spec/update/svn_spec.rb +100 -0
  284. metadata +486 -0
@@ -0,0 +1,107 @@
1
+ require 'thor/actions/empty_directory'
2
+
3
+ class 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 'thor/command'
2
+ require 'thor/core_ext/hash_with_indifferent_access'
3
+ require 'thor/core_ext/ordered_hash'
4
+ require 'thor/error'
5
+ require 'thor/invocation'
6
+ require 'thor/parser'
7
+ require 'thor/shell'
8
+ require 'thor/line_editor'
9
+ require 'thor/util'
10
+
11
+ class Thor
12
+ autoload :Actions, 'thor/actions'
13
+ autoload :RakeCompat, 'thor/rake_compat'
14
+ autoload :Group, 'thor/group'
15
+
16
+ # Shortcuts for help.
17
+ HELP_MAPPINGS = %w(-h -? --help -D)
18
+
19
+ # 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 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 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 = 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 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 = 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 Thor or 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 Thor or Thor::Group, we should track the
113
+ # class and the file on Thor::Base. This is the method responsible for it.
114
+ #
115
+ def register_klass_file(klass) #:nodoc:
116
+ file = caller[1].match(/(.*):\d+/)[1]
117
+ Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)
118
+
119
+ file_subclasses = 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 << Thor::Argument.new(name, options)
231
+ end
232
+
233
+ # Returns this class arguments, looking up in the ancestors chain.
234
+ #
235
+ # ==== Returns
236
+ # Array[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> 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 Thor class.
328
+ #
329
+ # ==== Returns
330
+ # OrderedHash:: An ordered hash with commands names as keys and Thor::Command
331
+ # objects as values.
332
+ #
333
+ def commands
334
+ @commands ||= Thor::CoreExt::OrderedHash.new
335
+ end
336
+ alias_method :tasks, :commands
337
+
338
+ # Returns the commands for this Thor class and all subclasses.
339
+ #
340
+ # ==== Returns
341
+ # OrderedHash:: An ordered hash with commands names as keys and Thor::Command
342
+ # objects as values.
343
+ #
344
+ def all_commands
345
+ @all_commands ||= from_superclass(:all_commands, 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 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 < 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 < 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 Thor or Thor::Group class. By default the
401
+ # namespace is retrieved from the class name. If your 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 ||= 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 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] ||= Thor::Base.shell.new
440
+ dispatch(nil, given_args.dup, nil, config)
441
+ rescue 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 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 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] = 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] = 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 Thor class, register the klass
575
+ # and file into baseclass.
576
+ def inherited(klass)
577
+ 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
+ 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