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
@@ -6,8 +6,6 @@ module PDK
6
6
  class BundleHelper; end
7
7
 
8
8
  def self.ensure_bundle!(gem_overrides = nil)
9
- require 'fileutils'
10
-
11
9
  bundle = BundleHelper.new
12
10
 
13
11
  # This will default ensure_bundle! to re-resolving everything to latest
@@ -34,11 +32,11 @@ module PDK
34
32
  original_lockfile = bundle.gemfile_lock
35
33
  temp_lockfile = "#{original_lockfile}.tmp"
36
34
 
37
- FileUtils.mv(original_lockfile, temp_lockfile)
35
+ PDK::Util::Filesystem.mv(original_lockfile, temp_lockfile)
38
36
 
39
37
  all_deps_available = bundle.installed?(gem_overrides)
40
38
  ensure
41
- FileUtils.mv(temp_lockfile, original_lockfile, force: true)
39
+ PDK::Util::Filesystem.mv(temp_lockfile, original_lockfile, force: true)
42
40
  end
43
41
 
44
42
  bundle.update_lock!(with: gem_overrides, local: all_deps_available)
@@ -91,7 +89,7 @@ module PDK
91
89
  end
92
90
 
93
91
  def locked?
94
- !gemfile_lock.nil? && File.file?(gemfile_lock)
92
+ !gemfile_lock.nil? && PDK::Util::Filesystem.file?(gemfile_lock)
95
93
  end
96
94
 
97
95
  def installed?(gem_overrides = {})
@@ -110,7 +108,6 @@ module PDK
110
108
 
111
109
  def lock!
112
110
  require 'pdk/util'
113
- require 'fileutils'
114
111
  require 'pdk/util/ruby_version'
115
112
 
116
113
  if PDK::Util.package_install?
@@ -121,7 +118,9 @@ module PDK
121
118
  File.join(PDK::Util.package_cachedir, 'Gemfile.lock'),
122
119
  ]
123
120
 
124
- vendored_gemfile_lock = vendored_lockfiles.find { |lockfile| File.exist?(lockfile) }
121
+ vendored_gemfile_lock = vendored_lockfiles.find do |lockfile|
122
+ PDK::Util::Filesystem.exist?(lockfile)
123
+ end
125
124
 
126
125
  unless vendored_gemfile_lock
