Class: Sourcery::Caster

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcery/caster.rb

Overview

Spell Caster renders ‘src/` files to `lib/`.

Instance Attribute Summary collapse

Instance Method Summary collapse

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(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

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

#filesObject (readonly)

Returns the value of attribute files.



18
19
20
# File 'lib/sourcery/caster.rb', line 18

def files
  @files
end

#skipObject

Returns the value of attribute skip.



24
25
26
# File 'lib/sourcery/caster.rb', line 24

def skip
  @skip
end

#sourceObject (readonly)

Source directory, defaults to ‘src`.



12
13
14
# File 'lib/sourcery/caster.rb', line 12

def source
  @source
end

#stdoutObject

Returns the value of attribute stdout.



27
28
29
# File 'lib/sourcery/caster.rb', line 27

def stdout
  @stdout
end

#targetObject (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

Returns:

  • (Boolean)


57
58
59
# File 'lib/sourcery/caster.rb', line 57

def ask?
  @ask
end

#callObject



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

#contextObject



120
121
122
# File 'lib/sourcery/caster.rb', line 120

def context
  @context ||= Context.new
end

#debug?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/sourcery/caster.rb', line 70

def debug?
  $DEBUG
end

#ensure_target_directoryObject



181
182
183
# File 'lib/sourcery/caster.rb', line 181

def ensure_target_directory
  FileUtils.mkdir(target) unless File.directory?(target)
end


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

Returns:

  • (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

Returns:

  • (Boolean)


75
76
77
# File 'lib/sourcery/caster.rb', line 75

def trial?
  $TRIAL
end