Class: Chef::TidySubstitutions
- Inherits:
-
Object
- Object
- Chef::TidySubstitutions
show all
- Defined in:
- lib/chef/tidy_substitutions.rb
Defined Under Namespace
Classes: NoSubstitutionFile
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(file_path = nil, tidy_common) ⇒ TidySubstitutions
Returns a new instance of TidySubstitutions.
12
13
14
15
16
|
# File 'lib/chef/tidy_substitutions.rb', line 12
def initialize(file_path = nil, tidy_common)
@file_path = file_path
@tidy = tidy_common
@backup_path = tidy_common.backup_path
end
|
Instance Attribute Details
#backup_path ⇒ Object
Returns the value of attribute backup_path.
10
11
12
|
# File 'lib/chef/tidy_substitutions.rb', line 10
def backup_path
@backup_path
end
|
#data ⇒ Object
Returns the value of attribute data.
10
11
12
|
# File 'lib/chef/tidy_substitutions.rb', line 10
def data
@data
end
|
#file_path ⇒ Object
Returns the value of attribute file_path.
10
11
12
|
# File 'lib/chef/tidy_substitutions.rb', line 10
def file_path
@file_path
end
|
Instance Method Details
#boiler_plate ⇒ Object
25
26
27
28
29
|
# File 'lib/chef/tidy_substitutions.rb', line 25
def boiler_plate
bp = ::File.join(File.dirname(__FILE__), '../../conf/substitutions.json.example')
@tidy.ui.stdout.puts "INFO: Creating boiler plate gsub file: 'substitutions.json'"
FileUtils.cp(bp, ::File.join(Dir.pwd, 'substitutions.json'))
end
|
#cookbook_version_from_path(path) ⇒ Object
31
32
33
34
35
|
# File 'lib/chef/tidy_substitutions.rb', line 31
def cookbook_version_from_path(path)
components = path.split(File::SEPARATOR)
name_version = components[components.index('cookbooks')+1]
name_version.match(/\d+\.\d+\.\d+/).to_s
end
|
#load_data ⇒ Object
18
19
20
21
22
23
|
# File 'lib/chef/tidy_substitutions.rb', line 18
def load_data
@tidy.ui.stdout.puts "INFO: Loading substitutions from #{file_path}"
@data = FFI_Yajl::Parser.parse(::File.read(@file_path), symbolize_names: false)
rescue Errno::ENOENT
raise NoSubstitutionFile, file_path
end
|
#revert ⇒ Object
37
38
39
|
# File 'lib/chef/tidy_substitutions.rb', line 37
def revert
end
|
#run_substitutions ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/chef/tidy_substitutions.rb', line 63
def run_substitutions
load_data
@data.keys.each do |entry|
@data[entry].keys.each do |glob|
@tidy.ui.stdout.puts "INFO: Running substitutions for #{entry} -> #{glob}"
Dir[::File.join(@backup_path, glob)].each do |file|
@data[entry][glob].each do |substitution|
search = Regexp.new(substitution['pattern'])
replace = substitution['replace'].dup
replace.gsub!(/\!COOKBOOK_VERSION\!/) { |m| "'" + cookbook_version_from_path(file) + "'" }
sub_in_file(file, search, replace)
end
end
end
end
end
|
#sub_in_file(path, search, replace) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/chef/tidy_substitutions.rb', line 41
def sub_in_file(path, search, replace)
temp_file = Tempfile.new('tidy')
begin
File.open(path, 'r') do |file|
file.each_line do |line|
if line.match(search)
temp_file.puts replace
@tidy.ui.stdout.puts "INFO: ++ #{path}"
else
temp_file.puts line
end
end
end
temp_file.close
FileUtils.cp(path, "#{path}.orig") unless ::File.exist?("#{path}.orig")
FileUtils.mv(temp_file.path, path)
ensure
temp_file.close
temp_file.unlink
end
end
|