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



189
190
191
# File 'lib/ymdp/compiler/base.rb', line 189

def app_path
  "#{base_path}/app"
end

#assets_pathObject



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

def assets_path
  "#{app_path}/assets"
end

#build?(file) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ymdp/compiler/base.rb', line 71

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 =~ /\.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.



118
119
120
121
122
123
124
125
126
# File 'lib/ymdp/compiler/base.rb', line 118

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)
end

#copy_imagesObject

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



162
163
164
165
166
167
168
169
# File 'lib/ymdp/compiler/base.rb', line 162

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.



151
152
153
154
155
156
157
158
# File 'lib/ymdp/compiler/base.rb', line 151

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


144
145
146
147
# File 'lib/ymdp/compiler/base.rb', line 144

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

#images_pathObject



205
206
207
# File 'lib/ymdp/compiler/base.rb', line 205

def images_path
  "#{assets_path}/images"
end

#layout?(file) ⇒ Boolean

A file in the layouts directory is a layout.

Returns:

  • (Boolean)


179
180
181
# File 'lib/ymdp/compiler/base.rb', line 179

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

#log(text) ⇒ Object

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



130
131
132
# File 'lib/ymdp/compiler/base.rb', line 130

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

#partial?(file) ⇒ Boolean

A filename beginning with an underscore is a partial.

Returns:

  • (Boolean)


173
174
175
# File 'lib/ymdp/compiler/base.rb', line 173

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.



38
39
40
41
42
43
44
45
46
# File 'lib/ymdp/compiler/base.rb', line 38

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.



61
62
63
64
65
# File 'lib/ymdp/compiler/base.rb', line 61

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.



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

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.



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

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.



54
55
56
57
# File 'lib/ymdp/compiler/base.rb', line 54

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

#servers_pathObject



193
194
195
# File 'lib/ymdp/compiler/base.rb', line 193

def servers_path
  "#{base_path}/servers"
end

#yrb_pathObject



201
202
203
# File 'lib/ymdp/compiler/base.rb', line 201

def yrb_path
  "#{assets_path}/yrb"
end