-
-
Save 0x1eef/14404285fc3748805c637012c9cd40d5 to your computer and use it in GitHub Desktop.
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
## | |
# 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 |
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
## | |
# 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