pdk 1.14.1 → 1.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +155 -2
  3. data/lib/pdk.rb +28 -19
  4. data/lib/pdk/answer_file.rb +2 -95
  5. data/lib/pdk/bolt.rb +19 -0
  6. data/lib/pdk/cli.rb +4 -5
  7. data/lib/pdk/cli/bundle.rb +5 -1
  8. data/lib/pdk/cli/config.rb +3 -1
  9. data/lib/pdk/cli/config/get.rb +3 -1
  10. data/lib/pdk/cli/console.rb +1 -1
  11. data/lib/pdk/cli/convert.rb +16 -10
  12. data/lib/pdk/cli/exec.rb +2 -1
  13. data/lib/pdk/cli/exec/command.rb +45 -4
  14. data/lib/pdk/cli/exec_group.rb +78 -43
  15. data/lib/pdk/cli/get.rb +20 -0
  16. data/lib/pdk/cli/get/config.rb +24 -0
  17. data/lib/pdk/cli/module/build.rb +1 -1
  18. data/lib/pdk/cli/module/generate.rb +1 -1
  19. data/lib/pdk/cli/new/class.rb +2 -2
  20. data/lib/pdk/cli/new/defined_type.rb +2 -2
  21. data/lib/pdk/cli/new/provider.rb +2 -2
  22. data/lib/pdk/cli/new/task.rb +2 -2
  23. data/lib/pdk/cli/new/test.rb +2 -2
  24. data/lib/pdk/cli/new/transport.rb +2 -2
  25. data/lib/pdk/cli/release.rb +192 -0
  26. data/lib/pdk/cli/release/prep.rb +39 -0
  27. data/lib/pdk/cli/release/publish.rb +40 -0
  28. data/lib/pdk/cli/remove.rb +20 -0
  29. data/lib/pdk/cli/remove/config.rb +80 -0
  30. data/lib/pdk/cli/set.rb +20 -0
  31. data/lib/pdk/cli/set/config.rb +119 -0
  32. data/lib/pdk/cli/update.rb +18 -8
  33. data/lib/pdk/cli/util.rb +7 -3
  34. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  35. data/lib/pdk/cli/validate.rb +26 -44
  36. data/lib/pdk/config.rb +265 -8
  37. data/lib/pdk/config/ini_file.rb +183 -0
  38. data/lib/pdk/config/ini_file_setting.rb +39 -0
  39. data/lib/pdk/config/namespace.rb +26 -6
  40. data/lib/pdk/config/setting.rb +3 -2
  41. data/lib/pdk/context.rb +99 -0
  42. data/lib/pdk/context/control_repo.rb +60 -0
  43. data/lib/pdk/context/module.rb +28 -0
  44. data/lib/pdk/context/none.rb +22 -0
  45. data/lib/pdk/control_repo.rb +90 -0
  46. data/lib/pdk/generate.rb +1 -0
  47. data/lib/pdk/generate/defined_type.rb +25 -32
  48. data/lib/pdk/generate/module.rb +42 -35
  49. data/lib/pdk/generate/provider.rb +16 -65
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -189
  52. data/lib/pdk/generate/resource_api_object.rb +55 -0
  53. data/lib/pdk/generate/task.rb +28 -46
  54. data/lib/pdk/generate/transport.rb +21 -75
  55. data/lib/pdk/module.rb +1 -1
  56. data/lib/pdk/module/build.rb +38 -25
  57. data/lib/pdk/module/convert.rb +61 -42
  58. data/lib/pdk/module/metadata.rb +1 -3
  59. data/lib/pdk/module/release.rb +254 -0
  60. data/lib/pdk/module/update.rb +24 -16
  61. data/lib/pdk/module/update_manager.rb +8 -1
  62. data/lib/pdk/report.rb +18 -12
  63. data/lib/pdk/report/event.rb +6 -3
  64. data/lib/pdk/template.rb +59 -0
  65. data/lib/pdk/template/fetcher.rb +98 -0
  66. data/lib/pdk/template/fetcher/git.rb +85 -0
  67. data/lib/pdk/template/fetcher/local.rb +28 -0
  68. data/lib/pdk/template/renderer.rb +96 -0
  69. data/lib/pdk/template/renderer/v1.rb +25 -0
  70. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  71. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  72. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  73. data/lib/pdk/template/template_dir.rb +67 -0
  74. data/lib/pdk/tests/unit.rb +5 -0
  75. data/lib/pdk/util.rb +55 -45
  76. data/lib/pdk/util/bundler.rb +9 -9
  77. data/lib/pdk/util/changelog_generator.rb +120 -0
  78. data/lib/pdk/util/env.rb +28 -11
  79. data/lib/pdk/util/filesystem.rb +62 -2
  80. data/lib/pdk/util/git.rb +60 -8
  81. data/lib/pdk/util/json_finder.rb +84 -0
  82. data/lib/pdk/util/puppet_strings.rb +3 -3
  83. data/lib/pdk/util/puppet_version.rb +4 -5
  84. data/lib/pdk/util/ruby_version.rb +9 -6
  85. data/lib/pdk/util/template_uri.rb +60 -48
  86. data/lib/pdk/util/version.rb +4 -4
  87. data/lib/pdk/validate.rb +79 -25
  88. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  89. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  90. data/lib/pdk/validate/external_command_validator.rb +208 -0
  91. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  92. data/lib/pdk/validate/invokable_validator.rb +215 -0
  93. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  94. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  95. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  96. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  97. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  98. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  99. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  100. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  101. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  102. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  103. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  104. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  105. data/lib/pdk/validate/validator.rb +118 -0
  106. data/lib/pdk/validate/validator_group.rb +104 -0
  107. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  108. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  109. data/lib/pdk/version.rb +1 -1
  110. data/locales/pdk.pot +755 -319
  111. metadata +66 -24
  112. data/lib/pdk/module/templatedir.rb +0 -391
  113. data/lib/pdk/template_file.rb +0 -96
  114. data/lib/pdk/validate/base_validator.rb +0 -215
  115. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  116. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  117. data/lib/pdk/validate/metadata_validator.rb +0 -26
  118. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -137
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -137
  121. data/lib/pdk/validate/puppet_validator.rb +0 -26
  122. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  123. data/lib/pdk/validate/ruby_validator.rb +0 -26
  124. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  125. data/lib/pdk/validate/tasks/name.rb +0 -90
  126. data/lib/pdk/validate/tasks_validator.rb +0 -29
  127. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  128. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -1,105 +1,12 @@
