-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rakefile.rb
156 lines (138 loc) · 4.76 KB
/
Rakefile.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
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
require "rubygems"
require "bundler/setup"
require "stringex"
## -- Config -- ##
posts_dir = "_posts" # directory for blog files
new_post_ext = "md" # default new post file extension when using the new_post task
new_page_ext = "md" # default new page file extension when using the new_page task
#############################
# Create a new Post or Page #
#############################
# usage: bundle exec rake new_post
desc "Create a new post in #{posts_dir}"
task :new_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your post: ")
end
filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
category = get_stdin("Enter category name to group your post in (leave blank for none): ")
tags = get_stdin("Enter tags to classify your post (comma separated): ")
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "category: [#{category}]"
post.puts "tags: [#{tags}]"
post.puts "image:"
post.puts " feature: "
post.puts " credit: "
post.puts " creditlink: "
post.puts "comments: "
post.puts "share: "
post.puts "---"
end
end
# usage: bundle exec rake new_page
desc "Create a new page"
task :new_page, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your page: ")
end
filename = "#{title.to_url}.#{new_page_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
tags = get_stdin("Enter tags to classify your page (comma separated): ")
puts "Creating new page: #{filename}"
open(filename, 'w') do |page|
page.puts "---"
page.puts "layout: page"
page.puts "permalink: /#{title.to_url}/"
page.puts "title: \"#{title}\""
page.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
page.puts "tags: [#{tags}]"
page.puts "image:"
page.puts " feature: "
page.puts " credit: "
page.puts " creditlink: "
page.puts "share: "
page.puts "---"
end
end
# usage: bundle exec rake publish
desc "Publish site to GitHub Pages"
task :publish do
if all_changes_committed? and successful_asset_compilation? and production_url_set?
puts "Pushing master branch to GitHub and Publishing site to GitHub Pages..."
system 'git branch -D gh-pages && git branch gh-pages && git push --all origin'
end
end
# usage: bundle exec rake preview
desc "Generate and preview site locally"
task :preview do
if successful_asset_compilation? and development_url_set?
jekyllPid = Process.spawn("jekyll serve -w")
trap("INT") {
[jekyllPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
}
[jekyllPid].each { |pid| Process.wait(pid) }
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def successful_asset_compilation?
compile = 'lessc -x assets/less/main.less > assets/css/main.min.css'
success = "Succesfully compiled LESS to ./assets/css/main.min.css"
failure = "Compilation of LESS files failed. See lessc output for more info."
raise LoadError unless less_compiler_installed?
puts "Compiling LESS..."
errors = `#{compile}`
puts errors.empty? ? success : failure
errors.empty?
rescue LoadError => e
puts "#{e}: LESS compiler is not installed. Install via Homebrew or npm and try again."
false
end
def clean_git_status?
not (`git status` =~ /nothing to commit, working directory clean/).nil?
end
def all_changes_committed?
puts "Commit your changes and try again." unless clean_git_status?
clean_git_status?
end
def less_compiler_installed?
(`which lessc` =~ /no lessc/).nil?
end
def production_url_set?
config_file = File.read('_config.yml')
commented_public_url = config_file =~ /^#\s+url:\s+https:\/\/.*portlandcodeschool/
uncommented_localhost = config_file =~ /^url:\s+http:\/\/0\.0\.0\.0\:4000/
prod_url_set = commented_public_url.nil? && uncommented_localhost.nil?
puts "Production URL is not set." unless prod_url_set
prod_url_set
end
def development_url_set?
localhost_url_set = !production_url_set?
puts "Development URL is not set. Check _config.yml." unless localhost_url_set
localhost_url_set
end