-
Notifications
You must be signed in to change notification settings - Fork 8
/
Rakefile
161 lines (127 loc) · 3.92 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'yaml'
require 'uri'
# use Jekyll configuration file
CONFIG = YAML.load_file("_config.yml")
task :default => :build_dev
# == Helpers ===========================================
def check_configuration
if CONFIG['wikiToJekyll'].nil? or CONFIG['wikiToJekyll'].empty?
raise "Please set your configuration in _config.yml. See the readme."
end
end
# shortener to get configuration parameter
def g(key)
CONFIG['wikiToJekyll'][ key ]
end
def get_wiki_repository_url
derived_url = 'https://github.com/' + g('user_name') + '/' + g('repository_name') + '.wiki.git'
url = g('wiki_repository_url') || derived_url
end
# IMPORTANT ++++++++++++++++
# you submodule MUST be added with the https:// scheme
# git add submoudle https://github.com/userName/RepositoryName.wiki.git
# otherwise you will have github errors
def update_wiki_submodule
cd g('wiki_source') do
pullCommand = 'git pull origin master'
puts "Updating wiki submodule"
output = `#{pullCommand}`
if output.include? 'Already up-to-date'
abort("No update necessary") # exit
end
end
end
def clean_wiki_folders
if File.exist?(g('wiki_dest'))
puts "remove older wiki pages"
Dir.glob("#{g('wiki_dest')}/*.md") do |wikiPage|
puts "removing #{g('wiki_dest')}"
rm_rf wikiPage
end
else
puts "create the dest dir for wiki pages"
FileUtils.mkdir(g('wiki_dest'))
end
end
def copy_wiki_pages
# here we only glob page beginning by a letter
# no _footer.md or thing like this
Dir.glob("#{g('wiki_source')}/[A-Za-z]*.md") do |wikiPage|
wikiPageFileName = File.basename(wikiPage).gsub(" ","-")
wikiPagePath = File.join("#{g('wiki_dest')}", wikiPageFileName)
# remove extension
wikiPageName = wikiPageFileName.sub(/.[^.]+\z/,'')
wikiPageTitle = wikiPageName.gsub("-"," ")
fileContent = File.read(wikiPage)
puts "generating #{wikiPagePath}"
# write the new file with yaml front matter
open(wikiPagePath, 'w') do |newWikiPage|
newWikiPage.puts "---"
newWikiPage.puts "layout: page"
newWikiPage.puts "title: #{wikiPageTitle}"
# used to transform links
newWikiPage.puts "wikiPageName: #{wikiPageName}"
# used to generate a wiki specific menu. see readme
newWikiPage.puts "menu: wiki"
newWikiPage.puts "---"
newWikiPage.puts ""
newWikiPage.puts fileContent
end
end
end
def build_jekyll
system 'jekyll build'
end
def deploy
puts "deploying"
system "git add -A"
message = "Site wiki update #{Time.now.utc}"
puts "\n## Committing: #{message}"
system "git commit -m \"#{message}\""
puts "\n## Pushing website"
system "git push #{g('deploy_remote')} #{g('deploy_branch')}"
puts "\n## Github Pages deploy complete"
end
# synch repository wiki pages with Jekyll
# needs a public wiki
task :wiki do |t|
check_configuration
update_wiki_submodule
Rake::Task[:wikibuild].execute
if g('commit_and_push') == true
deploy
end
puts "Wiki synchronisation success !"
end
# add wiki as a submodule
task :wikisub do |t|
puts "adding wiki as submodule"
check_configuration
wiki_repository = get_wiki_repository_url
command = 'git submodule add ' + wiki_repository + ' ' + g('wiki_source')
command += ' && git submodule init'
command += ' && git submodule update'
puts 'command : ' + command
output = `#{command}`
if output.include? 'failed'
abort("submodule add failed : verify you configuration and that you wiki is public") # exit
end
puts "wiki submodule OK"
end
task :wikibuild do |t|
puts 'rake:wikibuild'
clean_wiki_folders
copy_wiki_pages
build_jekyll
end
task :build_dev do |t|
puts "Building with dev parameters"
sh 'jekyll build --config _config.yml,_config_dev.yml --trace'
end
task :prod do |t|
puts "Building with production parameters"
sh 'jekyll build'
end
task :deploy do |t|
deploy
end