Skip to content

Commit f184625

Browse files
committed
[build] implement git portion of documentation updates
1 parent 6d706ea commit f184625

File tree

1 file changed

+118
-9
lines changed

1 file changed

+118
-9
lines changed

Rakefile

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require 'net/telnet'
88
require 'stringio'
99
require 'fileutils'
1010
require 'open-uri'
11+
require 'git'
1112

1213
include Rake::DSL
1314

@@ -50,6 +51,7 @@ $DEBUG = orig_verbose != Rake::FileUtilsExt::DEFAULT
5051
$DEBUG = true if ENV['debug'] == 'true'
5152

5253
verbose($DEBUG)
54+
@git = Git.open(__dir__)
5355

5456
def java_version
5557
File.foreach('java/version.bzl') do |line|
@@ -538,6 +540,11 @@ namespace :node do
538540
desc 'Release Node npm package'
539541
task deploy: :release
540542

543+
desc 'Generate Node documentation — currently not working'
544+
task :docs, [:skip_update] do |_task, arguments|
545+
puts "WARNING — Cannot currently update API Docs for JavaScript bindings"
546+
end
547+
541548
desc 'Update Node version'
542549
task :version, [:version] do |_task, arguments|
543550
old_version = node_version
@@ -579,7 +586,7 @@ namespace :py do
579586
end
580587

581588
desc 'Generate Python documentation'
582-
task :docs do
589+
task :docs, [:skip_update] do |_task, arguments|
583590
FileUtils.rm_rf('build/docs/api/py/')
584591
FileUtils.rm_rf('build/docs/doctrees/')
585592
begin
@@ -588,6 +595,11 @@ namespace :py do
588595
puts 'Ensure that tox is installed on your system'
589596
raise
590597
end
598+
599+
unless arguments[:skip_update]
600+
puts "Updating Python documentation"
601+
puts update_gh_pages ? "Python Docs updated!" : "Python Doc update cancelled"
602+
end
591603
end
592604

593605
desc 'Install Python wheel locally'
@@ -645,10 +657,15 @@ namespace :rb do
645657
end
646658

647659
desc 'Generate Ruby documentation'
648-
task :docs do
660+
task :docs, [:skip_update] do |_task, arguments|
649661
FileUtils.rm_rf('build/docs/api/rb/')
650662
Bazel.execute('run', [], '//rb:docs')
651663
FileUtils.cp_r('bazel-bin/rb/docs.rb.sh.runfiles/selenium/docs/api/rb/.', 'build/docs/api/rb')
664+
665+
unless arguments[:skip_update]
666+
puts "Updating Ruby documentation"
667+
puts update_gh_pages ? "Ruby Docs updated!" : "Ruby Doc update cancelled"
668+
end
652669
end
653670

654671
desc 'Update Ruby version'
@@ -701,7 +718,8 @@ namespace :dotnet do
701718
end
702719

703720
desc 'Generate .NET documentation'
704-
task :docs do
721+
task :docs, [:skip_update] do |_task, arguments|
722+
FileUtils.rm_rf('build/docs/api/dotnet/')
705723
begin
706724
sh 'dotnet tool update -g docfx'
707725
rescue StandardError
@@ -716,11 +734,16 @@ namespace :dotnet do
716734
when 127
717735
raise 'Ensure the dotnet/tools directory is added to your PATH environment variable (e.g., `~/.dotnet/tools`)'
718736
when 255
719-
puts 'Build failed, likely because of DevTools namespacing. This is ok; continuing'
737+
puts '.NET documentation build failed, likely because of DevTools namespacing. This is ok; continuing'
720738
else
721739
raise
722740
end
723741
end
742+
743+
unless arguments[:skip_update]
744+
puts "Updating .NET documentation"
745+
puts update_gh_pages ? ".NET Docs updated!" : ".NET Doc update cancelled"
746+
end
724747
end
725748

726749
desc 'Update .NET version'
@@ -765,7 +788,14 @@ namespace :java do
765788
task install: :'maven-install'
766789

767790
desc 'Generate Java documentation'
768-
task docs: :javadocs
791+
task :docs, [:skip_update] do |_task, arguments|
792+
Rake::Task['javadocs'].invoke
793+
794+
unless arguments[:skip_update]
795+
puts "Updating Java documentation"
796+
puts update_gh_pages ? "Java Docs updated!" : "Java Doc update cancelled"
797+
end
798+
end
769799

