pdk 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1329 -1321
  3. data/LICENSE +201 -201
  4. data/README.md +163 -163
  5. data/exe/pdk +10 -10
  6. data/lib/pdk/analytics/client/google_analytics.rb +143 -143
  7. data/lib/pdk/analytics/client/noop.rb +25 -25
  8. data/lib/pdk/analytics/util.rb +19 -19
  9. data/lib/pdk/analytics.rb +30 -30
  10. data/lib/pdk/answer_file.rb +12 -12
  11. data/lib/pdk/bolt.rb +19 -19
  12. data/lib/pdk/cli/build.rb +82 -82
  13. data/lib/pdk/cli/bundle.rb +48 -48
  14. data/lib/pdk/cli/config/get.rb +26 -26
  15. data/lib/pdk/cli/config.rb +22 -22
  16. data/lib/pdk/cli/console.rb +148 -148
  17. data/lib/pdk/cli/convert.rb +52 -52
  18. data/lib/pdk/cli/env.rb +52 -52
  19. data/lib/pdk/cli/errors.rb +25 -25
  20. data/lib/pdk/cli/exec/command.rb +293 -293
  21. data/lib/pdk/cli/exec/interactive_command.rb +114 -114
  22. data/lib/pdk/cli/exec.rb +84 -84
  23. data/lib/pdk/cli/exec_group.rb +104 -104
  24. data/lib/pdk/cli/get/config.rb +24 -24
  25. data/lib/pdk/cli/get.rb +20 -20
  26. data/lib/pdk/cli/module/build.rb +12 -12
  27. data/lib/pdk/cli/module/generate.rb +47 -47
  28. data/lib/pdk/cli/module.rb +14 -14
  29. data/lib/pdk/cli/new/class.rb +32 -32
  30. data/lib/pdk/cli/new/defined_type.rb +32 -32
  31. data/lib/pdk/cli/new/fact.rb +29 -29
  32. data/lib/pdk/cli/new/function.rb +29 -29
  33. data/lib/pdk/cli/new/module.rb +53 -53
  34. data/lib/pdk/cli/new/provider.rb +29 -29
  35. data/lib/pdk/cli/new/task.rb +34 -34
  36. data/lib/pdk/cli/new/test.rb +52 -52
  37. data/lib/pdk/cli/new/transport.rb +27 -27
  38. data/lib/pdk/cli/new.rb +21 -21
  39. data/lib/pdk/cli/release/prep.rb +39 -39
  40. data/lib/pdk/cli/release/publish.rb +50 -50
  41. data/lib/pdk/cli/release.rb +194 -194
  42. data/lib/pdk/cli/remove/config.rb +80 -80
  43. data/lib/pdk/cli/remove.rb +20 -20
  44. data/lib/pdk/cli/set/config.rb +119 -119
  45. data/lib/pdk/cli/set.rb +20 -20
  46. data/lib/pdk/cli/test/unit.rb +90 -90
  47. data/lib/pdk/cli/test.rb +11 -11
  48. data/lib/pdk/cli/update.rb +64 -64
  49. data/lib/pdk/cli/util/command_redirector.rb +27 -27
  50. data/lib/pdk/cli/util/interview.rb +72 -72
  51. data/lib/pdk/cli/util/option_normalizer.rb +55 -55
  52. data/lib/pdk/cli/util/option_validator.rb +68 -68
  53. data/lib/pdk/cli/util/spinner.rb +13 -13
  54. data/lib/pdk/cli/util/update_manager_printer.rb +82 -82
  55. data/lib/pdk/cli/util.rb +305 -305
  56. data/lib/pdk/cli/validate.rb +116 -116
  57. data/lib/pdk/cli.rb +175 -175
  58. data/lib/pdk/config/analytics_schema.json +26 -26
  59. data/lib/pdk/config/errors.rb +5 -5
  60. data/lib/pdk/config/ini_file.rb +183 -183
  61. data/lib/pdk/config/ini_file_setting.rb +39 -39
  62. data/lib/pdk/config/json.rb +34 -34
  63. data/lib/pdk/config/json_schema_namespace.rb +142 -142
  64. data/lib/pdk/config/json_schema_setting.rb +53 -53
  65. data/lib/pdk/config/json_with_schema.rb +49 -49
  66. data/lib/pdk/config/namespace.rb +354 -354
  67. data/lib/pdk/config/setting.rb +135 -135
  68. data/lib/pdk/config/validator.rb +31 -31
  69. data/lib/pdk/config/yaml.rb +46 -46
  70. data/lib/pdk/config/yaml_with_schema.rb +59 -59
  71. data/lib/pdk/config.rb +390 -390
  72. data/lib/pdk/context/control_repo.rb +60 -60
  73. data/lib/pdk/context/module.rb +28 -28
  74. data/lib/pdk/context/none.rb +22 -22
  75. data/lib/pdk/context.rb +99 -99
  76. data/lib/pdk/control_repo.rb +90 -90
  77. data/lib/pdk/generate/defined_type.rb +43 -43
  78. data/lib/pdk/generate/fact.rb +25 -25
  79. data/lib/pdk/generate/function.rb +48 -48
  80. data/lib/pdk/generate/module.rb +352 -352
  81. data/lib/pdk/generate/provider.rb +28 -28
  82. data/lib/pdk/generate/puppet_class.rb +43 -43
  83. data/lib/pdk/generate/puppet_object.rb +232 -232
  84. data/lib/pdk/generate/task.rb +68 -68
  85. data/lib/pdk/generate/transport.rb +33 -33
  86. data/lib/pdk/generate.rb +24 -24
  87. data/lib/pdk/i18n.rb +4 -4
  88. data/lib/pdk/logger.rb +45 -45
  89. data/lib/pdk/module/build.rb +322 -322
  90. data/lib/pdk/module/convert.rb +296 -296
  91. data/lib/pdk/module/metadata.rb +202 -202
  92. data/lib/pdk/module/release.rb +260 -260
  93. data/lib/pdk/module/update.rb +131 -131
  94. data/lib/pdk/module/update_manager.rb +227 -227
  95. data/lib/pdk/module.rb +30 -30
  96. data/lib/pdk/report/event.rb +370 -370
  97. data/lib/pdk/report.rb +121 -121
  98. data/lib/pdk/template/fetcher/git.rb +85 -85
  99. data/lib/pdk/template/fetcher/local.rb +28 -28
  100. data/lib/pdk/template/fetcher.rb +98 -98
  101. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -116
  102. data/lib/pdk/template/renderer/v1/renderer.rb +132 -132
  103. data/lib/pdk/template/renderer/v1/template_file.rb +102 -102
  104. data/lib/pdk/template/renderer/v1.rb +25 -25
  105. data/lib/pdk/template/renderer.rb +96 -96
  106. data/lib/pdk/template/template_dir.rb +67 -67
  107. data/lib/pdk/template.rb +59 -59
  108. data/lib/pdk/tests/unit.rb +252 -252
  109. data/lib/pdk/util/bundler.rb +259 -259
  110. data/lib/pdk/util/changelog_generator.rb +137 -137
  111. data/lib/pdk/util/env.rb +47 -47
  112. data/lib/pdk/util/filesystem.rb +138 -138
  113. data/lib/pdk/util/git.rb +179 -179
  114. data/lib/pdk/util/json_finder.rb +85 -85
  115. data/lib/pdk/util/puppet_strings.rb +125 -125
  116. data/lib/pdk/util/puppet_version.rb +266 -266
  117. data/lib/pdk/util/ruby_version.rb +179 -179
  118. data/lib/pdk/util/template_uri.rb +295 -295
  119. data/lib/pdk/util/vendored_file.rb +93 -93
  120. data/lib/pdk/util/version.rb +43 -43
  121. data/lib/pdk/util/windows/api_types.rb +82 -82
  122. data/lib/pdk/util/windows/file.rb +36 -36
  123. data/lib/pdk/util/windows/process.rb +79 -79
  124. data/lib/pdk/util/windows/string.rb +16 -16
  125. data/lib/pdk/util/windows.rb +15 -15
  126. data/lib/pdk/util.rb +278 -277
  127. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -23
  128. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -98
  129. data/lib/pdk/validate/external_command_validator.rb +208 -208
  130. data/lib/pdk/validate/internal_ruby_validator.rb +100 -100
  131. data/lib/pdk/validate/invokable_validator.rb +228 -228
  132. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -86
  133. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -78
  134. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -20
  135. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -133
  136. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -66
  137. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -137
  138. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -21
  139. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -80
  140. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -19
  141. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -88
  142. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -50
  143. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -20
  144. data/lib/pdk/validate/validator.rb +118 -118
  145. data/lib/pdk/validate/validator_group.rb +104 -104
  146. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -95
  147. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -19
  148. data/lib/pdk/validate.rb +94 -94
  149. data/lib/pdk/version.rb +4 -4
  150. data/lib/pdk.rb +76 -76
  151. data/locales/config.yaml +21 -21
  152. data/locales/pdk.pot +2094 -2094
  153. metadata +5 -6
