Source

stash-log-parser / logparser / Rakefile

Full commit
require 'fileutils'
require 'rubygems'

begin
  require 'sysinfo'
rescue LoadError
  puts "Please install the sysinfo gem first"
  exit -1
end

ARTIFACT="dist/build/logparser/logparser".freeze

desc "Clean build artifacts"
task :clean do
  cabal 'clean'
  `rm -f logparser-*.tar.gz`
end

desc "Create the logparser binary"
task :build => [:setup, :clean, :compile]

task :setup do
  ["install --only-dependencies"].each do |cmd|
    cabal cmd
  end
end

task :compile do
  %w(configure build).each do |cmd|
    cabal cmd
  end
  `strip #{ARTIFACT}`
end

desc "Build and package the logparser"
task :package => :build do
  include FileUtils
  with_temp_dir "logparser" do |package_dir|
    cp "../README.md", package_dir
    %w(NOTICE LICENSE).each {|f| cp f, package_dir }
    bin_dir = File.join(package_dir, "bin")
    mkdir bin_dir
    cp ARTIFACT, bin_dir
    cp_r "../gnuplot", package_dir
    cp Dir["../*.sh"], package_dir
    sysinfo = SysInfo.new
    `tar zfc logparser-#{sysinfo.impl}-#{sysinfo.arch}-#{version}.tar.gz logparser`
  end
end

desc "Run Hlint on the sources"
task :hlint => :build do
  print `hlint src -c --report --utf8`
end

desc "Rebuild and copy the logparser into the cabal bin directory"
task :rebuild => :build do
  cabal 'copy'
end


desc "Run the tests"
task :test do
  ["clean", "install --only-dependencies --enable-tests", "configure --enable-tests", "build", "test"].each do |cmd|
    cabal cmd
  end
end

desc "Update version to the next minor version, tag the result"
task :bump_minor_version do
  current_version = `git describe --tags`.strip.split("-").first.gsub(/^v/, '')
  major, minor = current_version.split(".").map {|e| e.to_i}
  next_version = [major, minor.next].join(".")

  # Update version in Main
  update_file(File.join("src", "Main.hs"), /appVersion = "[^"]+"/, "appVersion = \"#{next_version}\"")

  # Update cabal file
  update_file("logparser.cabal", /^version:.*$/, "version:                #{next_version}")

  # Commit and tag
  msg = "Bump version to #{next_version}"
  puts msg
  `git add -u`
  `git commit -m "#{msg}"`
  `git tag -a -m "#{msg}" v#{next_version}`
end

def version
  `git describe --tag --always`.strip
end

def cabal(cmd)
  print `cabal #{cmd}`
  raise "Failed to execute 'cabal #{cmd}'" unless $?.to_i.eql?(0)
end

def with_temp_dir(name)
  raise "The directory #{name} already exists" if File.directory?(name)
  mkdir name
  yield name
ensure
  rm_r name
end


def update_file(file, pattern, replace)
  updated = IO.read(file)
  File.open(file, "w") do |f|
    f << updated.gsub(pattern, replace)
  end
end