2013-01-18 19:49:31 -05:00
$: . unshift File . expand_path ( " lib " , File . dirname ( __FILE__ ) ) # For use/testing when no gem is installed
2013-02-10 03:47:53 -05:00
require 'rubygems'
require 'bundler/setup'
require 'stringex'
2013-01-07 16:47:07 -05:00
require 'time'
require 'tzinfo'
2013-01-18 19:49:31 -05:00
require 'yaml'
require 'octopress'
2013-02-10 03:47:53 -05:00
require 'rake/testtask'
require 'colors'
2013-01-18 19:49:31 -05:00
### Configuring Octopress:
### Under _config/ you will find:
### site.yml, deploy.yml
### Here you can override Octopress's default configurations or add your own.
### This Rakefile uses those config settings to do what it does.
### Please do not change anything below if you want help --
### otherwise, you're on your own ;-)
2010-05-26 03:23:47 -04:00
2013-02-16 20:47:31 -05:00
configurator = Octopress :: Configuration . new
configuration = configurator . read_configuration
2013-02-11 06:42:47 -05:00
full_stash_dir = " #{ configuration [ :source ] } / #{ configuration [ :stash_dir ] } "
2009-11-01 00:17:29 -04:00
2011-07-19 09:06:54 -04:00
desc " Initial setup for Octopress: copies the default theme into the path of Jekyll's generator. Rake install defaults to rake install[classic] to install a different theme run rake install[some_theme_name] "
2011-06-07 10:45:01 -04:00
task :install , :theme do | t , args |
2013-02-10 03:47:53 -05:00
theme = args . theme || 'classic'
theme_configuration = configurator . read_theme_configuration ( theme )
if File . directory? ( theme_configuration [ :source ] ) || File . directory? ( theme_configuration [ :theme ] [ :javascripts_dir ] ) || File . directory? ( theme_configuration [ :theme ] [ :stylesheets_dir ] )
2011-09-04 08:25:04 -04:00
abort ( " rake aborted! " ) if ask ( " A theme is already installed, proceeding will overwrite existing files. Are you sure? " , [ 'y' , 'n' ] ) == 'n'
end
2011-06-07 10:45:01 -04:00
# copy theme into working Jekyll directories
2013-02-10 03:47:53 -05:00
puts " # # Installing " + theme + " theme "
Rake :: Task [ " install_template " ] . invoke ( theme )
Rake :: Task [ " install_stylesheets " ] . invoke ( theme )
Rake :: Task [ " install_javascripts " ] . invoke ( theme )
Rake :: Task [ " install_configs " ] . invoke ( theme )
mkdir_p 'site'
end
task :install_configs , :theme do | t , args |
2011-06-07 10:45:01 -04:00
theme = args . theme || 'classic'
2013-02-10 03:47:53 -05:00
mkdir_p " _config "
if File . directory? " .themes/ #{ theme } /_config "
cp_r " .themes/ #{ theme } /_config/. " , " _config/defaults " , :remove_destination = > true
user_config_site = <<-EOF
- - -
# --------------------------- #
# User Configuration #
# --------------------------- #
EOF
File . open ( '_config/site.yml' , 'w' ) { | f | f . write user_config_site }
user_config_deploy = <<-EOF
- - -
# -------------------------- #
# Deployment Config #
# -------------------------- #
deploy_method : rsync
EOF
File . open ( '_config/deploy.yml' , 'w' ) { | f | f . write user_config_deploy }
end
end
desc " Install stylesheets for a theme "
task :install_stylesheets , :theme do | t , args |
theme = args . theme || 'classic'
theme_configuration = configurator . read_theme_configuration ( theme )
begin
stylesheets_dir = File . join ( " .themes/ #{ theme } " , theme_configuration [ :theme ] [ :stylesheets_dir ] )
rescue
" The #{ theme } theme must have a configuration file. This theme isn't compatable with Octopress 3.0 installation. You can probably still install it manually. " . yellow
end
mkdir_p " assets/stylesheets "
if File . directory? stylesheets_dir
cp_r " #{ stylesheets_dir } /. " , " assets/stylesheets "
end
end
desc " Install javascript assets for a theme "
task :install_javascripts , :theme do | t , args |
theme = args . theme || 'classic'
theme_configuration = configurator . read_theme_configuration ( theme )
begin
javascripts_dir = File . join ( " .themes/ #{ theme } " , theme_configuration [ :theme ] [ :javascripts_dir ] )
rescue
" The #{ theme } theme must have a configuration file. This theme isn't compatable with Octopress 3.0 installation. You can probably still install it manually. " . yellow
end
mkdir_p " assets/javascripts "
if File . directory? javascripts_dir
cp_r " #{ javascripts_dir } /. " , " assets/javascripts "
end
end
task :install_template , :theme do | t , args |
theme = args . theme || 'classic'
mkdir_p " source/_posts "
cp_r " .themes/ #{ theme } /source/. " , 'source'
2011-06-07 10:45:01 -04:00
end
2011-06-11 07:56:04 -04:00
#######################
# Working with Jekyll #
#######################
2010-03-10 13:23:30 -05:00
2011-06-22 23:44:02 -04:00
desc " Generate jekyll site "
2013-02-10 03:47:53 -05:00
task :generate do
raise " # # # You haven't set anything up yet. First run `rake install` to set up an Octopress theme. " if configuration [ :source ] . nil? || ! File . directory? ( configuration [ :source ] )
2013-02-16 20:47:31 -05:00
configurator . write_configs_for_generation
2011-06-22 23:44:02 -04:00
puts " # # Generating Site with Jekyll "
2013-01-18 19:49:31 -05:00
system " compass compile --css-dir #{ configuration [ :source ] } /stylesheets "
2013-02-10 03:47:53 -05:00
system " jekyll --no-server --no-auto #{ '--no-future' if Octopress . env == 'production' } "
unpublished = get_unpublished ( Dir . glob ( " #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } /*.* " ) , { env : Octopress . env , message : " \n These posts were not generated: " } )
2013-01-13 01:50:38 -05:00
puts unpublished unless unpublished . empty?
2013-02-16 20:47:31 -05:00
configurator . remove_configs_for_generation
2011-06-22 23:44:02 -04:00
end
2012-05-22 14:12:48 -04:00
# usage rake generate_only[my-post]
desc " Generate only the specified post (much faster) "
task :generate_only , :filename do | t , args |
if args . filename
filename = args . filename
else
filename = get_stdin ( " Enter a post file name: " )
end
puts " # # Stashing other posts "
Rake :: Task [ " isolate " ] . invoke ( filename )
Rake :: Task [ " generate " ] . execute
puts " # # Restoring stashed posts "
Rake :: Task [ " integrate " ] . execute
end
2013-02-10 03:47:53 -05:00
# - Check to see if site has been installed first rescue properly
2011-06-11 07:56:04 -04:00
desc " Watch the site and regenerate when it changes "
2013-02-10 03:47:53 -05:00
task :watch do
raise " # # # You haven't set anything up yet. First run `rake install` to set up an Octopress theme. " if configuration [ :source ] . nil? || ! File . directory? ( configuration [ :source ] )
2011-08-25 10:52:43 -04:00
puts " Starting to watch source with Jekyll and Compass. "
2013-02-10 03:47:53 -05:00
guardPid = Process . spawn ( " guard " )
2011-08-25 10:52:43 -04:00
trap ( " INT " ) {
2013-02-10 03:47:53 -05:00
Process . kill ( 9 , guardPid ) rescue Errno :: ESRCH
2011-09-12 15:36:14 -04:00
exit 0
2011-08-25 10:52:43 -04:00
}
2013-02-10 03:47:53 -05:00
Process . wait guardPid
2009-11-25 09:34:32 -05:00
end
2013-01-13 01:50:38 -05:00
desc " preview the site in a web browser. "
2013-02-10 03:47:53 -05:00
task :preview do
raise " # # # You haven't set anything up yet. First run `rake install` to set up an Octopress theme. " if configuration [ :source ] . nil? || ! File . directory? ( configuration [ :source ] )
guardPid = Process . spawn ( " guard " )
puts " Starting Rack, serving to http:// #{ configuration [ :server_host ] } : #{ configuration [ :server_port ] } "
2013-01-18 19:49:31 -05:00
rackupPid = Process . spawn ( " rackup --host #{ configuration [ :server_host ] } --port #{ configuration [ :server_port ] } " )
2011-08-25 10:52:43 -04:00
trap ( " INT " ) {
2013-02-10 03:47:53 -05:00
[ guardPid , rackupPid ] . each { | pid | Process . kill ( 9 , pid ) rescue Errno :: ESRCH }
2011-09-12 15:36:14 -04:00
exit 0
2011-08-25 10:52:43 -04:00
}
2013-02-10 03:47:53 -05:00
[ guardPid , rackupPid ] . each { | pid | Process . wait ( pid ) }
2009-11-05 22:30:03 -05:00
end
2011-07-19 16:09:42 -04:00
# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
2013-01-18 19:49:31 -05:00
desc " Begin a new post in #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } "
2011-07-19 16:09:42 -04:00
task :new_post , :title do | t , args |
2012-05-21 09:57:49 -04:00
if args . title
title = args . title
else
title = get_stdin ( " Enter a title for your post: " )
end
2013-01-18 19:49:31 -05:00
raise " # # # You haven't set anything up yet. First run `rake install` to set up an Octopress theme. " unless File . directory? ( configuration [ :source ] )
2013-02-14 11:44:39 -05:00
time = now_in_timezone ( configuration [ :timezone ] )
2013-01-18 19:49:31 -05:00
mkdir_p " #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } "
filename = " #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } / #{ Time . now . strftime ( '%Y-%m-%d' ) } - #{ title . to_url } . #{ configuration [ :new_post_ext ] } "
2011-09-03 09:06:36 -04:00
if File . exist? ( filename )
2011-09-04 09:47:36 -04:00
abort ( " rake aborted! " ) if ask ( " #{ filename } already exists. Do you want to overwrite? " , [ 'y' , 'n' ] ) == 'n'
2011-09-03 09:06:36 -04:00
end
2011-07-19 16:09:42 -04:00
puts " Creating new post: #{ filename } "
open ( filename , 'w' ) do | post |
2009-12-07 11:55:13 -05:00
post . puts " --- "
2011-04-17 23:49:30 -04:00
post . puts " layout: post "
2011-10-16 05:49:18 -04:00
post . puts " title: \" #{ title . gsub ( / & / , '&' ) } \" "
2013-01-07 16:47:07 -05:00
post . puts " date: #{ time . iso8601 } "
2011-07-19 16:09:42 -04:00
post . puts " comments: true "
2012-02-12 08:12:22 -05:00
post . puts " external-url: "
2011-07-19 09:06:54 -04:00
post . puts " categories: "
2009-12-07 11:55:13 -05:00
post . puts " --- "
end
end
2011-07-19 16:09:42 -04:00
# usage rake new_page[my-new-page] or rake new_page[my-new-page.html] or rake new_page (defaults to "new-page.markdown")
2013-01-18 19:49:31 -05:00
desc " Create a new page in #{ configuration [ :source ] } /(filename)/index. #{ configuration [ :new_page_ext ] } "
2011-07-19 16:09:42 -04:00
task :new_page , :filename do | t , args |
2013-01-18 19:49:31 -05:00
raise " # # # You haven't set anything up yet. First run `rake install` to set up an Octopress theme. " unless File . directory? ( configuration [ :source ] )
2011-07-19 16:09:42 -04:00
args . with_defaults ( :filename = > 'new-page' )
2013-01-18 19:49:31 -05:00
page_dir = [ configuration [ :source ] ]
2011-12-10 19:18:34 -05:00
if args . filename . downcase =~ / (^.+ \/ )?(.+) /
2011-10-16 10:22:37 -04:00
filename , dot , extension = $2 . rpartition ( '.' ) . reject ( & :empty? ) # Get filename and extension
title = filename
page_dir . concat ( $1 . downcase . sub ( / ^ \/ / , '' ) . split ( '/' ) ) unless $1 . nil? # Add path to page_dir Array
if extension . nil?
page_dir << filename
filename = " index "
end
2013-01-18 19:49:31 -05:00
extension || = configuration [ :new_page_ext ]
2011-10-16 10:22:37 -04:00
page_dir = page_dir . map! { | d | d = d . to_url } . join ( '/' ) # Sanitize path
filename = filename . downcase . to_url
2011-07-19 16:09:42 -04:00
mkdir_p page_dir
2011-10-16 10:22:37 -04:00
file = " #{ page_dir } / #{ filename } . #{ extension } "
2011-09-03 09:06:36 -04:00
if File . exist? ( file )
2011-09-04 09:47:36 -04:00
abort ( " rake aborted! " ) if ask ( " #{ file } already exists. Do you want to overwrite? " , [ 'y' , 'n' ] ) == 'n'
2011-09-03 09:06:36 -04:00
end
2011-07-19 16:09:42 -04:00
puts " Creating new page: #{ file } "
2013-02-14 11:44:39 -05:00
time = now_in_timezone ( configuration [ :timezone ] )
2011-07-19 16:09:42 -04:00
open ( file , 'w' ) do | page |
page . puts " --- "
page . puts " layout: page "
2011-10-16 10:22:37 -04:00
page . puts " title: \" #{ title } \" "
2013-01-07 16:47:07 -05:00
page . puts " date: #{ time . iso8601 } "
2011-07-19 16:09:42 -04:00
page . puts " comments: true "
page . puts " sharing: true "
page . puts " footer: true "
page . puts " --- "
end
else
puts " Syntax error: #{ args . filename } contains unsupported characters "
end
end
2010-01-23 15:47:43 -05:00
# usage rake isolate[my-post]
2013-02-21 15:15:39 -05:00
desc " Move all other posts than the one currently being worked on to a temporary stash location (stash) so regenerating the site happens much more quickly. "
2010-01-23 15:47:43 -05:00
task :isolate , :filename do | t , args |
2012-05-22 14:12:48 -04:00
if args . filename
filename = args . filename
else
filename = get_stdin ( " Enter a post file name: " )
end
2013-02-11 06:42:47 -05:00
FileUtils . mkdir ( full_stash_dir ) unless File . exist? ( full_stash_dir )
2013-01-18 19:49:31 -05:00
Dir . glob ( " #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } /*.* " ) do | post |
2013-02-11 06:42:47 -05:00
FileUtils . mv post , full_stash_dir unless post . include? ( filename )
2010-01-23 15:47:43 -05:00
end
end
desc " Move all stashed posts back into the posts directory, ready for site generation. "
task :integrate do
2013-02-11 06:42:47 -05:00
FileUtils . mv Dir . glob ( " #{ full_stash_dir } /*.* " ) , " #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } / "
2010-01-23 15:47:43 -05:00
end
2013-02-20 14:54:20 -05:00
desc " Clean out caches: .pygments-cache, .gist-cache, .sass-cache, and Compass-generated files. "
2011-06-17 22:24:48 -04:00
task :clean do
2013-02-20 14:54:20 -05:00
rm_rf [ " .pygments-cache " , " .gist-cache " ]
2012-06-15 13:50:34 -04:00
system " compass clean "
2013-02-10 03:47:53 -05:00
puts " # # Cleaned Compass-generated files, and various caches # # "
2012-05-22 14:12:48 -04:00
end
2013-02-20 14:56:54 -05:00
desc " Remove generated files ( #{ configuration [ :destination ] } directory). "
task :clobber do
rm_rf [ configuration [ :destination ] ]
end
2012-05-22 14:12:48 -04:00
desc " Update theme source and style "
task :update , :theme do | t , args |
theme = args . theme || 'classic'
2013-02-10 03:47:53 -05:00
Rake :: Task [ :update_template ] . invoke ( theme )
Rake :: Task [ :update_stylesheets ] . invoke ( theme )
Rake :: Task [ :update_javascripts ] . invoke ( theme )
2011-06-17 22:24:48 -04:00
end
2013-02-10 03:47:53 -05:00
desc " Move stylesheets to stylesheets.old, install stylesheets theme updates, replace stylesheets/custom with stylesheets.old/custom "
task :update_stylesheets , :theme do | t , args |
2011-07-19 22:18:34 -04:00
theme = args . theme || 'classic'
2013-02-10 03:47:53 -05:00
if File . directory? ( " #{ configuration [ :assets ] } .old/stylesheets " )
rm_r " #{ configuration [ :assets ] } .old/stylesheets " , :secure = > true
puts " Removed existing assets.old/stylesheets directory "
2011-07-27 18:22:57 -04:00
end
2013-02-10 03:47:53 -05:00
mkdir " #{ configuration [ :assets ] } .old "
mv " #{ configuration [ :assets ] } /stylesheets " , " #{ configuration [ :assets ] } .old/stylesheets "
puts " Moved styles into #{ configuration [ :assets ] } .old/stylesheets "
install_stylesheets ( theme )
cp_r " #{ configuration [ :assets ] } .old/stylesheets/custom " , " #{ configuration [ :assets ] } /stylesheets/custom "
puts " # # Updated Stylesheets # # "
2012-06-03 18:21:58 -04:00
rm_r " .sass-cache " , :secure = > true if File . directory? ( " .sass-cache " )
2011-07-19 22:18:34 -04:00
end
2013-02-10 03:47:53 -05:00
desc " Move javascripts to #{ configuration [ :assets ] } .old/javascripts, install javascripts theme updates. "
task :update_javascripts , :theme do | t , args |
if File . directory? ( " .themes/ #{ theme } /javascripts " )
theme = args . theme || 'classic'
if File . directory? ( " #{ configuration [ :assets ] } .old/javascripts " )
rm_r " #{ configuration [ :assets ] } .old/javascripts " , :secure = > true
puts " Removed existing assets.old/javascripts directory "
end
mkdir " #{ configuration [ :assets ] } .old "
cp_r " #{ configuration [ :assets ] } /javascripts/. " , " #{ configuration [ :assets ] } .old/javascripts "
puts " Copied styles into #{ configuration [ :assets ] } .old/javascripts "
install_javascripts ( theme )
puts " # # Updated Javascripts # # "
end
end
2011-07-19 22:18:34 -04:00
desc " Move source to source.old, install source theme updates, replace source/_includes/navigation.html with source.old's navigation "
2013-02-10 03:47:53 -05:00
task :update_template , :theme do | t , args |
2011-07-19 22:18:34 -04:00
theme = args . theme || 'classic'
2013-01-18 19:49:31 -05:00
if File . directory? ( " #{ configuration [ :source ] } .old " )
2013-02-10 03:47:53 -05:00
puts " Removed existing #{ configuration [ :source ] } .old directory "
2013-01-18 19:49:31 -05:00
rm_r " #{ configuration [ :source ] } .old " , :secure = > true
2011-07-27 18:22:57 -04:00
end
2013-01-18 19:49:31 -05:00
mkdir " #{ configuration [ :source ] } .old "
cp_r " #{ configuration [ :source ] } /. " , " #{ configuration [ :source ] } .old "
puts " # # Copied #{ configuration [ :source ] } into #{ configuration [ :source ] } .old/ "
2013-02-10 03:47:53 -05:00
cp_r " .themes/ " + theme + " /source/. " , configuration [ :source ] , :remove_destination = > true
2013-01-18 19:49:31 -05:00
cp_r " #{ configuration [ :source ] } .old/_includes/custom/. " , " #{ configuration [ :source ] } /_includes/custom/ " , :remove_destination = > true
mv " #{ configuration [ :source ] } /index.html " , " #{ configuration [ :blog_index_dir ] } " , :force = > true if configuration [ :blog_index_dir ] != configuration [ :source ]
cp " #{ configuration [ :source ] } .old/index.html " , configuration [ :source ] if configuration [ :blog_index_dir ] != configuration [ :source ] && File . exists? ( " #{ configuration [ :source ] } .old/index.html " )
if File . exists? ( " #{ configuration [ :source ] } /blog/archives/index.html " )
2012-05-20 23:23:18 -04:00
puts " # # Moving blog/archives to /archives (standard location as of 2.1) # # "
2013-01-18 19:49:31 -05:00
file = " #{ configuration [ :source ] } /_includes/custom/navigation.html "
2012-05-20 23:23:18 -04:00
navigation = IO . read ( file )
navigation = navigation . gsub ( / (.*) \/ blog( \/ archives)(.*$) /m , '\1\2\3' )
File . open ( file , 'w' ) do | f |
f . write navigation
end
2013-01-18 19:49:31 -05:00
rm_r " #{ configuration [ :source ] } /blog/archives "
rm_r " #{ configuration [ :source ] } /blog " if Dir . entries ( " #{ configuration [ :source ] } /blog " ) . join == " ... "
2012-02-12 13:27:17 -05:00
end
2013-01-18 19:49:31 -05:00
puts " # # Updated #{ configuration [ :source ] } # # "
2011-07-19 22:18:34 -04:00
end
2011-06-11 07:56:04 -04:00
##############
# Deploying #
##############
2011-06-29 14:01:06 -04:00
desc " Default deploy task "
2011-09-12 13:49:18 -04:00
task :deploy do
2013-01-18 19:49:31 -05:00
Rake :: Task [ " #{ configuration [ :deploy_default ] } " ] . execute
2011-09-12 14:48:26 -04:00
end
desc " Generate website and deploy "
2011-09-22 04:17:31 -04:00
task :gen_deploy = > [ :integrate , :generate , :deploy ] do
2011-08-25 10:52:43 -04:00
end
2013-02-20 14:38:10 -05:00
def ensure_trailing_slash ( val )
val = " #{ val } / " unless ( val . end_with? ( '/' ) )
return val
end
2011-06-11 07:56:04 -04:00
desc " Deploy website via rsync "
2011-06-29 14:01:06 -04:00
task :rsync do
2011-12-10 19:18:34 -05:00
exclude = " "
2011-12-10 19:30:33 -05:00
if File . exists? ( './rsync-exclude' )
2011-12-10 19:18:34 -05:00
exclude = " --exclude-from ' #{ File . expand_path ( './rsync-exclude' ) } ' "
end
2011-06-07 10:45:01 -04:00
puts " # # Deploying website via Rsync "
2013-02-28 22:53:11 -05:00
ssh_key = if ( ! configuration [ :ssh_key ] . nil? && ! configuration [ :ssh_key ] . empty? )
" -i #{ ENV [ 'HOME' ] } /.ssh/ #{ configuration [ :ssh_key ] } "
else
" "
end
2013-02-20 14:38:10 -05:00
document_root = ensure_trailing_slash ( configuration [ :document_root ] )
ok_failed system ( " rsync -avze 'ssh -p #{ configuration [ :ssh_port ] } #{ ssh_key } ' #{ exclude } #{ configuration [ :rsync_args ] } #{ " --delete " unless ! configuration [ :rsync_delete ] } #{ ensure_trailing_slash ( configuration [ :destination ] ) } #{ configuration [ :ssh_user ] } : #{ document_root } " )
2009-10-18 20:07:36 -04:00
end
2011-06-21 16:11:07 -04:00
desc " deploy public directory to github pages "
2011-08-25 10:52:43 -04:00
multitask :push do
2013-01-18 19:49:31 -05:00
if File . directory? ( configuration [ :deploy_dir ] )
2012-06-17 12:20:05 -04:00
puts " # # Deploying branch to GitHub Pages "
2013-01-18 19:49:31 -05:00
( Dir [ " #{ configuration [ :deploy_dir ] } /* " ] ) . each { | f | rm_rf ( f ) }
2012-05-20 11:38:56 -04:00
puts " Attempting pull, to sync local deployment repository "
2013-01-18 19:49:31 -05:00
cd " #{ configuration [ :deploy_dir ] } " do
system " git pull origin #{ configuration [ :deploy_branch ] } "
2012-05-20 11:38:56 -04:00
end
2013-01-18 19:49:31 -05:00
puts " \n # # copying #{ configuration [ :destination ] } to #{ configuration [ :deploy_dir ] } "
cp_r " #{ configuration [ :destination ] } /. " , configuration [ :deploy_dir ]
cd " #{ configuration [ :deploy_dir ] } " do
2012-05-27 01:49:35 -04:00
File . new ( " .nojekyll " , " w " ) . close
2012-05-20 11:38:56 -04:00
system " git add . "
system " git add -u "
message = " Site updated at #{ Time . now . utc } "
puts " \n # # Commiting: #{ message } "
system " git commit -m \" #{ message } \" "
2013-01-18 19:49:31 -05:00
puts " \n # # Pushing generated #{ configuration [ :deploy_dir ] } website "
if system " git push origin #{ configuration [ :deploy_branch ] } "
2012-12-19 22:45:11 -05:00
puts " \n # # GitHub Pages deploy complete "
else
2012-12-20 00:47:38 -05:00
remote = ` git remote -v `
repo_url = case remote
when / (http[^ \ s]+) /
$1
when / (git@[^ \ s]+) /
$1
else
" "
end
raise " \n # # Octopress could not push to #{ repo_url } "
2012-12-19 22:45:11 -05:00
end
2012-05-20 11:38:56 -04:00
end
else
2013-01-07 16:47:07 -05:00
puts " This project isn't configured for deploying to GitHub Pages \n Please run `rake setup_github_pages[your-deployment-repo-url]`. "
2011-06-21 16:11:07 -04:00
end
2009-11-24 17:40:47 -05:00
end
2011-07-16 14:52:50 -04:00
desc " Update configurations to support publishing to root or sub directory "
task :set_root_dir , :dir do | t , args |
if args . dir
2012-05-22 14:12:48 -04:00
dir = args . dir
else
dir = get_stdin ( " Please provide a directory: " )
end
if dir
if dir == " / "
2011-07-16 14:52:50 -04:00
dir = " "
else
dir = " / " + args . dir . sub ( / ( \/ *)(.+) / , " \\ 2 " ) . sub ( / \/ $ / , '' ) ;
end
2013-01-18 19:49:31 -05:00
# update personal configuration
2013-02-10 03:47:53 -05:00
site_configs = configurator . read_configuration ( 'site.yml' )
2013-01-18 19:49:31 -05:00
site_configs [ :destination ] = " public #{ dir } "
site_configs [ :subscribe_rss ] = " #{ dir } /atom.xml "
site_configs [ :root ] = " / #{ dir . sub ( / ^ \/ / , '' ) } "
2013-02-16 20:47:31 -05:00
configurator . write_config ( 'site.yml' , site_configs )
2013-02-22 00:53:04 -05:00
2013-01-18 19:49:31 -05:00
rm_rf configuration [ :destination ]
mkdir_p site_configs [ :destination ]
2012-05-22 14:12:48 -04:00
puts " \n ======================================================== "
puts " Site's root directory is now '/ #{ dir . sub ( / ^ \/ / , '' ) } ' "
puts " Don't forget to update your url in _config.yml "
puts " \n ======================================================== "
2011-07-16 14:52:50 -04:00
end
end
2012-06-17 12:20:05 -04:00
desc " Set up _deploy folder and deploy branch for GitHub Pages deployment "
2011-12-18 16:38:11 -05:00
task :setup_github_pages , :repo do | t , args |
if args . repo
repo_url = args . repo
else
2013-02-21 15:15:39 -05:00
puts " Enter the read/write url for your repository "
puts " (For example, 'git@github.com:your_username/your_username.github.com) "
repo_url = get_stdin ( " Repository url: " )
2011-12-18 16:38:11 -05:00
end
2012-12-20 00:37:48 -05:00
unless repo_url [ - 4 .. - 1 ] == " .git "
repo_url << " .git "
end
2013-01-13 11:00:43 -05:00
raise " !! The repo URL that was input was malformed. " unless ( repo_url =~ / https: \/ \/ github \ .com \/ [^ \/ ]+ \/ [^ \/ ]+ / ) . nil? or ( repo_url =~ / git@github \ .com:[^ \/ ]+ \/ [^ \/ ]+ / ) . nil?
user_match = repo_url . match ( / (:([^ \/ ]+)|(github \ .com \/ ([^ \/ ]+))) / )
2012-12-20 00:02:36 -05:00
user = user_match [ 2 ] || user_match [ 4 ]
2013-01-13 11:00:43 -05:00
branch = ( repo_url =~ / \/ [ \ w-]+ \ .github \ .com / ) . nil? ? 'gh-pages' : 'master'
2012-12-20 00:37:48 -05:00
project = ( branch == 'gh-pages' ) ? repo_url . match ( / \/ (.+)( \ .git) / ) [ 1 ] : ''
2012-05-20 11:38:56 -04:00
url = " http:// #{ user } .github.com "
url += " / #{ project } " unless project == ''
2013-01-13 11:00:43 -05:00
unless ( ` git remote -v ` =~ / origin.+?octopress(?: \ .git)? / ) . nil?
2011-09-22 05:41:40 -04:00
# If octopress is still the origin remote (from cloning) rename it to octopress
system " git remote rename origin octopress "
if branch == 'master'
# If this is a user/organization pages repository, add the correct origin remote
2013-02-10 03:47:53 -05:00
# and checkout the source branch for committing changes to the website's source.
2011-09-22 05:41:40 -04:00
system " git remote add origin #{ repo_url } "
puts " Added remote #{ repo_url } as origin "
system " git config branch.master.remote origin "
puts " Set origin as default remote "
system " git branch -m master source "
2013-02-10 03:47:53 -05:00
puts " Master branch renamed to 'source' for committing your website's source files "
2011-09-22 05:41:40 -04:00
else
2013-01-18 19:49:31 -05:00
unless ! configuration [ :destination ] . match ( " #{ project } " ) . nil?
2012-05-20 11:38:56 -04:00
Rake :: Task [ :set_root_dir ] . invoke ( project )
2011-09-22 05:41:40 -04:00
end
end
end
2012-05-20 11:38:56 -04:00
# Configure deployment repository
2013-01-18 19:49:31 -05:00
rm_rf configuration [ :deploy_dir ]
mkdir configuration [ :deploy_dir ]
cd " #{ configuration [ :deploy_dir ] } " do
2011-09-22 05:41:40 -04:00
system " git init "
system " git remote add origin #{ repo_url } "
2012-05-20 11:38:56 -04:00
puts " Attempting to pull from repository "
system " git pull origin #{ branch } "
unless File . exist? ( 'index.html' )
system " echo 'My Octopress Page is coming soon …' > index.html "
system " git add . "
system " git commit -m \" Octopress init \" "
system " git branch -m gh-pages " unless branch == 'master'
end
end
2013-01-18 19:49:31 -05:00
# Configure deployment setup in deploy.yml
2013-02-16 20:47:31 -05:00
deploy_configuration = configurator . read_config ( 'deploy.yml' )
2013-01-18 19:49:31 -05:00
deploy_configuration [ :deploy_default ] = " push "
deploy_configuration [ :deploy_branch ] = branch
2013-02-16 20:47:31 -05:00
deploy_configuration = configurator . read_config ( 'defaults/deploy/gh_pages.yml' ) . deep_merge ( deploy_configuration )
2013-01-18 19:49:31 -05:00
puts deploy_configuration
2013-02-16 20:47:31 -05:00
configurator . write_config ( 'deploy.yml' , deploy_configuration )
2012-05-20 11:38:56 -04:00
2013-01-07 16:47:07 -05:00
# Configure published url
2013-02-16 20:47:31 -05:00
site_configuration = configurator . read_config ( 'site.yml' )
2013-01-18 19:49:31 -05:00
site_configuration [ :url ] = url if site_configuration . has_key? ( :url ) && site_configuration [ :url ] == 'http://yoursite.com'
2013-02-16 20:47:31 -05:00
site_configuration = configurator . read_config ( 'defaults/jekyll.yml' ) . deep_merge ( site_configuration )
2012-05-20 11:38:56 -04:00
puts " \n ======================================================== "
2013-01-18 19:49:31 -05:00
has_cname = File . exists? ( " #{ configuration [ :source ] } /CNAME " )
2012-05-20 11:38:56 -04:00
if has_cname
2013-01-18 19:49:31 -05:00
cname = IO . read ( " #{ configuration [ :source ] } /CNAME " ) . chomp
2012-05-20 11:38:56 -04:00
current_short_url = / \/ {2}(.*$) / . match ( current_url ) [ 1 ]
if cname != current_short_url
puts " !! WARNING: Your CNAME points to #{ cname } but your _config.yml url is set to #{ current_short_url } !! "
puts " For help with setting up a CNAME follow the guide at http://help.github.com/pages/ # custom_domains "
else
2012-06-17 12:20:05 -04:00
puts " GitHub Pages will host your site at http:// #{ cname } "
2012-05-20 11:38:56 -04:00
end
else
2012-06-17 12:20:05 -04:00
puts " GitHub Pages will host your site at #{ url } . "
2013-01-18 19:49:31 -05:00
puts " To host at \" your-site.com \" , configure a CNAME: `echo \" your-domain.com \" > #{ configuration [ :source ] } /CNAME` "
2012-05-20 11:38:56 -04:00
puts " Then change the url in _config.yml from #{ current_url } to http://your-domain.com "
2012-06-17 12:20:05 -04:00
puts " Finally, follow the guide at http://help.github.com/pages/ # custom_domains for help pointing your domain to GitHub Pages "
2011-06-22 23:44:02 -04:00
end
2012-05-20 11:38:56 -04:00
puts " Deploy to #{ repo_url } with `rake deploy` "
2012-05-20 18:27:18 -04:00
puts " Note: generated content is copied into _deploy/ which is not in version control. "
puts " If starting with a fresh clone of this project you should re-run setup_github_pages. "
2012-05-20 11:38:56 -04:00
puts " ======================================================== "
2011-06-22 23:44:02 -04:00
end
2009-10-18 20:07:36 -04:00
2013-01-13 01:50:38 -05:00
# usage rake list_posts or rake list_posts[pub|unpub]
desc " List all unpublished/draft posts "
task :list_drafts do
2013-02-22 00:53:04 -05:00
posts = Dir . glob ( " #{ configuration [ :source ] } / #{ configuration [ :posts_dir ] } /*.* " )
2013-01-13 01:50:38 -05:00
unpublished = get_unpublished ( posts )
2013-01-13 23:54:47 -05:00
puts unpublished . empty? ? " There are no posts currently in draft " : unpublished
2013-01-13 01:50:38 -05:00
end
2013-02-28 15:47:22 -05:00
#
# Run tests for Octopress module, found in lib/.
#
Rake :: TestTask . new do | t |
2013-02-28 15:50:24 -05:00
t . pattern = " lib/spec/**/*_spec.rb "
2013-02-28 15:47:22 -05:00
end
2013-01-13 01:50:38 -05:00
def get_unpublished ( posts , options = { } )
result = " "
message = options [ :message ] || " These Posts will not be published: "
posts . sort . each do | post |
file = File . read ( post )
data = YAML . load file . match ( / (^-{3} \ n)(.+?)( \ n-{3}) /m ) [ 2 ]
2013-02-10 03:47:53 -05:00
if options [ :env ] == 'production'
2013-01-13 23:54:47 -05:00
future = Time . now < Time . parse ( data [ 'date' ] . to_s ) ? " future date: #{ data [ 'date' ] } " : false
2013-01-13 01:50:38 -05:00
end
draft = data [ 'published' ] == false ? 'published: false' : false
result << " - #{ data [ 'title' ] } ( #{ draft or future } ) \n " if draft or future
end
result = " #{ message } \n " + result unless result . empty?
result
end
2011-06-22 23:44:02 -04:00
def ok_failed ( condition )
if ( condition )
puts " OK "
else
puts " FAILED "
end
end
2009-10-18 20:07:36 -04:00
2011-09-04 08:25:04 -04:00
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
2013-01-07 16:47:07 -05:00
def now_in_timezone ( timezone )
time = Time . now
unless timezone . nil? || timezone . empty? || timezone == 'local'
tz = TZInfo :: Timezone . get ( timezone ) #setup Timezone object
adjusted_time = tz . utc_to_local ( time . utc ) #time object without correct offset
#time object with correct offset
time = Time . new (
adjusted_time . year ,
adjusted_time . month ,
adjusted_time . day ,
adjusted_time . hour ,
adjusted_time . min ,
adjusted_time . sec ,
tz . period_for_utc ( time . utc ) . utc_total_offset ( ) )
#convert offset to utc instead of just ±0 if that was specified
if [ 'utc' , 'zulu' , 'universal' , 'uct' , 'gmt' , 'gmt0' , 'gmt+0' , 'gmt-0' ] . include? timezone . downcase
time = time . utc
end
end
time
end
2011-06-11 07:56:04 -04:00
desc " list tasks "
task :list do
2011-08-11 08:13:30 -04:00
puts " Tasks: #{ ( Rake :: Task . tasks - [ Rake :: Task [ :list ] ] ) . join ( ', ' ) } "
2011-06-11 07:56:04 -04:00
puts " (type rake -T for more detail) \n \n "
2010-08-03 01:10:59 -04:00
end