Skip to content

Commit

Permalink
Merge pull request #667 from yast/erb_template
Browse files Browse the repository at this point in the history
Erb template
  • Loading branch information
jreidinger committed Aug 25, 2020
2 parents 52039cb + 7394194 commit a3ea469
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
7 changes: 7 additions & 0 deletions package/autoyast2.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Tue Aug 25 14:17:31 UTC 2020 - Josef Reidinger <jreidinger@suse.com>

- Add ability to use erb template as dynamic autoyast profile
(bsc#1175735)
- 4.3.38

-------------------------------------------------------------------
Tue Aug 25 07:33:20 UTC 2020 - Ladislav Slezák <lslezak@suse.cz>

Expand Down
2 changes: 1 addition & 1 deletion package/autoyast2.spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
%endif

Name: autoyast2
Version: 4.3.37
Version: 4.3.38
Release: 0
Summary: YaST2 - Automated Installation
License: GPL-2.0-only
Expand Down
23 changes: 23 additions & 0 deletions src/lib/autoinstall/y2erb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "yast"
require "erb"

module Y2Autoinstallation
class Y2ERB
def self.render(path)
env = TemplateEnvironment.new
template = ERB.new(File.read(path))
template.result(env.public_binding)
end

class TemplateEnvironment
def hardware
@hardware ||= Yast::SCR.Read(Yast::Path.new(".probe"))
end

# expose method bindings
def public_binding
binding
end
end
end
end
10 changes: 10 additions & 0 deletions src/modules/ProfileLocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "yast"

require "autoinstall/xml_checks"
require "autoinstall/y2erb"
require "y2storage"

module Yast
Expand Down Expand Up @@ -168,6 +169,12 @@ def Process
tmp = Convert.to_string(SCR.Read(path(".target.string"), localfile))
l = Builtins.splitstring(tmp, "\n")
end

# render erb template
if AutoinstConfig.filepath.end_with?(".erb")
res = Y2Autoinstallation::Y2ERB.render(localfile)
SCR.Write(path(".target.string"), localfile, res)
end
else
is_directory = true
end
Expand All @@ -191,6 +198,9 @@ def Process

return false if !is_directory && !profile_checker.valid_profile?

# no rules and classes support for erb templates
return true if AutoinstConfig.filepath.end_with?(".erb")

ret = if is_directory
Get(
AutoinstConfig.scheme,
Expand Down
40 changes: 40 additions & 0 deletions test/lib/y2erb_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"

require "tempfile"
require "autoinstall/y2erb"

describe Y2Autoinstallation::Y2ERB do
describe ".render" do
it "returns string with rendered template" do
# mock just for optimization
allow(Yast::SCR).to receive(:Read).and_return({})
file = Tempfile.new("test")
path = file.path
file.write("<test><%= hardware.inspect %></test>")
file.close
result = described_class.render(path)
file.unlink

expect(result).to match(/<test>{.*}<\/test>/)
end
end
end

0 comments on commit a3ea469

Please sign in to comment.