pdk 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1336 -1329
  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 -278
  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 +2111 -2094
  153. metadata +3 -3
data/lib/pdk/util/git.rb CHANGED
@@ -1,179 +1,179 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Util
5
- class GitError < StandardError
6
- attr_reader :stdout
7
- attr_reader :stderr
8
- attr_reader :exit_code
9
- attr_reader :args
10
-
11
- def initialze(args, result)
12
- @args = args
13
- @stdout = result[:stdout]
14
- @stderr = result[:stderr]
15
- @exit_code = result[:exit_code]
16
-
17
- super(_('Git command failed: git %{args}' % { args: args.join(' ') }))
18
- end
19
- end
20
-
21
- module Git
22
- GIT_QUERY_CACHE_TTL ||= 10
23
-
24
- def self.git_bindir
25
- @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin')
26
- end
27
-
28
- def self.git_paths
29
- @paths ||= begin
30
- paths = [File.join(PDK::Util.pdk_package_basedir, git_bindir)]
31
-
32
- if Gem.win_platform?
33
- paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'bin')
34
- paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'libexec', 'git-core')
35
- paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'usr', 'bin')
36
- end
37
-
38
- paths
39
- end
40
- end
41
-
42
- def self.git_bin
43
- require 'pdk/cli/exec'
44
-
45
- git_bin = Gem.win_platform? ? 'git.exe' : 'git'
46
- vendored_bin_path = File.join(git_bindir, git_bin)
47
-
48
- PDK::CLI::Exec.try_vendored_bin(vendored_bin_path, git_bin)
49
- end
50
-
51
- def self.git(*args)
52
- require 'pdk/cli/exec'
53
-
54
- PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git')
55
-
56
- PDK::CLI::Exec.execute(git_bin, *args)
57
- end
58
-
59
- def self.git_with_env(env, *args)
60
- require 'pdk/cli/exec'
61
-
62
- PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git')
63
-
64
- PDK::CLI::Exec.execute_with_env(env, git_bin, *args)
65
- end
66
-
67
- def self.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)
78
- end
79
-
80
- def self.bare_repo?(maybe_repo)
81
- env = { 'GIT_DIR' => maybe_repo }
82
- rev_parse = git_with_env(env, 'rev-parse', '--is-bare-repository')
83
-
84
- rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true'
85
- end
86
-
87
- def self.remote_repo?(maybe_repo)
88
- git('ls-remote', '--exit-code', maybe_repo)[:exit_code].zero?
89
- end
90
-
91
- def self.work_tree?(path)
92
- return false unless PDK::Util::Filesystem.directory?(path)
93
- result = cached_git_query(path, :work_tree?)
94
- return result unless result.nil?
95
-
96
- Dir.chdir(path) do
97
- rev_parse = git('rev-parse', '--is-inside-work-tree')
98
- cache_query_result(path, :work_tree?, rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true')
99
- end
100
- end
101
-
102
- def self.work_dir_clean?(repo)
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'))
105
-
106
- git('--work-tree', repo, '--git-dir', File.join(repo, '.git'), 'status', '--untracked-files=no', '--porcelain', repo)[:stdout].empty?
107
- end
108
-
109
- def self.ls_remote(repo, ref)
110
- if PDK::Util::Filesystem.directory?(repo)
111
- repo = 'file://' + repo
112
- end
113
-
114
- output = git('ls-remote', '--refs', repo, ref)
115
-
116
- unless output[:exit_code].zero?
117
- PDK.logger.error output[:stdout]
118
- PDK.logger.error output[:stderr]
119
- raise PDK::CLI::ExitWithError, _('Unable to access the template repository "%{repository}"') % {
120
- repository: repo,
121
- }
122
- end
123
-
124
- matching_refs = output[:stdout].split(%r{\r?\n}).map { |r| r.split("\t") }
125
- matching_ref = matching_refs.find { |_sha, remote_ref| remote_ref == "refs/tags/#{ref}" || remote_ref == "refs/remotes/origin/#{ref}" || remote_ref == "refs/heads/#{ref}" }
126
- raise PDK::CLI::ExitWithError, _('Unable to find a branch or tag named "%{ref}" in %{repo}') % { ref: ref, repo: repo } if matching_ref.nil?
127
- matching_ref.first
128
- end
129
-
130
- def self.describe(path, ref = nil)
131
- args = ['--git-dir', path, 'describe', '--all', '--long', '--always', ref].compact
132
- result = git(*args)
133
- raise PDK::Util::GitError, args, result unless result[:exit_code].zero?
134
- result[:stdout].strip
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
177
- end
178
- end
179
- end
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Util
5
+ class GitError < StandardError
6
+ attr_reader :stdout
7
+ attr_reader :stderr
8
+ attr_reader :exit_code
9
+ attr_reader :args
10
+
11
+ def initialze(args, result)
12
+ @args = args
13
+ @stdout = result[:stdout]
14
+ @stderr = result[:stderr]
15
+ @exit_code = result[:exit_code]
16
+
17
+ super(_('Git command failed: git %{args}' % { args: args.join(' ') }))
18
+ end
19
+ end
20
+
21
+ module Git
22
+ GIT_QUERY_CACHE_TTL ||= 10
23
+
24
+ def self.git_bindir
25
+ @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin')
26
+ end
27
+
28
+ def self.git_paths
29
+ @paths ||= begin
30
+ paths = [File.join(PDK::Util.pdk_package_basedir, git_bindir)]
31
+
32
+ if Gem.win_platform?
33
+ paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'bin')
34
+ paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'libexec', 'git-core')
35
+ paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'usr', 'bin')
36
+ end
37
+
38
+ paths
39
+ end
40
+ end
41
+
42
+ def self.git_bin
43
+ require 'pdk/cli/exec'
44
+
45
+ git_bin = Gem.win_platform? ? 'git.exe' : 'git'
46
+ vendored_bin_path = File.join(git_bindir, git_bin)
47
+
48
+ PDK::CLI::Exec.try_vendored_bin(vendored_bin_path, git_bin)
49
+ end
50
+
51
+ def self.git(*args)
52
+ require 'pdk/cli/exec'
53
+
54
+ PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git')
55
+
56
+ PDK::CLI::Exec.execute(git_bin, *args)
57
+ end
58
+
59
+ def self.git_with_env(env, *args)
60
+ require 'pdk/cli/exec'
61
+
62
+ PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git')
63
+
64
+ PDK::CLI::Exec.execute_with_env(env, git_bin, *args)
65
+ end
66
+
67
+ def self.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)
78
+ end
79
+
80
+ def self.bare_repo?(maybe_repo)
81
+ env = { 'GIT_DIR' => maybe_repo }
82
+ rev_parse = git_with_env(env, 'rev-parse', '--is-bare-repository')
83
+
84
+ rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true'
85
+ end
86
+
87
+ def self.remote_repo?(maybe_repo)
88
+ git('ls-remote', '--exit-code', maybe_repo)[:exit_code].zero?
89
+ end
90
+
91
+ def self.work_tree?(path)
92
+ return false unless PDK::Util::Filesystem.directory?(path)
93
+ result = cached_git_query(path, :work_tree?)
94
+ return result unless result.nil?
95
+
96
+ Dir.chdir(path) do
97
+ rev_parse = git('rev-parse', '--is-inside-work-tree')
98
+ cache_query_result(path, :work_tree?, rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true')
99
+ end
100
+ end
101
+
102
+ def self.work_dir_clean?(repo)
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'))
105
+
106
+ git('--work-tree', repo, '--git-dir', File.join(repo, '.git'), 'status', '--untracked-files=no', '--porcelain', repo)[:stdout].empty?
107
+ end
108
+
109
+ def self.ls_remote(repo, ref)
110
+ if PDK::Util::Filesystem.directory?(repo)
111
+ repo = 'file://' + repo
112
+ end
113
+
114
+ output = git('ls-remote', '--refs', repo, ref)
115
+
116
+ unless output[:exit_code].zero?
117
+ PDK.logger.error output[:stdout]
118
+ PDK.logger.error output[:stderr]
119
+ raise PDK::CLI::ExitWithError, _('Unable to access the template repository "%{repository}"') % {
120
+ repository: repo,
121
+ }
122
+ end
123
+
124
+ matching_refs = output[:stdout].split(%r{\r?\n}).map { |r| r.split("\t") }
125
+ matching_ref = matching_refs.find { |_sha, remote_ref| remote_ref == "refs/tags/#{ref}" || remote_ref == "refs/remotes/origin/#{ref}" || remote_ref == "refs/heads/#{ref}" }
126
+ raise PDK::CLI::ExitWithError, _('Unable to find a branch or tag named "%{ref}" in %{repo}') % { ref: ref, repo: repo } if matching_ref.nil?
127
+ matching_ref.first
128
+ end
129
+
130
+ def self.describe(path, ref = nil)
131
+ args = ['--git-dir', path, 'describe', '--all', '--long', '--always', ref].compact
132
+ result = git(*args)
133
+ raise PDK::Util::GitError, args, result unless result[:exit_code].zero?
134
+ result[:stdout].strip
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
177
+ end
178
+ end
179
+ end
@@ -1,85 +1,85 @@
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
- return [] if @objects.nil?
43
- @objects = @objects.compact
44
- end
45
-
46
- private
47
-
48
- # Recursively process the string to extract a complete JSON object.
49
- #
50
- # @param new_object [Boolean] Set to true if processing a new object to
51
- # capture the opening brace. Set to false if being called recursively
52
- # where the opening brace has already been captured.
53
- #
54
- # @return [String] The matched substring containing a JSON object.
55
- def read_object(new_object = false)
56
- matched_text = new_object ? @scanner.getch : ''
57
-
58
- until @scanner.eos?
59
- text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
60
- unless text
61
- @scanner.terminate
62
- return nil
63
- end
64
- matched_text += text
65
-
66
- case @scanner.matched
67
- when '}'
68
- break
69
- when '"'
70
- text = @scanner.scan_until(%r{(?<!\\)"})
71
- unless text
72
- @scanner.terminate
73
- return nil
74
- end
75
- matched_text += text
76
- else
77
- matched_text += read_object
78
- end
79
- end
80
-
81
- matched_text
82
- end
83
- end
84
- end
85
- end
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
+ return [] if @objects.nil?
43
+ @objects = @objects.compact
44
+ end
45
+
46
+ private
47
+
48
+ # Recursively process the string to extract a complete JSON object.
49
+ #
50
+ # @param new_object [Boolean] Set to true if processing a new object to
51
+ # capture the opening brace. Set to false if being called recursively
52
+ # where the opening brace has already been captured.
53
+ #
54
+ # @return [String] The matched substring containing a JSON object.
55
+ def read_object(new_object = false)
56
+ matched_text = new_object ? @scanner.getch : ''
57
+
58
+ until @scanner.eos?
59
+ text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
60
+ unless text
61
+ @scanner.terminate
62
+ return nil
63
+ end
64
+ matched_text += text
65
+
66
+ case @scanner.matched
67
+ when '}'
68
+ break
69
+ when '"'
70
+ text = @scanner.scan_until(%r{(?<!\\)"})
71
+ unless text
72
+ @scanner.terminate
73
+ return nil
74
+ end
75
+ matched_text += text
76
+ else
77
+ matched_text += read_object
78
+ end
79
+ end
80
+
81
+ matched_text
82
+ end
83
+ end
84
+ end
85
+ end