data/lib/pdk/report.rb CHANGED
@@ -1,121 +1,121 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- class Report
5
- autoload :Event, 'pdk/report/event'
6
-
7
- # @return [Array<String>] the list of supported report formats.
8
- def self.formats
9
- @report_formats ||= %w[junit text].freeze
10
- end
11
-
12
- # @return [Symbol] the method name of the default report format.
13
- def self.default_format
14
- :write_text
15
- end
16
-
17
- # @return [#write] the default target to write the report to.
18
- def self.default_target
19
- $stdout
20
- end
21
-
22
- # Memoised access to the report event storage hash.
23
- #
24
- # The keys of the Hash are the source names of the Events (see
25
- # PDK::Report::Event#source).
26
- #
27
- # @example accessing events from the puppet-lint validator
28
- # report = PDK::Report.new
29
- # report.events['puppet-lint']
30
- #
31
- # @return [Hash{String=>Array<PDK::Report::Event>}] the events stored in
32
- # the repuort.
33
- def events
34
- @events ||= {}
35
- end
36
-
37
- # Create a new PDK::Report::Event from a hash of values and add it to the
38
- # report.
39
- #
40
- # @param data [Hash] (see PDK::Report::Event#initialize)
41
- def add_event(data)
42
- (events[data[:source]] ||= []) << PDK::Report::Event.new(data)
43
- end
44
-
45
- # Renders the report as a JUnit XML document.
46
- #
47
- # @param target [#write] an IO object that the report will be written to.
48
- # Defaults to PDK::Report.default_target.
49
- def write_junit(target = self.class.default_target)
50
- require 'rexml/document'
51
- require 'time'
52
- require 'socket'
53
-
54
- document = REXML::Document.new
55
- document << REXML::XMLDecl.new
56
- testsuites = REXML::Element.new('testsuites')
57
-
58
- id = 0
59
- events.each do |testsuite_name, testcases|
60
- testsuite = REXML::Element.new('testsuite')
61
- testsuite.attributes['name'] = testsuite_name
62
- testsuite.attributes['tests'] = testcases.length
63
- testsuite.attributes['errors'] = testcases.select(&:error?).length
64
- testsuite.attributes['failures'] = testcases.select(&:failure?).length
65
- testsuite.attributes['skipped'] = testcases.select(&:skipped?).length
66
- testsuite.attributes['time'] = 0
67
- testsuite.attributes['timestamp'] = Time.now.strftime('%Y-%m-%dT%H:%M:%S')
68
- testsuite.attributes['hostname'] = Socket.gethostname
69
- testsuite.attributes['id'] = id
70
- testsuite.attributes['package'] = testsuite_name
71
- testsuite.add_element('properties')
72
- testcases.each { |r| testsuite.elements << r.to_junit }
73
- testsuite.add_element('system-out')
74
- testsuite.add_element('system-err')
75
-
76
- testsuites.elements << testsuite
77
- id += 1
78
- end
79
-
80
- document.elements << testsuites
81
- report = ''
82
- document.write(report, 2)
83
-
84
- if target.is_a?(String)
85
- PDK::Util::Filesystem.write_file(target, report)
86
- else
87
- target << report
88
- end
89
- end
90
-
91
- # Renders the report as plain text.
92
- #
93
- # This report is designed for interactive use by a human and so excludes
94
- # all passing events in order to be consise.
95
- #
96
- # @param target [#write] an IO object that the report will be written to.
97
- # Defaults to PDK::Report.default_target.
98
- def write_text(target = self.class.default_target)
99
- coverage_report = nil
100
- report = []
101
-
102
- events.each do |_tool, tool_events|
103
- tool_events.each do |event|
104
- if event.rspec_puppet_coverage?
105
- coverage_report = event.to_text
106
- else
107
- report << event.to_text unless event.pass? || event.skipped?
108
- end
109
- end
110
- end
111
-
112
- report << "\n#{coverage_report}" if coverage_report
113
-
114
- if target.is_a?(String)
115
- PDK::Util::Filesystem.write_file(target, report.join("\n"))
116
- elsif !report.empty?
117
- target << report.join("\n") << "\n"
118
- end
119
- end
120
- end
121
- end
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ class Report
5
+ autoload :Event, 'pdk/report/event'
6
+
7
+ # @return [Array<String>] the list of supported report formats.
8
+ def self.formats
9
+ @report_formats ||= %w[junit text].freeze
10
+ end
11
+
12
+ # @return [Symbol] the method name of the default report format.
13
+ def self.default_format
14
+ :write_text
15
+ end
16
+
17
+ # @return [#write] the default target to write the report to.
18
+ def self.default_target
19
+ $stdout
20
+ end
21
+
22
+ # Memoised access to the report event storage hash.
23
+ #
24
+ # The keys of the Hash are the source names of the Events (see
25
+ # PDK::Report::Event#source).
26
+ #
27
+ # @example accessing events from the puppet-lint validator
28
+ # report = PDK::Report.new
29
+ # report.events['puppet-lint']
30
+ #
31
+ # @return [Hash{String=>Array<PDK::Report::Event>}] the events stored in
32
+ # the repuort.
33
+ def events
34
+ @events ||= {}
35
+ end
36
+
37
+ # Create a new PDK::Report::Event from a hash of values and add it to the
38
+ # report.
39
+ #
40
+ # @param data [Hash] (see PDK::Report::Event#initialize)
41
+ def add_event(data)
42
+ (events[data[:source]] ||= []) << PDK::Report::Event.new(data)
43
+ end
44
+
45
+ # Renders the report as a JUnit XML document.
46
+ #
47
+ # @param target [#write] an IO object that the report will be written to.
48
+ # Defaults to PDK::Report.default_target.
49
+ def write_junit(target = self.class.default_target)
50
+ require 'rexml/document'
51
+ require 'time'
52
+ require 'socket'
53
+
54
+ document = REXML::Document.new
55
+ document << REXML::XMLDecl.new
56
+ testsuites = REXML::Element.new('testsuites')
57
+
58
+ id = 0
59
+ events.each do |testsuite_name, testcases|
60
+ testsuite = REXML::Element.new('testsuite')
61
+ testsuite.attributes['name'] = testsuite_name
62
+ testsuite.attributes['tests'] = testcases.length
63
+ testsuite.attributes['errors'] = testcases.select(&:error?).length
64
+ testsuite.attributes['failures'] = testcases.select(&:failure?).length
65
+ testsuite.attributes['skipped'] = testcases.select(&:skipped?).length
66
+ testsuite.attributes['time'] = 0
67
+ testsuite.attributes['timestamp'] = Time.now.strftime('%Y-%m-%dT%H:%M:%S')
68
+ testsuite.attributes['hostname'] = Socket.gethostname
69
+ testsuite.attributes['id'] = id
70
+ testsuite.attributes['package'] = testsuite_name
71
+ testsuite.add_element('properties')
72
+ testcases.each { |r| testsuite.elements << r.to_junit }
73
+ testsuite.add_element('system-out')
74
+ testsuite.add_element('system-err')
75
+
76
+ testsuites.elements << testsuite
77
+ id += 1
78
+ end
79
+
80
+ document.elements << testsuites
81
+ report = ''
82
+ document.write(report, 2)
83
+
84
+ if target.is_a?(String)
85
+ PDK::Util::Filesystem.write_file(target, report)
86
+ else
87
+ target << report
88
+ end
89
+ end
90
+
91
+ # Renders the report as plain text.
92
+ #
93
+ # This report is designed for interactive use by a human and so excludes
94
+ # all passing events in order to be consise.
95
+ #
96
+ # @param target [#write] an IO object that the report will be written to.
97
+ # Defaults to PDK::Report.default_target.
98
+ def write_text(target = self.class.default_target)
99
+ coverage_report = nil
100
+ report = []
101
+
102
+ events.each do |_tool, tool_events|
103
+ tool_events.each do |event|
104
+ if event.rspec_puppet_coverage?
105
+ coverage_report = event.to_text
106
+ else
107
+ report << event.to_text unless event.pass? || event.skipped?
108
+ end
109
+ end
110
+ end
111
+
112
+ report << "\n#{coverage_report}" if coverage_report
113
+
114
+ if target.is_a?(String)
115
+ PDK::Util::Filesystem.write_file(target, report.join("\n"))
116
+ elsif !report.empty?
117
+ target << report.join("\n") << "\n"
118
+ end
119
+ end
120
+ end
121
+ end
@@ -1,85 +1,85 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Template
5
- module Fetcher
6
- class Git < PDK::Template::Fetcher::AbstractFetcher
7
- # Whether the passed uri is fetchable by Git.
8
- # @see PDK::Template::Fetcher.instance
9
- # @return [Boolean]
10
- def self.fetchable?(uri, _options = {})
11
- PDK::Util::Git.repo?(uri.bare_uri)
12
- end
13
-
14
- # @see PDK::Template::Fetcher::AbstractTemplateFetcher.fetch!
15
- def fetch!
16
- return if fetched
17
- super
18
-
19
- # Default metadata for all git fetching methods
20
- @metadata['template-url'] = uri.metadata_format
21
-
22
- # We don't do a checkout of local-path repos. There are lots of edge
23
- # cases or user un-expectations.
24
- if PDK::Util::Git.work_tree?(uri.shell_path)
25
- PDK.logger.warn _("Repository '%{repo}' has a work-tree; skipping git reset.") % {
26
- repo: uri.shell_path,
27
- }
28
- @path = uri.shell_path
29
- @temporary = false
30
- @metadata['template-ref'] = describe_path_and_ref(@path)
31
- return
32
- end
33
-
34
- # This is either a bare local repo or a remote. either way it needs cloning.
35
- # A "remote" can also be git repo on the local filsystem.
36
- # @todo When switching this over to using rugged, cache the cloned
37
- # template repo in `%AppData%` or `$XDG_CACHE_DIR` and update before
38
- # use.
39
- require 'pdk/util'
40
- require 'pdk/util/git'
41
-
42
- temp_dir = PDK::Util.make_tmpdir_name('pdk-templates')
43
- @temporary = true
44
- origin_repo = uri.bare_uri
45
- git_ref = uri.uri_fragment
46
-
47
- # Clone the repository
48
- clone_result = PDK::Util::Git.git('clone', origin_repo, temp_dir)
49
- unless clone_result[:exit_code].zero?
50
- PDK.logger.error clone_result[:stdout]
51
- PDK.logger.error clone_result[:stderr]
52
- raise PDK::CLI::FatalError, _("Unable to clone git repository at '%{repo}' into '%{dest}'.") % { repo: origin_repo, dest: temp_dir }
53
- end
54
- @path = PDK::Util.canonical_path(temp_dir)
55
-
56
- # Checkout the git reference
57
- if PDK::Util::Git.work_dir_clean?(temp_dir)
58
- Dir.chdir(temp_dir) do
59
- full_ref = PDK::Util::Git.ls_remote(temp_dir, git_ref)
60
- @metadata['template-ref'] = describe_path_and_ref(temp_dir, full_ref)
61
- reset_result = PDK::Util::Git.git('reset', '--hard', full_ref)
62
- return if reset_result[:exit_code].zero?
63
-
64
- PDK.logger.error reset_result[:stdout]
65
- PDK.logger.error reset_result[:stderr]
66
- raise PDK::CLI::FatalError, _("Unable to checkout '%{ref}' of git repository at '%{path}'.") % { ref: git_ref, path: temp_dir }
67
- end
68
- else
69
- PDK.logger.warn _("Uncommitted changes found when attempting to checkout '%{ref}' of git repository at '%{path}'; skipping git reset.") % { ref: git_ref, path: temp_dir }
70
- @metadata['template-ref'] = describe_path_and_ref(temp_dir)
71
- end
72
- end
73
-
74
- private
75
-
76
- #:nocov: This is a just a wrapper for another method
77
- def describe_path_and_ref(path, ref = nil)
78
- require 'pdk/util/git'
79
- PDK::Util::Git.describe(File.join(path, '.git'), ref)
80
- end
81
- #:nocov:
82
- end
83
- end
84
- end
85
- end
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Fetcher
6
+ class Git < PDK::Template::Fetcher::AbstractFetcher
7
+ # Whether the passed uri is fetchable by Git.
8
+ # @see PDK::Template::Fetcher.instance
9
+ # @return [Boolean]
10
+ def self.fetchable?(uri, _options = {})
11
+ PDK::Util::Git.repo?(uri.bare_uri)
12
+ end
13
+
14
+ # @see PDK::Template::Fetcher::AbstractTemplateFetcher.fetch!
15
+ def fetch!
16
+ return if fetched
17
+ super
18
+
19
+ # Default metadata for all git fetching methods
20
+ @metadata['template-url'] = uri.metadata_format
21
+
22
+ # We don't do a checkout of local-path repos. There are lots of edge
23
+ # cases or user un-expectations.
24
+ if PDK::Util::Git.work_tree?(uri.shell_path)
25
+ PDK.logger.warn _("Repository '%{repo}' has a work-tree; skipping git reset.") % {
26
+ repo: uri.shell_path,
27
+ }
28
+ @path = uri.shell_path
29
+ @temporary = false
30
+ @metadata['template-ref'] = describe_path_and_ref(@path)
31
+ return
32
+ end
33
+
34
+ # This is either a bare local repo or a remote. either way it needs cloning.
35
+ # A "remote" can also be git repo on the local filsystem.
36
+ # @todo When switching this over to using rugged, cache the cloned
37
+ # template repo in `%AppData%` or `$XDG_CACHE_DIR` and update before
38
+ # use.
39
+ require 'pdk/util'
40
+ require 'pdk/util/git'
41
+
42
+ temp_dir = PDK::Util.make_tmpdir_name('pdk-templates')
43
+ @temporary = true
44
+ origin_repo = uri.bare_uri
45
+ git_ref = uri.uri_fragment
46
+
47
+ # Clone the repository
48
+ clone_result = PDK::Util::Git.git('clone', origin_repo, temp_dir)
49
+ unless clone_result[:exit_code].zero?
50
+ PDK.logger.error clone_result[:stdout]
51
+ PDK.logger.error clone_result[:stderr]
52
+ raise PDK::CLI::FatalError, _("Unable to clone git repository at '%{repo}' into '%{dest}'.") % { repo: origin_repo, dest: temp_dir }
53
+ end
54
+ @path = PDK::Util.canonical_path(temp_dir)
55
+
56
+ # Checkout the git reference
57
+ if PDK::Util::Git.work_dir_clean?(temp_dir)
58
+ Dir.chdir(temp_dir) do
59
+ full_ref = PDK::Util::Git.ls_remote(temp_dir, git_ref)
60
+ @metadata['template-ref'] = describe_path_and_ref(temp_dir, full_ref)
61
+ reset_result = PDK::Util::Git.git('reset', '--hard', full_ref)
62
+ return if reset_result[:exit_code].zero?
63
+
64
+ PDK.logger.error reset_result[:stdout]
65
+ PDK.logger.error reset_result[:stderr]
66
+ raise PDK::CLI::FatalError, _("Unable to checkout '%{ref}' of git repository at '%{path}'.") % { ref: git_ref, path: temp_dir }
67
+ end
68
+ else
69
+ PDK.logger.warn _("Uncommitted changes found when attempting to checkout '%{ref}' of git repository at '%{path}'; skipping git reset.") % { ref: git_ref, path: temp_dir }
70
+ @metadata['template-ref'] = describe_path_and_ref(temp_dir)
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ #:nocov: This is a just a wrapper for another method
77
+ def describe_path_and_ref(path, ref = nil)
78
+ require 'pdk/util/git'
79
+ PDK::Util::Git.describe(File.join(path, '.git'), ref)
80
+ end
81
+ #:nocov:
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,28 +1,28 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Template
5
- module Fetcher
6
- class Local < PDK::Template::Fetcher::AbstractFetcher
7
- # Whether the passed uri is fetchable. This is a catch-all and all URIs
8
- # are considered on-disk already.
9
- #
10
- # @see PDK::Template::Fetcher.instance
11
- # @return [Boolean]
12
- def self.fetchable?(_uri, _options = {})
13
- true
14
- end
15
-
16
- # @see PDK::Template::Fetcher::AbstractTemplateFetcher.fetch!
17
- def fetch!
18
- return if fetched
19
- super
20
-
21
- @path = uri.shell_path
22
- @temporary = false
23
- @metadata['template-url'] = uri.bare_uri
24
- end
25
- end
26
- end
27
- end
28
- end
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Fetcher
6
+ class Local < PDK::Template::Fetcher::AbstractFetcher
7
+ # Whether the passed uri is fetchable. This is a catch-all and all URIs
8
+ # are considered on-disk already.
9
+ #
10
+ # @see PDK::Template::Fetcher.instance
11
+ # @return [Boolean]
12
+ def self.fetchable?(_uri, _options = {})
13
+ true
14
+ end
15
+
16
+ # @see PDK::Template::Fetcher::AbstractTemplateFetcher.fetch!
17
+ def fetch!
18
+ return if fetched
19
+ super
20
+
21
+ @path = uri.shell_path
22
+ @temporary = false
23
+ @metadata['template-url'] = uri.bare_uri
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,98 +1,98 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Template
5
- module Fetcher
6
- autoload :Git, 'pdk/template/fetcher/git'
7
- autoload :Local, 'pdk/template/fetcher/local'
8
-
9
- # Returns a Template Fetcher implementation for the given Template URI
10
- # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch
11
- # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
12
- #
13
- # @return [PDK::Template::Fetcher::AbstractTemplateFetcher] An instance of a class which implements the AbstractFetcher class
14
- def self.instance(uri, options = {})
15
- return Git.new(uri, options) if Git.fetchable?(uri, options)
16
- Local.new(uri, options)
17
- end
18
-
19
- # Creates an instance of a PDK::Template::Fetcher::AbstractTemplateFetcher object with the path or URL to the template
20
- # and the block of code to run to be run while the template is fetched.
21
- #
22
- # The fetched directory is only guaranteed to be available on disk
23
- # within the scope of the block passed to this method.
24
- #
25
- # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch.
26
- # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
27
- #
28
- # @yieldparam fetcher [PDK::Template::Fetcher::AbstractTemplateFetcher] The initialised fetcher with
29
- # the template already fetched
30
- #
31
- # @example Using a git repository as a template
32
- # PDK::Template::Fetcher.with('https://github.com/puppetlabs/pdk-templates') do |fetcher|
33
- # end
34
- #
35
- # @raise [ArgumentError] If no block is given to this method.
36
- # @return [void]
37
- def self.with(uri, options = {})
38
- raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name } unless block_given?
39
- fetcher = instance(uri, options)
40
-
41
- begin
42
- fetcher.fetch!
43
- yield fetcher
44
- ensure
45
- # If the the path is temporary, clean it up
46
- PDK::Util::Filesystem.rm_rf(fetcher.path) if fetcher.temporary
47
- end
48
- nil
49
- end
50
-
51
- # An abstract class which all Template Fetchers should subclass. This class is responsible for
52
- # downloading or copying a Template Directory that is pointed to by a Template URI
53
- #
54
- # @api private
55
- # @abstract
56
- class AbstractFetcher
57
- # @return [PDK::Util::TemplateURI] The URI of the template that is to be fetched
58
- attr_reader :uri
59
-
60
- # @return [String] The local filesystem path of the fetched template
61
- attr_reader :path
62
-
63
- # @return [Boolean] Whether the fetched path should be considered temporary and be deleted after use
64
- attr_reader :temporary
65
-
66
- # @return [Boolean] Whether the template has been fetched yet
67
- attr_reader :fetched
68
-
69
- # @return [Hash] The metadata hash for this template.
70
- attr_reader :metadata
71
-
72
- # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch
73
- # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
74
- def initialize(uri, options)
75
- @uri = uri
76
- # Defaults
77
- @path = nil
78
- @metadata = {
79
- 'pdk-version' => PDK::Util::Version.version_string,
80
- 'template-url' => nil,
81
- 'template-ref' => nil,
82
- }
83
- @fetched = false
84
- @temporary = false
85
- @options = options
86
- end
87
-
88
- # Fetches the template directory and populates the path property
89
- #
90
- # @return [void]
91
- # @abstract
92
- def fetch!
93
- @fetched = true
94
- end
95
- end
96
- end
97
- end
98
- end
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Fetcher
6
+ autoload :Git, 'pdk/template/fetcher/git'
7
+ autoload :Local, 'pdk/template/fetcher/local'
8
+
9
+ # Returns a Template Fetcher implementation for the given Template URI
10
+ # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch
11
+ # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
12
+ #
13
+ # @return [PDK::Template::Fetcher::AbstractTemplateFetcher] An instance of a class which implements the AbstractFetcher class
14
+ def self.instance(uri, options = {})
15
+ return Git.new(uri, options) if Git.fetchable?(uri, options)
16
+ Local.new(uri, options)
17
+ end
18
+
19
+ # Creates an instance of a PDK::Template::Fetcher::AbstractTemplateFetcher object with the path or URL to the template
20
+ # and the block of code to run to be run while the template is fetched.
21
+ #
22
+ # The fetched directory is only guaranteed to be available on disk
23
+ # within the scope of the block passed to this method.
24
+ #
25
+ # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch.
26
+ # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
27
+ #
28
+ # @yieldparam fetcher [PDK::Template::Fetcher::AbstractTemplateFetcher] The initialised fetcher with
29
+ # the template already fetched
30
+ #
31
+ # @example Using a git repository as a template
32
+ # PDK::Template::Fetcher.with('https://github.com/puppetlabs/pdk-templates') do |fetcher|
33
+ # end
34
+ #
35
+ # @raise [ArgumentError] If no block is given to this method.
36
+ # @return [void]
37
+ def self.with(uri, options = {})
38
+ raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name } unless block_given?
39
+ fetcher = instance(uri, options)
40
+
41
+ begin
42
+ fetcher.fetch!
43
+ yield fetcher
44
+ ensure
45
+ # If the the path is temporary, clean it up
46
+ PDK::Util::Filesystem.rm_rf(fetcher.path) if fetcher.temporary
47
+ end
48
+ nil
49
+ end
50
+
51
+ # An abstract class which all Template Fetchers should subclass. This class is responsible for
52
+ # downloading or copying a Template Directory that is pointed to by a Template URI
53
+ #
54
+ # @api private
55
+ # @abstract
56
+ class AbstractFetcher
57
+ # @return [PDK::Util::TemplateURI] The URI of the template that is to be fetched
58
+ attr_reader :uri
59
+
60
+ # @return [String] The local filesystem path of the fetched template
61
+ attr_reader :path
62
+
63
+ # @return [Boolean] Whether the fetched path should be considered temporary and be deleted after use
64
+ attr_reader :temporary
65
+
66
+ # @return [Boolean] Whether the template has been fetched yet
67
+ attr_reader :fetched
68
+
69
+ # @return [Hash] The metadata hash for this template.
70
+ attr_reader :metadata
71
+
72
+ # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch
73
+ # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
74
+ def initialize(uri, options)
75
+ @uri = uri
76
+ # Defaults
77
+ @path = nil
78
+ @metadata = {
79
+ 'pdk-version' => PDK::Util::Version.version_string,
80
+ 'template-url' => nil,
81
+ 'template-ref' => nil,
82
+ }
83
+ @fetched = false
84
+ @temporary = false
85
+ @options = options
86
+ end
87
+
88
+ # Fetches the template directory and populates the path property
89
+ #
90
+ # @return [void]
91
+ # @abstract
92
+ def fetch!
93
+ @fetched = true
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end