@@ -51,6 +51,42 @@ task :conformance_server do
5151 Conformance ::Server . new ( **options ) . start
5252end
5353
54+ namespace :docs do
55+ desc "Serve the documentation site locally at http://localhost:4000 (PORT)"
56+ task :preview do
57+ docs_dir = File . expand_path ( "docs" , __dir__ )
58+ generate_docs_versions_data ( docs_dir )
59+
60+ env = {
61+ "BUNDLE_GEMFILE" => File . join ( docs_dir , "Gemfile" ) ,
62+ "RUBYOPT" => "-r#{ File . join ( docs_dir , "_preview" , "taint_shim.rb" ) } " ,
63+ }
64+ port = ENV . fetch ( "PORT" , "4000" )
65+
66+ Bundler . with_unbundled_env do
67+ system ( env , "bundle" , "install" , "--quiet" , chdir : docs_dir , exception : true )
68+ system ( env , "bundle" , "exec" , "jekyll" , "serve" , "--port" , port , chdir : docs_dir , exception : true )
69+ rescue Interrupt
70+ # Ctrl-C is the way to stop the preview, not an error.
71+ end
72+ end
73+ end
74+
75+ # Mirrors bin/generate-gh-pages.sh: the released site receives `_data/versions.yml` from
76+ # the version tags at deploy time, and the preview generates the same data so the nav footer
77+ # shows the released-gem version line.
78+ def generate_docs_versions_data ( docs_dir )
79+ versions = %x(git tag --list) . split ( "\n " ) . filter_map { |tag |
80+ tag [ /\A [^0-9]*(\d +\. \d +\. \d +(?:-[a-zA-Z0-9.-]+)?)\z / , 1 ]
81+ } . sort_by { |version |
82+ Gem ::Version . new ( version )
83+ } . reverse
84+ return if versions . empty?
85+
86+ mkdir_p ( File . join ( docs_dir , "_data" ) )
87+ File . write ( File . join ( docs_dir , "_data" , "versions.yml" ) , versions . map { |version | "- #{ version } \n " } . join )
88+ end
89+
5490def npx_available? ( task_name )
5591 return true if system ( "which" , "npx" , out : File ::NULL , err : File ::NULL )
5692
0 commit comments