1
1
  require 'pdk'
2
- autoload :JSON, 'json'
3
2
 
4
3
  module PDK
5
4
  class AnswerFile
6
- attr_reader :answers
7
- attr_reader :answer_file_path
8
-
9
- include PDK::Util::Filesystem
10
-
11
- # Initialises the AnswerFile object, which stores the responses to certain
12
- # interactive questions.
13
- #
14
- # @param answer_file_path [String, nil] The path on disk to the file where
15
- # the answers will be stored and read from. If not specified (or `nil`),
16
- # the default path will be used (see #default_answer_file_path).
17
- #
18
- # @raise (see #read_from_disk)
19
- def initialize(answer_file_path = nil)
20
- @answer_file_path = answer_file_path || default_answer_file_path
21
- @answers = read_from_disk
22
- end
23
-
24
- # Retrieve the stored answer to a question.
25
- #
26
- # @param question [String] The question name/identifying string.
27
- #
28
- # @return [Object] The answer to the question, or `nil` if no answer found.
29
- def [](question)
30
- answers[question]
31
- end
32
-
33
- # Update the stored answers in memory and then save them to disk.
34
- #
35
- # @param new_answers [Hash{String => Object}] The new questions and answers
36
- # to be merged into the existing answers.
37
- #
38
- # @raise [PDK::CLI::FatalError] if the new answers are not provided as
39
- # a Hash.
40
- # @raise (see #save_to_disk)
41
- def update!(new_answers = {})
42
- unless new_answers.is_a?(Hash)
43
- raise PDK::CLI::FatalError, _('Answer file can be updated only with a Hash')
44
- end
45
-
46
- answers.merge!(new_answers)
47
-
48
- save_to_disk
49
- end
50
-
51
- private
52
-
53
5
  # Determine the default path to the answer file.
