-
Notifications
You must be signed in to change notification settings - Fork 187
/
publish.rb
executable file
·104 lines (88 loc) · 3.63 KB
/
publish.rb
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
#!/usr/bin/env ruby
#
# publish.rb
#
# This script publishes the ev3dev website to the gh-pages branch of your
# personal fork. This allows you to share a preview of your changes with others.
#
# Usage: ./publish.rb { <gh-user> } [ --ssh ] [ --test '<command>' ] [ --no-fix-links ]
#
# <gh-user> is your github user name.
#
# --ssh will use SSH instead of HTTPS when connecting to GitHub.
#
# --test '<command>' will run <command> in a shell and return the result. The
# working directory will be a temporary directory containing the fixed up files.
#
# --no-fix-links will prevent modification of links in anchor and img elements.
#
# Use BASENAME environment variable to override basename. Use "@FULL_PATH@" as a
# placeholder for the full path to the temporary directory that is created.
require 'tmpdir'
# TODO: We should use a real parser library. This has many ways it could go wrong.
if ARGV.count < 1 || ARGV.include?('--test') && ARGV[ARGV.index('--test') + 1].nil?
STDERR.puts "Usage: ./publish.rb { <gh-user> } [ --ssh ] [ --test '<command>' ] [ --no-fix-links ]"
exit(1)
end
Dir.mktmpdir do |tmp|
FileUtils.cp_r "_site/.", tmp
Dir.chdir tmp
system "git init"
unless ARGV.include? "--test"
gh_user = ARGV[0]
if ARGV.include?("--ssh")
git_url = "[email protected]:#{gh_user}/ev3dev.github.io.git"
else
git_url = "https://github.com/#{gh_user}/ev3dev.github.io.git"
end
system "git remote add origin #{git_url}"
# make sure the remote is reachable before continuing
if not system "git ls-remote #{git_url} HEAD"
STDERR.puts "Repository: #{git_url}"
exit(1)
end
end
# Fix up the files for publishing on gh-pages
# having cname sends you annoying email
FileUtils.rm 'CNAME'
# adding a .nojekyll file disables unnecessary build job on GH Pages
FileUtils.touch '.nojekyll'
system "git add ."
unless ARGV.include? "--no-fix-links"
# prepend ev3dev.github.io to all root-relative urls
basename = ENV['BASENAME'] || "/ev3dev.github.io"
basename = basename.gsub(/@FULL_PATH@/, tmp)
html_file_names = `git ls-files | grep '.html$'`
html_file_names.each_line do |file_name|
file_name = file_name.strip
text = File.read(file_name)
new_contents = text.gsub(/(href|src)="\//, "\\1=\"#{basename}/")
File.open(file_name, "w") { |file| file.puts new_contents }
end
css_file_names = `git ls-files | grep '.css$'`
css_file_names.each_line do |file_name|
file_name = file_name.strip
text = File.read(file_name)
new_contents = text.gsub(/(url\(\s*)"\//, "\\1\"#{basename}/")
File.open(file_name, "w") { |file| file.puts new_contents }
end
# Do the same thing for seach files
file_name = 'javascripts/search.js'
text = File.read(file_name)
new_contents = text.gsub(/(\/search-index.json)/, "#{basename}\\1")
File.open(file_name, "w") { |file| file.puts new_contents }
file_name = 'search-index.json'
text = File.read(file_name)
new_contents = text.gsub(/("href"\s*:\s*")\//, "\\1#{basename}/")
File.open(file_name, "w") { |file| file.puts new_contents }
end
if ARGV.include? '--test'
# run test command
exit(system ARGV[ARGV.index('--test') + 1])
elsif git_url
system "git add ."
message = "Site updated at #{Time.now.utc}"
system "git commit -m #{message.inspect}"
system "git push origin master:refs/heads/gh-pages --force"
end
end