-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
192 lines (164 loc) · 4.48 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Setup and manage stasis ready github pages directory
#
# provides a couple handy methods for using stasis on github pages to use
# simple copy to a new directory to be used as the base for your github page
#
# rake setup_repository_for_github_user_page
# will initialize the repository as needed
#
# rake publish
# will publish a new version of the site from whatever is generated by stasis
#
# rake development (default so just run rake)
# will run a stasis development server ready on port 3000 locally
#
require 'tmpdir'
desc 'Setup the repository for Github user or organization page run this in an empty folder'
task :setup_repository_for_github_user_page do
`git init`
`git checkout -b source`
write_default_files
`bundle install`
`git add -A`
`git commit -m'setup source branch'`
`git checkout --orphan master`
rm_rf FileList['*']
`touch index.html`
`git add -A`
`git commit -m'setup content branch'`
`git checkout source`
Rake::Task[:publish].invoke
puts <<-INFO
Repository setup locally now create a repository named USERNAME.github.com
and point the origin it's way. For more information check the README
INFO
end
desc 'Generate page'
task :generate do
generate
end
desc 'Remove generated files from public'
task :clean do
rm_rf 'public'
end
desc 'Remove generated files including the stasis base layout'
task :deep_clean do
rm_rf 'public'
rm FileList['controller.rb', 'Gemfile', 'index.html.erb']
end
desc 'Publish updated page'
task :publish => [:generate] do
Dir.mktmpdir { |dir|
mv FileList['public/*'], dir
current_commit = `git rev-parse --short HEAD`
`git stash`
`git checkout master`
rm_rf FileList['*']
mv FileList["#{dir}/*"], '.'
`git add -A`
`git commit -m 'published from #{current_commit}'`
`git checkout source`
`git stash pop`
}
end
desc 'Push live'
task :live do
`git stash`
`git checkout master`
`git push`
`git checkout source`
`git stash pop`
end
desc 'Start development'
task :development do
puts 'Starting stasis watcher and server, visit http://localhost:3000 to view'
pid = fork { exec 'bundle exec stasis -d 3000' }
begin
Process.wait pid
rescue Object
Process.kill 'TERM', pid
Process.wait pid
puts 'Something went wrong, try bundle exec stasis -d 3000 for more information' unless $?.success?
end
end
task :default => :development
# write some default files to have a valid stasis setup
def write_default_files
templates = load_templates
File.open('controller.rb', 'w') { |f| f.write(templates[:controller]) }
File.open('README.md', 'w') { |f| f.write(templates[:readme]) }
File.open('index.html.erb', 'w') { |f| f.write(templates[:index]) }
File.open('Gemfile', 'w') { |f| f.write(templates[:gemfile]) }
end
# generate the public files to serve
def generate
`bundle exec stasis`
end
# Load embedded templates from the file
# added from Sinatra
def load_templates
templates = {}
file = __FILE__
begin
io = ::IO.respond_to?(:binread) ? ::IO.binread(file) : ::IO.read(file)
app, data = io.gsub("\r\n", "\n").split(/^__END__$/, 2)
rescue Errno::ENOENT
app, data = nil
end
if data
lines = app.count("\n") + 1
template = nil
data.each_line do |line|
lines += 1
if line =~ /^@@\s*(.*\S)\s*$/
template = ''
templates[$1.to_sym] = template
elsif template
template << line
end
end
end
templates
end
__END__
@@ gemfile
source "https://rubygems.org"
gem "stasis"
gem "redcarpet"
@@ controller
# ignore everything needed to build
ignore 'Gemfile.lock'
ignore 'Gemfile'
ignore 'Rakefile'
# ignore all the git stuff
ignore '.gitignore'
ignore '.ruby-version'
ignore '.git'
# we don't want to render the readme
ignore 'README.md'
@@ index
<!DOCTYPE html>
<html>
<head>
<title>My Github user page</title>
</head>
<body>
<div class="container">
<h1>My Github user page</h1>
<p>checkout <a href='http://stasis.me'>stasis<a> to know how to work with me. To publish just run</p>
<code>rake publish<code>
</div>
</body>
</html>
@@ readme
Repository setup locally now create a repository named USERNAME.github.com
and point the origin it's way.
The rest of the Rakefile assumes a stasis default setup to be used so
rake development
runs a local development server on port 3000, watching the current dir
rake generate
generates the current page inside public
rake publish
publishes whatever is currently generated to the master branch ready to push
Have a great day!
@sideshowcoder <Twitter @ischi>