From 9b5fe69509ca2f0ffd1ad939d679951683fbb4cb Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Fri, 4 Apr 2014 11:11:46 -0600 Subject: [PATCH] Publish new docs when releasing a new version --- .gitignore | 1 + Rakefile | 108 +++++++++++++++++++++++++++++ doc_config/gh-pages/index.html.erb | 9 +++ 3 files changed, 118 insertions(+) create mode 100644 doc_config/gh-pages/index.html.erb diff --git a/.gitignore b/.gitignore index 67d487ba..19d1b8fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.swo *.swp .bundle +.gh-pages .svn/ .yardoc coverage diff --git a/Rakefile b/Rakefile index 8378d186..baf19cd4 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,10 @@ require 'bundler/gem_tasks' require 'rspec/core/rake_task' require 'cucumber/rake/task' require 'appraisal' +require 'erb' +require_relative 'lib/shoulda/matchers/version' + +CURRENT_VERSION = Shoulda::Matchers::VERSION RSpec::Core::RakeTask.new do |t| t.pattern = "spec/**/*_spec.rb" @@ -27,3 +31,107 @@ end task :appraise do exec 'appraisal install && appraisal rake' end + +GH_PAGES_DIR = '.gh-pages' + +namespace :docs do + file GH_PAGES_DIR do + sh "git clone git@github.com:thoughtbot/shoulda-matchers.git #{GH_PAGES_DIR} --branch gh-pages" + end + + task :setup => GH_PAGES_DIR do + within_gh_pages do + sh 'git fetch origin' + sh 'git reset --hard origin/gh-pages' + end + end + + desc 'Generate docs for a particular version' + task :generate, [:version, :latest_version] => :setup do |t, args| + generate_docs(args.version, latest_version: latest_version) + end + + desc 'Generate docs for a particular version and push them to GitHub' + task :publish, [:version, :latest_version] => :setup do |t, args| + generate_docs(args.version, latest_version: latest_version) + publish_docs(args.version, latest_version: latest_version) + end + + desc "Generate docs for version #{CURRENT_VERSION} and push them to GitHub" + task :publish_latest => :setup do + version = Gem::Version.new(CURRENT_VERSION) + + unless version.prerelease? + latest_version = version.to_s + end + + generate_docs(CURRENT_VERSION, latest_version: latest_version) + publish_docs(CURRENT_VERSION, latest_version: latest_version) + end + + def rewrite_index_to_inject_version(ref, version) + within_gh_pages do + filename = "#{ref}/index.html" + content = File.read(filename) + content.sub!(%r{

shoulda-matchers.+

}, "

shoulda-matchers (#{version})

") + File.open(filename, 'w') {|f| f.write(content) } + end + end + + def generate_docs(version, options = {}) + ref = determine_ref_from(version) + + sh "rm -rf #{GH_PAGES_DIR}/#{ref}" + sh "bundle exec yard -o #{GH_PAGES_DIR}/#{ref}" + + rewrite_index_to_inject_version(ref, version) + + within_gh_pages do + sh "git add --ignore-removal #{ref}" + end + + if options[:latest_version] + generate_file_that_redirects_to_latest_version(options[:latest_version]) + end + end + + def publish_docs(version, options = {}) + message = build_commit_message(version) + + within_gh_pages do + sh 'git clean -f' + sh "git commit -m '#{message}'" + sh 'git push' + end + end + + def generate_file_that_redirects_to_latest_version(version) + ref = determine_ref_from(version) + locals = { ref: ref } + + erb = ERB.new(File.read('doc_config/gh-pages/index.html.erb')) + + within_gh_pages do + File.open('index.html', 'w') { |f| f.write(erb.result(binding)) } + sh 'git add index.html' + end + end + + def determine_ref_from(version) + if version =~ /^\d+\.\d+\.\d+/ + 'v' + version + else + version + end + end + + def build_commit_message(version) + "Regenerated docs for version #{version}" + end + + def within_gh_pages(&block) + Dir.chdir(GH_PAGES_DIR, &block) + end +end + +task release: 'docs:publish_latest' diff --git a/doc_config/gh-pages/index.html.erb b/doc_config/gh-pages/index.html.erb new file mode 100644 index 00000000..0d2026a9 --- /dev/null +++ b/doc_config/gh-pages/index.html.erb @@ -0,0 +1,9 @@ + + + + shoulda-matchers Documentation - latest + + + + +