Class: EY::Serverside::RailsAssets

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/engineyard-serverside/rails_assets.rb,
lib/engineyard-serverside/rails_assets/strategy.rb

Defined Under Namespace

Modules: Strategy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, shell, runner) ⇒ RailsAssets

Returns a new instance of RailsAssets.



15
16
17
# File 'lib/engineyard-serverside/rails_assets.rb', line 15

def initialize(config, shell, runner)
  @config, @shell, @runner = config, shell, runner
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/engineyard-serverside/rails_assets.rb', line 13

def config
  @config
end

#runnerObject (readonly)

Returns the value of attribute runner.



13
14
15
# File 'lib/engineyard-serverside/rails_assets.rb', line 13

def runner
  @runner
end

#shellObject (readonly)

Returns the value of attribute shell.



13
14
15
# File 'lib/engineyard-serverside/rails_assets.rb', line 13

def shell
  @shell
end

Class Method Details

.detect_and_compile(*args) ⇒ Object



9
10
11
# File 'lib/engineyard-serverside/rails_assets.rb', line 9

def self.detect_and_compile(*args)
  new(*args).detect_and_compile
end

Instance Method Details

#active_revisionObject



63
64
65
# File 'lib/engineyard-serverside/rails_assets.rb', line 63

def active_revision
  @active_revision ||= config.active_revision
end

#app_assetsObject



139
140
141
# File 'lib/engineyard-serverside/rails_assets.rb', line 139

def app_assets
  File.join('app','assets')
end

#app_assets_pathObject



143
144
145
# File 'lib/engineyard-serverside/rails_assets.rb', line 143

def app_assets_path
  paths.active_release.join(app_assets)
end

#app_disables_assets?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/engineyard-serverside/rails_assets.rb', line 117

def app_disables_assets?
  application_rb_path.open do |fd|
    fd.grep(/^[^#]*config\.assets\.enabled\s*=\s*(false|nil)/).any?
  end
end

#app_has_asset_task?Boolean

This check is very expensive, and has been deemed not worth the time. Leaving this here in case someone comes up with a faster way.

Runs 'rake -T' to see if there is an assets:precompile task.

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/engineyard-serverside/rails_assets.rb', line 127

def app_has_asset_task?
  # We just run this locally on the app master; everybody else should
  # have the same code anyway.
  task_check = "PATH=#{paths.binstubs}:$PATH #{framework_envs} rake -T #{precompile_assets_task} | grep '#{precompile_assets_task}'"
  cmd = "cd #{paths.active_release} && #{task_check}"
  shell.logged_system(cmd).success?
end

#application_rb_pathObject



135
136
137
# File 'lib/engineyard-serverside/rails_assets.rb', line 135

def application_rb_path
  paths.active_release.join('config','application.rb')
end

#asset_strategyObject



147
148
149
# File 'lib/engineyard-serverside/rails_assets.rb', line 147

def asset_strategy
  @asset_strategy ||= RailsAssets::Strategy.fetch(config.asset_strategy, paths, runner)
end

#detect_and_compileObject



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
49
# File 'lib/engineyard-serverside/rails_assets.rb', line 24

def detect_and_compile
  runner.roles asset_roles do
    if precompile_assets?
      if precompile_unchanged_assets?
        shell.status "Precompiling assets without change detection. (precompile_unchanged_assets: true)"
        run_precompile_assets_task
      elsif reuse_assets?
        shell.status "Reusing existing assets. (configured asset_dependencies unchanged from #{previous_revision[0,7]}..#{active_revision[0,7]})"
        asset_strategy.reuse
      else
        shell.status "Precompiling assets. (precompile_assets: true)"
        run_precompile_assets_task
      end
    elsif skip_precompile_assets?
      shell.status "Skipping asset precompilation. (precompile_assets: false)"
    elsif !application_rb_path.readable? || !app_assets_path.directory?
      # Not a Rails app. Ignore assets completely.
    elsif app_disables_assets?
      shell.status "Skipping asset precompilation. ('config/application.rb' disables assets.)"
    elsif paths.public_assets.exist?
      shell.status "Skipping asset precompilation. ('public/assets' directory already exists.)"
    else
      precompile_detected_assets
    end
  end
end

#precompile_detected_assetsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/engineyard-serverside/rails_assets.rb', line 79

def precompile_detected_assets
  shell.status "Precompiling assets. ('#{app_assets}' exists, 'public/assets' not found, not disabled in config.)"
  if !runner.dependency_manager.rails_version
    shell.warning "Precompiling assets even though Rails was not bundled."
  end

  run_precompile_assets_task

  shell.warning <<-WARN
Inferred asset compilation succeeded, but failures may be silently ignored!

ACTION REQUIRED: Add precompile_assets option to ey.yml.
  precompile_assets: true  # precompile assets when asset changes detected
  WARN
rescue EY::Serverside::RemoteFailure => e
  # If we are implicitly precompiling, we want to fail non-destructively
  # because we don't know if the rake task exists or if the user
  # actually intended for assets to be compiled.
  if e.to_s =~ /Don't know how to build task '#{precompile_assets_task}'/
    shell.warning <<-WARN
Asset precompilation detected but compilation failure ignored!
Rake task '#{precompile_assets_task}' was not found.

ACTION REQUIRED: Add precompile_assets option to ey.yml.
  precompile_assets: false # disable assets to avoid this error.
    WARN
  else
    shell.error <<-ERROR
Asset precompilation detected but compilation failed!

ACTION REQUIRED: Add precompile_assets option to ey.yml.
  precompile_assets: true  # precompile assets when asset changes detected
  precompile_assets: false # disable asset compilation.
    ERROR
    raise
  end
end

#previous_revisionObject



59
60
61
# File 'lib/engineyard-serverside/rails_assets.rb', line 59

def previous_revision
  @previous_revision ||= config.previous_revision
end

#reuse_assets?Boolean

Note on reusing assets when assets may fail silently: It's difficult and error prone to reuse assets that may have failed silently in the previous deploy. If the assets are unchanged during this deploy, but failed last deploy, we would incorrectly reuse silentely failed assets. Only reusing when assets are enabled ensures that existing assets were successful.

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/engineyard-serverside/rails_assets.rb', line 73

def reuse_assets?
  previous_revision &&
    active_revision &&
    runner.strategy.same?(previous_revision, active_revision, asset_dependencies)
end

#run_precompile_assets_taskObject



51
52
53
54
55
56
57
# File 'lib/engineyard-serverside/rails_assets.rb', line 51

def run_precompile_assets_task
  asset_strategy.prepare do
    cd   = "cd #{paths.active_release}"
    task = "PATH=#{paths.binstubs}:$PATH #{framework_envs} rake #{precompile_assets_task} RAILS_GROUPS=assets"
    runner.run "#{cd} && #{task}"
  end
end