Class: RCompile::Compiler

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::SH
Defined in:
lib/rcompile/compiler.rb

Constant Summary collapse

XSL =
<<-EOXSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="indent-increment" select="'  '"/>
  <xsl:template name="newline">
<xsl:text disable-output-escaping="yes">
</xsl:text>
  </xsl:template>
  <xsl:template match="comment() | processing-instruction()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:copy />
  </xsl:template>
  <xsl:template match="text()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:value-of select="normalize-space(.)"/>
  </xsl:template>
  <xsl:template match="text()[normalize-space(.)='']"/>
  <xsl:template match="*">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
  <xsl:choose>
   <xsl:when test="count(child::*) > 0">
    <xsl:copy>
     <xsl:copy-of select="@*"/>
     <xsl:apply-templates select="*|text()">
       <xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
     </xsl:apply-templates>
     <xsl:call-template name="newline"/>
     <xsl:value-of select="$indent"/>
    </xsl:copy>
   </xsl:when>
   <xsl:otherwise>
    <xsl:copy-of select="."/>
   </xsl:otherwise>
 </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
EOXSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler

Returns a new instance of Compiler.



56
57
58
59
# File 'lib/rcompile/compiler.rb', line 56

def initialize(options = {})
  @options = options
  set_sh_logger nil
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



54
55
56
# File 'lib/rcompile/compiler.rb', line 54

def options
  @options
end

Instance Method Details

#clean_directoriesObject



109
110
111
# File 'lib/rcompile/compiler.rb', line 109

def clean_directories
  exec "rm -rf #{options[:release_dir]} #{options[:asset_dir]}"
end

#clean_rails_assetsObject



113
114
115
# File 'lib/rcompile/compiler.rb', line 113

def clean_rails_assets
  exec 'bundle exec rake assets:clean'
end

#compileObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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_fontsObject



125
126
127
# 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_directoryObject



121
122
123
# File 'lib/rcompile/compiler.rb', line 121

def create_font_directory
  exec 'mkdir -p public/fonts'
end

#download_htmlObject



133
134
135
# File 'lib/rcompile/compiler.rb', line 133

def download_html
  exec 'wget -H -r -l 10 -k -p -P html -nH localhost:3311'
end

#exec(command) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
80
# File 'lib/rcompile/compiler.rb', line 76

def parse_caller(at)
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
    Regexp.last_match[3]
  end
end

#precompile_rails_assetsObject



117
118
119
# File 'lib/rcompile/compiler.rb', line 117

def precompile_rails_assets
  exec 'bundle exec rake assets:precompile'
end

#prepareObject



101
102
103
104
105
106
107
# File 'lib/rcompile/compiler.rb', line 101

def prepare
  clean_directories
  clean_rails_assets
  precompile_rails_assets
  create_font_directory
  copy_gem_fonts
end

#prettifyObject



137
138
139
140
# File 'lib/rcompile/compiler.rb', line 137

def prettify
  prettify_html
  prettify_css
end

#prettify_cssObject



152
153
154
# File 'lib/rcompile/compiler.rb', line 152

def prettify_css

end

#prettify_htmlObject



142
143
144
145
146
147
148
149
150
# 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_serverObject



129
130
131
# File 'lib/rcompile/compiler.rb', line 129

def start_rails_server
  exec 'bundle exec rails s -p 3311 -d -e production'
end

#stop_rails_serverObject



97
98
99
# 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