diff --git a/.gitignore b/.gitignore index 386798e1f5..9078bf8e8c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /dist/ /doc/ /guides/output/ +/preview/ Brewfile.lock.json debug.log* node_modules/ diff --git a/Rakefile b/Rakefile index a67f8fd028..65a9c7a335 100644 --- a/Rakefile +++ b/Rakefile @@ -5,6 +5,7 @@ require "net/http" $:.unshift __dir__ require "tasks/release" require "railties/lib/rails/api/task" +require "tools/preview_docs" desc "Build gem files for all projects" task build: "all:build" @@ -50,6 +51,20 @@ else Rails::API::StableTask.new("rdoc") end +desc "Generate documentation for previewing" +task :preview_docs do + FileUtils.mkdir_p("preview") + PreviewDocs.new.render("preview") + + require "guides/rails_guides" + Rake::Task[:rdoc].invoke + + FileUtils.cp_r("doc/rdoc", "preview/api") + FileUtils.cp_r("guides/output", "preview/guides") + + system("tar -czf preview.tar.gz preview") +end + desc "Bump all versions to match RAILS_VERSION" task update_versions: "all:update_versions" diff --git a/tools/preview_docs.rb b/tools/preview_docs.rb new file mode 100644 index 0000000000..ed7940277d --- /dev/null +++ b/tools/preview_docs.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require "erb" +require "cgi" + +puts "required tools/preview_docs" + +# How to test: +# +# export BUILDKITE_COMMIT="c8b601a225" +# export BUILDKITE_BUILD_CREATOR="zzak" +# export BUILDKITE_REPO="https://github.com/rails/rails.git" +# export BUILDKITE_BUILD_NUMBER="60" +# export BUILDKITE_BUILD_URL="https://buildkite.com/rails/docs-preview/builds/60" +# export BUILDKITE_BRANCH="preview_docs" +# export BUILDKITE_MESSAGE="commit message" +# export BUILDKITE_PULL_REQUEST="42" +# bundle exec rake preview_docs +# open preview/index.html +class PreviewDocs + attr_reader :commit, :author, :build, :repo, :branch + + def initialize + @commit = link_to(EnvVars.sha[0, 7], "#{EnvVars.repo}/commit/#{EnvVars.sha}") + @author = EnvVars.actor + @build = link_to(EnvVars.build_number, EnvVars.build_url) + @repo = link_to(EnvVars.repo_slug, "#{EnvVars.repo}") + @branch = link_to(EnvVars.branch, "#{EnvVars.repo}/tree/#{EnvVars.branch}") + @message = EnvVars.message || "n/a" + @pull_request = EnvVars.pull_request ? link_to("##{EnvVars.pull_request}", "#{EnvVars.repo}/pull/#{EnvVars.pull_request}") : "n/a" + end + + def render(outdir) + template = File.open("tools/preview_docs/index.html.erb").read + result = ERB.new(template).result(binding) + File.open("#{outdir}/index.html", "w") do |f| + f.write result + end + end + + def link_to(name, url) + "#{escape(name)}" + end + + def escape(str) + CGI.escapeHTML(str) + end +end + +module EnvVars + def self.sha + fetch "BUILDKITE_COMMIT" + end + + def self.actor + fetch "BUILDKITE_BUILD_CREATOR" + end + + def self.repo + fetch("BUILDKITE_REPO").gsub(".git", "") + end + + def self.repo_slug + repo.slice(/\w+\/\w+\Z/) + end + + def self.build_number + fetch "BUILDKITE_BUILD_NUMBER" + end + + def self.build_url + fetch "BUILDKITE_BUILD_URL" + end + + def self.branch + fetch "BUILDKITE_BRANCH" + end + + def self.message + ENV.fetch "BUILDKITE_MESSAGE" + end + + def self.pull_request + pr = ENV.fetch("BUILDKITE_PULL_REQUEST") + pr == "false" ? false : pr + end + + private + def self.fetch(env) + ENV.fetch(env) { raise "#{env} env var undefined!" } + end +end diff --git a/tools/preview_docs/index.html.erb b/tools/preview_docs/index.html.erb new file mode 100644 index 0000000000..21abb4a532 --- /dev/null +++ b/tools/preview_docs/index.html.erb @@ -0,0 +1,109 @@ + + + + + + + + + + + + +
+ +
+ +
+ +
+ +
+ +
+ + +