Source

ruby-hglib / Rakefile

Full commit
require 'rake/testtask'
require 'yard'
require 'yard/rake/yardoc_task'

VERSION_FILE = 'lib/hglib/version.rb'

def build_version_file
  puts "Generating #{VERSION_FILE}..."
  nodeid = `hg id -i`.chomp
  version = `hg parents --template '{latesttag}+{latesttagdistance}'`
  case version
  when /^null+/
    full_version = "#{version.sub(/^null/, '0.0.0')}-#{nodeid}"
  when /\+0$/
    full_version = version[0 ... -2]
  else
    full_version = "#{version}-#{nodeid}"
  end

  File.open(VERSION_FILE, 'w') do |dest|
    dest.puts '# this file is autogenerated by rake'
    dest.puts %Q|module Hglib; VERSION = "#{full_version}"; end|
  end
end

# make sure to create version.rb so that gem_tasks can be loaded
build_version_file unless FileTest.exist? VERSION_FILE
require 'bundler/gem_tasks'

task :build => VERSION_FILE
file VERSION_FILE => '.hg/dirstate' do
  build_version_file
end

YARD::Rake::YardocTask.new do |t|
  t.files = ['lib/**/*.rb', '-', 'README.md']
end

Rake::TestTask.new

def hg(*args)
  fullargs = ['hg', '--config', 'defaults.commit=-d"0 0"',
              '--config', 'ui.username=test'] + args.map { |e| e.to_s }
  sh *fullargs
end

task :test => :setup_test_fixture
task :setup_test_fixture => ['tmp/emptyrepo', 'tmp/singlefilerepo',
                             'tmp/manyfilesrepo', 'tmp/mergedrepo',
                             'tmp/branchedrepo', 'tmp/taggedrepo']

directory 'tmp'

file 'tmp/emptyrepo' => 'tmp' do |t|
  hg 'init', t.name
end

file 'tmp/singlefilerepo' => 'tmp' do |t|
  hg 'init', t.name
  File.open("#{t.name}/file", 'wb') do |f|
    %w(foo bar baz).each do |l|
      f.puts l
      f.fsync
      hg '-R', t.name, 'ci', '-Am', "append #{l}"
    end
  end
end

file 'tmp/manyfilesrepo' => 'tmp' do |t|
  hg 'init', t.name
  %w(foo bar baz).each { |e| touch "#{t.name}/#{e}" }
  hg '-R', t.name, 'ci', '-Am', 'add files'
end

file 'tmp/multiheadsrepo' => 'tmp' do |t|
  hg 'init', t.name
  touch "#{t.name}/foo"
  hg '-R', t.name, 'ci', '-Am', 'add foo'
  %w(bar baz).each do |e|
    hg '-R', t.name, 'update', '-r0'
    touch "#{t.name}/#{e}"
    hg '-R', t.name, 'ci', '-Am', "add #{e}"
  end
end

file 'tmp/mergedrepo' => 'tmp/multiheadsrepo' do |t|
  hg 'clone', t.prerequisites.first, t.name
  hg '-R', t.name, 'merge', '--tool=internal:merge'
  hg '-R', t.name, 'ci', '-m', 'merge'
end

file 'tmp/branchedrepo' => 'tmp' do |t|
  hg 'init', t.name
  touch "#{t.name}/foo"
  hg '-R', t.name, 'ci', '-Am', 'add foo to default branch'
  %w(bar baz).each do |e|
    hg '-R', t.name, 'update', '-r0'
    hg '-R', t.name, 'branch', e
    hg '-R', t.name, 'ci', '-Am', "create branch #{e}"
  end
end

file 'tmp/taggedrepo' => 'tmp/singlefilerepo' do |t|
  hg 'clone', t.prerequisites.first, t.name
  %w(foo bar baz).each_with_index do |n, i|
    hg '-R', t.name, 'tag', '-r', i, n
  end
end

desc 'Remove itermediate files'
task :clean do
  rm_rf 'tmp'
  rm Dir['**/*.{orig,rej}']
end

desc 'Remove all generated files'
task :clobber => :clean do
  rm_rf ['doc', '.yardoc', 'lib/hglib/version.rb']
end