770800
desc 'Update Maven dependencies'
771801
task :update do
@@ -839,10 +869,16 @@ end
839869
namespace :all do
840870
desc 'Update all API Documentation'
841871
task :docs do
842-
Rake::Task['java:docs'].invoke
843-
Rake::Task['py:docs'].invoke
844-
Rake::Task['rb:docs'].invoke
845-
Rake::Task['dotnet:docs'].invoke
872+
FileUtils.rm_rf('build/docs/api') if Dir.exist?('build/docs/api')
873+
874+
Rake::Task['java:docs'].invoke(true)
875+
Rake::Task['py:docs'].invoke(true)
876+
Rake::Task['rb:docs'].invoke(true)
877+
Rake::Task['dotnet:docs'].invoke(true)
878+
Rake::Task['node:docs'].invoke(true)
879+
880+
puts "Updating All API Docs"
881+
puts update_gh_pages ? "AP Docs updated!" : "API Doc update cancelled"
846882
end
847883

848884
desc 'Build all artifacts for all language bindings'
@@ -910,3 +946,76 @@ def updated_version(current, desired = nil)
910946
end
911947
version.join('.')
912948
end
949+
950+
def update_gh_pages
951+
origin_reference = @git.current_branch
952+
origin_reference ||= begin
953+
# This allows updating docs from a tagged commit instead of a branch
954+
puts "commit is not at HEAD, checking for matching tag"
955+
tag = @git.tags.detect {|tag| tag.sha == @git.revparse("HEAD") }
956+
tag ? tag.name : raise(StandardError, "Must be on a tagged commit or at the HEAD of a branch to update API Docs")
957+
end
958+
959+
puts "Checking out gh-pages"
960+
begin
961+
@git.checkout('gh-pages')
962+
rescue Git::FailedError => ex
963+
# This happens when the working directory is not clean and things need to be stashed or committed
964+
line = ex.message.lines[2].gsub("output: \"error: ", '')
965+
puts line.gsub('\t', "\t").split('\n')[0...-2].join("\n")
966+
# TODO: we could offer to automatically fix with a stash, but there may be edge cases
967+
print "Manually Fix and Retry? (Y/n):"
968+
response = STDIN.gets.chomp.downcase
969+
return false unless response == 'y' || response == 'yes'
970+
971+
retry
972+
end
973+
974+
puts "Updating gh-pages branch from upstream repository"
975+
begin
976+
@git.pull
977+
rescue Git::FailedError => ex
978+
# This happens when upstream is not already set
979+
line = ex.message.lines[2].gsub("output: \"error: ", '')
980+
puts line.gsub('\t', "\t").split('\n').delete_if(&:empty?)[-2...-1].join("\n")
981+
print "Manually Fix and Retry? (Y/n):"
982+
response = STDIN.gets.chomp.downcase
983+
return restore_git(origin_reference) unless response == 'y' || response == 'yes'
984+
985+
retry
986+
end
987+
988+
%w[java rb py dotnet].each do |language|
989+
if Dir.exist?("build/docs/api/#{language}") && !Dir.empty?("build/docs/api/#{language}")
990+
puts "Deleting #{language} directory in docs/api since corresponding directory in build/docs/api is not empty"
991+
FileUtils.rm_rf("docs/api/#{language}")
992+
puts "Moving documentation files from untracked build directory to tracked docs directory"
993+
FileUtils.mv("build/docs/api/#{language}", "docs/api/#{language}")
994+
end
995+
end
996+
997+
puts "Staging changes for commit"
998+
@git.add('docs/api', all: true)
999+
1000+
print 'Do you want to commit the changes? (Y/n): '
1001+
response = STDIN.gets.chomp.downcase
1002+
return restore_git(origin_reference) unless response == 'y' || response == 'yes'
1003+
1004+
puts "Committing changes"
1005+
@git.commit('updating all API docs')
1006+
1007+
puts "Pushing changes to upstream repository"
1008+
@git.push
1009+
1010+
puts "Checking out originating branch/tag — #{origin_reference}"
1011+
@git.checkout(origin_reference)
1012+
true
1013+
end
1014+
1015+
def restore_git(origin_reference)
1016+
puts "Stashing docs changes for gh-pages"
1017+
Git::Stash.new(@git, "docs changes for gh-pages")
1018+
puts "Checking out originating branch/tag — #{origin_reference}"
1019+
@git.checkout(origin_reference)
1020+
false
1021+
end

0 commit comments

Comments
 (0)