54 lines
968 B
Ruby
54 lines
968 B
Ruby
|
require "ostruct"
|
||
|
require "fileutils"
|
||
|
require "erb"
|
||
|
require "json"
|
||
|
require "pathname"
|
||
|
require "pry"
|
||
|
|
||
|
class Registry
|
||
|
PATH = Pathname.new(File.expand_path("../../registry.json", __FILE__))
|
||
|
def initialize
|
||
|
@data = JSON.load(File.open(Registry::PATH))
|
||
|
end
|
||
|
attr_accessor :data
|
||
|
def save
|
||
|
f = File.open(Registry::PATH, "w+")
|
||
|
f.puts JSON.pretty_generate(@data)
|
||
|
f.close
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def atomic_write(path, content)
|
||
|
temp_path = path.to_s + ".tmp"
|
||
|
File.open(temp_path, 'w+') do |f|
|
||
|
f.write(content)
|
||
|
end
|
||
|
|
||
|
FileUtils.mv(temp_path, path)
|
||
|
end
|
||
|
|
||
|
def sh(cmd, *args)
|
||
|
puts "$ #{cmd} "+ args.map {|a| "'#{a}'" }.join(" ")
|
||
|
#system(cmd, *args)
|
||
|
end
|
||
|
|
||
|
def die(msg)
|
||
|
$stderr.puts(msg)
|
||
|
exit(1)
|
||
|
end
|
||
|
|
||
|
class TemplateContext < OpenStruct
|
||
|
def get_binding
|
||
|
binding
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class Template
|
||
|
def initialize(path)
|
||
|
@erb = ERB.new(File.read(path), nil, '-')
|
||
|
end
|
||
|
def render(params={})
|
||
|
@erb.result(TemplateContext.new(params).get_binding)
|
||
|
end
|
||
|
end
|