Class: EY::Serverside::RailsAssets
- Inherits:
-
Object
- Object
- EY::Serverside::RailsAssets
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#runner ⇒ Object
readonly
Returns the value of attribute runner.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Class Method Summary collapse
Instance Method Summary collapse
- #active_revision ⇒ Object
- #app_assets ⇒ Object
- #app_assets_path ⇒ Object
- #app_disables_assets? ⇒ Boolean
-
#app_has_asset_task? ⇒ Boolean
This check is very expensive, and has been deemed not worth the time.
- #application_rb_path ⇒ Object
- #asset_strategy ⇒ Object
- #detect_and_compile ⇒ Object
-
#initialize(config, shell, runner) ⇒ RailsAssets
constructor
A new instance of RailsAssets.
- #precompile_detected_assets ⇒ Object
- #previous_revision ⇒ Object
-
#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.
- #run_precompile_assets_task ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 13 def config @config end |
#runner ⇒ Object (readonly)
Returns the value of attribute runner.
13 14 15 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 13 def runner @runner end |
#shell ⇒ Object (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_revision ⇒ Object
64 65 66 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 64 def active_revision @active_revision ||= config.active_revision end |
#app_assets ⇒ Object
145 146 147 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 145 def app_assets File.join('app','assets') end |
#app_assets_path ⇒ Object
149 150 151 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 149 def app_assets_path paths.active_release.join(app_assets) end |
#app_disables_assets? ⇒ Boolean
123 124 125 126 127 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 123 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.
133 134 135 136 137 138 139 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 133 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_path ⇒ Object
141 142 143 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 141 def application_rb_path paths.active_release.join('config','application.rb') end |
#asset_strategy ⇒ Object
153 154 155 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 153 def asset_strategy @asset_strategy ||= RailsAssets::Strategy.fetch(config.asset_strategy, paths, runner) end |
#detect_and_compile ⇒ Object
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 50 |
# 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 shell.status "Precompiling assets. ('#{app_assets}' exists, 'public/assets' not found, not disabled in config.)" precompile_detected_assets end end end |
#precompile_detected_assets ⇒ Object
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 116 117 118 119 120 121 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 81 def precompile_detected_assets if !runner.rails_application? shell.warning "Precompiling assets even though Rails was not bundled." end run_precompile_assets_task shell.warning <<-WARN Assets were detected and precompiled for this application, but asset precompile failures may be silently ignored in the future. ACTION REQUIRED: Add or update config/ey.yml in your project to ensure assets are compiled every deploy and halted on failure. precompile_assets: true # precompile assets This warning will continue until you update and commit config/ey.yml. 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_revision ⇒ Object
60 61 62 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 60 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.
74 75 76 77 78 79 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 74 def reuse_assets? asset_strategy.reusable? && previous_revision && active_revision && runner.unchanged_diff_between_revisions?(previous_revision, active_revision, asset_dependencies) end |
#run_precompile_assets_task ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/engineyard-serverside/rails_assets.rb', line 52 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 |