Class: Guard::RailsAssets::RailsRunner

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/guard/rails-assets/rails_runner.rb

Constant Summary collapse

@@rails_booted =

Only one rails app is allowed, so make it a class var

false
@@rails_env =
nil
@@digest =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RailsRunner

Returns a new instance of RailsRunner.



11
12
13
14
# File 'lib/guard/rails-assets/rails_runner.rb', line 11

def initialize(options={})
  @@rails_env = (options[:rails_env] || 'test').to_s unless @@rails_booted
  @@digest = options[:digest]
end

Class Method Details

.apply_hacksObject



16
17
18
19
20
# File 'lib/guard/rails-assets/rails_runner.rb', line 16

def self.apply_hacks
  # TODO: Hack due to Rails 3.1 issue: https://github.com/rails/rails/issues/2663#issuecomment-1990121
  ENV["RAILS_GROUPS"] ||= "assets"
  ENV["RAILS_ENV"]    ||= @@rails_env
end

.boot_railsObject

Methods to run the asset pipeline See as a reference github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/guard/rails-assets/rails_runner.rb', line 24

def self.boot_rails
  return if @@rails_booted
  puts "Booting Rails for #{@@rails_env} environment."
  require "fileutils"

  apply_hacks
  require 'rake'
  require "#{Dir.pwd}/config/environment.rb"
  app = ::Rails.application

  app.assets.cache = nil # don't touch my FS pls. (can we use `app.config.assets.cache_store = false` instead)?
  app.load_tasks
  @@rails_booted = true
end

Instance Method Details

#cleanObject



39
40
41
42
43
44
45
# File 'lib/guard/rails-assets/rails_runner.rb', line 39

def clean
  Rake::Task["tmp:cache:clear"].execute
  # copy from the "assets:clean" Rake task
  config = ::Rails.application.config
  public_asset_path = File.join(::Rails.public_path, config.assets.prefix)
  rm_rf public_asset_path, :secure => true
end

#compile_assetsBoolean

Runs the asset pipeline compiler.

Returns:

  • (Boolean)

    Whether the compilation was successful or not



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/guard/rails-assets/rails_runner.rb', line 79

def compile_assets
  self.class.boot_rails
  return false unless @@rails_booted
  begin
    clean
    precompile
    true
  rescue => e
    puts "An error occurred compiling assets: #{e}"
    false
  ensure
    ::Rails.application.assets.instance_eval { expire_index! }
  end
end

#precompileObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/guard/rails-assets/rails_runner.rb', line 47

def precompile
  config = ::Rails.application.config
  unless config.assets.enabled
    warn "Cannot precompile assets if sprockets is disabled. Enabling it."
    config.assets.enabled = true
  end

  # Ensure that action view is loaded and the appropriate
  # sprockets hooks get executed
  _ = ActionView::Base

  digest = @@digest.nil? ? config.assets.digest : @@digest

  config.assets.compile = true
  config.assets.digest  = digest
  config.assets.digests = {}

  env      = ::Rails.application.assets
  target   = File.join(::Rails.public_path, config.assets.prefix)
  compiler = Sprockets::StaticCompiler.new(env,
                                           target,
                                           config.assets.precompile,
                                           :manifest_path => config.assets.manifest,
                                           :digest => config.assets.digest,
                                           :manifest => config.assets.digest.nil?)
  compiler.compile
end