2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-16 03:29:12 +08:00
|
|
|
def public_root
|
|
|
|
"#{Rails.root}/public"
|
|
|
|
end
|
|
|
|
|
2018-10-03 22:15:43 +08:00
|
|
|
def public_js
|
2020-07-16 03:29:12 +08:00
|
|
|
"#{public_root}/javascripts"
|
2018-10-03 22:15:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def vendor_js
|
|
|
|
"#{Rails.root}/vendor/assets/javascripts"
|
|
|
|
end
|
|
|
|
|
|
|
|
def library_src
|
|
|
|
"#{Rails.root}/node_modules"
|
|
|
|
end
|
|
|
|
|
2020-11-25 23:58:19 +08:00
|
|
|
def html_for_section(group)
|
|
|
|
icons = group["icons"].map do |icon|
|
|
|
|
class_attr = icon["diversity"] ? " class=\"diversity\"" : ""
|
|
|
|
" {{replace-emoji \":#{icon['name']}:\" (hash lazy=true#{class_attr})}}"
|
|
|
|
end
|
|
|
|
|
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
|
|
|
<<~HTML
|
2020-11-25 23:58:19 +08:00
|
|
|
<div class="section" data-section="#{group["name"]}">
|
|
|
|
<div class="section-header">
|
|
|
|
<span class="title">{{i18n "emoji_picker.#{group["name"]}"}}</span>
|
|
|
|
</div>
|
|
|
|
<div class="section-group">
|
|
|
|
#{icons.join("\n").strip}
|
|
|
|
</div>
|
|
|
|
</div>
|
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
|
|
|
HTML
|
2020-11-25 23:58:19 +08:00
|
|
|
end
|
|
|
|
|
2020-09-12 01:53:56 +08:00
|
|
|
def write_template(path, task_name, template)
|
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
|
|
|
header = <<~JS
|
2020-05-09 01:25:25 +08:00
|
|
|
// DO NOT EDIT THIS FILE!!!
|
2020-09-12 01:53:56 +08:00
|
|
|
// Update it by running `rake javascript:#{task_name}`
|
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
|
|
|
JS
|
2020-05-12 03:29:46 +08:00
|
|
|
|
2020-05-12 03:43:09 +08:00
|
|
|
basename = File.basename(path)
|
|
|
|
output_path = "#{Rails.root}/app/assets/javascripts/#{path}"
|
|
|
|
|
|
|
|
File.write(output_path, "#{header}\n\n#{template}")
|
|
|
|
puts "#{basename} created"
|
|
|
|
%x{yarn run prettier --write #{output_path}}
|
|
|
|
puts "#{basename} prettified"
|
|
|
|
end
|
2020-05-09 01:25:25 +08:00
|
|
|
|
2020-11-25 23:58:19 +08:00
|
|
|
def write_hbs_template(path, task_name, template)
|
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
|
|
|
header = <<~HBS
|
2020-11-25 23:58:19 +08:00
|
|
|
{{!-- DO NOT EDIT THIS FILE!!! --}}
|
|
|
|
{{!-- Update it by running `rake javascript:#{task_name}` --}}
|
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
|
|
|
HBS
|
2020-11-25 23:58:19 +08:00
|
|
|
|
|
|
|
basename = File.basename(path)
|
|
|
|
output_path = "#{Rails.root}/app/assets/javascripts/#{path}"
|
|
|
|
File.write(output_path, "#{header}\n#{template}")
|
|
|
|
puts "#{basename} created"
|
|
|
|
end
|
|
|
|
|
2020-09-22 03:06:56 +08:00
|
|
|
def dependencies
|
|
|
|
[
|
2018-10-03 22:15:43 +08:00
|
|
|
{
|
2020-08-14 09:53:27 +08:00
|
|
|
source: 'ace-builds/src-min-noconflict/ace.js',
|
2020-09-22 03:06:56 +08:00
|
|
|
destination: 'ace.js',
|
2018-10-03 22:15:43 +08:00
|
|
|
public: true
|
2021-03-01 22:15:17 +08:00
|
|
|
}, {
|
|
|
|
source: '@json-editor/json-editor/dist/jsoneditor.js',
|
|
|
|
package_name: '@json-editor/json-editor',
|
|
|
|
public: true
|
2018-10-03 22:15:43 +08:00
|
|
|
}, {
|
2022-06-16 01:49:04 +08:00
|
|
|
source: 'chart.js/dist/chart.min.js',
|
2018-10-03 22:15:43 +08:00
|
|
|
public: true
|
2020-08-06 23:57:06 +08:00
|
|
|
}, {
|
|
|
|
source: 'chartjs-plugin-datalabels/dist/chartjs-plugin-datalabels.min.js',
|
2021-02-18 22:07:26 +08:00
|
|
|
public: true
|
|
|
|
}, {
|
|
|
|
source: 'diffhtml/dist/diffhtml.min.js',
|
2020-08-06 23:57:06 +08:00
|
|
|
public: true
|
2018-10-03 22:15:43 +08:00
|
|
|
}, {
|
|
|
|
source: 'magnific-popup/dist/jquery.magnific-popup.min.js',
|
|
|
|
public: true
|
|
|
|
}, {
|
|
|
|
source: 'pikaday/pikaday.js',
|
|
|
|
public: true
|
2019-01-15 01:46:35 +08:00
|
|
|
}, {
|
2021-03-19 06:21:23 +08:00
|
|
|
source: '@highlightjs/cdn-assets/.',
|
2019-01-15 01:46:35 +08:00
|
|
|
destination: 'highlightjs'
|
2018-10-03 22:15:43 +08:00
|
|
|
}, {
|
|
|
|
source: 'markdown-it/dist/markdown-it.js'
|
2019-02-13 02:57:52 +08:00
|
|
|
}, {
|
|
|
|
source: 'moment/moment.js'
|
|
|
|
}, {
|
2019-02-15 00:05:28 +08:00
|
|
|
source: 'moment/locale/.',
|
2019-02-13 02:57:52 +08:00
|
|
|
destination: 'moment-locale',
|
|
|
|
}, {
|
2019-05-08 11:42:49 +08:00
|
|
|
source: 'moment-timezone/builds/moment-timezone-with-data-10-year-range.js',
|
|
|
|
destination: 'moment-timezone-with-data.js'
|
2019-02-26 03:40:02 +08:00
|
|
|
}, {
|
2022-02-26 09:06:12 +08:00
|
|
|
source: '@discourse/moment-timezone-names-translations/locales/.',
|
2019-02-26 03:40:02 +08:00
|
|
|
destination: 'moment-timezone-names-locale'
|
2019-07-16 00:05:55 +08:00
|
|
|
}, {
|
|
|
|
source: 'workbox-sw/build/.',
|
|
|
|
destination: 'workbox',
|
2020-09-22 03:06:56 +08:00
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
2019-07-16 00:05:55 +08:00
|
|
|
}, {
|
|
|
|
source: 'workbox-routing/build/.',
|
|
|
|
destination: 'workbox',
|
2020-09-22 03:06:56 +08:00
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
2019-07-16 00:05:55 +08:00
|
|
|
}, {
|
|
|
|
source: 'workbox-core/build/.',
|
|
|
|
destination: 'workbox',
|
2020-09-22 03:06:56 +08:00
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
2019-07-16 00:05:55 +08:00
|
|
|
}, {
|
|
|
|
source: 'workbox-strategies/build/.',
|
|
|
|
destination: 'workbox',
|
2020-09-22 03:06:56 +08:00
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
2019-07-16 00:05:55 +08:00
|
|
|
}, {
|
|
|
|
source: 'workbox-expiration/build/.',
|
|
|
|
destination: 'workbox',
|
2020-09-22 03:06:56 +08:00
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
2021-02-02 14:08:29 +08:00
|
|
|
}, {
|
|
|
|
source: 'workbox-cacheable-response/build/.',
|
|
|
|
destination: 'workbox',
|
2021-03-01 22:15:17 +08:00
|
|
|
skip_versioning: true,
|
2021-02-02 14:08:29 +08:00
|
|
|
public: true
|
2021-04-29 04:12:08 +08:00
|
|
|
},
|
2021-06-23 23:31:12 +08:00
|
|
|
{
|
|
|
|
source: 'squoosh/codecs/mozjpeg/enc/mozjpeg_enc.js',
|
|
|
|
destination: 'squoosh',
|
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: 'squoosh/codecs/mozjpeg/enc/mozjpeg_enc.wasm',
|
|
|
|
destination: 'squoosh',
|
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: 'squoosh/codecs/resize/pkg/squoosh_resize.js',
|
|
|
|
destination: 'squoosh',
|
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: 'squoosh/codecs/resize/pkg/squoosh_resize_bg.wasm',
|
|
|
|
destination: 'squoosh',
|
|
|
|
public: true,
|
|
|
|
skip_versioning: true
|
2021-07-07 08:39:33 +08:00
|
|
|
}
|
2018-10-03 22:15:43 +08:00
|
|
|
]
|
2020-09-22 03:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def node_package_name(f)
|
2021-03-01 22:15:17 +08:00
|
|
|
f[:package_name] || f[:source].split('/').first
|
2020-09-22 03:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_path_name(f)
|
|
|
|
f[:destination] || node_package_name(f)
|
|
|
|
end
|
|
|
|
|
2022-05-02 23:10:26 +08:00
|
|
|
def absolute_sourcemap(dest)
|
|
|
|
File.open(dest) do |file|
|
|
|
|
contents = file.read
|
|
|
|
contents.gsub!(/sourceMappingURL=(.*)/, 'sourceMappingURL=/\1')
|
|
|
|
File.open(dest, "w+") { |d| d.write(contents) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-22 03:06:56 +08:00
|
|
|
task 'javascript:update_constants' => :environment do
|
|
|
|
task_name = 'update_constants'
|
|
|
|
|
|
|
|
write_template("discourse/app/lib/constants.js", task_name, <<~JS)
|
|
|
|
export const SEARCH_PRIORITIES = #{Searchable::PRIORITIES.to_json};
|
|
|
|
|
|
|
|
export const SEARCH_PHRASE_REGEXP = '#{Search::PHRASE_MATCH_REGEXP_PATTERN}';
|
|
|
|
JS
|
|
|
|
|
2020-10-09 03:40:06 +08:00
|
|
|
pretty_notifications = Notification.types.map do |n|
|
|
|
|
" #{n[0]}: #{n[1]},"
|
|
|
|
end.join("\n")
|
|
|
|
|
|
|
|
write_template("discourse/tests/fixtures/concerns/notification-types.js", task_name, <<~JS)
|
|
|
|
export const NOTIFICATION_TYPES = {
|
|
|
|
#{pretty_notifications}
|
|
|
|
};
|
|
|
|
JS
|
|
|
|
|
2020-09-22 03:06:56 +08:00
|
|
|
write_template("pretty-text/addon/emoji/data.js", task_name, <<~JS)
|
|
|
|
export const emojis = #{Emoji.standard.map(&:name).flatten.inspect};
|
|
|
|
export const tonableEmojis = #{Emoji.tonable_emojis.flatten.inspect};
|
|
|
|
export const aliases = #{Emoji.aliases.inspect.gsub("=>", ":")};
|
|
|
|
export const searchAliases = #{Emoji.search_aliases.inspect.gsub("=>", ":")};
|
|
|
|
export const translations = #{Emoji.translations.inspect.gsub("=>", ":")};
|
|
|
|
export const replacements = #{Emoji.unicode_replacements_json};
|
|
|
|
JS
|
|
|
|
|
|
|
|
write_template("pretty-text/addon/emoji/version.js", task_name, <<~JS)
|
|
|
|
export const IMAGE_VERSION = "#{Emoji::EMOJI_VERSION}";
|
|
|
|
JS
|
2020-11-25 23:58:19 +08:00
|
|
|
|
|
|
|
groups_json = JSON.parse(File.read("lib/emoji/groups.json"))
|
|
|
|
|
|
|
|
emoji_buttons = groups_json.map do |group|
|
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
|
|
|
<<~HTML
|
2020-11-25 23:58:19 +08:00
|
|
|
<button type="button" data-section="#{group["name"]}" {{action onCategorySelection "#{group["name"]}"}} class="btn btn-default category-button emoji">
|
|
|
|
{{replace-emoji ":#{group["tabicon"]}:"}}
|
|
|
|
</button>
|
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
|
|
|
HTML
|
2020-11-25 23:58:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
emoji_sections = groups_json.map { |group| html_for_section(group) }
|
|
|
|
|
|
|
|
components_dir = "discourse/app/templates/components"
|
|
|
|
write_hbs_template("#{components_dir}/emoji-group-buttons.hbs", task_name, emoji_buttons.join)
|
|
|
|
write_hbs_template("#{components_dir}/emoji-group-sections.hbs", task_name, emoji_sections.join)
|
|
|
|
|
2020-09-22 03:06:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
task 'javascript:update' => 'clean_up' do
|
|
|
|
require 'uglifier'
|
|
|
|
|
|
|
|
yarn = system("yarn install")
|
|
|
|
abort('Unable to run "yarn install"') unless yarn
|
2018-10-03 22:15:43 +08:00
|
|
|
|
2020-09-12 01:53:56 +08:00
|
|
|
versions = {}
|
2018-10-03 22:15:43 +08:00
|
|
|
start = Time.now
|
|
|
|
|
|
|
|
dependencies.each do |f|
|
|
|
|
src = "#{library_src}/#{f[:source]}"
|
|
|
|
|
|
|
|
unless f[:destination]
|
|
|
|
filename = f[:source].split("/").last
|
|
|
|
else
|
|
|
|
filename = f[:destination]
|
|
|
|
end
|
|
|
|
|
2021-03-19 06:21:23 +08:00
|
|
|
if src.include? "highlightjs"
|
|
|
|
puts "Cleanup highlightjs styles and install smaller test bundle"
|
|
|
|
system("rm -rf node_modules/@highlightjs/cdn-assets/styles")
|
2020-05-29 19:05:44 +08:00
|
|
|
|
2020-07-21 03:34:24 +08:00
|
|
|
# We don't need every language for tests
|
|
|
|
langs = ['javascript', 'sql', 'ruby']
|
2020-05-29 19:05:44 +08:00
|
|
|
test_bundle_dest = 'vendor/assets/javascripts/highlightjs/highlight-test-bundle.min.js'
|
|
|
|
File.write(test_bundle_dest, HighlightJs.bundle(langs))
|
2019-01-15 01:46:35 +08:00
|
|
|
end
|
|
|
|
|
2020-07-16 03:29:12 +08:00
|
|
|
if f[:public_root]
|
|
|
|
dest = "#{public_root}/#{filename}"
|
|
|
|
elsif f[:public]
|
2020-09-22 03:06:56 +08:00
|
|
|
if f[:skip_versioning]
|
|
|
|
dest = "#{public_js}/#{filename}"
|
|
|
|
else
|
|
|
|
package_dir_name = public_path_name(f)
|
|
|
|
package_version = JSON.parse(File.read("#{library_src}/#{node_package_name(f)}/package.json"))["version"]
|
2022-06-16 01:49:04 +08:00
|
|
|
versions[filename.downcase] = "#{package_dir_name}/#{package_version}/#{filename}"
|
2020-09-22 03:06:56 +08:00
|
|
|
|
|
|
|
path = "#{public_js}/#{package_dir_name}/#{package_version}"
|
|
|
|
dest = "#{path}/#{filename}"
|
2020-09-12 01:53:56 +08:00
|
|
|
|
2022-01-06 01:45:08 +08:00
|
|
|
FileUtils.mkdir_p(path) unless File.exist?(path)
|
2020-09-22 03:06:56 +08:00
|
|
|
end
|
2018-10-03 22:15:43 +08:00
|
|
|
else
|
|
|
|
dest = "#{vendor_js}/#{filename}"
|
|
|
|
end
|
|
|
|
|
2020-08-14 09:53:27 +08:00
|
|
|
if src.include? "ace.js"
|
2020-09-22 03:06:56 +08:00
|
|
|
versions["ace/ace.js"] = versions.delete("ace.js")
|
2020-08-14 09:53:27 +08:00
|
|
|
ace_root = "#{library_src}/ace-builds/src-min-noconflict/"
|
2021-12-30 00:02:37 +08:00
|
|
|
addtl_files = [ "ext-searchbox", "mode-html", "mode-scss", "mode-sql", "theme-chrome", "theme-chaos", "worker-html"]
|
2020-09-22 03:06:56 +08:00
|
|
|
dest_path = dest.split('/')[0..-2].join('/')
|
2020-08-14 09:53:27 +08:00
|
|
|
addtl_files.each do |file|
|
2020-09-22 03:06:56 +08:00
|
|
|
FileUtils.cp_r("#{ace_root}#{file}.js", dest_path)
|
2020-08-14 09:53:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-06 01:45:08 +08:00
|
|
|
unless File.exist?(dest)
|
2018-10-03 22:15:43 +08:00
|
|
|
STDERR.puts "New dependency added: #{dest}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if f[:uglify]
|
|
|
|
File.write(dest, Uglifier.new.compile(File.read(src)))
|
|
|
|
else
|
|
|
|
FileUtils.cp_r(src, dest)
|
2021-01-20 21:59:34 +08:00
|
|
|
end
|
2018-10-03 22:15:43 +08:00
|
|
|
end
|
|
|
|
|
2020-09-12 01:53:56 +08:00
|
|
|
write_template("discourse/app/lib/public-js-versions.js", "update", <<~JS)
|
|
|
|
export const PUBLIC_JS_VERSIONS = #{versions.to_json};
|
|
|
|
JS
|
|
|
|
|
2018-10-03 22:15:43 +08:00
|
|
|
STDERR.puts "Completed copying dependencies: #{(Time.now - start).round(2)} secs"
|
|
|
|
end
|
2020-09-22 03:06:56 +08:00
|
|
|
|
|
|
|
task 'javascript:clean_up' do
|
|
|
|
processed = []
|
|
|
|
dependencies.each do |f|
|
|
|
|
next unless f[:public] && !f[:skip_versioning]
|
|
|
|
|
|
|
|
package_dir_name = public_path_name(f)
|
|
|
|
next if processed.include?(package_dir_name)
|
|
|
|
|
|
|
|
versions = Dir["#{File.join(public_js, package_dir_name)}/*"].collect { |p| p.split('/').last }
|
|
|
|
next unless versions.present?
|
|
|
|
|
|
|
|
versions = versions.sort { |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) }
|
|
|
|
puts "Keeping #{package_dir_name} version: #{versions[-1]}"
|
|
|
|
|
|
|
|
# Keep the most recent version
|
|
|
|
versions[0..-2].each do |version|
|
|
|
|
remove_path = File.join(public_js, package_dir_name, version)
|
|
|
|
puts "Removing: #{remove_path}"
|
|
|
|
FileUtils.remove_dir(remove_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
processed << package_dir_name
|
|
|
|
end
|
|
|
|
end
|