Class: YMDP::Compiler::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ymdp/compiler/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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, options={})
  # raise options.inspect
  @domain = domain
  @git_hash = git_hash
  @options = options
  @base_path = options[:base_path]
  @server = options[:server]
  @host = options[: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_pathObject

Returns the value of attribute base_path.



15
16
17
# File 'lib/ymdp/compiler/base.rb', line 15

def base_path
  @base_path
end

#domainObject

Returns the value of attribute domain.



15
16
17
# File 'lib/ymdp/compiler/base.rb', line 15

def domain
  @domain
end

#git_hashObject

Returns the value of attribute git_hash.



15
16
17
# File 'lib/ymdp/compiler/base.rb', line 15

def git_hash
  @git_hash
end

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/ymdp/compiler/base.rb', line 15

def host
  @host
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/ymdp/compiler/base.rb', line 15

def options
  @options
end

#serverObject

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_pathObject



247
248
249
# File 'lib/ymdp/compiler/base.rb', line 247

def app_path
  "#{base_path}/app"
end

#assets_pathObject



255
256
257
# File 'lib/ymdp/compiler/base.rb', line 255

def assets_path
  "#{app_path}/assets"
end

#build?(file) ⇒ Boolean

Build if it’s not a partial and not a layout.

Returns:

  • (Boolean)


239
240
241
# File 'lib/ymdp/compiler/base.rb', line 239

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ymdp/compiler/base.rb', line 121

def build_file(file)
  params = {
    :file => file, 
    :domain => domain, 
    :git_hash => git_hash, 
    :message => options[:message], 
    :verbose => options[: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 =~ /\.coffee$/
      YMDP::Compiler::Template::CoffeeScript.new(params).build
    elsif file =~ /\.js$/
      YMDP::Compiler::Template::JavaScript.new(params).build
    end
  end
end

#clean_domainObject

Creates a fresh destination directory structure for the code to be compiled into.



171
172
173
174
175
176
177
178
179
180
# File 'lib/ymdp/compiler/base.rb', line 171

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

#config_pathObject



243
244
245
# File 'lib/ymdp/compiler/base.rb', line 243

def config_path
  "#{base_path}/config"
end

#copy_authObject



99
100
101
102
103
104
# File 'lib/ymdp/compiler/base.rb', line 99

def copy_auth
  $stdout.puts "Copying auth.xml for #{domain}"
  source_path = "#{config_path}/environments/#{domain}/auth.xml"
  destination_path = "#{servers_path}/#{domain}/auth.xml"
  FileUtils.cp_r(source_path, destination_path)
end

#copy_configObject



92
93
94
95
96
97
# File 'lib/ymdp/compiler/base.rb', line 92

def copy_config
  $stdout.puts "Copying config.xml for #{domain}"
  source_path = "#{config_path}/environments/#{domain}/config.xml"
  destination_path = "#{servers_path}/#{domain}/config.xml"
  FileUtils.cp_r(source_path, destination_path)
end

#copy_config_filesObject

Copy the appropriate version of the configuration files (config.xml, auth.xml) into the compiled source code.



86
87
88
89
90
# File 'lib/ymdp/compiler/base.rb', line 86

def copy_config_files
  copy_config
  copy_auth
  copy_config_images
end

#copy_config_imagesObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/ymdp/compiler/base.rb', line 106

def copy_config_images
  ["full", "icon", "thumbnail"].each do |image|
    source_path = "#{config_path}/environments/#{domain}/#{image}.png"
    destination_path = "#{servers_path}/#{domain}/#{image}.png"
    if File.exists?(source_path)
      $stdout.puts "Copying #{image}.png for #{domain}"
      FileUtils.cp_r(source_path, destination_path)
    end
  end
end

#copy_imagesObject

Images don’t require any processing, just copy them over into this domain’s assets directory.



216
217
218
219
220
221
222
223
# File 'lib/ymdp/compiler/base.rb', line 216

def copy_images
  if options[: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.



205
206
207
208
209
210
211
212
# File 'lib/ymdp/compiler/base.rb', line 205

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


198
199
200
201
# File 'lib/ymdp/compiler/base.rb', line 198

def destination(path)
  destination = path.dup
  destination.gsub!("#{base_path}/app", "#{servers_path}/#{domain}")
end

#images_pathObject



263
264
265
# File 'lib/ymdp/compiler/base.rb', line 263

def images_path
  "#{assets_path}/images"
end

#layout?(file) ⇒ Boolean

A file in the layouts directory is a layout.

Returns:

  • (Boolean)


233
234
235
# File 'lib/ymdp/compiler/base.rb', line 233

def layout?(file)
  file =~ /\/app\/views\/layouts\//
end

#log(text) ⇒ Object

Format text in a standard way for output to the screen.



184
185
186
# File 'lib/ymdp/compiler/base.rb', line 184

def log(text)
  "#{Time.now.to_s} #{text}"
end

#partial?(file) ⇒ Boolean

A filename beginning with an underscore is a partial.

Returns:

  • (Boolean)


227
228
229
# File 'lib/ymdp/compiler/base.rb', line 227

def partial?(file)
  file.split("/").last =~ /^_/
end

#process_allObject

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
63
# 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_config_files
  copy_images
end

#process_all_files(path) ⇒ Object

Process all code files (HTML and JavaScript) into usable, complete HTML files.



78
79
80
81
82
# File 'lib/ymdp/compiler/base.rb', line 78

def process_all_files(path)
  Dir["#{path}**/*"].each do |f|
    build_file(f)
  end
end

#process_all_translationsObject

Convert all YRB translation files from YRB “.pres” format into a single JSON file per language.



145
146
147
148
149
150
# File 'lib/ymdp/compiler/base.rb', line 145

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.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ymdp/compiler/base.rb', line 154

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 = YMDP::Compiler::Template::Yaml.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.



71
72
73
74
# File 'lib/ymdp/compiler/base.rb', line 71

def process_path(path)
  $stdout.puts "Processing #{path} for #{domain}"
  process_all_files(path)
end

#servers_pathObject



251
252
253
# File 'lib/ymdp/compiler/base.rb', line 251

def servers_path
  "#{base_path}/servers"
end

#yrb_pathObject



259
260
261
# File 'lib/ymdp/compiler/base.rb', line 259

def yrb_path
  "#{config_path}/locales"
end