class RCompile::Compiler

Constants

XSL

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/rcompile/compiler.rb, line 56
def initialize(options = {})
  @options = options
  set_sh_logger nil
end

Public Instance Methods

clean_directories() click to toggle source
# File lib/rcompile/compiler.rb, line 109
def clean_directories
  exec "rm -rf #{options[:release_dir]} #{options[:asset_dir]}"
end
clean_rails_assets() click to toggle source
# File lib/rcompile/compiler.rb, line 113
def clean_rails_assets
  exec 'bundle exec rake assets:clean'
end
compile() click to toggle source
# File lib/rcompile/compiler.rb, line 82
def compile
  stop_rails_server
  prepare

  start_rails_server
  download_html

  stop_rails_server

  prettify

  path = options[:release_dir].to_s.bold.green
  puts "Saved a version of the website into: #{path}".green
end
copy_gem_fonts() click to toggle source
# File lib/rcompile/compiler.rb, line 125
def copy_gem_fonts
  exec 'cp -r $(bundle show bootstrap-sass-rails)/app/assets/fonts/* public/assets/'
end
create_font_directory() click to toggle source
# File lib/rcompile/compiler.rb, line 121
def create_font_directory
  exec 'mkdir -p public/fonts'
end
download_html() click to toggle source
# File lib/rcompile/compiler.rb, line 133
def download_html
  exec 'wget -H -E -r -l 10 -k -K -m -p -np --restrict-file-names=nocontrol -D localhost -P html -nH localhost:3311'
end
exec(command) click to toggle source
# File lib/rcompile/compiler.rb, line 61
def exec(command)
  command_name = parse_caller(caller(1).first)
  begin
    success = sh(command) == 0
    puts command_name.green if success
  rescue
    puts "#{command_name} failed to run".red
  end

  if options[:fail_on_error] && !success
    $stderr.puts "#{command} failed"
    exit $?.exitstatus
  end
end
parse_caller(at) click to toggle source
# File lib/rcompile/compiler.rb, line 76
def parse_caller(at)
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
    Regexp.last_match[3]
  end
end
precompile_rails_assets() click to toggle source
# File lib/rcompile/compiler.rb, line 117
def precompile_rails_assets
  exec 'bundle exec rake assets:precompile'
end
prepare() click to toggle source
# File lib/rcompile/compiler.rb, line 101
def prepare
  clean_directories
  clean_rails_assets
  precompile_rails_assets
  create_font_directory
  copy_gem_fonts
end
prettify() click to toggle source
# File lib/rcompile/compiler.rb, line 137
def prettify
  prettify_html
  prettify_css
end
prettify_css() click to toggle source
# File lib/rcompile/compiler.rb, line 152
def prettify_css
  css_files_to_prettify.each do |file_name|
    text = File.open(file_name) { |f| f.read }
    engine = Sass::Engine.new(text, syntax: :scss, style: :compress)
    text = engine.render
    engine = Sass::Engine.new(text, syntax: :scss, style: :compact)
    File.open(file_name, 'w') { |f| f.puts engine.render  }
  end
end
prettify_html() click to toggle source
# File lib/rcompile/compiler.rb, line 142
def prettify_html
  html_files_to_prettify.each do |file_name|
    xsl = Nokogiri::XSLT(XSL)
    xml = Nokogiri(File.open(file_name))
    File.open(file_name, 'w') do |f|
      f.write xsl.apply_to(xml).to_s
    end
  end
end
start_rails_server() click to toggle source
# File lib/rcompile/compiler.rb, line 129
def start_rails_server
  exec 'bundle exec rails s -p 3311 -d -e production'
end
stop_rails_server() click to toggle source
# File lib/rcompile/compiler.rb, line 97
def stop_rails_server
  exec "ps -eo pid,command | grep 'rails' | grep -v grep | awk '{print $1}' | xargs kill"
end

Private Instance Methods

css_files_to_prettify() click to toggle source
# File lib/rcompile/compiler.rb, line 168
def css_files_to_prettify
  Dir[ "#{options[:release_dir]}/**/*.css" ].sort.map(&:shellescape)
end
html_files_to_prettify() click to toggle source
# File lib/rcompile/compiler.rb, line 164
def html_files_to_prettify
  Dir[ "#{options[:release_dir]}/**/*.html" ].sort.map(&:shellescape)
end