Module: Lucky7::Builder

Defined in:
lib/lucky7/builder.rb

Constant Summary collapse

SrcRegex =
/src/
BuildDirectory =
"build"

Class Method Summary collapse

Class Method Details

.buildObject



72
73
74
75
76
77
78
79
80
# File 'lib/lucky7/builder.rb', line 72

def build
  m = modified_files

  build_sass m[:sass]
  build_haml m[:haml]
  build_jabs m[:jabs]
  build_spec m[:spec]
  pack
end

.build_continuously(loop = true) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/lucky7/builder.rb', line 64

def build_continuously loop=true
  cache_mtimes!
  begin
    build
    sleep 1 if loop
  end while loop
end

.build_haml(paths) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/lucky7/builder.rb', line 86

def build_haml paths
  paths.each do |path|
    file= File.new build_path_for(:html, path), 'w'
    haml= File.read(path)
    en= Haml::Engine.new(haml)
    html= en.render haml_render_context
    file.write(html)
    file.close
  end
end

.build_jabs(files) ⇒ Object



119
120
121
# File 'lib/lucky7/builder.rb', line 119

def build_jabs files
  
end

.build_path_for(extension, src_path) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/lucky7/builder.rb', line 131

def build_path_for extension, src_path
  path= File.dirname(src_path).gsub(SrcRegex, BuildDirectory)
  src_path= src_path.split('.').first
  path+= "/#{File.basename(src_path)}.#{extension}"
  ensure_build_path! path
  path
end

.build_sass(paths) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/lucky7/builder.rb', line 97

def build_sass paths
  paths.each do |path|
    file= File.new build_path_for(:css, path), 'w'
    sass= File.read(path)
    en= Sass::Engine.new(sass)
    css= en.render
    file.write(css)
    file.close
  end        
end

.build_spec(paths) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/lucky7/builder.rb', line 108

def build_spec paths
  paths.each do |path|
    file= File.new build_path_for('spec.html', path), 'w'
    jass= File.read(path)
    en= Jass::Engine.new(jass)
    js_spec= en.render
    file.write js_spec
    file.close 
  end
end

.cache_mtimes!Object



47
48
49
# File 'lib/lucky7/builder.rb', line 47

def cache_mtimes!
  @cached_mtimes = mtimes
end

.cached_mtimesObject



51
52
53
# File 'lib/lucky7/builder.rb', line 51

def cached_mtimes
  @cached_mtimes
end

.ensure_build_path!(path) ⇒ Object



127
128
129
# File 'lib/lucky7/builder.rb', line 127

def ensure_build_path! path
  FileUtils.mkdir_p File.dirname(path)
end

.filesObject



26
27
28
29
30
31
32
33
34
# File 'lib/lucky7/builder.rb', line 26

def files
  {:haml=>haml_glob, 
  :sass=>sass_glob,
  :jabs=>jabs_glob,
  :spec=>spec_glob}.inject({}) do |hash, pair|
    hash[pair.first]= Dir.glob(pair.last)
    hash
  end
end

.files_flattenedObject



36
37
38
# File 'lib/lucky7/builder.rb', line 36

def files_flattened
  files.map{|pair| pair.last}.flatten
end

.haml_globObject



10
11
12
# File 'lib/lucky7/builder.rb', line 10

def haml_glob
  "#{Lucky7Root}/**/*.html.haml"
end

.haml_render_contextObject



82
83
84
# File 'lib/lucky7/builder.rb', line 82

def haml_render_context
  Lucky7::Renders
end

.jabs_globObject



18
19
20
# File 'lib/lucky7/builder.rb', line 18

def jabs_glob
  "#{Lucky7Root}/**/*.js.jabs"
end

.modified_filesObject



55
56
57
58
59
60
61
62
# File 'lib/lucky7/builder.rb', line 55

def modified_files
  files.inject({}) do |hash, pair|
    hash[pair.first] = pair.last.select do |file|
      File.stat(file).mtime.to_i > cached_mtimes[file].to_i
    end
    hash
  end 
end

.mtimesObject



40
41
42
43
44
45
# File 'lib/lucky7/builder.rb', line 40

def mtimes
  files_flattened.inject({}) do |hash, filename|
    hash[filename] = File.stat(filename).mtime
    hash
  end
end

.packObject



123
124
125
# File 'lib/lucky7/builder.rb', line 123

def pack
  
end

.sass_globObject



14
15
16
# File 'lib/lucky7/builder.rb', line 14

def sass_glob
  "#{Lucky7Root}/**/*.css.sass"
end

.spec_globObject



22
23
24
# File 'lib/lucky7/builder.rb', line 22

def spec_glob
  "#{Lucky7Root}/**/*.html.jass"
end