Skip to content

Instantly share code, notes, and snippets.

@0x1eef
Last active March 17, 2023 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x1eef/14404285fc3748805c637012c9cd40d5 to your computer and use it in GitHub Desktop.
Save 0x1eef/14404285fc3748805c637012c9cd40d5 to your computer and use it in GitHub Desktop.
##
# frozen_string_literal: true
require "bundler/setup"
require "erb"
require "ryo"
require "yaml"
require_relative "../tasks.lib/erb_context"
read_options = ->(env:) do
path = File.join(Dir.getwd, "config", "#{env}.yml")
Ryo.from(YAML.load_file(path))
end
task "config:build", :env do |task, args|
options = read_options.call(**args)
context = ERBContext.with_locals(options)
glob = File.join("config", args[:env], "etc", "*.conf.erb")
etc_files = Dir.glob(glob)
etc_files.each do |file|
dest = File.join(File.dirname(file), File.basename(file, ".erb"))
File.binwrite dest,
ERB.new(File.binread(file), trim_mode: "-").result(context)
print "View #{dest} [y/n]:"
system("cat #{dest} | less") if $stdin.gets.chomp == "y"
end
end
##
# frozen_string_literals: true
require_relative "pf"
class ERBContext
include PF
def self.with_locals(locals)
new(locals).context
end
def initialize(locals)
@locals = locals
end
def context
binding.tap do |b|
Ryo.each(@locals) { |k,v| b.local_variable_set(k, v) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment