-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
111 lines (94 loc) · 3.16 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true
require 'yaml'
require 'bundler'
SOURCE_DIR = 'src'
INDEX_HTML = 'files/railties/RDOC_MAIN_md.html'
OLD_INDEX_HTML = 'files/railties/RDOC_MAIN_rdoc.html'
MY_SDOC_BRANCH = 'main'
desc 'Generate documentation for default Rails version and build Jekyll site'
task build: :switch_default_rails do
generate_rails_rdoc
generate_src(target_version: default_rails_version)
sh 'bundle exec jekyll build'
end
desc 'Switch to default Rails version'
task :switch_default_rails do
switch_rails(default_rails_version)
end
desc 'Generate and build documentation for older versions of Rails'
task :build_multi, [:versions] do |_t, args|
rails_versions = config['rails_versions'].reverse_each.to_h # Versions from oldest to newest
unless args[:versions].nil?
versions = args[:versions].split(',')
rails_versions.select! { |version, _| versions.include?(version) }
end
rails_versions.each do |version, detail|
if detail['latest']
puts "=== Skip Rails v#{version} because it's latest version ==="
next
else
puts "=== Build Rails v#{version} documentation ==="
end
dir = "#{SOURCE_DIR}/#{version}"
mkdir dir unless Dir.exist?(dir)
bulid_version = detail['specific_version']
switch_rails(bulid_version)
generate_rails_rdoc
generate_src(target_version: version)
end
puts "=== Build Jekyll site ==="
sh 'bundle exec jekyll build'
end
def config
@config ||= YAML.load_file('./_config.yml')
end
def default_rails_version
config['default_rails_version']
end
def switch_rails(version)
cd 'rails' do
sh 'git fetch'
sh 'git reset --hard'
sh "git switch refs/tags/v#{version} -C v#{version}"
end
end
def generate_rails_rdoc
cd 'rails' do
Bundler.with_unbundled_env do
ENV['BUNDLE_WITHOUT'] = %w[db view job storage cable ujs test rubocop lint mdl].join(':')
# Replace sdoc gem with my forked one
gemfile = File.read('Gemfile')
gemfile.gsub!(/"sdoc".*$/, %("sdoc", github: "toshimaru/sdoc", branch: "#{MY_SDOC_BRANCH}"))
File.write('Gemfile', gemfile)
sh 'bundle install && bundle update sdoc'
rm_rf 'doc'
sh 'bundle exec rake rdoc'
end
end
end
def generate_src(target_version:)
copy_sources = Dir.glob('rails/doc/rdoc/*').reject { |path| path.end_with?('panel', 'js', 'created.rid') }
target_dir = target_version == default_rails_version ? SOURCE_DIR : "#{SOURCE_DIR}/#{target_version}"
remove_existing_files(target_dir)
cp_r copy_sources, target_dir
cd target_dir do
# Generate index.html
if Gem::Version.new(target_version) >= Gem::Version.new('7.1')
cp INDEX_HTML, 'index.html'
else
cp OLD_INDEX_HTML, 'index.html'
end
# Prepend version number to the absolute path in navigation.html
unless target_version == default_rails_version
content = File.read('navigation.html')
content.gsub!('<a href="/', "<a href=\"/#{target_version}/")
File.write('navigation.html', content)
end
end
end
EXISTING_DIR_AND_FILES = %w[classes files index.html navigation.html].freeze
def remove_existing_files(target_dir)
EXISTING_DIR_AND_FILES.each do |dir|
rm_rf "#{target_dir}/#{dir}"
end
end