Created
December 31, 2013 00:58
-
-
Save firstclown/8190760 to your computer and use it in GitHub Desktop.
Wanted an easy way to take a directory, copy it, and then fill in all the files within it with custom values.
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
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'securerandom' | |
require 'fileutils' | |
require 'erb' | |
params = {} | |
target_directory = "New" | |
# Parse args | |
opt_parser = OptionParser.new do |opt| | |
opt.banner = "Usage: fillin [OPTIONS] DIRECTORY" | |
opt.separator "" | |
opt.separator "Will copy the supplied DIRECTORY and fill in all text files with options passed in." | |
opt.on("-dMANDATORY", "Define parameter") do |param| | |
(key,value) = param.split("=") | |
params[key.to_s] = value | |
end | |
opt.on(:REQUIRED, "-tMANDATORY", "--target MANDATORY", "Name of new directory") do |target| | |
target_directory = target | |
end | |
opt.on("-h","--help","help") do | |
puts opt_parser | |
end | |
end | |
opt_parser.parse! | |
directory = ARGV[0] | |
puts directory | |
# Make copy of directory with new name | |
FileUtils.cp_r(directory, target_directory) | |
# Open target and process files | |
Dir.foreach(target_directory) do |filename| | |
filename = target_directory + "/" + filename | |
puts filename | |
if( File.file?(filename) ) | |
puts "It's a file, going to parse it" | |
contents = '' | |
File.open(filename, "r") do |file| | |
template = ERB.new(file.read) | |
contents = template.result( binding ) | |
end | |
File.open(filename, "w") do |file| | |
file.write(contents) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment