2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-23 17:51:31 +08:00
|
|
|
if !defined?(EMBER_CLI)
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
Previously, accessing the Rails app directly in development mode would give you assets from our 'legacy' Ember asset pipeline. The only way to run with Ember CLI assets was to run ember-cli as a proxy. This was quite limiting when working on things which are bypassed when using the ember-cli proxy (e.g. changes to `application.html.erb`). Also, since `ember-auto-import` introduced chunking, visiting `/theme-qunit` under Ember CLI was failing to include all necessary chunks.
This commit teaches Sprockets about our Ember CLI assets so that they can be used in development mode, and are automatically collected up under `/public/assets` during `assets:precompile`. As a bonus, this allows us to remove all the custom manifest modification from `assets:precompile`.
The key changes are:
- Introduce a shared `EmberCli.enabled?` helper
- When ember-cli is enabled, add ember-cli `/dist/assets` as the top-priority Rails asset directory
- Have ember-cli output a `chunks.json` manifest, and teach `preload_script` to read it and append the correct chunks to their associated `afterFile`
- Remove most custom ember-cli logic from the `assets:precompile` step. Instead, rely on Rails to take care of pulling the 'precompiled' assets into the `public/assets` directory. Move the 'renaming' logic to runtime, so it can be used in development mode as well.
- Remove fingerprinting from `ember-cli-build`, and allow Rails to take care of things
Long-term, we may want to replace Sprockets with the lighter-weight Propshaft. The changes made in this commit have been made with that long-term goal in mind.
tldr: when you visit the rails app directly, you'll now be served the current ember-cli assets. To keep these up-to-date make sure either `ember serve`, or `ember build --watch` is running. If you really want to load the old non-ember-cli assets, then you should start the server with `EMBER_CLI_PROD_ASSETS=0`. (the legacy asset pipeline will be removed very soon)
2022-04-21 23:26:34 +08:00
|
|
|
EMBER_CLI = EmberCli.enabled?
|
2022-02-23 17:51:31 +08:00
|
|
|
end
|
2022-02-14 19:49:46 +08:00
|
|
|
|
2013-12-19 09:33:17 +08:00
|
|
|
task 'assets:precompile:before' do
|
2013-11-04 06:58:34 +08:00
|
|
|
|
2014-05-15 13:52:09 +08:00
|
|
|
require 'uglifier'
|
2017-11-24 23:40:49 +08:00
|
|
|
require 'open3'
|
2014-05-15 13:52:09 +08:00
|
|
|
|
2013-12-19 09:33:17 +08:00
|
|
|
unless %w{profile production}.include? Rails.env
|
|
|
|
raise "rake assets:precompile should only be run in RAILS_ENV=production, you are risking unminified assets"
|
2013-11-04 06:58:34 +08:00
|
|
|
end
|
2013-12-19 09:33:17 +08:00
|
|
|
|
2022-02-14 19:49:46 +08:00
|
|
|
if EMBER_CLI && !(ENV["EMBER_CLI_COMPILE_DONE"] == "1")
|
2022-03-16 20:02:21 +08:00
|
|
|
compile_command = "NODE_OPTIONS='--max-old-space-size=2048' yarn --cwd app/assets/javascripts/discourse run ember build -prod"
|
|
|
|
only_assets_precompile_remaining = (ARGV.last == "assets:precompile")
|
|
|
|
|
|
|
|
if only_assets_precompile_remaining
|
|
|
|
# Using exec to free up Rails app memory during ember build
|
|
|
|
exec "#{compile_command} && EMBER_CLI_COMPILE_DONE=1 bin/rake assets:precompile"
|
|
|
|
else
|
|
|
|
system compile_command
|
|
|
|
end
|
2022-02-14 19:49:46 +08:00
|
|
|
end
|
|
|
|
|
2014-02-07 08:36:44 +08:00
|
|
|
# Ensure we ALWAYS do a clean build
|
|
|
|
# We use many .erbs that get out of date quickly, especially with plugins
|
2019-05-09 01:31:13 +08:00
|
|
|
STDERR.puts "Purging temp files"
|
2014-02-07 08:36:44 +08:00
|
|
|
`rm -fr #{Rails.root}/tmp/cache`
|
|
|
|
|
2017-07-05 16:36:06 +08:00
|
|
|
# Ensure we clear emoji cache before pretty-text/emoji/data.js.es6.erb
|
|
|
|
# is recompiled
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
2021-05-05 21:02:48 +08:00
|
|
|
$node_compress = `which terser`.present? && !ENV['SKIP_NODE_UGLIFY']
|
2014-12-12 15:53:26 +08:00
|
|
|
|
2017-03-10 05:44:50 +08:00
|
|
|
unless ENV['USE_SPROCKETS_UGLIFY']
|
|
|
|
$bypass_sprockets_uglify = true
|
2019-05-09 01:31:13 +08:00
|
|
|
Rails.configuration.assets.js_compressor = nil
|
|
|
|
Rails.configuration.assets.gzip = false
|
2017-03-10 05:44:50 +08:00
|
|
|
end
|
|
|
|
|
2019-05-09 01:31:13 +08:00
|
|
|
STDERR.puts "Bundling assets"
|
2014-12-12 15:53:26 +08:00
|
|
|
|
2013-12-19 09:33:17 +08:00
|
|
|
# in the past we applied a patch that removed asset postfixes, but it is terrible practice
|
|
|
|
# leaving very complicated build issues
|
|
|
|
# https://github.com/rails/sprockets-rails/issues/49
|
2014-02-06 13:55:53 +08:00
|
|
|
|
|
|
|
require 'sprockets'
|
|
|
|
require 'digest/sha1'
|
2021-05-05 21:02:48 +08:00
|
|
|
|
2022-02-14 19:49:46 +08:00
|
|
|
if EMBER_CLI
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
Previously, accessing the Rails app directly in development mode would give you assets from our 'legacy' Ember asset pipeline. The only way to run with Ember CLI assets was to run ember-cli as a proxy. This was quite limiting when working on things which are bypassed when using the ember-cli proxy (e.g. changes to `application.html.erb`). Also, since `ember-auto-import` introduced chunking, visiting `/theme-qunit` under Ember CLI was failing to include all necessary chunks.
This commit teaches Sprockets about our Ember CLI assets so that they can be used in development mode, and are automatically collected up under `/public/assets` during `assets:precompile`. As a bonus, this allows us to remove all the custom manifest modification from `assets:precompile`.
The key changes are:
- Introduce a shared `EmberCli.enabled?` helper
- When ember-cli is enabled, add ember-cli `/dist/assets` as the top-priority Rails asset directory
- Have ember-cli output a `chunks.json` manifest, and teach `preload_script` to read it and append the correct chunks to their associated `afterFile`
- Remove most custom ember-cli logic from the `assets:precompile` step. Instead, rely on Rails to take care of pulling the 'precompiled' assets into the `public/assets` directory. Move the 'renaming' logic to runtime, so it can be used in development mode as well.
- Remove fingerprinting from `ember-cli-build`, and allow Rails to take care of things
Long-term, we may want to replace Sprockets with the lighter-weight Propshaft. The changes made in this commit have been made with that long-term goal in mind.
tldr: when you visit the rails app directly, you'll now be served the current ember-cli assets. To keep these up-to-date make sure either `ember serve`, or `ember build --watch` is running. If you really want to load the old non-ember-cli assets, then you should start the server with `EMBER_CLI_PROD_ASSETS=0`. (the legacy asset pipeline will be removed very soon)
2022-04-21 23:26:34 +08:00
|
|
|
# Add ember cli chunks
|
|
|
|
Rails.configuration.assets.precompile.push(
|
2022-05-11 17:23:32 +08:00
|
|
|
*EmberCli.script_chunks.values.flatten.flat_map { |name| ["#{name}.js", "#{name}.map"] }
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
Previously, accessing the Rails app directly in development mode would give you assets from our 'legacy' Ember asset pipeline. The only way to run with Ember CLI assets was to run ember-cli as a proxy. This was quite limiting when working on things which are bypassed when using the ember-cli proxy (e.g. changes to `application.html.erb`). Also, since `ember-auto-import` introduced chunking, visiting `/theme-qunit` under Ember CLI was failing to include all necessary chunks.
This commit teaches Sprockets about our Ember CLI assets so that they can be used in development mode, and are automatically collected up under `/public/assets` during `assets:precompile`. As a bonus, this allows us to remove all the custom manifest modification from `assets:precompile`.
The key changes are:
- Introduce a shared `EmberCli.enabled?` helper
- When ember-cli is enabled, add ember-cli `/dist/assets` as the top-priority Rails asset directory
- Have ember-cli output a `chunks.json` manifest, and teach `preload_script` to read it and append the correct chunks to their associated `afterFile`
- Remove most custom ember-cli logic from the `assets:precompile` step. Instead, rely on Rails to take care of pulling the 'precompiled' assets into the `public/assets` directory. Move the 'renaming' logic to runtime, so it can be used in development mode as well.
- Remove fingerprinting from `ember-cli-build`, and allow Rails to take care of things
Long-term, we may want to replace Sprockets with the lighter-weight Propshaft. The changes made in this commit have been made with that long-term goal in mind.
tldr: when you visit the rails app directly, you'll now be served the current ember-cli assets. To keep these up-to-date make sure either `ember serve`, or `ember build --watch` is running. If you really want to load the old non-ember-cli assets, then you should start the server with `EMBER_CLI_PROD_ASSETS=0`. (the legacy asset pipeline will be removed very soon)
2022-04-21 23:26:34 +08:00
|
|
|
)
|
2021-05-05 21:02:48 +08:00
|
|
|
end
|
2013-11-04 06:58:34 +08:00
|
|
|
end
|
2013-12-19 09:33:17 +08:00
|
|
|
|
2014-05-03 05:46:03 +08:00
|
|
|
task 'assets:precompile:css' => 'environment' do
|
2015-10-13 07:48:21 +08:00
|
|
|
if ENV["DONT_PRECOMPILE_CSS"] == "1"
|
|
|
|
STDERR.puts "Skipping CSS precompilation, ensure CSS lives in a shared directory across hosts"
|
|
|
|
else
|
|
|
|
STDERR.puts "Start compiling CSS: #{Time.zone.now}"
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
2021-07-07 01:11:10 +08:00
|
|
|
# CSS will get precompiled during first request if tables do not exist.
|
2017-04-14 22:33:35 +08:00
|
|
|
if ActiveRecord::Base.connection.table_exists?(Theme.table_name)
|
2021-07-07 01:11:10 +08:00
|
|
|
STDERR.puts "-------------"
|
|
|
|
STDERR.puts "Compiling CSS for #{db} #{Time.zone.now}"
|
2017-05-06 01:50:48 +08:00
|
|
|
begin
|
2021-07-07 01:11:10 +08:00
|
|
|
Stylesheet::Manager.precompile_css if db == "default"
|
|
|
|
Stylesheet::Manager.precompile_theme_css
|
2020-06-02 13:18:03 +08:00
|
|
|
rescue PG::UndefinedColumn, ActiveModel::MissingAttributeError, NoMethodError => e
|
2019-02-07 22:27:42 +08:00
|
|
|
STDERR.puts "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
|
2017-05-06 01:50:48 +08:00
|
|
|
STDERR.puts "Skipping precompilation of CSS cause schema is old, you are precompiling prior to running migrations."
|
|
|
|
end
|
2014-06-13 02:41:37 +08:00
|
|
|
end
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|
2015-10-12 14:31:37 +08:00
|
|
|
|
2015-10-13 07:48:21 +08:00
|
|
|
STDERR.puts "Done compiling CSS: #{Time.zone.now}"
|
|
|
|
end
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|
2020-06-11 04:07:37 +08:00
|
|
|
|
|
|
|
task 'assets:flush_sw' => 'environment' do
|
|
|
|
begin
|
2021-05-26 06:39:31 +08:00
|
|
|
hostname = Discourse.current_hostname
|
|
|
|
default_port = SiteSetting.force_https? ? 443 : 80
|
|
|
|
port = SiteSetting.port.to_i > 0 ? SiteSetting.port : default_port
|
|
|
|
STDERR.puts "Flushing service worker script"
|
|
|
|
`curl -s -m 1 --resolve '#{hostname}:#{port}:127.0.0.1' #{Discourse.base_url}/service-worker.js > /dev/null`
|
|
|
|
STDERR.puts "done"
|
2020-06-11 04:07:37 +08:00
|
|
|
rescue
|
|
|
|
STDERR.puts "Warning: unable to flush service worker script"
|
|
|
|
end
|
|
|
|
end
|
2014-05-03 05:46:03 +08:00
|
|
|
|
2022-02-10 23:35:54 +08:00
|
|
|
def is_ember_cli_asset?(name)
|
2022-02-14 19:49:46 +08:00
|
|
|
return false if !EMBER_CLI
|
2022-03-15 00:11:13 +08:00
|
|
|
%w(
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
Previously, accessing the Rails app directly in development mode would give you assets from our 'legacy' Ember asset pipeline. The only way to run with Ember CLI assets was to run ember-cli as a proxy. This was quite limiting when working on things which are bypassed when using the ember-cli proxy (e.g. changes to `application.html.erb`). Also, since `ember-auto-import` introduced chunking, visiting `/theme-qunit` under Ember CLI was failing to include all necessary chunks.
This commit teaches Sprockets about our Ember CLI assets so that they can be used in development mode, and are automatically collected up under `/public/assets` during `assets:precompile`. As a bonus, this allows us to remove all the custom manifest modification from `assets:precompile`.
The key changes are:
- Introduce a shared `EmberCli.enabled?` helper
- When ember-cli is enabled, add ember-cli `/dist/assets` as the top-priority Rails asset directory
- Have ember-cli output a `chunks.json` manifest, and teach `preload_script` to read it and append the correct chunks to their associated `afterFile`
- Remove most custom ember-cli logic from the `assets:precompile` step. Instead, rely on Rails to take care of pulling the 'precompiled' assets into the `public/assets` directory. Move the 'renaming' logic to runtime, so it can be used in development mode as well.
- Remove fingerprinting from `ember-cli-build`, and allow Rails to take care of things
Long-term, we may want to replace Sprockets with the lighter-weight Propshaft. The changes made in this commit have been made with that long-term goal in mind.
tldr: when you visit the rails app directly, you'll now be served the current ember-cli assets. To keep these up-to-date make sure either `ember serve`, or `ember build --watch` is running. If you really want to load the old non-ember-cli assets, then you should start the server with `EMBER_CLI_PROD_ASSETS=0`. (the legacy asset pipeline will be removed very soon)
2022-04-21 23:26:34 +08:00
|
|
|
discourse.js
|
2022-03-15 00:11:13 +08:00
|
|
|
admin.js
|
|
|
|
ember_jquery.js
|
|
|
|
pretty-text-bundle.js
|
|
|
|
start-discourse.js
|
|
|
|
vendor.js
|
|
|
|
).include?(name) || name.start_with?("chunk.")
|
2022-02-10 23:35:54 +08:00
|
|
|
end
|
|
|
|
|
2014-12-12 15:53:26 +08:00
|
|
|
def assets_path
|
|
|
|
"#{Rails.root}/public/assets"
|
|
|
|
end
|
|
|
|
|
2020-11-23 09:59:45 +08:00
|
|
|
def global_path_klass
|
|
|
|
@global_path_klass ||= Class.new do
|
|
|
|
extend GlobalPath
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cdn_path(p)
|
2020-11-23 10:03:49 +08:00
|
|
|
global_path_klass.cdn_path(p)
|
2020-11-23 09:59:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cdn_relative_path(p)
|
|
|
|
global_path_klass.cdn_relative_path(p)
|
|
|
|
end
|
|
|
|
|
2014-12-12 15:53:26 +08:00
|
|
|
def compress_node(from, to)
|
|
|
|
to_path = "#{assets_path}/#{to}"
|
2016-02-05 10:05:47 +08:00
|
|
|
assets = cdn_relative_path("/assets")
|
2020-01-24 01:44:00 +08:00
|
|
|
assets_additional_path = (d = File.dirname(from)) == "." ? "" : "/#{d}"
|
|
|
|
source_map_root = assets + assets_additional_path
|
2022-02-10 23:37:44 +08:00
|
|
|
source_map_url = "#{File.basename(to)}.map"
|
2020-01-24 01:44:00 +08:00
|
|
|
base_source_map = assets_path + assets_additional_path
|
2014-12-12 15:53:26 +08:00
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
cmd = <<~SH
|
2022-02-12 04:38:53 +08:00
|
|
|
terser '#{assets_path}/#{from}' -m -c -o '#{to_path}' --source-map "base='#{base_source_map}',root='#{source_map_root}',url='#{source_map_url}',includeSources=true"
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
SH
|
2014-12-18 01:14:12 +08:00
|
|
|
|
|
|
|
STDERR.puts cmd
|
2015-09-22 09:28:15 +08:00
|
|
|
result = `#{cmd} 2>&1`
|
|
|
|
unless $?.success?
|
|
|
|
STDERR.puts result
|
|
|
|
exit 1
|
|
|
|
end
|
2014-12-18 01:14:12 +08:00
|
|
|
|
2015-09-22 09:28:15 +08:00
|
|
|
result
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def compress_ruby(from, to)
|
|
|
|
data = File.read("#{assets_path}/#{from}")
|
|
|
|
|
|
|
|
uglified, map = Uglifier.new(comments: :none,
|
2017-03-10 05:44:50 +08:00
|
|
|
source_map: {
|
|
|
|
filename: File.basename(from),
|
|
|
|
output_filename: File.basename(to)
|
|
|
|
}
|
2014-12-12 15:53:26 +08:00
|
|
|
)
|
|
|
|
.compile_with_map(data)
|
|
|
|
dest = "#{assets_path}/#{to}"
|
|
|
|
|
2015-07-22 02:52:54 +08:00
|
|
|
File.write(dest, uglified << "\n//# sourceMappingURL=#{cdn_path "/assets/#{to}.map"}")
|
2014-12-12 15:53:26 +08:00
|
|
|
File.write(dest + ".map", map)
|
2017-03-11 00:35:54 +08:00
|
|
|
|
|
|
|
GC.start
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def gzip(path)
|
2017-11-24 22:52:08 +08:00
|
|
|
STDERR.puts "gzip -f -c -9 #{path} > #{path}.gz"
|
2018-07-04 07:42:21 +08:00
|
|
|
STDERR.puts `gzip -f -c -9 #{path} > #{path}.gz`.strip
|
2017-11-24 22:52:08 +08:00
|
|
|
raise "gzip compression failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
|
2016-06-07 14:55:57 +08:00
|
|
|
end
|
|
|
|
|
2019-04-11 10:36:02 +08:00
|
|
|
# different brotli versions use different parameters
|
2019-05-09 01:31:13 +08:00
|
|
|
def brotli_command(path, max_compress)
|
|
|
|
compression_quality = max_compress ? "11" : "6"
|
|
|
|
"brotli -f --quality=#{compression_quality} #{path} --output=#{path}.br"
|
2017-11-24 23:40:49 +08:00
|
|
|
end
|
|
|
|
|
2019-05-09 01:31:13 +08:00
|
|
|
def brotli(path, max_compress)
|
|
|
|
STDERR.puts brotli_command(path, max_compress)
|
|
|
|
STDERR.puts `#{brotli_command(path, max_compress)}`
|
2019-04-11 10:36:02 +08:00
|
|
|
raise "brotli compression failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
|
|
|
|
STDERR.puts `chmod +r #{path}.br`.strip
|
|
|
|
raise "chmod failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
2019-05-09 01:31:13 +08:00
|
|
|
def max_compress?(path, locales)
|
2019-05-09 01:26:28 +08:00
|
|
|
return false if Rails.configuration.assets.skip_minification.include? path
|
2022-05-11 17:23:32 +08:00
|
|
|
return false if EmberCli.is_ember_cli_asset?(path)
|
2019-05-09 01:26:28 +08:00
|
|
|
return true unless path.include? "locales/"
|
|
|
|
|
|
|
|
path_locale = path.delete_prefix("locales/").delete_suffix(".js")
|
|
|
|
return true if locales.include? path_locale
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2014-12-12 15:53:26 +08:00
|
|
|
def compress(from, to)
|
2021-05-05 21:02:48 +08:00
|
|
|
if $node_compress
|
2014-12-12 15:53:26 +08:00
|
|
|
compress_node(from, to)
|
|
|
|
else
|
|
|
|
compress_ruby(from, to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-20 09:34:30 +08:00
|
|
|
def concurrent?
|
2016-06-07 15:03:05 +08:00
|
|
|
if ENV["SPROCKETS_CONCURRENT"] == "1"
|
2016-04-20 09:34:30 +08:00
|
|
|
concurrent_compressors = []
|
2021-06-03 09:37:06 +08:00
|
|
|
executor = Concurrent::FixedThreadPool.new(Concurrent.processor_count)
|
2018-10-03 16:43:18 +08:00
|
|
|
yield(Proc.new { |&block| concurrent_compressors << Concurrent::Future.execute(executor: executor) { block.call } })
|
2016-04-20 09:34:30 +08:00
|
|
|
concurrent_compressors.each(&:wait!)
|
|
|
|
else
|
|
|
|
yield(Proc.new { |&block| block.call })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-11 10:43:08 +08:00
|
|
|
def current_timestamp
|
|
|
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_task_duration(task_description, &task)
|
|
|
|
task_start = current_timestamp
|
|
|
|
task.call
|
|
|
|
STDERR.puts "Done '#{task_description}' : #{(current_timestamp - task_start).round(2)} secs"
|
|
|
|
STDERR.puts
|
|
|
|
end
|
|
|
|
|
2019-05-28 09:47:58 +08:00
|
|
|
def geolite_dbs
|
|
|
|
@geolite_dbs ||= %w{
|
2019-05-27 14:51:24 +08:00
|
|
|
GeoLite2-City
|
|
|
|
GeoLite2-ASN
|
2019-05-28 09:47:58 +08:00
|
|
|
}
|
|
|
|
end
|
2019-05-27 14:51:24 +08:00
|
|
|
|
2019-05-28 09:47:58 +08:00
|
|
|
def get_mmdb_time(root_path)
|
|
|
|
mmdb_time = nil
|
2019-05-27 14:51:24 +08:00
|
|
|
|
2019-05-28 09:47:58 +08:00
|
|
|
geolite_dbs.each do |name|
|
|
|
|
path = File.join(root_path, "#{name}.mmdb")
|
2019-05-27 14:51:24 +08:00
|
|
|
if File.exist?(path)
|
|
|
|
mmdb_time = File.mtime(path)
|
|
|
|
else
|
|
|
|
mmdb_time = nil
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mmdb_time
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_maxmind(from_path, to_path)
|
|
|
|
puts "Copying MaxMindDB from #{from_path} to #{to_path}"
|
|
|
|
|
2019-05-28 09:47:58 +08:00
|
|
|
geolite_dbs.each do |name|
|
2019-05-27 14:51:24 +08:00
|
|
|
from = File.join(from_path, "#{name}.mmdb")
|
|
|
|
to = File.join(to_path, "#{name}.mmdb")
|
|
|
|
FileUtils.cp(from, to, preserve: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-03 05:46:03 +08:00
|
|
|
task 'assets:precompile' => 'assets:precompile:before' do
|
2019-05-27 14:51:24 +08:00
|
|
|
refresh_days = GlobalSetting.refresh_maxmind_db_during_precompile_days
|
|
|
|
|
|
|
|
if refresh_days.to_i > 0
|
|
|
|
|
|
|
|
mmdb_time = get_mmdb_time(DiscourseIpInfo.path)
|
|
|
|
|
|
|
|
backup_mmdb_time =
|
|
|
|
if GlobalSetting.maxmind_backup_path.present?
|
|
|
|
get_mmdb_time(GlobalSetting.maxmind_backup_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
mmdb_time ||= backup_mmdb_time
|
|
|
|
if backup_mmdb_time && backup_mmdb_time >= mmdb_time
|
|
|
|
copy_maxmind(GlobalSetting.maxmind_backup_path, DiscourseIpInfo.path)
|
2019-06-13 15:41:18 +08:00
|
|
|
mmdb_time = backup_mmdb_time
|
2019-05-27 14:51:24 +08:00
|
|
|
end
|
|
|
|
|
2019-04-10 18:44:58 +08:00
|
|
|
if !mmdb_time || mmdb_time < refresh_days.days.ago
|
2019-04-10 17:37:29 +08:00
|
|
|
puts "Downloading MaxMindDB..."
|
|
|
|
mmdb_thread = Thread.new do
|
2019-05-27 14:51:24 +08:00
|
|
|
begin
|
2019-05-28 09:47:58 +08:00
|
|
|
geolite_dbs.each do |db|
|
|
|
|
DiscourseIpInfo.mmdb_download(db)
|
|
|
|
end
|
2019-05-27 14:51:24 +08:00
|
|
|
|
|
|
|
if GlobalSetting.maxmind_backup_path.present?
|
|
|
|
copy_maxmind(DiscourseIpInfo.path, GlobalSetting.maxmind_backup_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue OpenURI::HTTPError => e
|
|
|
|
STDERR.puts("*" * 100)
|
|
|
|
STDERR.puts("MaxMindDB (#{name}) could not be downloaded: #{e}")
|
|
|
|
STDERR.puts("*" * 100)
|
|
|
|
Rails.logger.warn("MaxMindDB (#{name}) could not be downloaded: #{e}")
|
|
|
|
end
|
2019-04-10 17:37:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-12-12 15:53:26 +08:00
|
|
|
|
2017-03-10 05:44:50 +08:00
|
|
|
if $bypass_sprockets_uglify
|
2014-12-12 15:53:26 +08:00
|
|
|
puts "Compressing Javascript and Generating Source Maps"
|
|
|
|
manifest = Sprockets::Manifest.new(assets_path)
|
2021-05-05 21:02:48 +08:00
|
|
|
|
2019-05-09 01:26:28 +08:00
|
|
|
locales = Set.new(["en"])
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
locales.add(SiteSetting.default_locale)
|
|
|
|
end
|
2015-02-21 04:48:45 +08:00
|
|
|
|
2021-08-11 10:43:08 +08:00
|
|
|
log_task_duration('Done compressing all JS files') {
|
|
|
|
concurrent? do |proc|
|
|
|
|
manifest.files
|
|
|
|
.select { |k, v| k =~ /\.js$/ }
|
|
|
|
.each do |file, info|
|
|
|
|
|
|
|
|
path = "#{assets_path}/#{file}"
|
|
|
|
_file = (d = File.dirname(file)) == "." ? "_#{file}" : "#{d}/_#{File.basename(file)}"
|
|
|
|
_path = "#{assets_path}/#{_file}"
|
|
|
|
max_compress = max_compress?(info["logical_path"], locales)
|
2022-01-06 01:45:08 +08:00
|
|
|
if File.exist?(_path)
|
2021-08-11 10:43:08 +08:00
|
|
|
STDERR.puts "Skipping: #{file} already compressed"
|
|
|
|
elsif file.include? "discourse/tests"
|
|
|
|
STDERR.puts "Skipping: #{file}"
|
|
|
|
else
|
|
|
|
proc.call do
|
|
|
|
log_task_duration(file) {
|
|
|
|
STDERR.puts "Compressing: #{file}"
|
|
|
|
|
|
|
|
if max_compress
|
|
|
|
FileUtils.mv(path, _path)
|
|
|
|
compress(_file, file)
|
|
|
|
end
|
|
|
|
|
|
|
|
info["size"] = File.size(path)
|
|
|
|
info["mtime"] = File.mtime(path).iso8601
|
|
|
|
gzip(path)
|
|
|
|
brotli(path, max_compress)
|
|
|
|
}
|
2016-04-20 09:34:30 +08:00
|
|
|
end
|
|
|
|
end
|
2021-08-11 10:43:08 +08:00
|
|
|
end
|
2016-04-20 09:34:30 +08:00
|
|
|
end
|
2021-08-11 10:43:08 +08:00
|
|
|
}
|
2019-05-09 01:26:28 +08:00
|
|
|
|
2014-12-12 15:53:26 +08:00
|
|
|
# protected
|
|
|
|
manifest.send :save
|
2017-03-21 01:05:39 +08:00
|
|
|
|
2017-03-21 03:59:06 +08:00
|
|
|
if GlobalSetting.fallback_assets_path.present?
|
2017-03-21 01:43:59 +08:00
|
|
|
begin
|
2017-03-21 03:59:06 +08:00
|
|
|
FileUtils.cp_r("#{Rails.root}/public/assets/.", GlobalSetting.fallback_assets_path)
|
2017-03-21 01:43:59 +08:00
|
|
|
rescue => e
|
2017-03-21 03:59:06 +08:00
|
|
|
STDERR.puts "Failed to backup assets to #{GlobalSetting.fallback_assets_path}"
|
2017-03-21 01:43:59 +08:00
|
|
|
STDERR.puts e
|
|
|
|
STDERR.puts e.backtrace
|
|
|
|
end
|
2017-03-21 01:05:39 +08:00
|
|
|
end
|
2017-04-14 22:30:19 +08:00
|
|
|
end
|
2017-03-21 01:05:39 +08:00
|
|
|
|
2019-04-10 17:37:29 +08:00
|
|
|
mmdb_thread.join if mmdb_thread
|
2017-04-14 22:30:19 +08:00
|
|
|
end
|
2017-03-21 01:05:39 +08:00
|
|
|
|
2017-04-14 22:30:19 +08:00
|
|
|
Rake::Task["assets:precompile"].enhance do
|
|
|
|
class Sprockets::Manifest
|
|
|
|
def reload
|
2017-04-15 03:06:52 +08:00
|
|
|
@filename = find_directory_manifest(@directory)
|
2017-04-14 22:30:19 +08:00
|
|
|
@data = json_decode(File.read(@filename))
|
|
|
|
end
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
2017-04-15 03:06:52 +08:00
|
|
|
|
2017-04-14 22:30:19 +08:00
|
|
|
# cause on boot we loaded a blank manifest,
|
|
|
|
# we need to know where all the assets are to precompile CSS
|
|
|
|
# cause CSS uses asset_path
|
|
|
|
Rails.application.assets_manifest.reload
|
|
|
|
Rake::Task["assets:precompile:css"].invoke
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|