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



78
79
80
# File 'lib/engineyard-serverside/rails_assets.rb', line 78

def active_revision
  @active_revision ||= config.active_revision
end

#app_assets_pathObject



159
160
161
# File 'lib/engineyard-serverside/rails_assets.rb', line 159

def app_assets_path
  paths.path(:active_release,'app','assets')
end

#app_disables_assets?Boolean

Returns:

  • (Boolean)


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

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)


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

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



155
156
157
# File 'lib/engineyard-serverside/rails_assets.rb', line 155

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

#asset_strategyObject



163
164
165
# File 'lib/engineyard-serverside/rails_assets.rb', line 163

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

#detect_and_compileObject



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

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_assetsObject



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/engineyard-serverside/rails_assets.rb', line 95

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_revisionObject



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

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)


88
89
90
91
92
93
# File 'lib/engineyard-serverside/rails_assets.rb', line 88

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_taskObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/engineyard-serverside/rails_assets.rb', line 53

def run_precompile_assets_task
  asset_strategy.prepare do
    cd   = "cd #{paths.active_release}"
    task = "PATH=#{paths.binstubs}:$PATH #{framework_envs} #{precompile_assets_command}"

    # This is a hack right now, but I haven't iterated over it enough for a good solution yet.
    if config.experimental_sync_assets?
      shell.status "Compiling assets once on localhost (experimental_sync_assets: true)"
      compilation_result = shell.logged_system("sh -l -c '#{cd} && #{task}'")
      raise "Assets compilation error" unless compilation_result.success?

      shell.status "Syncing assets to other remote servers (experimental_sync_assets: true)"
      runner.servers.remote.run_for_each do |server|
        server.sync_directory_command(paths.public_assets)
      end
    else
      runner.run "#{cd} && #{task}"
    end
  end
end