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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1329 -1321
- data/LICENSE +201 -201
- data/README.md +163 -163
- data/exe/pdk +10 -10
- data/lib/pdk/analytics/client/google_analytics.rb +143 -143
- data/lib/pdk/analytics/client/noop.rb +25 -25
- data/lib/pdk/analytics/util.rb +19 -19
- data/lib/pdk/analytics.rb +30 -30
- data/lib/pdk/answer_file.rb +12 -12
- data/lib/pdk/bolt.rb +19 -19
- data/lib/pdk/cli/build.rb +82 -82
- data/lib/pdk/cli/bundle.rb +48 -48
- data/lib/pdk/cli/config/get.rb +26 -26
- data/lib/pdk/cli/config.rb +22 -22
- data/lib/pdk/cli/console.rb +148 -148
- data/lib/pdk/cli/convert.rb +52 -52
- data/lib/pdk/cli/env.rb +52 -52
- data/lib/pdk/cli/errors.rb +25 -25
- data/lib/pdk/cli/exec/command.rb +293 -293
- data/lib/pdk/cli/exec/interactive_command.rb +114 -114
- data/lib/pdk/cli/exec.rb +84 -84
- data/lib/pdk/cli/exec_group.rb +104 -104
- data/lib/pdk/cli/get/config.rb +24 -24
- data/lib/pdk/cli/get.rb +20 -20
- data/lib/pdk/cli/module/build.rb +12 -12
- data/lib/pdk/cli/module/generate.rb +47 -47
- data/lib/pdk/cli/module.rb +14 -14
- data/lib/pdk/cli/new/class.rb +32 -32
- data/lib/pdk/cli/new/defined_type.rb +32 -32
- data/lib/pdk/cli/new/fact.rb +29 -29
- data/lib/pdk/cli/new/function.rb +29 -29
- data/lib/pdk/cli/new/module.rb +53 -53
- data/lib/pdk/cli/new/provider.rb +29 -29
- data/lib/pdk/cli/new/task.rb +34 -34
- data/lib/pdk/cli/new/test.rb +52 -52
- data/lib/pdk/cli/new/transport.rb +27 -27
- data/lib/pdk/cli/new.rb +21 -21
- data/lib/pdk/cli/release/prep.rb +39 -39
- data/lib/pdk/cli/release/publish.rb +50 -50
- data/lib/pdk/cli/release.rb +194 -194
- data/lib/pdk/cli/remove/config.rb +80 -80
- data/lib/pdk/cli/remove.rb +20 -20
- data/lib/pdk/cli/set/config.rb +119 -119
- data/lib/pdk/cli/set.rb +20 -20
- data/lib/pdk/cli/test/unit.rb +90 -90
- data/lib/pdk/cli/test.rb +11 -11
- data/lib/pdk/cli/update.rb +64 -64
- data/lib/pdk/cli/util/command_redirector.rb +27 -27
- data/lib/pdk/cli/util/interview.rb +72 -72
- data/lib/pdk/cli/util/option_normalizer.rb +55 -55
- data/lib/pdk/cli/util/option_validator.rb +68 -68
- data/lib/pdk/cli/util/spinner.rb +13 -13
- data/lib/pdk/cli/util/update_manager_printer.rb +82 -82
- data/lib/pdk/cli/util.rb +305 -305
- data/lib/pdk/cli/validate.rb +116 -116
- data/lib/pdk/cli.rb +175 -175
- data/lib/pdk/config/analytics_schema.json +26 -26
- data/lib/pdk/config/errors.rb +5 -5
- data/lib/pdk/config/ini_file.rb +183 -183
- data/lib/pdk/config/ini_file_setting.rb +39 -39
- data/lib/pdk/config/json.rb +34 -34
- data/lib/pdk/config/json_schema_namespace.rb +142 -142
- data/lib/pdk/config/json_schema_setting.rb +53 -53
- data/lib/pdk/config/json_with_schema.rb +49 -49
- data/lib/pdk/config/namespace.rb +354 -354
- data/lib/pdk/config/setting.rb +135 -135
- data/lib/pdk/config/validator.rb +31 -31
- data/lib/pdk/config/yaml.rb +46 -46
- data/lib/pdk/config/yaml_with_schema.rb +59 -59
- data/lib/pdk/config.rb +390 -390
- data/lib/pdk/context/control_repo.rb +60 -60
- data/lib/pdk/context/module.rb +28 -28
- data/lib/pdk/context/none.rb +22 -22
- data/lib/pdk/context.rb +99 -99
- data/lib/pdk/control_repo.rb +90 -90
- data/lib/pdk/generate/defined_type.rb +43 -43
- data/lib/pdk/generate/fact.rb +25 -25
- data/lib/pdk/generate/function.rb +48 -48
- data/lib/pdk/generate/module.rb +352 -352
- data/lib/pdk/generate/provider.rb +28 -28
- data/lib/pdk/generate/puppet_class.rb +43 -43
- data/lib/pdk/generate/puppet_object.rb +232 -232
- data/lib/pdk/generate/task.rb +68 -68
- data/lib/pdk/generate/transport.rb +33 -33
- data/lib/pdk/generate.rb +24 -24
- data/lib/pdk/i18n.rb +4 -4
- data/lib/pdk/logger.rb +45 -45
- data/lib/pdk/module/build.rb +322 -322
- data/lib/pdk/module/convert.rb +296 -296
- data/lib/pdk/module/metadata.rb +202 -202
- data/lib/pdk/module/release.rb +260 -260
- data/lib/pdk/module/update.rb +131 -131
- data/lib/pdk/module/update_manager.rb +227 -227
- data/lib/pdk/module.rb +30 -30
- data/lib/pdk/report/event.rb +370 -370
- data/lib/pdk/report.rb +121 -121
- data/lib/pdk/template/fetcher/git.rb +85 -85
- data/lib/pdk/template/fetcher/local.rb +28 -28
- data/lib/pdk/template/fetcher.rb +98 -98
- data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -116
- data/lib/pdk/template/renderer/v1/renderer.rb +132 -132
- data/lib/pdk/template/renderer/v1/template_file.rb +102 -102
- data/lib/pdk/template/renderer/v1.rb +25 -25
- data/lib/pdk/template/renderer.rb +96 -96
- data/lib/pdk/template/template_dir.rb +67 -67
- data/lib/pdk/template.rb +59 -59
- data/lib/pdk/tests/unit.rb +252 -252
- data/lib/pdk/util/bundler.rb +259 -259
- data/lib/pdk/util/changelog_generator.rb +137 -137
- data/lib/pdk/util/env.rb +47 -47
- data/lib/pdk/util/filesystem.rb +138 -138
- data/lib/pdk/util/git.rb +179 -179
- data/lib/pdk/util/json_finder.rb +85 -85
- data/lib/pdk/util/puppet_strings.rb +125 -125
- data/lib/pdk/util/puppet_version.rb +266 -266
- data/lib/pdk/util/ruby_version.rb +179 -179
- data/lib/pdk/util/template_uri.rb +295 -295
- data/lib/pdk/util/vendored_file.rb +93 -93
- data/lib/pdk/util/version.rb +43 -43
- data/lib/pdk/util/windows/api_types.rb +82 -82
- data/lib/pdk/util/windows/file.rb +36 -36
- data/lib/pdk/util/windows/process.rb +79 -79
- data/lib/pdk/util/windows/string.rb +16 -16
- data/lib/pdk/util/windows.rb +15 -15
- data/lib/pdk/util.rb +278 -277
- data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -23
- data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -98
- data/lib/pdk/validate/external_command_validator.rb +208 -208
- data/lib/pdk/validate/internal_ruby_validator.rb +100 -100
- data/lib/pdk/validate/invokable_validator.rb +228 -228
- data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -86
- data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -78
- data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -20
- data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -133
- data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -66
- data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -137
- data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -21
- data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -80
- data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -19
- data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -88
- data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -50
- data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -20
- data/lib/pdk/validate/validator.rb +118 -118
- data/lib/pdk/validate/validator_group.rb +104 -104
- data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -95
- data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -19
- data/lib/pdk/validate.rb +94 -94
- data/lib/pdk/version.rb +4 -4
- data/lib/pdk.rb +76 -76
- data/locales/config.yaml +21 -21
- data/locales/pdk.pot +2094 -2094
- metadata +5 -6
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
require 'pdk'
|
|
2
|
-
|
|
3
|
-
module PDK
|
|
4
|
-
module Module
|
|
5
|
-
class UpdateManager
|
|
6
|
-
# Initialises a blank UpdateManager object, which is used to store and
|
|
7
|
-
# process file additions/removals/modifications.
|
|
8
|
-
def initialize
|
|
9
|
-
require 'set'
|
|
10
|
-
|
|
11
|
-
@modified_files = Set.new
|
|
12
|
-
@added_files = Set.new
|
|
13
|
-
@removed_files = Set.new
|
|
14
|
-
@diff_cache = {}
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# Store a pending modification to an existing file.
|
|
18
|
-
#
|
|
19
|
-
# @param path [String] The path to the file to be modified.
|
|
20
|
-
# @param content [String] The new content of the file.
|
|
21
|
-
def modify_file(path, content)
|
|
22
|
-
@modified_files << { path: path, content: content }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Store a pending file addition.
|
|
26
|
-
#
|
|
27
|
-
# @param path [String] The path where the file will be created.
|
|
28
|
-
# @param content [String] The content of the new file.
|
|
29
|
-
def add_file(path, content)
|
|
30
|
-
@added_files << { path: path, content: content }
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Store a pending file removal.
|
|
34
|
-
#
|
|
35
|
-
# @param path [String] The path to the file to be removed.
|
|
36
|
-
def remove_file(path)
|
|
37
|
-
@removed_files << path
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Generate a summary of the changes that will be applied to the module.
|
|
41
|
-
#
|
|
42
|
-
# @raise (see #calculate_diffs)
|
|
43
|
-
# @return [Hash{Symbol => Set,Hash}] the summary of the pending changes.
|
|
44
|
-
def changes
|
|
45
|
-
require 'pdk/util/filesystem'
|
|
46
|
-
|
|
47
|
-
calculate_diffs
|
|
48
|
-
|
|
49
|
-
{
|
|
50
|
-
added: @added_files,
|
|
51
|
-
removed: @removed_files.select { |f| PDK::Util::Filesystem.exist?(f) },
|
|
52
|
-
modified: @diff_cache.reject { |_, value| value.nil? },
|
|
53
|
-
}
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# Check if there are any pending changes to apply to the module.
|
|
57
|
-
#
|
|
58
|
-
# @raise (see #changes)
|
|
59
|
-
# @return [Boolean] true if there are changes to apply to the module.
|
|
60
|
-
def changes?
|
|
61
|
-
!changes[:added].empty? ||
|
|
62
|
-
!changes[:removed].empty? ||
|
|
63
|
-
changes[:modified].any? { |_, value| !value.nil? }
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Check if the update manager will change the specified file upon sync.
|
|
67
|
-
#
|
|
68
|
-
# @param path [String] The path to the file.
|
|
69
|
-
#
|
|
70
|
-
# @raise (see #changes)
|
|
71
|
-
# @return [Boolean] true if the file will be changed.
|
|
72
|
-
def changed?(path)
|
|
73
|
-
changes[:added].any? { |add| add[:path] == path } ||
|
|
74
|
-
changes[:removed].include?(path) ||
|
|
75
|
-
changes[:modified].key?(path)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def clear!
|
|
79
|
-
@modified_files.clear
|
|
80
|
-
@added_files.clear
|
|
81
|
-
@removed_files.clear
|
|
82
|
-
nil
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
# Apply any pending changes stored in the UpdateManager to the module.
|
|
86
|
-
#
|
|
87
|
-
# @raise (see #calculate_diffs)
|
|
88
|
-
# @raise (see #write_file)
|
|
89
|
-
# @raise (see #unlink_file)
|
|
90
|
-
def sync_changes!
|
|
91
|
-
calculate_diffs
|
|
92
|
-
|
|
93
|
-
files_to_write = @added_files
|
|
94
|
-
files_to_write += @modified_files.reject { |file| @diff_cache[file[:path]].nil? }
|
|
95
|
-
|
|
96
|
-
@removed_files.each do |file|
|
|
97
|
-
unlink_file(file)
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
files_to_write.each do |file|
|
|
101
|
-
write_file(file[:path], file[:content])
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# Remove a file from disk.
|
|
106
|
-
#
|
|
107
|
-
# Like FileUtils.rm_f, this method will not fail if the file does not
|
|
108
|
-
# exist. Unlike FileUtils.rm_f, this method will not blindly swallow all
|
|
109
|
-
# exceptions.
|
|
110
|
-
#
|
|
111
|
-
# @param path [String] The path to the file to be removed.
|
|
112
|
-
#
|
|
113
|
-
# @raise [PDK::CLI::ExitWithError] if the file could not be removed.
|
|
114
|
-
def unlink_file(path)
|
|
115
|
-
require 'pdk/util/filesystem'
|
|
116
|
-
|
|
117
|
-
if PDK::Util::Filesystem.file?(path)
|
|
118
|
-
PDK.logger.debug(_("unlinking '%{path}'") % { path: path })
|
|
119
|
-
PDK::Util::Filesystem.rm(path)
|
|
120
|
-
else
|
|
121
|
-
PDK.logger.debug(_("'%{path}': already gone") % { path: path })
|
|
122
|
-
end
|
|
123
|
-
rescue => e
|
|
124
|
-
raise PDK::CLI::ExitWithError, _("Unable to remove '%{path}': %{message}") % {
|
|
125
|
-
path: path,
|
|
126
|
-
message: e.message,
|
|
127
|
-
}
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
private
|
|
131
|
-
|
|
132
|
-
# Loop through all the files to be modified and cache of unified diff of
|
|
133
|
-
# the changes to be made to each file.
|
|
134
|
-
#
|
|
135
|
-
# @raise [PDK::CLI::ExitWithError] if a file being modified isn't
|
|
136
|
-
# readable.
|
|
137
|
-
def calculate_diffs
|
|
138
|
-
require 'pdk/util/filesystem'
|
|
139
|
-
|
|
140
|
-
@modified_files.each do |file|
|
|
141
|
-
next if @diff_cache.key?(file[:path])
|
|
142
|
-
|
|
143
|
-
unless PDK::Util::Filesystem.readable?(file[:path])
|
|
144
|
-
raise PDK::CLI::ExitWithError, _("Unable to open '%{path}' for reading") % { path: file[:path] }
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
old_content = PDK::Util::Filesystem.read_file(file[:path])
|
|
148
|
-
file_diff = unified_diff(file[:path], old_content, file[:content])
|
|
149
|
-
@diff_cache[file[:path]] = file_diff
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
# Write or overwrite a file with the specified content.
|
|
154
|
-
#
|
|
155
|
-
# @param path [String] The path to be written to.
|
|
156
|
-
# @param content [String] The data to be written to the file.
|
|
157
|
-
#
|
|
158
|
-
# @raise [PDK::CLI::ExitWithError] if the file is not writeable.
|
|
159
|
-
def write_file(path, content)
|
|
160
|
-
require 'pdk/util/filesystem'
|
|
161
|
-
|
|
162
|
-
PDK::Util::Filesystem.mkdir_p(File.dirname(path))
|
|
163
|
-
PDK.logger.debug(_("writing '%{path}'") % { path: path })
|
|
164
|
-
PDK::Util::Filesystem.write_file(path, content)
|
|
165
|
-
rescue Errno::EACCES
|
|
166
|
-
raise PDK::CLI::ExitWithError, _("You do not have permission to write to '%{path}'") % { path: path }
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
# Generate a unified diff of the changes to be made to a file.
|
|
170
|
-
#
|
|
171
|
-
# @param path [String] The path to the file being diffed (only used to
|
|
172
|
-
# generate the diff header).
|
|
173
|
-
# @param old_content [String] The current content of the file.
|
|
174
|
-
# @param new_content [String] The new content of the file if the pending
|
|
175
|
-
# change is applied.
|
|
176
|
-
# @param lines_of_context [Integer] The maximum number of lines of
|
|
177
|
-
# context to include around the changed lines in the diff output
|
|
178
|
-
# (default: 3).
|
|
179
|
-
#
|
|
180
|
-
# @return [String] The unified diff of the pending changes to the file.
|
|
181
|
-
def unified_diff(path, old_content, new_content, lines_of_context = 3)
|
|
182
|
-
require 'diff/lcs'
|
|
183
|
-
require 'English'
|
|
184
|
-
|
|
185
|
-
output = []
|
|
186
|
-
|
|
187
|
-
old_lines = old_content.split($INPUT_RECORD_SEPARATOR).map(&:chomp)
|
|
188
|
-
new_lines = new_content.split($INPUT_RECORD_SEPARATOR).map(&:chomp)
|
|
189
|
-
|
|
190
|
-
diffs = Diff::LCS.diff(old_lines, new_lines)
|
|
191
|
-
|
|
192
|
-
return if diffs.empty?
|
|
193
|
-
|
|
194
|
-
require 'diff/lcs/hunk'
|
|
195
|
-
|
|
196
|
-
file_mtime = PDK::Util::Filesystem.stat(path).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
|
|
197
|
-
now = Time.now.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
|
|
198
|
-
|
|
199
|
-
output << "--- #{path}\t#{file_mtime}"
|
|
200
|
-
output << "+++ #{path}.pdknew\t#{now}"
|
|
201
|
-
|
|
202
|
-
oldhunk = hunk = nil
|
|
203
|
-
file_length_difference = 0
|
|
204
|
-
|
|
205
|
-
diffs.each do |piece|
|
|
206
|
-
begin
|
|
207
|
-
hunk = Diff::LCS::Hunk.new(old_lines, new_lines, piece, lines_of_context, file_length_difference)
|
|
208
|
-
file_length_difference = hunk.file_length_difference
|
|
209
|
-
|
|
210
|
-
next unless oldhunk
|
|
211
|
-
|
|
212
|
-
# If the hunk overlaps with the oldhunk, merge them.
|
|
213
|
-
next if lines_of_context > 0 && hunk.merge(oldhunk)
|
|
214
|
-
|
|
215
|
-
output << oldhunk.diff(:unified)
|
|
216
|
-
ensure
|
|
217
|
-
oldhunk = hunk
|
|
218
|
-
end
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
output << oldhunk.diff(:unified)
|
|
222
|
-
|
|
223
|
-
output.join($INPUT_RECORD_SEPARATOR)
|
|
224
|
-
end
|
|
225
|
-
end
|
|
226
|
-
end
|
|
227
|
-
end
|
|
1
|
+
require 'pdk'
|
|
2
|
+
|
|
3
|
+
module PDK
|
|
4
|
+
module Module
|
|
5
|
+
class UpdateManager
|
|
6
|
+
# Initialises a blank UpdateManager object, which is used to store and
|
|
7
|
+
# process file additions/removals/modifications.
|
|
8
|
+
def initialize
|
|
9
|
+
require 'set'
|
|
10
|
+
|
|
11
|
+
@modified_files = Set.new
|
|
12
|
+
@added_files = Set.new
|
|
13
|
+
@removed_files = Set.new
|
|
14
|
+
@diff_cache = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Store a pending modification to an existing file.
|
|
18
|
+
#
|
|
19
|
+
# @param path [String] The path to the file to be modified.
|
|
20
|
+
# @param content [String] The new content of the file.
|
|
21
|
+
def modify_file(path, content)
|
|
22
|
+
@modified_files << { path: path, content: content }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Store a pending file addition.
|
|
26
|
+
#
|
|
27
|
+
# @param path [String] The path where the file will be created.
|
|
28
|
+
# @param content [String] The content of the new file.
|
|
29
|
+
def add_file(path, content)
|
|
30
|
+
@added_files << { path: path, content: content }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Store a pending file removal.
|
|
34
|
+
#
|
|
35
|
+
# @param path [String] The path to the file to be removed.
|
|
36
|
+
def remove_file(path)
|
|
37
|
+
@removed_files << path
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Generate a summary of the changes that will be applied to the module.
|
|
41
|
+
#
|
|
42
|
+
# @raise (see #calculate_diffs)
|
|
43
|
+
# @return [Hash{Symbol => Set,Hash}] the summary of the pending changes.
|
|
44
|
+
def changes
|
|
45
|
+
require 'pdk/util/filesystem'
|
|
46
|
+
|
|
47
|
+
calculate_diffs
|
|
48
|
+
|
|
49
|
+
{
|
|
50
|
+
added: @added_files,
|
|
51
|
+
removed: @removed_files.select { |f| PDK::Util::Filesystem.exist?(f) },
|
|
52
|
+
modified: @diff_cache.reject { |_, value| value.nil? },
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Check if there are any pending changes to apply to the module.
|
|
57
|
+
#
|
|
58
|
+
# @raise (see #changes)
|
|
59
|
+
# @return [Boolean] true if there are changes to apply to the module.
|
|
60
|
+
def changes?
|
|
61
|
+
!changes[:added].empty? ||
|
|
62
|
+
!changes[:removed].empty? ||
|
|
63
|
+
changes[:modified].any? { |_, value| !value.nil? }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Check if the update manager will change the specified file upon sync.
|
|
67
|
+
#
|
|
68
|
+
# @param path [String] The path to the file.
|
|
69
|
+
#
|
|
70
|
+
# @raise (see #changes)
|
|
71
|
+
# @return [Boolean] true if the file will be changed.
|
|
72
|
+
def changed?(path)
|
|
73
|
+
changes[:added].any? { |add| add[:path] == path } ||
|
|
74
|
+
changes[:removed].include?(path) ||
|
|
75
|
+
changes[:modified].key?(path)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def clear!
|
|
79
|
+
@modified_files.clear
|
|
80
|
+
@added_files.clear
|
|
81
|
+
@removed_files.clear
|
|
82
|
+
nil
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Apply any pending changes stored in the UpdateManager to the module.
|
|
86
|
+
#
|
|
87
|
+
# @raise (see #calculate_diffs)
|
|
88
|
+
# @raise (see #write_file)
|
|
89
|
+
# @raise (see #unlink_file)
|
|
90
|
+
def sync_changes!
|
|
91
|
+
calculate_diffs
|
|
92
|
+
|
|
93
|
+
files_to_write = @added_files
|
|
94
|
+
files_to_write += @modified_files.reject { |file| @diff_cache[file[:path]].nil? }
|
|
95
|
+
|
|
96
|
+
@removed_files.each do |file|
|
|
97
|
+
unlink_file(file)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
files_to_write.each do |file|
|
|
101
|
+
write_file(file[:path], file[:content])
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Remove a file from disk.
|
|
106
|
+
#
|
|
107
|
+
# Like FileUtils.rm_f, this method will not fail if the file does not
|
|
108
|
+
# exist. Unlike FileUtils.rm_f, this method will not blindly swallow all
|
|
109
|
+
# exceptions.
|
|
110
|
+
#
|
|
111
|
+
# @param path [String] The path to the file to be removed.
|
|
112
|
+
#
|
|
113
|
+
# @raise [PDK::CLI::ExitWithError] if the file could not be removed.
|
|
114
|
+
def unlink_file(path)
|
|
115
|
+
require 'pdk/util/filesystem'
|
|
116
|
+
|
|
117
|
+
if PDK::Util::Filesystem.file?(path)
|
|
118
|
+
PDK.logger.debug(_("unlinking '%{path}'") % { path: path })
|
|
119
|
+
PDK::Util::Filesystem.rm(path)
|
|
120
|
+
else
|
|
121
|
+
PDK.logger.debug(_("'%{path}': already gone") % { path: path })
|
|
122
|
+
end
|
|
123
|
+
rescue => e
|
|
124
|
+
raise PDK::CLI::ExitWithError, _("Unable to remove '%{path}': %{message}") % {
|
|
125
|
+
path: path,
|
|
126
|
+
message: e.message,
|
|
127
|
+
}
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
# Loop through all the files to be modified and cache of unified diff of
|
|
133
|
+
# the changes to be made to each file.
|
|
134
|
+
#
|
|
135
|
+
# @raise [PDK::CLI::ExitWithError] if a file being modified isn't
|
|
136
|
+
# readable.
|
|
137
|
+
def calculate_diffs
|
|
138
|
+
require 'pdk/util/filesystem'
|
|
139
|
+
|
|
140
|
+
@modified_files.each do |file|
|
|
141
|
+
next if @diff_cache.key?(file[:path])
|
|
142
|
+
|
|
143
|
+
unless PDK::Util::Filesystem.readable?(file[:path])
|
|
144
|
+
raise PDK::CLI::ExitWithError, _("Unable to open '%{path}' for reading") % { path: file[:path] }
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
old_content = PDK::Util::Filesystem.read_file(file[:path])
|
|
148
|
+
file_diff = unified_diff(file[:path], old_content, file[:content])
|
|
149
|
+
@diff_cache[file[:path]] = file_diff
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Write or overwrite a file with the specified content.
|
|
154
|
+
#
|
|
155
|
+
# @param path [String] The path to be written to.
|
|
156
|
+
# @param content [String] The data to be written to the file.
|
|
157
|
+
#
|
|
158
|
+
# @raise [PDK::CLI::ExitWithError] if the file is not writeable.
|
|
159
|
+
def write_file(path, content)
|
|
160
|
+
require 'pdk/util/filesystem'
|
|
161
|
+
|
|
162
|
+
PDK::Util::Filesystem.mkdir_p(File.dirname(path))
|
|
163
|
+
PDK.logger.debug(_("writing '%{path}'") % { path: path })
|
|
164
|
+
PDK::Util::Filesystem.write_file(path, content)
|
|
165
|
+
rescue Errno::EACCES
|
|
166
|
+
raise PDK::CLI::ExitWithError, _("You do not have permission to write to '%{path}'") % { path: path }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Generate a unified diff of the changes to be made to a file.
|
|
170
|
+
#
|
|
171
|
+
# @param path [String] The path to the file being diffed (only used to
|
|
172
|
+
# generate the diff header).
|
|
173
|
+
# @param old_content [String] The current content of the file.
|
|
174
|
+
# @param new_content [String] The new content of the file if the pending
|
|
175
|
+
# change is applied.
|
|
176
|
+
# @param lines_of_context [Integer] The maximum number of lines of
|
|
177
|
+
# context to include around the changed lines in the diff output
|
|
178
|
+
# (default: 3).
|
|
179
|
+
#
|
|
180
|
+
# @return [String] The unified diff of the pending changes to the file.
|
|
181
|
+
def unified_diff(path, old_content, new_content, lines_of_context = 3)
|
|
182
|
+
require 'diff/lcs'
|
|
183
|
+
require 'English'
|
|
184
|
+
|
|
185
|
+
output = []
|
|
186
|
+
|
|
187
|
+
old_lines = old_content.split($INPUT_RECORD_SEPARATOR).map(&:chomp)
|
|
188
|
+
new_lines = new_content.split($INPUT_RECORD_SEPARATOR).map(&:chomp)
|
|
189
|
+
|
|
190
|
+
diffs = Diff::LCS.diff(old_lines, new_lines)
|
|
191
|
+
|
|
192
|
+
return if diffs.empty?
|
|
193
|
+
|
|
194
|
+
require 'diff/lcs/hunk'
|
|
195
|
+
|
|
196
|
+
file_mtime = PDK::Util::Filesystem.stat(path).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
|
|
197
|
+
now = Time.now.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
|
|
198
|
+
|
|
199
|
+
output << "--- #{path}\t#{file_mtime}"
|
|
200
|
+
output << "+++ #{path}.pdknew\t#{now}"
|
|
201
|
+
|
|
202
|
+
oldhunk = hunk = nil
|
|
203
|
+
file_length_difference = 0
|
|
204
|
+
|
|
205
|
+
diffs.each do |piece|
|
|
206
|
+
begin
|
|
207
|
+
hunk = Diff::LCS::Hunk.new(old_lines, new_lines, piece, lines_of_context, file_length_difference)
|
|
208
|
+
file_length_difference = hunk.file_length_difference
|
|
209
|
+
|
|
210
|
+
next unless oldhunk
|
|
211
|
+
|
|
212
|
+
# If the hunk overlaps with the oldhunk, merge them.
|
|
213
|
+
next if lines_of_context > 0 && hunk.merge(oldhunk)
|
|
214
|
+
|
|
215
|
+
output << oldhunk.diff(:unified)
|
|
216
|
+
ensure
|
|
217
|
+
oldhunk = hunk
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
output << oldhunk.diff(:unified)
|
|
222
|
+
|
|
223
|
+
output.join($INPUT_RECORD_SEPARATOR)
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
data/lib/pdk/module.rb
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
module PDK
|
|
2
|
-
module Module
|
|
3
|
-
autoload :Build, 'pdk/module/build'
|
|
4
|
-
autoload :Convert, 'pdk/module/convert'
|
|
5
|
-
autoload :Metadata, 'pdk/module/metadata'
|
|
6
|
-
autoload :Release, 'pdk/module/release'
|
|
7
|
-
autoload :UpdateManager, 'pdk/module/update_manager'
|
|
8
|
-
autoload :Update, 'pdk/module/update'
|
|
9
|
-
|
|
10
|
-
DEFAULT_IGNORED = [
|
|
11
|
-
'/pkg/',
|
|
12
|
-
'~*',
|
|
13
|
-
'/coverage',
|
|
14
|
-
'/checksums.json',
|
|
15
|
-
'/REVISION',
|
|
16
|
-
'/spec/fixtures/modules/',
|
|
17
|
-
'/vendor/',
|
|
18
|
-
'.DS_Store',
|
|
19
|
-
].freeze
|
|
20
|
-
|
|
21
|
-
def default_ignored_pathspec(ignore_dotfiles = true)
|
|
22
|
-
require 'pathspec'
|
|
23
|
-
|
|
24
|
-
PathSpec.new(DEFAULT_IGNORED).tap do |ps|
|
|
25
|
-
ps.add('.*') if ignore_dotfiles
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
module_function :default_ignored_pathspec
|
|
29
|
-
end
|
|
30
|
-
end
|
|
1
|
+
module PDK
|
|
2
|
+
module Module
|
|
3
|
+
autoload :Build, 'pdk/module/build'
|
|
4
|
+
autoload :Convert, 'pdk/module/convert'
|
|
5
|
+
autoload :Metadata, 'pdk/module/metadata'
|
|
6
|
+
autoload :Release, 'pdk/module/release'
|
|
7
|
+
autoload :UpdateManager, 'pdk/module/update_manager'
|
|
8
|
+
autoload :Update, 'pdk/module/update'
|
|
9
|
+
|
|
10
|
+
DEFAULT_IGNORED = [
|
|
11
|
+
'/pkg/',
|
|
12
|
+
'~*',
|
|
13
|
+
'/coverage',
|
|
14
|
+
'/checksums.json',
|
|
15
|
+
'/REVISION',
|
|
16
|
+
'/spec/fixtures/modules/',
|
|
17
|
+
'/vendor/',
|
|
18
|
+
'.DS_Store',
|
|
19
|
+
].freeze
|
|
20
|
+
|
|
21
|
+
def default_ignored_pathspec(ignore_dotfiles = true)
|
|
22
|
+
require 'pathspec'
|
|
23
|
+
|
|
24
|
+
PathSpec.new(DEFAULT_IGNORED).tap do |ps|
|
|
25
|
+
ps.add('.*') if ignore_dotfiles
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
module_function :default_ignored_pathspec
|
|
29
|
+
end
|
|
30
|
+
end
|