Class: Pixii

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Pixii

Returns a new instance of Pixii.



38
39
40
# File 'lib/pixii.rb', line 38

def initialize(name, options)
  self.name, self.opts, self.steps = name, self.class.defaults.merge(options), []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optsObject

Returns the value of attribute opts.



23
24
25
# File 'lib/pixii.rb', line 23

def opts
  @opts
end

#stepsObject

Returns the value of attribute steps.



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

def steps
  @steps
end

Class Method Details

.called(name, options = {}) {|g, g.opts| ... } ⇒ Object

Yields:



32
33
34
35
36
# File 'lib/pixii.rb', line 32

def self.called(name, options={})
  g = new(name, options)
  g.normalize_opts!
  yield g, g.opts
end

.defaultsObject



26
27
28
# File 'lib/pixii.rb', line 26

def self.defaults
  @@defaults ||= {:project => ARGV.first}
end

.defaults=(hsh) ⇒ Object



30
# File 'lib/pixii.rb', line 30

def self.defaults=(hsh); @@defaults = hsh; end

Instance Method Details

#clone(src, dest) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/pixii.rb', line 65

def clone(src, dest)
  step do
    src = inside(:templates, src) if URI::parse(src).scheme.nil?
    open(src) do |sf|
      File.open(inside(:dest, dest), 'wb') do |f|
        f.write(sf.read)
      end # down
    end   #   down
  end     #     to
end

#dir(*dirs) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/pixii.rb', line 57

def dir(*dirs)
  dirs.each do |d|
    step do
      FileUtils.mkdir_p inside(:dest, d)
    end
  end
end

#inside(dir, *file) ⇒ Object

private



98
99
100
# File 'lib/pixii.rb', line 98

def inside(dir, *file)
  File.join(opts.send("#{dir}_dir"), *file)
end

#magic!Object



88
89
90
91
92
93
94
# File 'lib/pixii.rb', line 88

def magic!
  make_dest
  steps.each do |step|
    puts step.first unless step.first.nil?
    step.last.call
  end
end

#make_destObject



102
103
104
# File 'lib/pixii.rb', line 102

def make_dest
  FileUtils.mkdir_p opts.dest_dir
end

#normalize_opts!Object



46
47
48
49
50
51
# File 'lib/pixii.rb', line 46

def normalize_opts!
  # TODO fix caller[1] hack
  opts.src_dir ||= File.expand_path(File.dirname(caller[1].split(':').first))
  opts.templates_dir = opts.templates || inside(:src, '..', 'templates')
  opts.dest_dir ||= File.expand_path(File.join(Dir.pwd, opts.project))
end

#step(msg = nil, &blk) ⇒ Object



53
54
55
# File 'lib/pixii.rb', line 53

def step(msg=nil, &blk)
  steps << [msg, blk]
end

#template(src_tmpl, dest, env = {}) ⇒ Object

funkytown



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pixii.rb', line 76

def template(src_tmpl, dest, env={})
  step do
    src_tmpl = inside(:templates, src_tmpl)
    tmpl = File.open(src_tmpl){|f| f.read }

    env = env.merge!(opts).to_mash
    File.open(inside(:dest, dest), 'w') do |f|
      f.write(ERB.new(tmpl).result(env.binding))
    end
  end
end