rails/guides/Rakefile
Hartley McGuire a42863f514
Add rake task to vendor guide javascript
The Rails guides were migrated from Turbolinks to Turbo in
0f6575a5344246d385d1a724e8b0bb3544ab6478

The PR discussion explains that the file was grabbed from unpkg, and a
decision was made to not run it through a minifier.

This commit adds a rake task to automate the process of grabbing the
turbo js file using Importmap::Packager, which is what importmap-rails
uses for `bin/importmap pin --download`.

The extra Importmap module definition is necessary because the
Importmap::Packager file uses the shorthand module syntax, meaning that
an error is thrown if the Importmap module is not previously defined.
`require "importmap-rails"` would normally define this module, but one
of its dependent requires will not load outside of a Rails application.

The turbo.css file was removed as it appears to be leftover from
Turbolinks and Turbo does not provide any css files in its dist.
2023-02-13 00:58:58 -05:00

102 lines
3.0 KiB
Ruby

# frozen_string_literal: true
namespace :guides do
desc 'Generate guides (for authors), use ONLY=foo to process just "foo.md"'
task generate: "generate:html"
namespace :generate do
desc "Generate HTML guides"
task :html do
ruby "-Eutf-8:utf-8", "rails_guides.rb"
end
desc "Generate .mobi file"
task :kindle do
require "active_support/deprecation"
ActiveSupport::Deprecation.warn("The guides:generate:kindle rake task is deprecated and will be removed in 7.2. Run rake guides:generate:epub instead")
Rake::Task["guides:generate:epub"].invoke
end
desc "Generate .epub file"
task :epub do
ENV["EPUB"] = "1"
Rake::Task["guides:generate:html"].invoke
end
end
# Validate guides -------------------------------------------------------------------------
desc 'Validate guides, use ONLY=foo to process just "foo.html"'
task :validate do
ruby "w3c_validator.rb"
end
task :vendor_javascript do
module Importmap; end
require "importmap/packager"
packager = Importmap::Packager.new(vendor_path: "assets/javascripts")
imports = packager.import("@hotwired/turbo", from: "unpkg")
imports.each do |package, url|
umd_url = url.gsub("esm.js", "umd.js")
puts %(Vendoring "#{package}" to #{packager.vendor_path}/#{package}.js via download from #{umd_url})
packager.download(package, umd_url)
end
end
desc "Show help"
task :help do
puts <<HELP
Guides are taken from the source directory, and the result goes into the
output directory. Assets are stored under files, and copied to output/files as
part of the generation process.
You can generate HTML, Kindle or both formats using the `guides:generate` task.
All of these processes are handled via rake tasks, here's a full list of them:
#{%x[rake -T]}
Some arguments may be passed via environment variables:
RAILS_VERSION=tag
If guides are being generated for a specific Rails version set the Git tag
here, otherwise the current SHA1 is going to be used to generate edge guides.
ALL=1
Force generation of all guides.
ONLY=name
Useful if you want to generate only one or a set of guides.
Generate only association_basics.html:
ONLY=assoc
Separate many using commas:
ONLY=assoc,migrations
GUIDES_LANGUAGE
Use it when you want to generate translated guides in
source/<GUIDES_LANGUAGE> folder (such as source/es)
Examples:
$ rake guides:generate ALL=1 RAILS_VERSION=v5.1.0
$ rake guides:generate ONLY=migrations
$ rake guides:generate:epub
$ rake guides:generate GUIDES_LANGUAGE=es
HELP
end
end
task :test do
templates = Dir.glob("bug_report_templates/*.rb")
counter = templates.count do |file|
puts "--- Running #{file}"
Bundler.unbundled_system(Gem.ruby, "-w", file) ||
puts("+++ 💥 FAILED (exit #{$?.exitstatus})")
end
puts "+++ #{counter} / #{templates.size} templates executed successfully"
exit 1 if counter < templates.size
end
task default: "guides:help"