From e2df5cd5720e87fd5a2cc74900011d0ce2ad6513 Mon Sep 17 00:00:00 2001
From: Stan Lo <stan.lo@shopify.com>
Date: Wed, 29 May 2024 19:01:41 +0100
Subject: [PATCH 1/3] Allow installing RDoc from a git source

Co-authored-by: Vinicius Stock <vinistock@users.noreply.github.com>
---
 Rakefile            | 14 ++++++++++++--
 ext/rdoc/extconf.rb |  9 +++++++++
 rdoc.gemspec        |  1 +
 3 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 ext/rdoc/extconf.rb

diff --git a/Rakefile b/Rakefile
index 5977d0adec..6ff7c19820 100644
--- a/Rakefile
+++ b/Rakefile
@@ -88,8 +88,18 @@ parsed_files = PARSER_FILES.map do |parser_file|
 end
 
 task "#{path}.gem" => package_parser_files
-desc "Generate all files used racc and kpeg"
-task :generate => parsed_files
+
+desc "Generate all files use racc and kpeg"
+task :generate do
+  unless ENV.key?('BUNDLE_GEMFILE')
+    Gem.install 'racc', '> 1.4.10'
+    Gem.install 'kpeg', '>= 1.3.3'
+  end
+
+  parsed_files.each do |file_name|
+    Rake::Task[file_name].invoke
+  end
+end
 
 task :clean do
   parsed_files.each do |path|
diff --git a/ext/rdoc/extconf.rb b/ext/rdoc/extconf.rb
new file mode 100644
index 0000000000..676daec19d
--- /dev/null
+++ b/ext/rdoc/extconf.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+# Generate all parse files using racc and kpeg. This is not necessary for regular gem installation, but it is when
+# installing RDoc from the git source. Without this, the generated parse files would not exist and RDoc would not work
+system("rake generate") if Dir.exist?(File.join("..", "..", ".git"))
+
+# RDoc doesn't actually have a native extension, but a Makefile needs to exist in order to successfully install the gem
+require "mkmf"
+create_makefile("rdoc/rdoc")
diff --git a/rdoc.gemspec b/rdoc.gemspec
index 3144df52d6..f9d8668f83 100644
--- a/rdoc.gemspec
+++ b/rdoc.gemspec
@@ -35,6 +35,7 @@ RDoc includes the +rdoc+ and +ri+ tools for generating and displaying documentat
   s.bindir = "exe"
   s.executables = ["rdoc", "ri"]
   s.require_paths = ["lib"]
+  s.extensions = ["ext/rdoc/extconf.rb"]
   # for ruby core repository. It was generated by
   # `git ls-files -z`.split("\x0").each {|f| puts "    #{f.dump}," unless f.start_with?(*%W[test/ spec/ features/ .]) }
   non_lib_files = [

From c75f094920b32465cfc6a081364ae1a5cedb18b5 Mon Sep 17 00:00:00 2001
From: Stan Lo <stan001212@gmail.com>
Date: Sat, 1 Jun 2024 12:11:31 +0100
Subject: [PATCH 2/3] Apply suggestions from code review

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
---
 Rakefile            | 11 +++++------
 ext/rdoc/extconf.rb |  2 +-
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/Rakefile b/Rakefile
index 6ff7c19820..3bb549d4f0 100644
--- a/Rakefile
+++ b/Rakefile
@@ -90,15 +90,14 @@ end
 task "#{path}.gem" => package_parser_files
 
 desc "Generate all files use racc and kpeg"
-task :generate do
-  unless ENV.key?('BUNDLE_GEMFILE')
+task :generate => parsed_files
+
+unless ENV.key?('BUNDLE_GEMFILE')
+  task :gem_install do
     Gem.install 'racc', '> 1.4.10'
     Gem.install 'kpeg', '>= 1.3.3'
   end
-
-  parsed_files.each do |file_name|
-    Rake::Task[file_name].invoke
-  end
+  file parsed_files => :gem_install
 end
 
 task :clean do
diff --git a/ext/rdoc/extconf.rb b/ext/rdoc/extconf.rb
index 676daec19d..3687ff3dd3 100644
--- a/ext/rdoc/extconf.rb
+++ b/ext/rdoc/extconf.rb
@@ -2,7 +2,7 @@
 
 # Generate all parse files using racc and kpeg. This is not necessary for regular gem installation, but it is when
 # installing RDoc from the git source. Without this, the generated parse files would not exist and RDoc would not work
-system("rake generate") if Dir.exist?(File.join("..", "..", ".git"))
+system("rake generate") if File.exist?("../../.git")
 
 # RDoc doesn't actually have a native extension, but a Makefile needs to exist in order to successfully install the gem
 require "mkmf"

From 2e8fea617214de13babb6a1d5031b92d89f727af Mon Sep 17 00:00:00 2001
From: Stan Lo <stan001212@gmail.com>
Date: Sun, 23 Mar 2025 10:48:36 +0000
Subject: [PATCH 3/3] Improve extconf

---
 ext/rdoc/extconf.rb | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/ext/rdoc/extconf.rb b/ext/rdoc/extconf.rb
index 3687ff3dd3..59ffc976c5 100644
--- a/ext/rdoc/extconf.rb
+++ b/ext/rdoc/extconf.rb
@@ -2,7 +2,11 @@
 
 # Generate all parse files using racc and kpeg. This is not necessary for regular gem installation, but it is when
 # installing RDoc from the git source. Without this, the generated parse files would not exist and RDoc would not work
-system("rake generate") if File.exist?("../../.git")
+Dir.chdir(File.expand_path("../..", __dir__)) do
+  if !File.exist?("lib/rdoc/markdown.rb") && Dir.exist?(".git")
+    system("rake generate", exception: true)
+  end
+end
 
 # RDoc doesn't actually have a native extension, but a Makefile needs to exist in order to successfully install the gem
 require "mkmf"