54
6
  #
55
7
  # @return [String] The path on disk to the default answer file.
56
- def default_answer_file_path
57
- File.join(PDK::Util.cachedir, 'answers.json')
58
- end
59
-
60
- # Read existing answers into memory from the answer file on disk.
61
- #
62
- # @raise [PDK::CLI::FatalError] If the answer file exists but can not be
63
- # read.
64
- #
65
- # @return [Hash{String => Object}] The existing questions and answers.
66
- def read_from_disk
67
- return {} if !File.file?(answer_file_path) || File.zero?(answer_file_path)
68
-
69
- unless File.readable?(answer_file_path)
70
- raise PDK::CLI::FatalError, _("Unable to open '%{file}' for reading") % {
71
- file: answer_file_path,
72
- }
73
- end
74
-
75
- answers = JSON.parse(File.read(answer_file_path))
76
- if answers.is_a?(Hash)
77
- answers
78
- else
79
- PDK.logger.warn _("Answer file '%{path}' did not contain a valid set of answers, recreating it") % {
80
- path: answer_file_path,
81
- }
82
- {}
83
- end
84
- rescue JSON::JSONError
85
- PDK.logger.warn _("Answer file '%{path}' did not contain valid JSON, recreating it") % {
86
- path: answer_file_path,
87
- }
88
- {}
89
- end
90
-
91
- # Save the in memory answer set to the answer file on disk.
92
- #
93
- # @raise [PDK::CLI::FatalError] if the answer file can not be written to.
94
- def save_to_disk
95
- FileUtils.mkdir_p(File.dirname(answer_file_path))
96
-
97
- write_file(answer_file_path, JSON.pretty_generate(answers))
98
- rescue SystemCallError, IOError => e
99
- raise PDK::CLI::FatalError, _("Unable to write '%{file}': %{msg}") % {
100
- file: answer_file_path,
101
- msg: e.message,
102
- }
8
+ def self.default_answer_file_path
9
+ PDK::Util::Filesystem.expand_path(File.join(PDK::Util.cachedir, 'answers.json'))
103
10
  end
104
11
  end
105
12
  end
@@ -0,0 +1,19 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Bolt
5
+ # Returns true or false depending on if any of the common files and directories in
6
+ # a Bolt Project are found in the specified directory. If a directory is not specified,
7
+ # the current working directory is used.
8
+ #
9
+ # @see https://puppet.com/docs/bolt/latest/bolt_project_directories.html
10
+ #
11
+ # @return [boolean] True if any bolt specific files or directories are present
12
+ #
13
+ def bolt_project_root?(path = Dir.pwd)
14
+ return true if File.basename(path) == 'Boltdir' && PDK::Util::Filesystem.directory?(path)
15
+ PDK::Util::Filesystem.file?(File.join(path, 'bolt.yaml'))
16
+ end
17
+ module_function :bolt_project_root?
18
+ end
19
+ end
@@ -150,23 +150,22 @@ module PDK::CLI
150
150
  flag :d, :debug, _('Enable debug output.') do |_, _|
151
151
  PDK.logger.enable_debug_output
152
152
  end
153
-
154
- option nil, 'answer-file', _('Path to an answer file.'), argument: :required, hidden: true do |value|
155
- require 'pdk/answer_file'
156
- PDK.answer_file = value
157
- end
158
153
  end
159
154
 
160
155
  require 'pdk/cli/bundle'
161
156
  require 'pdk/cli/build'
162
157
  require 'pdk/cli/config'
163
158
  require 'pdk/cli/convert'
159
+ require 'pdk/cli/get'
164
160
  require 'pdk/cli/new'
