FIX: production css digest not changing

This commit is contained in:
Neil Lalonde 2014-05-29 15:42:50 -04:00
parent 1926702cff
commit d69472c9fb
2 changed files with 28 additions and 14 deletions

View file

@ -3,6 +3,8 @@ require_dependency 'sass/discourse_sass_compiler'
class DiscourseStylesheets class DiscourseStylesheets
CACHE_PATH = 'uploads/stylesheet-cache' CACHE_PATH = 'uploads/stylesheet-cache'
MANIFEST_DIR = "#{Rails.root}/tmp/cache/assets/#{Rails.env}"
MANIFEST_FULL_PATH = "#{MANIFEST_DIR}/stylesheet-manifest"
@lock = Mutex.new @lock = Mutex.new
@ -15,14 +17,36 @@ class DiscourseStylesheets
end end
end end
def self.compile(target = :desktop) def self.compile(target = :desktop, opts={})
@lock.synchronize do @lock.synchronize do
FileUtils.rm(MANIFEST_FULL_PATH, force: true) if opts[:force] # Force a recompile, even in production env
builder = self.new(target) builder = self.new(target)
builder.compile builder.compile
builder.stylesheet_filename builder.stylesheet_filename
end end
end end
def self.last_file_updated
if Rails.env.production?
@last_file_updated ||= if File.exists?(MANIFEST_FULL_PATH)
File.readlines(MANIFEST_FULL_PATH, 'r')[0]
else
mtime = max_file_mtime
FileUtils.mkdir_p(MANIFEST_DIR)
File.open(MANIFEST_FULL_PATH, "w") { |f| f.print(mtime) }
mtime
end
else
max_file_mtime
end
end
def self.max_file_mtime
[ Dir.glob("#{Rails.root}/app/assets/stylesheets/**/*.*css").map {|x| File.mtime(x) }.max,
Dir.glob("#{Rails.root}/plugins/**/assets/stylesheets/**/*.*css").map {|x| File.mtime(x) }.max ].compact.max.to_i
end
def initialize(target = :desktop) def initialize(target = :desktop)
@target = target @target = target
@ -76,21 +100,11 @@ class DiscourseStylesheets
"#{@target}.css" "#{@target}.css"
end end
# digest encodes the things that trigger a recompile
def digest def digest
@digest ||= begin @digest ||= begin
# Watch for file changes unless in production env.
# In production, file changes only happen during deploy, followed by assets:precompile.
last_file_updated = if Rails.env.production?
0
else
[ Dir.glob("#{Rails.root}/app/assets/stylesheets/**/*.*css").map {|x| File.mtime(x) }.max,
Dir.glob("#{Rails.root}/plugins/**/assets/stylesheets/**/*.*css").map {|x| File.mtime(x) }.max ].compact.max.to_i
end
theme = (cs = ColorScheme.enabled) ? "#{cs.id}-#{cs.version}" : 0 theme = (cs = ColorScheme.enabled) ? "#{cs.id}-#{cs.version}" : 0
Digest::SHA1.hexdigest("#{RailsMultisite::ConnectionManagement.current_db}-#{theme}-#{DiscourseStylesheets.last_file_updated}")
# digest encodes the things that trigger a recompile
Digest::SHA1.hexdigest("#{RailsMultisite::ConnectionManagement.current_db}-#{theme}-#{last_file_updated}")
end end
end end
end end

View file

@ -50,7 +50,7 @@ task 'assets:precompile:css' => 'environment' do
RailsMultisite::ConnectionManagement.each_connection do |db| RailsMultisite::ConnectionManagement.each_connection do |db|
puts "Compiling css for #{db}" puts "Compiling css for #{db}"
[:desktop, :mobile].each do |target| [:desktop, :mobile].each do |target|
puts DiscourseStylesheets.compile(target) puts DiscourseStylesheets.compile(target, force: true)
end end
end end
end end