class Sourcery::Caster
Spell Caster
renders `src/` files to `lib/`.
Attributes
ask[RW]
files[R]
skip[RW]
source[R]
Source directory, defaults to `src`.
stdout[RW]
target[R]
Target directory, defaults to `lib`.
Public Class Methods
new(options={})
click to toggle source
# File lib/sourcery/caster.rb, line 33 def initialize(options={}) @source = options[:source] || 'src' @target = options[:target] || 'lib' @force = options[:ask] @skip = options[:skip] @stdout = options[:stdout] #@delete = options[:delete] if options[:files] @files = options[:files] else @files = collect_files(source) end end
Public Instance Methods
ask?()
click to toggle source
# File lib/sourcery/caster.rb, line 57 def ask? @ask end
call()
click to toggle source
# File lib/sourcery/caster.rb, line 80 def call ensure_target_directory copy_map = {} files.each do |file| output = target_file(file) if output == file raise "output and source file are identical -- #{file}." end copy_map[file] = output end copy_map.each do |file, output| render(file, output) end end
collect_files(dir)
click to toggle source
Collect all files from source except those starting with an `_` or `.`.
# File lib/sourcery/caster.rb, line 49 def collect_files(dir) Dir[File.join(dir,'**/*')].reject do |f| basename = File.basename(f) basename.start_with?('_') or basename.start_with?('.') end end
context()
click to toggle source
# File lib/sourcery/caster.rb, line 120 def context @context ||= Context.new end
debug?()
click to toggle source
# File lib/sourcery/caster.rb, line 70 def debug? $DEBUG end
ensure_target_directory()
click to toggle source
# File lib/sourcery/caster.rb, line 181 def ensure_target_directory FileUtils.mkdir(target) unless File.directory?(target) end
print_line(label, string)
click to toggle source
# File lib/sourcery/caster.rb, line 154 def print_line(label, string) "%11s %s" % [label, string] end
render(file, output)
click to toggle source
Render and save ERB template `file` to `output` file.
# File lib/sourcery/caster.rb, line 99 def render(file, output) if File.file?(output) && skip? print_line('SKIP', output) else template = ERB.new(File.read(file)) result = template.result(context.__binding__) if stdout puts result else save(result, output, file) end end end
save(text, output, source_file)
click to toggle source
# File lib/sourcery/caster.rb, line 125 def save(text, output, source_file) name = output # relative_path(output) if trial? puts " CAST #{name} (dryrun)" else save = false if File.exist?(output) if FileUtils.uptodate?(output, [source_file]) print_line('UNCHANGED', name) elsif ask? case ask("%11s %s?" % ['OVERWRITE', name]) when 'y', 'yes' save = true end else save = true end else save = true end if save save_file(output, text) puts " CAST #{name}" end end end
save_file(file, text)
click to toggle source
Save file and make it read-only.
# File lib/sourcery/caster.rb, line 159 def save_file(file, text) #File.open(file, 'w'){ |f| << text } mode = nil if File.exist?(file) mode = File.stat(file).mode mask = mode | 0000200 File.chmod(mask, file) # make writeable end File.open(file, 'w'){ |f| f << text } mode = mode || File.stat(file).mode mask = mode & (mode ^ 0000222) File.chmod(mask, file) # make read-only end
skip?()
click to toggle source
# File lib/sourcery/caster.rb, line 62 def skip? @skip end
target_file(file)
click to toggle source
Determine output file name given source `file`.
# File lib/sourcery/caster.rb, line 114 def target_file(file) name = file.sub(source+'/', '') File.join(target, name) end
trial?()
click to toggle source
# File lib/sourcery/caster.rb, line 75 def trial? $TRIAL end