forked from talkable/talkable-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
109 lines (91 loc) · 3.9 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
# frozen_string_literal: true
require 'mkmf'
SPHINX_BUILD = ENV['SPHINX_BUILD'] || 'sphinx-build'
SOURCE_DIR = 'source'
BUILD_DIR = 'build'
SPHINX_OPTS = "-b html -d #{BUILD_DIR}/doctrees #{SOURCE_DIR} #{BUILD_DIR}/html"
task :default => :help
task :help do
system 'rake --tasks'
end
desc 'Set up deploys'
task :setup do
sh 'git remote add staging [email protected]:talkable/void-talkable-docs.git'
end
task :environment do
ENV['LANG'] = ENV['LC_ALL'] = 'en_US.UTF-8'
find_executable(SPHINX_BUILD) || abort("The '#{SPHINX_BUILD}' command was not found. Make sure you have Sphinx installed, then set the SPHINX_BUILD environment variable to point to the full path of the '#{SPHINX_BUILD}' executable. Alternatively you can add the directory with the executable to your PATH. If you do not have Sphinx installed, grab it from http://sphinx-doc.org/")
regexp = /(\d+\.)?(\d+\.)?(\*|\d+)/
required = File.read('requirements.txt').match(regexp).to_s
installed = `#{SPHINX_BUILD} --version`.match(regexp).to_s rescue ''
if !required.empty? && !installed.empty? && Gem::Version.new(required) > Gem::Version.new(installed)
abort "\nYou are running an outdated version of Sphinx #{installed}. Required version is #{required}. Run `pip3 install -r requirements.txt --break-system-packages` to upgrade Sphinx."
end
end
desc 'Run build in test mode'
task :test => :environment do
sh "#{SPHINX_BUILD} -nW #{SPHINX_OPTS}"
end
task :build => :environment do
sh "#{SPHINX_BUILD} #{SPHINX_OPTS}"
Rake::FileList["#{BUILD_DIR}/html/**/*.html"].each do |filename|
File.open(filename, "r+") do |file|
old_content = file.read
new_content = old_content.gsub(%r{<a(.+)href="(https?://(?!(?:www\.)?talkable\.com).*?)"}, '<a\1rel="nofollow" href="\2"')
file.tap(&:rewind).write(new_content) if old_content != new_content
end
end
puts "\nBuild finished. The HTML pages are in #{File.expand_path("#{BUILD_DIR}/html")}."
end
task :clean do
sh "rm -rf #{BUILD_DIR}/*"
end
desc 'Make standalone HTML files'
task :preview => [:clean, :build]
desc 'Run the server on localhost:5001 and open a browser'
task :server => :preview do
sh '(sleep 2 && open "http://localhost:5001")&'
sh 'bundle exec foreman start'
end
desc 'Commit and deploy changes to https://docs.talkable.com'
task :deploy => :'deploy:production'
namespace :deploy do
def deploy(domain:, html_branch:, source_branch:, disallow_robots:, push_command:)
sh "git checkout #{html_branch}"
sh 'git pull'
sh "find . -not -path './.git' -not -path './.git/*' -not -path './Rakefile' -not -path './.circleci' -not -path './.circleci/*' -delete"
sh "git checkout #{source_branch} #{SOURCE_DIR} .gitignore requirements.txt"
sh 'git reset HEAD'
Rake::Task[:build].invoke
sh "(cd #{BUILD_DIR}/html && tar c ./) | (cd ./ && tar xf -)"
sh "rm -rf #{BUILD_DIR} #{SOURCE_DIR} .buildinfo"
File.write('.nojekyll', '')
File.write('CNAME', domain)
File.write('robots.txt', "User-agent: *\nDisallow: /") if disallow_robots
sh 'git add -A'
sh "git commit -m \"Generated gh-pages for `git log #{source_branch} -1 --pretty=short --abbrev-commit`\" && #{push_command} ; git checkout #{source_branch}"
puts "\nDeployment finished. Check updated docs at https://#{domain}"
end
task :production do
deploy(
domain: 'docs.talkable.com',
html_branch: 'gh-pages',
source_branch: 'master',
disallow_robots: false,
push_command: 'git push origin gh-pages'
)
end
desc 'Commit and deploy changes to https://void-docs.talkable.com'
task :staging do
sh 'git remote show staging' do |ok, _|
Rake::Task[:setup].invoke unless ok
end
deploy(
domain: 'void-docs.talkable.com',
html_branch: 'void-gh-pages',
source_branch: 'void',
disallow_robots: true,
push_command: 'git push -f origin void-gh-pages && git push -f staging void-gh-pages:gh-pages'
)
end
end