diff --git a/Rakefile b/Rakefile index 1f66492..ed8475e 100644 --- a/Rakefile +++ b/Rakefile @@ -221,7 +221,6 @@ task :deploy do File.delete(".preview-mode") Rake::Task[:generate].execute end - Rake::Task[:copydot].invoke(source_dir, public_dir) Rake::Task["#{deploy_default}"].execute end @@ -249,20 +248,28 @@ end desc "deploy public directory to github pages" multitask :push do - puts "## Deploying branch to Github Pages " - (Dir["#{deploy_dir}/*"]).each { |f| rm_rf(f) } - Rake::Task[:copydot].invoke(public_dir, deploy_dir) - puts "\n## copying #{public_dir} to #{deploy_dir}" - cp_r "#{public_dir}/.", deploy_dir - cd "#{deploy_dir}" do - system "git add ." - system "git add -u" - puts "\n## Commiting: Site updated at #{Time.now.utc}" - message = "Site updated at #{Time.now.utc}" - system "git commit -m \"#{message}\"" - puts "\n## Pushing generated #{deploy_dir} website" - system "git push origin #{deploy_branch} --force" - puts "\n## Github Pages deploy complete" + if File.directory?(deploy_dir) + puts "## Deploying branch to Github Pages " + (Dir["#{deploy_dir}/*"]).each { |f| rm_rf(f) } + Rake::Task[:copydot].invoke(public_dir, deploy_dir) + puts "Attempting pull, to sync local deployment repository" + cd "#{deploy_dir}" do + system "git pull origin #{deploy_branch}" + end + puts "\n## copying #{public_dir} to #{deploy_dir}" + cp_r "#{public_dir}/.", deploy_dir + cd "#{deploy_dir}" do + system "git add ." + system "git add -u" + message = "Site updated at #{Time.now.utc}" + puts "\n## Commiting: #{message}" + system "git commit -m \"#{message}\"" + puts "\n## Pushing generated #{deploy_dir} website" + system "git push origin #{deploy_branch}" + puts "\n## Github Pages deploy complete" + end + else + puts "This project isn't configured for deploying to Github Pages\nPlease run `rake setup_github_pages[your-deployment-repo-url]`." end end @@ -311,6 +318,8 @@ task :setup_github_pages, :repo do |t, args| user = repo_url.match(/:([^\/]+)/)[1] branch = (repo_url.match(/\/[\w-]+.github.com/).nil?) ? 'gh-pages' : 'master' project = (branch == 'gh-pages') ? repo_url.match(/\/([^\.]+)/)[1] : '' + url = "http://#{user}.github.com" + url += "/#{project}" unless project == '' unless `git remote -v`.match(/origin.+?octopress.git/).nil? # If octopress is still the origin remote (from cloning) rename it to octopress system "git remote rename origin octopress" @@ -325,34 +334,65 @@ task :setup_github_pages, :repo do |t, args| puts "Master branch renamed to 'source' for committing your blog source files" else unless !public_dir.match("#{project}").nil? - system "rake set_root_dir[#{project}]" + Rake::Task[:set_root_dir].invoke(project) end end end - url = "http://#{user}.github.com" - url += "/#{project}" unless project == '' - jekyll_config = IO.read('_config.yml') - jekyll_config.sub!(/^url:.*$/, "url: #{url}") - File.open('_config.yml', 'w') do |f| - f.write jekyll_config - end + + # Configure deployment repository rm_rf deploy_dir mkdir deploy_dir cd "#{deploy_dir}" do system "git init" - 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' system "git remote add origin #{repo_url}" - rakefile = IO.read(__FILE__) - rakefile.sub!(/deploy_branch(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_branch\\1=\\2\\3#{branch}\\3") - rakefile.sub!(/deploy_default(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_default\\1=\\2\\3push\\3") - File.open(__FILE__, 'w') do |f| - f.write rakefile + 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 - puts "\n---\n## Now you can deploy to #{url} with `rake deploy` ##" + + # Configure deployment setup in Rakefile + rakefile = IO.read(__FILE__) + rakefile.sub!(/deploy_branch(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_branch\\1=\\2\\3#{branch}\\3") + rakefile.sub!(/deploy_default(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_default\\1=\\2\\3push\\3") + File.open(__FILE__, 'w') do |f| + f.write rakefile + end + + # Configure published url + jekyll_config = IO.read('_config.yml') + current_url = /^url:\s?(.*$)/.match(jekyll_config)[1] + has_cname = File.exists?("#{source_dir}/CNAME") + if current_url == 'http://yoursite.com' + jekyll_config.sub!(/^url:.*$/, "url: #{url}") + File.open('_config.yml', 'w') do |f| + f.write jekyll_config + end + current_url = url + end + + puts "\n========================================================" + if has_cname + cname = IO.read("#{source_dir}/CNAME").chomp + 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 + puts "Github Pages will host your site at http://#{cname}" + end + else + puts "Github Pages will host your site at #{url}." + puts "To host at \"your-site.com\", configure a CNAME: `echo \"your-domain.com\" > #{source_dir}/CNAME`" + puts "Then change the url in _config.yml from #{current_url} to http://your-domain.com" + puts "Finally, follow the guide at http://help.github.com/pages/#custom_domains for help pointing your domain to Github Pages" + end + puts "Deploy to #{repo_url} with `rake deploy`" + puts "========================================================" end def ok_failed(condition)