Sha256: 2d18da75034a76530dee782c3c48c32c10cc498b4019d16d02bdaff116337af7

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

#!/usr/bin/env ruby

require 'erb'
require 'find'
require 'pathname'

PROJECT_FILE = './erb_asterisk_project.rb'.freeze
if File.exist?(PROJECT_FILE)
  require PROJECT_FILE
end

$exports = {}
$templates = ''

# Render template
def render(template, vars)
  tpl = File.read("#{$templates}/#{template}.erb")
  e = ERB.new(tpl)

  b = TOPLEVEL_BINDING
  vars.each do |name, value|
    b.local_variable_set(name, value)
  end

  e.result
end

# Declare current config file inclusion to file_name
def include_to(file_name)
  return unless TOPLEVEL_BINDING.local_variable_defined?(:current_conf_file)
  $exports[file_name] = [] if $exports[file_name].nil?
  arr = $exports[file_name]

  current_conf_file = TOPLEVEL_BINDING.local_variable_get(:current_conf_file)
  if arr.include?(current_conf_file)
    puts "Skip #{current_conf_file} duplicate inclusion to #{file_name}"
    return
  end

  arr << current_conf_file
  "; Included to \"#{file_name}\""
end

def asterisk_root
  return './' if File.exist?('asterisk.conf')
  return 'asterisk/' if Dir.exist?('asterisk/')
  raise 'Asterisk configuration not found'
end

root = asterisk_root
$templates = "#{root}templates".freeze

# Render ERB files
Find.find(root) do |f|
  next if File.directory?(f)
  next if f.start_with?($templates)
  next unless f.end_with?('.erb')

  output_config = f.chomp('.erb')

  TOPLEVEL_BINDING.local_variable_set(:current_conf_file,
                                      output_config.sub(root, ''))

  File.write(output_config, ERB.new(File.read(f)).result)
end

# Save includes
$exports.each do |include_file, content|
  s = ''
  content.each do |file|
    s << "#include \"#{file}\"\n"
  end

  File.write("#{root}#{include_file}", s)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erb_asterisk-0.0.2 exe/erb_asterisk