Class: Sourcery::Caster
- Inherits:
-
Object
- Object
- Sourcery::Caster
- Defined in:
- lib/sourcery/caster.rb
Overview
Spell Caster renders ‘src/` files to `lib/`.
Instance Attribute Summary collapse
-
#ask(prompt = nil) ⇒ Object
Returns the value of attribute ask.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#skip ⇒ Object
Returns the value of attribute skip.
-
#source ⇒ Object
readonly
Source directory, defaults to ‘src`.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
-
#target ⇒ Object
readonly
Target directory, defaults to ‘lib`.
Instance Method Summary collapse
- #ask? ⇒ Boolean
- #call ⇒ Object
-
#collect_files(dir) ⇒ Object
Collect all files from source except those starting with an ‘_` or `.`.
- #context ⇒ Object
- #debug? ⇒ Boolean
- #ensure_target_directory ⇒ Object
-
#initialize(options = {}) ⇒ Caster
constructor
A new instance of Caster.
- #print_line(label, string) ⇒ Object
-
#render(file, output) ⇒ Object
Render and save ERB template ‘file` to `output` file.
- #save(text, output, source_file) ⇒ Object
-
#save_file(file, text) ⇒ Object
Save file and make it read-only.
- #skip? ⇒ Boolean
-
#target_file(file) ⇒ Object
Determine output file name given source ‘file`.
- #trial? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Caster
Returns a new instance of Caster.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sourcery/caster.rb', line 33 def initialize(={}) @source = [:source] || 'src' @target = [:target] || 'lib' @force = [:ask] @skip = [:skip] @stdout = [:stdout] #@delete = options[:delete] if [:files] @files = [:files] else @files = collect_files(source) end end |
Instance Attribute Details
#ask(prompt = nil) ⇒ Object
Returns the value of attribute ask.
21 22 23 |
# File 'lib/sourcery/caster.rb', line 21 def ask @ask end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
18 19 20 |
# File 'lib/sourcery/caster.rb', line 18 def files @files end |
#skip ⇒ Object
Returns the value of attribute skip.
24 25 26 |
# File 'lib/sourcery/caster.rb', line 24 def skip @skip end |
#source ⇒ Object (readonly)
Source directory, defaults to ‘src`.
12 13 14 |
# File 'lib/sourcery/caster.rb', line 12 def source @source end |
#stdout ⇒ Object
Returns the value of attribute stdout.
27 28 29 |
# File 'lib/sourcery/caster.rb', line 27 def stdout @stdout end |
#target ⇒ Object (readonly)
Target directory, defaults to ‘lib`.
15 16 17 |
# File 'lib/sourcery/caster.rb', line 15 def target @target end |
Instance Method Details
#ask? ⇒ Boolean
57 58 59 |
# File 'lib/sourcery/caster.rb', line 57 def ask? @ask end |
#call ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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) ⇒ Object
Collect all files from source except those starting with an ‘_` or `.`.
49 50 51 52 53 54 |
# 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 ⇒ Object
120 121 122 |
# File 'lib/sourcery/caster.rb', line 120 def context @context ||= Context.new end |
#debug? ⇒ Boolean
70 71 72 |
# File 'lib/sourcery/caster.rb', line 70 def debug? $DEBUG end |
#ensure_target_directory ⇒ Object
181 182 183 |
# File 'lib/sourcery/caster.rb', line 181 def ensure_target_directory FileUtils.mkdir(target) unless File.directory?(target) end |
#print_line(label, string) ⇒ Object
154 155 156 |
# File 'lib/sourcery/caster.rb', line 154 def print_line(label, string) "%11s %s" % [label, string] end |
#render(file, output) ⇒ Object
Render and save ERB template ‘file` to `output` file.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# 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) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# 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) ⇒ Object
Save file and make it read-only.
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# 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? ⇒ Boolean
62 63 64 |
# File 'lib/sourcery/caster.rb', line 62 def skip? @skip end |
#target_file(file) ⇒ Object
Determine output file name given source ‘file`.
114 115 116 117 |
# File 'lib/sourcery/caster.rb', line 114 def target_file(file) name = file.sub(source+'/', '') File.join(target, name) end |
#trial? ⇒ Boolean
75 76 77 |
# File 'lib/sourcery/caster.rb', line 75 def trial? $TRIAL end |