161
+ require 'pdk/cli/set'
165
162
  require 'pdk/cli/test'
166
163
  require 'pdk/cli/update'
167
164
  require 'pdk/cli/validate'
168
165
  require 'pdk/cli/module'
169
166
  require 'pdk/cli/console'
167
+ require 'pdk/cli/release'
168
+ require 'pdk/cli/remove'
170
169
 
171
170
  @base_cmd.add_command Cri::Command.new_basic_help
172
171
  end
@@ -20,7 +20,11 @@ EOF
20
20
 
21
21
  PDK::CLI::Util.validate_puppet_version_opts({})
22
22
 
23
- PDK::CLI::Util.analytics_screen_view('bundle')
23
+ screen_view_name = ['bundle']
24
+ screen_view_name << args[0] if args.size >= 1
25
+ screen_view_name << args[1] if args.size >= 2 && args[0] == 'exec'
26
+
27
+ PDK::CLI::Util.analytics_screen_view(screen_view_name.join('_'))
24
28
 
25
29
  # Ensure that the correct Ruby is activated before running command.
26
30
  puppet_env = PDK::CLI::Util.puppet_from_opts_or_env({})
@@ -2,10 +2,12 @@ module PDK::CLI
2
2
  @config_cmd = @base_cmd.define_command do
3
3
  name 'config'
4
4
  usage _('config [subcommand] [options]')
5
- summary _('Configure the Puppet Development Kit.')
5
+ summary _('(Deprecated) Configure the Puppet Development Kit.')
6
6
  default_subcommand 'help'
7
7
 
8
8
  run do |_opts, args, _cmd|
9
+ PDK.logger.warn _('The \'pdk config\' command is deprecated, please use \'pdk get config\' and \'pdk set config\' instead.')
10
+
9
11
  if args == ['help']
10
12
  PDK::CLI.run(%w[config --help])
11
13
  exit 0
@@ -2,9 +2,11 @@ module PDK::CLI
2
2
  @config_get_cmd = @config_cmd.define_command do
3
3
  name 'get'
4
4
  usage _('config get [name]')
5
- summary _('Retrieve the configuration for <name>. If not specified, retrieve all configuration settings')
5
+ summary _('(Deprecated) Retrieve the configuration for <name>. If not specified, retrieve all configuration settings')
6
6
 
7
7
  run do |_opts, args, _cmd|
8
+ PDK.logger.warn _('The \'pdk config get\' command is deprecated, please use \'pdk get config\' instead.')
9
+
8
10
  item_name = args[0]
9
11
  resolved_config = PDK.config.resolve(item_name)
10
12
  # If the user wanted to know a setting but it doesn't exist, raise an error
@@ -84,7 +84,7 @@ EOF
84
84
 
85
85
  def check_fixtures_dir
86
86
  existing_path = base_module_path.split(':').find do |path|
87
- Dir.exist?(path) && Dir.entries(path).length > 2
87
+ PDK::Util::Filesystem.directory?(path) && Dir.entries(path).length > 2
88
88
  end
89
89
  PDK.logger.warn _('Module fixtures not found, please run pdk bundle exec rake spec_prep.') unless existing_path
90
90
  end
@@ -11,23 +11,29 @@ module PDK::CLI
11
11
  flag nil, :noop, _('Do not convert the module, just output what would be done.')
12
12
  flag nil, :force, _('Convert the module automatically, with no prompts.')
13
13
  flag nil, :'add-tests', _('Add any missing tests while converting the module.')
14
+ flag nil, :'default-template', _('Convert the module to use the default PDK template.')
14
15
 
15
16
  run do |opts, _args, _cmd|
16
- require 'pdk/module/convert'
17
- require 'pdk/cli/util'
17
+ # Write the context information to the debug log
18
+ PDK.context.to_debug_log
18
19
 