127
126
  raise PDK::CLI::FatalError, _('Vendored Gemfile.lock (%{source}) not found.') % {
@@ -130,7 +129,7 @@ module PDK
130
129
  end
131
130
 
132
131
  PDK.logger.debug(_('Using vendored Gemfile.lock from %{source}.') % { source: vendored_gemfile_lock })
133
- FileUtils.cp(vendored_gemfile_lock, File.join(PDK::Util.module_root, 'Gemfile.lock'))
132
+ PDK::Util::Filesystem.cp(vendored_gemfile_lock, File.join(PDK::Util.module_root, 'Gemfile.lock'))
134
133
  else
135
134
  argv = ['lock']
136
135
 
@@ -211,8 +210,9 @@ module PDK
211
210
  end
212
211
 
213
212
  def binstubs!(gems)
213
+ raise PDK::CLI::FatalError, _('Unable to install requested binstubs as the Gemfile is missing') if gemfile.nil?
214
214
  binstub_dir = File.join(File.dirname(gemfile), 'bin')
215
- return true if gems.all? { |gem| File.file?(File.join(binstub_dir, gem)) }
215
+ return true if gems.all? { |gem| PDK::Util::Filesystem.file?(File.join(binstub_dir, gem)) }
216
216
 
217
217
  cmd = bundle_command('binstubs', *gems, '--force')
218
218
  result = cmd.execute!
@@ -0,0 +1,120 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Util
5
+ module ChangelogGenerator
6
+ # Taken from the version regex in https://forgeapi.puppet.com/schemas/module.json
7
+ VERSION_REGEX = %r{^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$}
8
+ GEM = 'github_changelog_generator'.freeze
9
+
10
+ # Raises if the github_changelog_generator is not available
11
+ def self.github_changelog_generator_available!
12
+ require 'bundler'
13
+ return if ::Bundler.rubygems.find_name(GEM).any?
14
+
15
+ raise PDK::CLI::ExitWithError, _(
16
+ 'Unable to generate the changelog as the %{gem} gem is not installed',
17
+ ) % { gem: GEM }
18
+ end
19
+
20
+ # Runs the Changelog Generator gem (in the module's context) to automatically create a CHANGLELOG.MD file
21
+ #
22
+ # @returns [String] The content of the new Changelog
23
+ def self.generate_changelog
24
+ github_changelog_generator_available!
25
+
26
+ changelog_command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, 'exec', 'rake', 'changelog')
27
+ changelog_command.context = :module
28
+
29
+ result = changelog_command.execute!
30
+ raise PDK::CLI::ExitWithError, _('Error generating changelog: %{stdout}' % { stdout: result[:stdout] }) unless result[:exit_code].zero?
31
+
32
+ output = changelog_content
33
+
34
+ raise PDK::CLI::ExitWithError, _('The generated changelog contains uncategorized Pull Requests. Please label them and try again. See %{changelog_file} for more details' % { changelog_file: changelog_file }) if output =~ %r{UNCATEGORIZED PRS; GO LABEL THEM} # rubocop:disable Metrics/LineLength
35
+ output
36
+ end
37
+
38
+ # Computes the next version, based on the content of a changelog
39
+ #
40
+ # @param current_version [String, Gem::Version] The current version of the module
41
+ # @return [String] The new version. May be the same as the current version if there are no notable changes
42
+ def self.compute_next_version(current_version)
43
+ raise PDK::CLI::ExitWithError, _('Invalid version string %{version}' % { version: current_version }) unless current_version =~ VERSION_REGEX
44
+ version = Gem::Version.create(current_version).segments
45
+ PDK.logger.info _('Determing the target version from \'%{file}\'') % { file: changelog_file }
46
+
47
+ # Grab all lines that start with ## between from the latest changes
48
+ # For example given the changelog below
49
+
50
+ # ```
51
+ # # Change log
52
+ #
53
+ # All notable changes to this project will be documented in this file.
54
+ #
55
+ # ## [v4.0.0](https://github.com/puppetlabs/puppetlabs-inifile/tree/v4.
56
+ #
57
+ # [Full Changelog](https://github.com/puppetlabs/puppetlabs-inifile/com --+
58
+ # |
59
+ # ### Changed |
60
+ # |
61
+ # - pdksync - FM-8499 - remove ubuntu14 support [\#363](https://github. | It's this piece of text we are interested in
62
+ # |
63
+ # ### Added |
64
+ # |
65
+ # - FM-8402 add debian 10 support [\#352](https://github.com/puppetlabs |
66
+ # |
67
+ # ## [v3.1.0](https://github.com/puppetlabs/puppetlabs-inifile/tree/v3. |
68
+ # --+
69
+ # [Full Changelog](https://github.com/puppetlabs/puppetlabs-inifile/com
70
+ #
71
+ # ### Added
72
+ #
73
+ # - FM-8222 - Port Module inifile to Litmus [\#344](https://github.com/
74
+ # - \(FM-8154\) Add Windows Server 2019 support [\#340](https://github.
75
+ # - \(FM-8041\) Add RedHat 8 support [\#339](https://github.com/puppetl
76
+ # ````
77
+ data = ''
78
+ in_changelog_entry = false
79
+ changelog_content.each_line do |line|
80
+ line.strip!
81
+ if line.start_with?('[')
82
+ # We're leaving the latest changes so we can break
83
+ break if in_changelog_entry
84
+ in_changelog_entry = true
85
+ end
86
+ if in_changelog_entry && line.start_with?('##')
87
+ data += line
88
+ end
89
+ end
90
+
91
+ # Check for meta headers in first two header line matches
92
+ if data =~ %r{^### Changed}
93
+ # Major Version bump
94
+ version[0] += 1
95
+ version[1] = 0
96
+ version[2] = 0
97
+ elsif data =~ %r{^### Added}
98
+ # Minor Version bump
99
+ version[1] += 1
100
+ version[2] = 0
101
+ elsif data =~ %r{^### Fixed}
102
+ # Patch Version bump
103
+ version[2] += 1
104
+ end
105
+
106
+ version.join('.')
107
+ end
108
+
109
+ def self.changelog_file
110
+ # Default Changelog file is CHANGELOG.md, but also search for the .MD prefix as well.
111
+ @changelog_file ||= ['CHANGELOG.md', 'CHANGELOG.MD'].map { |file| PDK::Util::Filesystem.expand_path(file) }.find { |path| PDK::Util::Filesystem.file?(path) }
112
+ end
113
+
114
+ def self.changelog_content
115
+ return '' if changelog_file.nil?
116
+ PDK::Util::Filesystem.read_file(changelog_file, open_args: 'rb:utf-8')
117
+ end
118
+ end
119
+ end
120
+ end
@@ -4,25 +4,42 @@ require 'forwardable'
4
4
  module PDK
5
5
  module Util
6
6
  class Env
7
- class << self
7
+ class WindowsEnv
8
8
  extend Forwardable
9
9
 
10
- def_delegators :env_hash, :[], :key?, :fetch, :select, :reject
10
+ # Note, these delegators may not have case insensitive keys
11
+ def_delegators :env_hash, :fetch, :select, :reject
11
12
 
12
13
  def []=(key, value)
13
- if Gem.win_platform?
14
- PDK::Util::Windows::Process.set_environment_variable(key, value)
15
- else
16
- ENV[key] = value
14
+ PDK::Util::Windows::Process.set_environment_variable(key, value)
15
+ end
16
+
17
+ def key?(key)
18
+ !env_hash.keys.find { |item| key.casecmp(item).zero? }.nil?
19
+ end
20
+
21
+ def [](key)
22
+ env_hash.each do |item, value|
23
+ next unless key.casecmp(item).zero?
24
+ return value
17
25
  end
26
+ nil
18
27
  end
19
28
 
29
+ private
30
+
20
31
  def env_hash
21
- if Gem.win_platform?
22
- PDK::Util::Windows::Process.environment_hash
23
- else
24
- ENV
25
- end
32
+ PDK::Util::Windows::Process.environment_hash
33
+ end
34
+ end
35
+
36
+ class << self
37
+ extend Forwardable
38
+
39
+ def_delegators :implementation, :key?, :[], :[]=, :fetch, :select, :reject
40
+
41
+ def implementation
42
+ @implementation ||= Gem.win_platform? ? WindowsEnv.new : ENV
26
43
  end
27
44
  end
28
45
  end
@@ -1,4 +1,5 @@
1
1
  require 'pdk'
2
+ autoload :FileUtils, 'fileutils'
2
3
 
3
4
  module PDK
4
5
  module Util
@@ -17,8 +18,8 @@ module PDK
17
18
  end
18
19
  module_function :write_file
19
20
 
20
- def read_file(file, nil_on_error: false)
21
- File.read(file)
21
+ def read_file(file, nil_on_error: false, open_args: 'r')
22
+ File.read(file, open_args: Array(open_args))
22
23
  rescue => e
23
24
  raise e unless nil_on_error
24
25
  nil
@@ -58,6 +59,11 @@ module PDK
58
59
  end
59
60
  module_function :fnmatch
60
61
 
62
+ def fnmatch?(*args)
63
+ File.fnmatch?(*args)
64
+ end
65
+ module_function :fnmatch?
66
+
61
67
  def readable?(*args)
62
68
  File.readable?(*args)
63
69
  end
@@ -72,6 +78,60 @@ module PDK
72
78
  FileUtils.rm(*args)
73
79
  end
74
80
  module_function :rm
81
+
82
+ def rm_f(*args)
83
+ FileUtils.rm_f(*args)
84
+ end
85
+ module_function :rm_f
86
+
87
+ def rm_rf(*args)
88
+ FileUtils.rm_rf(*args)
89
+ end
90
+ module_function :rm_rf
91
+
92
+ def remove_entry_secure(*args)
93
+ FileUtils.remove_entry_secure(*args)
94
+ end
95
+ module_function :remove_entry_secure
96
+
97
+ def zero?(*args)
98
+ File.zero?(*args)
99
+ end
100
+ module_function :zero?
101
+
102
+ def stat(*args)
103
+ File.stat(*args)
104
+ end
105
+ module_function :stat
106
+
107
+ def symlink?(*args)
108
+ File.symlink?(*args)
109
+ end
110
+ module_function :symlink?
111
+
112
+ def cp(*args)
113
+ FileUtils.cp(*args)
114
+ end
115
+ module_function :cp
116
+
117
+ def mv(*args)
118
+ FileUtils.mv(*args)
119
+ rescue Errno::ENOENT
120
+ # PDK-1169 - FileUtils.mv raises Errno::ENOENT when moving files inside
121
+ # VMWare shared folders on Windows. So we need to catch this
122
+ # case, check if the file exists to see if the exception is
123
+ # legit and "move" the file with cp & rm.
124
+ src, dest, opts = args
125
+ raise unless File.exist?(src)
126
+
127
+ FileUtils.cp(src, dest, preserve: true)
128
+ if (opts ||= {})[:secure]
129
+ FileUtils.remove_entry_secure(src, opts[:force])
130
+ else
131
+ FileUtils.remove_entry(src, opts[:force])
132
+ end
133
+ end
134
+ module_function :mv
75
135
  #:nocov:
76
136
  end
77
137
  end
@@ -19,6 +19,8 @@ module PDK
19
19
  end
20
20
 
21
21
  module Git
22
+ GIT_QUERY_CACHE_TTL ||= 10
23
+
22
24
  def self.git_bindir
23
25
  @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin')
24
26
  end
@@ -63,9 +65,16 @@ module PDK
63
65
  end
64
66
 
65
67
  def self.repo?(maybe_repo)
66
- return bare_repo?(maybe_repo) if File.directory?(maybe_repo)
67
-
68
- remote_repo?(maybe_repo)
68
+ result = cached_git_query(maybe_repo, :repo?)
69
+ return result unless result.nil?
70
+ result = if PDK::Util::Filesystem.directory?(maybe_repo)
71
+ # Use boolean shortcircuiting here. The mostly likely type of git repo
72
+ # is a "normal" repo with a working tree. Bare repos do not have work tree
73
+ work_tree?(maybe_repo) || bare_repo?(maybe_repo)
74
+ else
75
+ remote_repo?(maybe_repo)
76
+ end
77
+ cache_query_result(maybe_repo, :repo?, result)
69
78
  end
70
79
 
71
80
  def self.bare_repo?(maybe_repo)
@@ -80,23 +89,25 @@ module PDK
80
89
  end
81
90
 
82
91
  def self.work_tree?(path)
83
- return false unless File.directory?(path)
92
+ return false unless PDK::Util::Filesystem.directory?(path)
93
+ result = cached_git_query(path, :work_tree?)
94
+ return result unless result.nil?
84
95
 
85
96
  Dir.chdir(path) do
86
97
  rev_parse = git('rev-parse', '--is-inside-work-tree')
87
- rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true'
98
+ cache_query_result(path, :work_tree?, rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true')
88
99
  end
89
100
  end
90
101
 
91
102
  def self.work_dir_clean?(repo)
92
- raise PDK::CLI::ExitWithError, _('Unable to locate git work dir "%{workdir}"') % { workdir: repo } unless File.directory?(repo)
93
- raise PDK::CLI::ExitWithError, _('Unable to locate git dir "%{gitdir}"') % { gitdir: repo } unless File.directory?(File.join(repo, '.git'))
103
+ raise PDK::CLI::ExitWithError, _('Unable to locate git work dir "%{workdir}"') % { workdir: repo } unless PDK::Util::Filesystem.directory?(repo)
104
+ raise PDK::CLI::ExitWithError, _('Unable to locate git dir "%{gitdir}"') % { gitdir: repo } unless PDK::Util::Filesystem.directory?(File.join(repo, '.git'))
94
105
 
95
106
  git('--work-tree', repo, '--git-dir', File.join(repo, '.git'), 'status', '--untracked-files=no', '--porcelain', repo)[:stdout].empty?
96
107
  end
97
108
 
98
109
  def self.ls_remote(repo, ref)
99
- if File.directory?(repo)
110
+ if PDK::Util::Filesystem.directory?(repo)
100
111
  repo = 'file://' + repo
101
112
  end
102
113
 
@@ -122,6 +133,47 @@ module PDK
122
133
  raise PDK::Util::GitError, args, result unless result[:exit_code].zero?
123
134
  result[:stdout].strip
124
135
  end
136
+
137
+ def self.tag?(git_remote, tag_name)
138
+ git('ls-remote', '--tags', '--exit-code', git_remote, tag_name)[:exit_code].zero?
139
+ end
140
+
141
+ # Clears any cached information for git queries
142
+ # Should only be used during testing
143
+ # @api private
144
+ def self.clear_cached_information
145
+ @git_repo_expire_cache = nil
146
+ @git_repo_cache = nil
147
+ end
148
+
149
+ def self.cached_git_query(repo, query)
150
+ # TODO: Not thread safe
151
+ if @git_repo_expire_cache.nil?
152
+ @git_repo_expire_cache = Time.now + GIT_QUERY_CACHE_TTL # Expire the cache every GIT_QUERY_CACHE_TTL seconds
153
+ @git_repo_cache = {}
154
+ elsif Time.now > @git_repo_expire_cache
155
+ @git_repo_expire_cache = Time.now + GIT_QUERY_CACHE_TTL
156
+ @git_repo_cache = {}
157
+ end
158
+ return nil if @git_repo_cache[repo].nil?
159
+ @git_repo_cache[repo][query]
160
+ end
161
+ private_class_method :cached_git_query
162
+
163
+ def self.cache_query_result(repo, query, result)
164
+ # TODO: Not thread safe?
165
+ if @git_repo_expire_cache.nil?
166
+ @git_repo_expire_cache = Time.now + GIT_QUERY_CACHE_TTL
167
+ @git_repo_cache = {}
168
+ end
169
+ if @git_repo_cache[repo].nil?
170
+ @git_repo_cache[repo] = { query => result }
171
+ else
172
+ @git_repo_cache[repo][query] = result
173
+ end
174
+ result
175
+ end
176
+ private_class_method :cache_query_result
125
177
  end
126
178
  end
127
179
  end
@@ -0,0 +1,84 @@
1
+ module PDK
2
+ module Util
3
+ # Processes a given string, looking for JSON objects and parsing them.
4
+ #
5
+ # @example A string with a JSON object and some junk characters
6
+ # PDK::Util::JSONFinder.new('foo{"bar":1}').objects
7
+ # => [{ 'bar' => 1 }]
8
+ #
9
+ # @example A string with mulitple JSON objects
10
+ # PDK::Util::JSONFinder.new('foo{"bar":1}baz{"gronk":2}').objects
11
+ # => [{ 'bar' => 1 }, { 'gronk' => 2 }]
12
+ class JSONFinder
13
+ # Creates a new instance of PDK::Util::JSONFinder.
14
+ #
15
+ # @param string [String] the string to find JSON objects inside of.
16
+ #
17
+ # @return [PDK::Util::JSONFinder] a new PDK::Util::JSONFinder object.
18
+ def initialize(string)
19
+ require 'strscan'
20
+
21
+ @scanner = StringScanner.new(string)
22
+ end
23
+
24
+ # Returns the parsed JSON objects from the string.
25
+ #
26
+ # @return [Array[Hash]] the parsed JSON objects present in the string.
27
+ def objects
28
+ return @objects unless @objects.nil?
29
+
30
+ require 'json'
31
+
32
+ until @scanner.eos?
33
+ @scanner.getch until @scanner.peek(1) == '{' || @scanner.eos?
34
+
35
+ (@objects ||= []) << begin
36
+ JSON.parse(read_object(true) || '')
37
+ rescue JSON::ParserError
38
+ nil
39
+ end
40
+ end
41
+
42
+ @objects = @objects.compact
43
+ end
44
+
45
+ private
46
+
47
+ # Recursively process the string to extract a complete JSON object.
48
+ #
49
+ # @param new_object [Boolean] Set to true if processing a new object to
50
+ # capture the opening brace. Set to false if being called recursively
51
+ # where the opening brace has already been captured.
52
+ #
53
+ # @return [String] The matched substring containing a JSON object.
54
+ def read_object(new_object = false)
55
+ matched_text = new_object ? @scanner.getch : ''
56
+
57
+ until @scanner.eos?
58
+ text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
59
+ unless text
60
+ @scanner.terminate
61
+ return nil
62
+ end
63
+ matched_text += text
64
+
65
+ case @scanner.matched
66
+ when '}'
67
+ break
68
+ when '"'
69
+ text = @scanner.scan_until(%r{(?<!\\)"})
70
+ unless text
71
+ @scanner.terminate
72
+ return nil
73
+ end
74
+ matched_text += text
75
+ else
76
+ matched_text += read_object
77
+ end
78
+ end
79
+
80
+ matched_text
81
+ end
82
+ end
83
+ end
84
+ end