Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ERB templates in software. #79

Merged
merged 2 commits into from
Dec 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/omnibus/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

require 'forwardable'
require 'omnibus/exceptions'
require 'ostruct'

module Omnibus
class Builder
Expand All @@ -31,6 +32,7 @@ class DSLProxy
# @todo def_delegators :@builder, :patch, :command, :ruby, ...

def_delegator :@builder, :patch
def_delegator :@builder, :erb
def_delegator :@builder, :command
def_delegator :@builder, :ruby
def_delegator :@builder, :gem
Expand Down Expand Up @@ -149,6 +151,25 @@ def patch(*args)
end
end

def erb(*args)
args = args.dup.pop

source_path = File.expand_path("#{Omnibus.project_root}/config/templates/#{name}/#{args[:source]}")

unless File.exists?(source_path)
raise MissingTemplate.new(source, "#{Omnibus.project_root}/config/templates/#{name}")
end

block do
template = ERB.new(File.new(source_path).read, nil, "%")
File.open(args[:dest], "w") do |file|
file.write(template.result(OpenStruct.new(args[:vars]).instance_eval { binding }))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the OpenStruct here for? To let you access variables in your template as methods? I have kind of a pathological hatred of OpenStruct because it has the most performance hostile implementation possible for a ruby library, but performance of the ruby code in Omnibus isn't the critical path, so maybe I can get over it.

end

File.chmod(args[:mode], args[:dest])
end
end

# @todo all these ruby commands (ruby, gem, bundle, rake) could
# all be collapsed into a single underlying implementation, since
# they all just differ on the executable being called
Expand Down
15 changes: 15 additions & 0 deletions lib/omnibus/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ def to_s
end
end

class MissingTemplate < RuntimeError
def initialize(template_name, search_paths)
@template_name, @search_paths = template_name, search_paths
end

def to_s
"""
Attempting to evaluate the template #{@template_name}, but it was not
found at any of the following locations:

#{@search_paths.join("\n ")}
"""
end
end

class MissingProjectDependency < RuntimeError
def initialize(dep_name, search_paths)
@dep_name, @search_paths = dep_name, search_paths
Expand Down