19
- PDK::CLI::Util.ensure_in_module!(
20
- check_module_layout: true,
21
- message: _('`pdk convert` can only be run from inside a valid module directory.'),
22
- log_level: :info,
23
- )
24
-
25
- PDK::CLI::Util.validate_template_opts(opts)
20
+ unless PDK.context.is_a?(PDK::Context::Module)
21
+ raise PDK::CLI::ExitWithError, _('`pdk convert` can only be run from inside a valid module directory.')
22
+ end
26
23
 
27
24
  if opts[:noop] && opts[:force]
28
25
  raise PDK::CLI::ExitWithError, _('You can not specify --noop and --force when converting a module')
29
26
  end
30
27
 
28
+ if opts[:'default-template']
29
+ raise PDK::CLI::ExitWithError, _('You can not specify --template-url and --default-template.') if opts[:'template-url']
30
+
31
+ opts[:'template-url'] = PDK::Util::TemplateURI.default_template_addressable_uri.to_s
32
+ PDK.config.set(%w[user module_defaults template-url], nil)
33
+ end
34
+
35
+ PDK::CLI::Util.validate_template_opts(opts)
36
+
31
37
  PDK::CLI::Util.analytics_screen_view('convert', opts)
32
38
 
33
39
  if opts[:'skip-interview'] && opts[:'full-interview']
@@ -40,7 +46,7 @@ module PDK::CLI
40
46
  opts[:'full-interview'] = false
41
47
  end
42
48
 
43
- PDK::Module::Convert.invoke(opts)
49
+ PDK::Module::Convert.invoke(PDK.context.root_path, opts)
44
50
  end
45
51
  end
46
52
  end
@@ -67,7 +67,8 @@ module PDK
67
67
 
68
68
  vendored_bin_full_path = File.join(PDK::Util.pdk_package_basedir, vendored_bin_path)
69
69
 
