Skip to content

Commit 9279038

Browse files
committed
Rakefile added
1 parent 1e43f1c commit 9279038

File tree

1 file changed

+312
-0
lines changed

1 file changed

+312
-0
lines changed

Rakefile

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
require "rubygems"
2+
require 'rake'
3+
require 'yaml'
4+
require 'time'
5+
6+
SOURCE = "."
7+
CONFIG = {
8+
'version' => "0.3.0",
9+
'themes' => File.join(SOURCE, "_includes", "themes"),
10+
'layouts' => File.join(SOURCE, "_layouts"),
11+
'posts' => File.join(SOURCE, "_posts"),
12+
'post_ext' => "md",
13+
'theme_package_version' => "0.1.0"
14+
}
15+
16+
# Path configuration helper
17+
module JB
18+
class Path
19+
SOURCE = "."
20+
Paths = {
21+
:layouts => "_layouts",
22+
:themes => "_includes/themes",
23+
:theme_assets => "assets/themes",
24+
:theme_packages => "_theme_packages",
25+
:posts => "_posts"
26+
}
27+
28+
def self.base
29+
SOURCE
30+
end
31+
32+
# build a path relative to configured path settings.
33+
def self.build(path, opts = {})
34+
opts[:root] ||= SOURCE
35+
path = "#{opts[:root]}/#{Paths[path.to_sym]}/#{opts[:node]}".split("/")
36+
path.compact!
37+
File.__send__ :join, path
38+
end
39+
40+
end #Path
41+
end #JB
42+
43+
# Usage: rake post title="A Title" [date="2012-02-09"] [tags=[tag1,tag2]] [category="category"]
44+
desc "Begin a new post in #{CONFIG['posts']}"
45+
task :post do
46+
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
47+
title = ENV["title"] || "new-post"
48+
tags = ENV["tags"] || "[]"
49+
cover = ENV["cover"] || ""
50+
category = ENV["category"] || ""
51+
category = "\"#{category.gsub(/-/,' ')}\"" if !category.empty?
52+
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
53+
begin
54+
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
55+
rescue => e
56+
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
57+
exit -1
58+
end
59+
filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
60+
if File.exist?(filename)
61+
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
62+
end
63+
64+
puts "Creating new post: #{filename}"
65+
open(filename, 'w') do |post|
66+
post.puts "---"
67+
post.puts "layout: post"
68+
post.puts "title: \"#{title.gsub(/-/,' ')}\""
69+
post.puts 'description: ""'
70+
post.puts "category: #{category}"
71+
post.puts "tags: #{tags}"
72+
post.puts "cover: #{cover}"
73+
post.puts "---"
74+
end
75+
end # task :post
76+
77+
# Usage: rake page name="about.html"
78+
# You can also specify a sub-directory path.
79+
# If you don't specify a file extention we create an index.html at the path specified
80+
desc "Create a new page."
81+
task :page do
82+
name = ENV["name"] || "new-page.md"
83+
filename = File.join(SOURCE, "#{name}")
84+
filename = File.join(filename, "index.html") if File.extname(filename) == ""
85+
title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
86+
if File.exist?(filename)
87+
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
88+
end
89+
90+
mkdir_p File.dirname(filename)
91+
puts "Creating new page: #{filename}"
92+
open(filename, 'w') do |post|
93+
post.puts "---"
94+
post.puts "layout: page"
95+
post.puts "title: \"#{title}\""
96+
post.puts 'description: ""'
97+
post.puts "---"
98+
post.puts "{% include JB/setup %}"
99+
end
100+
end # task :page
101+
102+
desc "Launch preview environment"
103+
task :preview do
104+
system "jekyll serve -w"
105+
end # task :preview
106+
107+
# Public: Alias - Maintains backwards compatability for theme switching.
108+
task :switch_theme => "theme:switch"
109+
110+
namespace :theme do
111+
112+
# Public: Switch from one theme to another for your blog.
113+
#
114+
# name - String, Required. name of the theme you want to switch to.
115+
# The theme must be installed into your JB framework.
116+
#
117+
# Examples
118+
#
119+
# rake theme:switch name="the-program"
120+
#
121+
# Returns Success/failure messages.
122+
desc "Switch between Jekyll-bootstrap themes."
123+
task :switch do
124+
theme_name = ENV["name"].to_s
125+
theme_path = File.join(CONFIG['themes'], theme_name)
126+
settings_file = File.join(theme_path, "settings.yml")
127+
non_layout_files = ["settings.yml"]
128+
129+
abort("rake aborted: name cannot be blank") if theme_name.empty?
130+
abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path)
131+
abort("rake aborted: '#{CONFIG['layouts']}' directory not found.") unless FileTest.directory?(CONFIG['layouts'])
132+
133+
Dir.glob("#{theme_path}/*") do |filename|
134+
next if non_layout_files.include?(File.basename(filename).downcase)
135+
puts "Generating '#{theme_name}' layout: #{File.basename(filename)}"
136+
137+
open(File.join(CONFIG['layouts'], File.basename(filename)), 'w') do |page|
138+
if File.basename(filename, ".html").downcase == "default"
139+
page.puts "---"
140+
page.puts File.read(settings_file) if File.exist?(settings_file)
141+
page.puts "---"
142+
else
143+
page.puts "---"
144+
page.puts "layout: default"
145+
page.puts "---"
146+
end
147+
page.puts "{% include JB/setup %}"
148+
page.puts "{% include themes/#{theme_name}/#{File.basename(filename)} %}"
149+
end
150+
end
151+
152+
puts "=> Theme successfully switched!"
153+
puts "=> Reload your web-page to check it out =)"
154+
end # task :switch
155+
156+
# Public: Install a theme using the theme packager.
157+
# Version 0.1.0 simple 1:1 file matching.
158+
#
159+
# git - String, Optional path to the git repository of the theme to be installed.
160+
# name - String, Optional name of the theme you want to install.
161+
# Passing name requires that the theme package already exist.
162+
#
163+
# Examples
164+
#
165+
# rake theme:install git="https://github.com/jekyllbootstrap/theme-twitter.git"
166+
# rake theme:install name="cool-theme"
167+
#
168+
# Returns Success/failure messages.
169+
desc "Install theme"
170+
task :install do
171+
if ENV["git"]
172+
manifest = theme_from_git_url(ENV["git"])
173+
name = manifest["name"]
174+
else
175+
name = ENV["name"].to_s.downcase
176+
end
177+
178+
packaged_theme_path = JB::Path.build(:theme_packages, :node => name)
179+
180+
abort("rake aborted!
181+
=> ERROR: 'name' cannot be blank") if name.empty?
182+
abort("rake aborted!
183+
=> ERROR: '#{packaged_theme_path}' directory not found.
184+
=> Installable themes can be added via git. You can find some here: http://github.com/jekyllbootstrap
185+
=> To download+install run: `rake theme:install git='[PUBLIC-CLONE-URL]'`
186+
=> example : rake theme:install git='git@github.com:jekyllbootstrap/theme-the-program.git'
187+
") unless FileTest.directory?(packaged_theme_path)
188+
189+
manifest = verify_manifest(packaged_theme_path)
190+
191+
# Get relative paths to packaged theme files
192+
# Exclude directories as they'll be recursively created. Exclude meta-data files.
193+
packaged_theme_files = []
194+
FileUtils.cd(packaged_theme_path) {
195+
Dir.glob("**/*.*") { |f|
196+
next if ( FileTest.directory?(f) || f =~ /^(manifest|readme|packager)/i )
197+
packaged_theme_files << f
198+
}
199+
}
200+
201+
# Mirror each file into the framework making sure to prompt if already exists.
202+
packaged_theme_files.each do |filename|
203+
file_install_path = File.join(JB::Path.base, filename)
204+
if File.exist? file_install_path and ask("#{file_install_path} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
205+
next
206+
else
207+
mkdir_p File.dirname(file_install_path)
208+
cp_r File.join(packaged_theme_path, filename), file_install_path
209+
end
210+
end
211+
212+
puts "=> #{name} theme has been installed!"
213+
puts "=> ---"
214+
if ask("=> Want to switch themes now?", ['y', 'n']) == 'y'
215+
system("rake switch_theme name='#{name}'")
216+
end
217+
end
218+
219+
# Public: Package a theme using the theme packager.
220+
# The theme must be structured using valid JB API.
221+
# In other words packaging is essentially the reverse of installing.
222+
#
223+
# name - String, Required name of the theme you want to package.
224+
#
225+
# Examples
226+
#
227+
# rake theme:package name="twitter"
228+
#
229+
# Returns Success/failure messages.
230+
desc "Package theme"
231+
task :package do
232+
name = ENV["name"].to_s.downcase
233+
theme_path = JB::Path.build(:themes, :node => name)
234+
asset_path = JB::Path.build(:theme_assets, :node => name)
235+
236+
abort("rake aborted: name cannot be blank") if name.empty?
237+
abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path)
238+
abort("rake aborted: '#{asset_path}' directory not found.") unless FileTest.directory?(asset_path)
239+
240+
## Mirror theme's template directory (_includes)
241+
packaged_theme_path = JB::Path.build(:themes, :root => JB::Path.build(:theme_packages, :node => name))
242+
mkdir_p packaged_theme_path
243+
cp_r theme_path, packaged_theme_path
244+
245+
## Mirror theme's asset directory
246+
packaged_theme_assets_path = JB::Path.build(:theme_assets, :root => JB::Path.build(:theme_packages, :node => name))
247+
mkdir_p packaged_theme_assets_path
248+
cp_r asset_path, packaged_theme_assets_path
249+
250+
## Log packager version
251+
packager = {"packager" => {"version" => CONFIG["theme_package_version"].to_s } }
252+
open(JB::Path.build(:theme_packages, :node => "#{name}/packager.yml"), "w") do |page|
253+
page.puts packager.to_yaml
254+
end
255+
256+
puts "=> '#{name}' theme is packaged and available at: #{JB::Path.build(:theme_packages, :node => name)}"
257+
end
258+
259+
end # end namespace :theme
260+
261+
# Internal: Download and process a theme from a git url.
262+
# Notice we don't know the name of the theme until we look it up in the manifest.
263+
# So we'll have to change the folder name once we get the name.
264+
#
265+
# url - String, Required url to git repository.
266+
#
267+
# Returns theme manifest hash
268+
def theme_from_git_url(url)
269+
tmp_path = JB::Path.build(:theme_packages, :node => "_tmp")
270+
abort("rake aborted: system call to git clone failed") if !system("git clone #{url} #{tmp_path}")
271+
manifest = verify_manifest(tmp_path)
272+
new_path = JB::Path.build(:theme_packages, :node => manifest["name"])
273+
if File.exist?(new_path) && ask("=> #{new_path} theme package already exists. Override?", ['y', 'n']) == 'n'
274+
remove_dir(tmp_path)
275+
abort("rake aborted: '#{manifest["name"]}' already exists as theme package.")
276+
end
277+
278+
remove_dir(new_path) if File.exist?(new_path)
279+
mv(tmp_path, new_path)
280+
manifest
281+
end
282+
283+
# Internal: Process theme package manifest file.
284+
#
285+
# theme_path - String, Required. File path to theme package.
286+
#
287+
# Returns theme manifest hash
288+
def verify_manifest(theme_path)
289+
manifest_path = File.join(theme_path, "manifest.yml")
290+
manifest_file = File.open( manifest_path )
291+
abort("rake aborted: repo must contain valid manifest.yml") unless File.exist? manifest_file
292+
manifest = YAML.load( manifest_file )
293+
manifest_file.close
294+
manifest
295+
end
296+
297+
def ask(message, valid_options)
298+
if valid_options
299+
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
300+
else
301+
answer = get_stdin(message)
302+
end
303+
answer
304+
end
305+
306+
def get_stdin(message)
307+
print message
308+
STDIN.gets.chomp
309+
end
310+
311+
#Load custom rake scripts
312+
Dir['_rake/*.rake'].each { |r| load r }

0 commit comments

Comments
 (0)