-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from boltops-tools/graph-tfvars
improve graph dependency processing
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Terraspace::Compiler::Erb | ||
class Rewrite | ||
def initialize(src_path) | ||
@src_path = src_path | ||
end | ||
|
||
def rewrite | ||
input = IO.read(@src_path) | ||
output = replace(input) | ||
tfvar_path = @src_path.sub(Terraspace.root,'') | ||
temp_path = "/tmp/terraspace/rewrite#{tfvar_path}" | ||
FileUtils.mkdir_p(File.dirname(temp_path)) | ||
IO.write(temp_path, output) | ||
temp_path | ||
end | ||
|
||
# Replace contents so only the `output` and `depends_on` are evaluated | ||
def replace(input) | ||
lines = input.split("\n").map {|l| l+"\n"} # mimic IO.readlines | ||
new_lines = lines.map do |line| | ||
new_line(line) | ||
end | ||
new_lines.join('') | ||
end | ||
|
||
def new_line(line) | ||
md = line.match(/.*(<% |<%=)/) || line.match(/.*<%$/) | ||
if md | ||
words = %w[output depends_on] # TODO: consider allowing user customizations | ||
# IE: <%= output or <% depends_on | ||
regexp = Regexp.new(".*<%.*#{words.join('|')}.*") | ||
if line.match(regexp) | ||
line # passthrough | ||
else | ||
line.sub('<%', '<%#') # replace with ERB opening comment | ||
end | ||
else | ||
line # passthrough | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
describe Terraspace::Compiler::Erb::Rewrite do | ||
let(:rewrite) { described_class.new(src_path) } | ||
|
||
context "has output" do | ||
let(:src_path) { fixture("rewrite/dev.tfvars") } | ||
it "replace" do | ||
input =<<~EOL | ||
length = <%= output('b1.length') %> | ||
foo = <%= foo %> | ||
<% depends_on "b1" %> | ||
<% | ||
3.times do |i| | ||
puts i | ||
end | ||
%> | ||
EOL | ||
text = rewrite.replace(input) | ||
expect(text).to eq <<~EOL | ||
length = <%= output('b1.length') %> | ||
foo = <%#= foo %> | ||
<% depends_on "b1" %> | ||
<%# | ||
3.times do |i| | ||
puts i | ||
end | ||
%> | ||
EOL | ||
end | ||
end | ||
end |