70
- unless File.exist?(vendored_bin_full_path)
70
+ require 'pdk/util/filesystem'
71
+ unless PDK::Util::Filesystem.exist?(vendored_bin_full_path)
71
72
  PDK.logger.debug(_("Could not find '%{vendored_bin}' in PDK package. Trying '%{fallback}' from the system PATH instead.") % {
72
73
  fallback: fallback,
73
74
  vendored_bin: vendored_bin_full_path,
@@ -10,6 +10,14 @@ module PDK
10
10
  attr_accessor :environment
11
11
  attr_writer :exec_group
12
12
 
13
+ # The spinner for this command.
14
+ # This should only be used for testing
15
+ #
16
+ # @return [TTY::Spinner, nil]
17
+ #
18
+ # @api private
19
+ attr_reader :spinner
20
+
13
21
  TEMPFILE_MODE = File::RDWR | File::BINARY | File::CREAT | File::TRUNC
14
22
 
15
23
  def initialize(*argv)
@@ -71,6 +79,11 @@ module PDK
71
79
  @environment.merge!(additional_env)
72
80
  end
73
81
 
82
+ # @return [Hash[Symbol => Object]] The result from executing the command
83
+ # :stdout => String : The result of STDOUT
84
+ # :stderr => String : The result of STDERR
85
+ # :exit_code => Integer : The exit code from the command
86
+ # :duration => Float : Number seconds it took to execute
74
87
  def execute!
75
88
  # Start spinning if configured.
76
89
  @spinner.auto_spin if @spinner
@@ -163,7 +176,8 @@ module PDK
163
176
  require 'pdk/util/ruby_version'
164
177
 
165
178
  resolved_env['GEM_HOME'] = PDK::Util::RubyVersion.gem_home
166
- resolved_env['GEM_PATH'] = PDK::Util::RubyVersion.gem_path
179
+ gem_path = PDK::Util::RubyVersion.gem_path
180
+ resolved_env['GEM_PATH'] = gem_path.empty? ? resolved_env['GEM_HOME'] : gem_path
167
181
 
168
182
  # Make sure invocation of Ruby prefers our private installation.
169
183
  package_binpath = PDK::Util.package_install? ? File.join(PDK::Util.pdk_package_basedir, 'bin') : nil
@@ -171,7 +185,7 @@ module PDK
171
185
  resolved_env['PATH'] = [
172
186
  PDK::Util::RubyVersion.bin_path,
173
187
  File.join(resolved_env['GEM_HOME'], 'bin'),
174
- PDK::Util::RubyVersion.gem_paths_raw.map { |gem_path| File.join(gem_path, 'bin') },
188
+ PDK::Util::RubyVersion.gem_paths_raw.map { |gem_path_raw| File.join(gem_path_raw, 'bin') },
175
189
  package_binpath,
176
190
  PDK::Util.package_install? ? PDK::Util::Git.git_paths : nil,
177
191
  PDK::Util::Env['PATH'],
@@ -195,8 +209,13 @@ module PDK
195
209
  def run_process_in_clean_env!
196
210
  require 'bundler'
197
211
 
198
- ::Bundler.with_clean_env do
199
- run_process!
212
+ # Bundler 2.1.0 has deprecated the use of `Bundler.with_clean_env` in favour of
213
+ # `Bundler.with_unbundled_env`. So prefer to use the newer method if it exists
214
+ # otherwise revert back to the old method.
215
+ if ::Bundler.respond_to?(:with_unbundled_env)
216
+ run_process_with_unbundled_env!
217
+ else
218
+ run_process_with_clean_env!
200
219
  end
201
220
  end
202
221
 
@@ -236,6 +255,28 @@ module PDK
236
255
  PDK.logger.debug(_("Execution of '%{command}' complete (duration: %{duration_in_seconds}s; exit code: %{exit_code})") %
237
256
  { command: command_string, duration_in_seconds: @duration, exit_code: @process.exit_code })
238
257
  end
258
+
259
+ private
260
+
261
+ #:nocov:
262
+ # These are just bundler helper methods and are tested via the public run_process_in_clean_env! method
263
+ def run_process_with_unbundled_env!
264
+ # Bundler 2.1.0 or greater
265
+ ::Bundler.with_unbundled_env do
266
+ run_process!
267
+ end
268
+ end
269
+ #:nocov:
270
+
271
+ #:nocov:
272
+ # These are just bundler helper methods and are tested via the public run_process_in_clean_env! method
273
+ def run_process_with_clean_env!
274
+ # Bundler 2.0.2 or less
275
+ ::Bundler.with_clean_env do
276
+ run_process!
277
+ end
278
+ end
279
+ #:nocov:
239
280
  end
240
281
  end
241
282
  end
@@ -3,66 +3,101 @@ require 'pdk'
3
3
  module PDK
4
4
  module CLI
5
5
  class ExecGroup
6
- def initialize(message, opts = {})
7
- require 'pdk/cli/util'
8
- @options = opts.merge(PDK::CLI::Util.spinner_opts_for_platform)
9
-
10
- if PDK::CLI::Util.interactive?
11
- require 'pdk/cli/util/spinner'
12
-
13
- @spinner = if parallel?
14
- TTY::Spinner::Multi.new("[:spinner] #{message}", @options)
15
- else
16
- TTY::Spinner.new("[:spinner] #{message}", @options)
17
- end
18
- @spinner.auto_spin
6
+ # Execution Group (ExecGroup) factory.
7
+ #
8
+ # @param message [String] A name or message for this group. Provided for backwards compatibility during refactor
9
+ #
10
+ # @param create_options [Hash] A hash options used during creation of the ExecGroup. This are not passed to the new object
11
+ # @option create_options :parallel [Boolean] Whether the group should be executed in Parallel (True) or Serial (False)
12
+ #
13
+ # @param group_opts [Hash] A hash of options used to configure the execution group. Provided for backwards compatibility during refactor
14
+ #
15
+ # @return [ExecGroup]
16
+ def self.create(message, create_options = {}, group_opts = {})
17
+ if create_options[:parallel]
18
+ ParallelExecGroup.new(message, group_opts)
19
+ else
20
+ SerialExecGroup.new(message, group_opts)
19
21
  end
20
-
21
- @threads_or_procs = []
22
- @exit_codes = []
23
22
  end
24
23
 
25
- def parallel?
26
- @options[:parallel].nil? ? true : @options[:parallel]
24
+ # Base class for an Exection Group
25
+ #
26
+ # @param message [String] A name or message for this group. Provided for backwards compatibility during refactor
27
+ #
28
+ # @param opts [Hash] A hash of options used to configure the execution group. Provided for backwards compatibility during refactor
29
+ #
30
+ # @api private
31
+ def initialize(_message, opts = {})
32
+ @options = opts
27
33
  end
28
34
 
29
- def register(&block)
35
+ # Register something to execute as a group
36
+ #
37
+ # @param block [Block] A block of ruby to execute
38
+ #
39
+ # @api private
40
+ def register(&_block)
30
41
  raise PDK::CLI::FatalError, _('No block registered') unless block_given?
42
+ end
43
+
44
+ # The return code of running all registered blocks
45
+ #
46
+ # @return [int] The highest exit code from the blocks
47
+ #
48
+ # @abstract
49
+ def exit_code; end
50
+ end
31
51
 
32
- @threads_or_procs << if parallel?
33
- Thread.new do
34
- GettextSetup.initialize(File.absolute_path('../../../locales', File.dirname(__FILE__)))
35
- GettextSetup.negotiate_locale!(GettextSetup.candidate_locales)
36
- @exit_codes << yield
37
- end
38
- else
39
- block
40
- end
52
+ # Executes registered blocks in serial
53
+ #
54
+ # @see PDK::CLI::ExecGroup
55
+ class SerialExecGroup < ExecGroup
56
+ def initialize(message, opts = {})
57
+ super(message, opts)
58
+ @procs = []
41
59
  end
42
60
 
43
- def add_spinner(message, opts = {})
44
- require 'pdk/cli/util'
61
+ def register(&block)
62
+ super(&block)
45
63
 
46
- return unless PDK::CLI::Util.interactive?
47
- @spinner.register("[:spinner] #{message}", @options.merge(opts).merge(PDK::CLI::Util.spinner_opts_for_platform))
64
+ @procs << block
48
65
  end
49
66
 
50
67
  def exit_code
51
- if parallel?
52
- @threads_or_procs.each(&:join)
53
- else
54
- @exit_codes = @threads_or_procs.map(&:call)
55
- end
68
+ exit_codes = @procs.map(&:call)
69
+ exit_codes.nil? ? 0 : exit_codes.max
70
+ end
71
+ end
56
72
 
57
- exit_code = @exit_codes.max
73
+ # Executes registered blocks in parallel using Ruby threads
74
+ #
75
+ # @see PDK::CLI::ExecGroup
76
+ class ParallelExecGroup < ExecGroup
77
+ def initialize(message, opts = {})
78
+ super(message, opts)
79
+ @threads = []
80
+ @exit_codes = []
81
+ end
82
+
83
+ def register(&block)
84
+ super(&block)
58
85
 
59
- if exit_code.zero? && @spinner
60
- @spinner.success
61
- elsif @spinner
62
- @spinner.error
86
+ # TODO: This executes the thread immediately, whereas the SerialExecGroup executes only when exit_code
87
+ # is called. Need to change this so it uses a kind of ThreadPool to limit to number on concurrent jobs
88
+ # and only starts on the call to exit_code
89
+ # e.g. max_threads = No. of CPUs
90
+ @threads << Thread.new do
91
+ GettextSetup.initialize(File.absolute_path('../../../locales', File.dirname(__FILE__)))
92
+ GettextSetup.negotiate_locale!(GettextSetup.candidate_locales)
93
+ @exit_codes << yield
63
94
  end
95
+ end
64
96
 
65
- exit_code
97
+ def exit_code
98
+ @threads.each(&:join)
99
+ return 0 if @exit_codes.empty?
100
+ @exit_codes.max
66
101
  end
67
102
  end
68
103
  end