Class: YMDP::Compiler::Base
- Inherits:
-
Object
- Object
- YMDP::Compiler::Base
- Defined in:
- lib/ymdp/compiler/base.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
Returns the value of attribute base_path.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#git_hash ⇒ Object
Returns the value of attribute git_hash.
-
#host ⇒ Object
Returns the value of attribute host.
-
#options ⇒ Object
Returns the value of attribute options.
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
- #app_path ⇒ Object
- #assets_path ⇒ Object
-
#build?(file) ⇒ Boolean
Build if it’s not a partial and not a layout.
-
#build_file(file) ⇒ Object
Build this file if it’s either: * a view, but not a partial or layout, or * a JavaScript file.
-
#clean_domain ⇒ Object
Creates a fresh destination directory structure for the code to be compiled into.
-
#copy_images ⇒ Object
Images don’t require any processing, just copy them over into this domain’s assets directory.
-
#create_directory(path) ⇒ Object
If this directory doesn’t exist, create it and print that it’s being created.
-
#destination(path) ⇒ Object
Convert a file’s path from its source to its destination.
- #images_path ⇒ Object
-
#initialize(domain, git_hash, options = {}) ⇒ Base
constructor
A TemplateCompiler instance covers a single domain, handling all the processing necessary to convert the application source code into usable destination files ready for upload.
-
#layout?(file) ⇒ Boolean
A file in the layouts directory is a layout.
-
#log(text) ⇒ Object
Format text in a standard way for output to the screen.
-
#partial?(file) ⇒ Boolean
A filename beginning with an underscore is a partial.
-
#process_all ⇒ Object
Perform all the processing for a single domain.
-
#process_all_files(path) ⇒ Object
Process all code files (HTML and JavaScript) into usable, complete HTML files.
-
#process_all_translations ⇒ Object
Convert all YRB translation files from YRB “.pres” format into a single JSON file per language.
-
#process_each_yrb(lang) ⇒ Object
Convert the YRB translation files of a single language for this domain into a single JSON file.
-
#process_path(path) ⇒ Object
Do all the processing necessary to convert all the application source code from the given path into usable destination files ready for upload to the server: * create server directory if necessary, * for each file in the source path, build the file, and * copy the images from the source to the destination directory.
- #servers_path ⇒ Object
- #yrb_path ⇒ Object
Constructor Details
#initialize(domain, git_hash, options = {}) ⇒ Base
A TemplateCompiler instance covers a single domain, handling all the processing necessary to convert the application source code into usable destination files ready for upload.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ymdp/compiler/base.rb', line 20 def initialize(domain, git_hash, ={}) # raise options.inspect @domain = domain @git_hash = git_hash @options = @base_path = [:base_path] @server = [:server] @host = [:host] raise ArgumentError.new("domain is required") unless @domain raise ArgumentError.new("server is required") unless @server raise ArgumentError.new("base_path is required") unless @base_path if CONFIG.exists?(@domain) @domain_config = CONFIG[@domain] else @domain_config = CONFIG end @domain_config["validate"]["embedded_js"]["build"] = false if @domain_config["external_assets"]["javascripts"] @domain_config["validate"]["embedded_js"]["deploy"] = false if @domain_config["external_assets"]["javascripts"] YMDP::Base.configure do |config| config.verbose = @domain_config["verbose"] config.compress = @domain_config["compress"] config.validate = @domain_config["validate"] config.external_assets = @domain_config["external_assets"] end end |
Instance Attribute Details
#base_path ⇒ Object
Returns the value of attribute base_path.
15 16 17 |
# File 'lib/ymdp/compiler/base.rb', line 15 def base_path @base_path end |
#domain ⇒ Object
Returns the value of attribute domain.
15 16 17 |
# File 'lib/ymdp/compiler/base.rb', line 15 def domain @domain end |
#git_hash ⇒ Object
Returns the value of attribute git_hash.
15 16 17 |
# File 'lib/ymdp/compiler/base.rb', line 15 def git_hash @git_hash end |
#host ⇒ Object
Returns the value of attribute host.
15 16 17 |
# File 'lib/ymdp/compiler/base.rb', line 15 def host @host end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/ymdp/compiler/base.rb', line 15 def @options end |
#server ⇒ Object
Returns the value of attribute server.
15 16 17 |
# File 'lib/ymdp/compiler/base.rb', line 15 def server @server end |
Instance Method Details
#app_path ⇒ Object
206 207 208 |
# File 'lib/ymdp/compiler/base.rb', line 206 def app_path "#{base_path}/app" end |
#assets_path ⇒ Object
214 215 216 |
# File 'lib/ymdp/compiler/base.rb', line 214 def assets_path "#{app_path}/assets" end |
#build?(file) ⇒ Boolean
Build if it’s not a partial and not a layout.
202 203 204 |
# File 'lib/ymdp/compiler/base.rb', line 202 def build?(file) !partial?(file) && !layout?(file) end |
#build_file(file) ⇒ Object
Build this file if it’s either:
-
a view, but not a partial or layout, or
-
a JavaScript file.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ymdp/compiler/base.rb', line 87 def build_file(file) params = { :file => file, :domain => domain, :git_hash => git_hash, :message => [:message], :verbose => [:verbose], :base_path => base_path, :server => server, :host => host } if build?(file) if file =~ /(\.haml|\.erb)$/ YMDP::Compiler::Template::View.new(params).build elsif file =~ /\.js$/ YMDP::Compiler::Template::JavaScript.new(params).build end end end |
#clean_domain ⇒ Object
Creates a fresh destination directory structure for the code to be compiled into.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ymdp/compiler/base.rb', line 134 def clean_domain dir = "#{servers_path}/#{domain}" FileUtils.rm_rf(Dir.glob("#{dir}/views/*")) FileUtils.rm_rf(Dir.glob("#{dir}/assets/javascripts/*")) FileUtils.rm_rf(Dir.glob("#{dir}/assets/stylesheets/*")) FileUtils.rm_rf(Dir.glob("#{dir}/assets/yrb/*")) FileUtils.rm_rf(Dir.glob("#{TMP_PATH}/*")) FileUtils.mkdir_p(TMP_PATH) FileUtils.mkdir_p("#{dir}/assets/stylesheets/") end |
#copy_images ⇒ Object
Images don’t require any processing, just copy them over into this domain’s assets directory.
179 180 181 182 183 184 185 186 |
# File 'lib/ymdp/compiler/base.rb', line 179 def copy_images if [:verbose] $stdout.puts log("Moving images into #{servers_path}/#{domain}/assets/images...") end FileUtils.rm_rf(Dir.glob("#{servers_path}/#{domain}/assets/images/*")) FileUtils.mkdir_p("#{servers_path}/#{domain}/assets") FileUtils.cp_r("#{images_path}/", "#{servers_path}/#{domain}/assets") end |
#create_directory(path) ⇒ Object
If this directory doesn’t exist, create it and print that it’s being created.
168 169 170 171 172 173 174 175 |
# File 'lib/ymdp/compiler/base.rb', line 168 def create_directory(path) dest = destination(path) unless File.exists?("#{base_path}/#{path}") $stdout.puts " create #{path}" FileUtils.mkdir_p("#{base_path}/#{path}") end end |
#destination(path) ⇒ Object
Convert a file’s path from its source to its destination.
The source directory is in the ‘app’ directory.
The destination directory is made from the ‘servers’ root and the domain name.
For example:
- ./servers/staging
- ./servers/alpha
161 162 163 164 |
# File 'lib/ymdp/compiler/base.rb', line 161 def destination(path) destination = path.dup destination.gsub!("#{base_path}/app", "#{servers_path}/#{domain}") end |
#images_path ⇒ Object
222 223 224 |
# File 'lib/ymdp/compiler/base.rb', line 222 def images_path "#{assets_path}/images" end |
#layout?(file) ⇒ Boolean
A file in the layouts directory is a layout.
196 197 198 |
# File 'lib/ymdp/compiler/base.rb', line 196 def layout?(file) file =~ /\/app\/views\/layouts\// end |
#log(text) ⇒ Object
Format text in a standard way for output to the screen.
147 148 149 |
# File 'lib/ymdp/compiler/base.rb', line 147 def log(text) "#{Time.now.to_s} #{text}" end |
#partial?(file) ⇒ Boolean
A filename beginning with an underscore is a partial.
190 191 192 |
# File 'lib/ymdp/compiler/base.rb', line 190 def partial?(file) file.split("/").last =~ /^_/ end |
#process_all ⇒ Object
Perform all the processing for a single domain.
This is the main method on this object.
54 55 56 57 58 59 60 61 62 |
# File 'lib/ymdp/compiler/base.rb', line 54 def process_all create_directory("servers/#{domain}") clean_domain ["views", "assets"].each do |dir| process_path("#{base_path}/app/#{dir}/") end process_all_translations copy_images end |
#process_all_files(path) ⇒ Object
Process all code files (HTML and JavaScript) into usable, complete HTML files.
77 78 79 80 81 |
# File 'lib/ymdp/compiler/base.rb', line 77 def process_all_files(path) Dir["#{path}**/*"].each do |f| build_file(f) end end |
#process_all_translations ⇒ Object
Convert all YRB translation files from YRB “.pres” format into a single JSON file per language.
109 110 111 112 113 114 |
# File 'lib/ymdp/compiler/base.rb', line 109 def process_all_translations $stdout.puts "Processing ./app/assets/yrb/ for #{domain}" YMDP::ApplicationView.supported_languages.each do |lang| process_each_yrb(lang) end end |
#process_each_yrb(lang) ⇒ Object
Convert the YRB translation files of a single language for this domain into a single JSON file.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ymdp/compiler/base.rb', line 118 def process_each_yrb(lang) tmp_file = "#{TMP_PATH}/keys_#{lang}.pres" # Concatenate together all the YRB ".pres" files for this language into one file in the tmp dir. # F.concat_files("#{yrb_path}/#{lang}/*", tmp_file) yrb = YMDP::Compiler::Template::YRB.new(:file => tmp_file, :domain => domain) yrb.build yrb.validate if CONFIG.validate_json_assets? FileUtils.rm(tmp_file) end |
#process_path(path) ⇒ Object
Do all the processing necessary to convert all the application source code from the given path into usable destination files ready for upload to the server:
-
create server directory if necessary,
-
for each file in the source path, build the file, and
-
copy the images from the source to the destination directory.
70 71 72 73 |
# File 'lib/ymdp/compiler/base.rb', line 70 def process_path(path) $stdout.puts "Processing #{path} for #{domain}" process_all_files(path) end |
#servers_path ⇒ Object
210 211 212 |
# File 'lib/ymdp/compiler/base.rb', line 210 def servers_path "#{base_path}/servers" end |
#yrb_path ⇒ Object
218 219 220 |
# File 'lib/ymdp/compiler/base.rb', line 218 def yrb_path "#{assets_path}/yrb" end |