binarybabel/gem-versioneer

View on GitHub
bin/versioneer

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'optparse'
require 'versioneer'

cli_opts = {}
repo_opts = {}

opparser = OptionParser.new do |opts|
  opts.banner = [
      '-------------------------------------',
      'Usage: versioneer [action] [options]',
      'Guide: https://git.io/versioneer-help',
      '-------------------------------------',
      ' Actions',
      '    %-33s%s' % ['status', 'Report on versions for all environments (default action)'],
      '    %-33s%s' % ['print', 'Print version number for current environment'],
      '    %-33s%s' % ['init', 'Generate default configuration file'],
      '    %-33s%s' % ['lock', 'Lock (save) current version (for deployments without VCS repo)'],
      '    %-33s%s' % ['unlock', 'Unlock (discard) previously saved version'],
      '',
      ' Environment Options',
      '    %-33s%s' % ['VERSIONEER_ENV > RAILS_ENV > RACK_ENV > ENV     (searched in order, default value = production)', ''],
      '',
      ' Options',
  ].join("\n")
  opts.on('-c FILE', '--config=FILE', 'Config file, default: .versioneer.yml') do |v|
    cli_opts[:config] = v
  end
  opts.on('-e ENVIRONMENT', '--env=ENVIRONMENT', '(development|production) ... overrides system vars, see guide') do |v|
    repo_opts[:environment] = v
  end
  opts.on('-q', '--quiet', 'Silence action output') do |v|
    cli_opts[:quiet] = true
  end
end
opparser.parse!

if (config_base = cli_opts[:config])
  unless config_base.match(%r{#{File::SEPARATOR}})
    config_base = File.join(Dir.getwd, cli_opts[:config])
  end
  unless File.exist?(config_base)
    STDERR.puts("ERROR: Versioneer config file does not exist. (#{config_base})")
    exit(1)
  end
else
  config_base = Dir.getwd
end

begin
  v = Versioneer::Config.new(config_base, repo_opts)
rescue Versioneer::MissingConfigError => e
  if cli_opts[:config]
    # Rethrow missing config error if file given on command-line.
    raise e
  else
    # Use default VCS if config file not given.
    v = Versioneer.const_get(Versioneer::Config::DEFAULT_TYPE.to_sym).new(Dir.getwd, repo_opts)
  end
end

case (cmd = ARGV.shift)
  when nil, 'status'
    puts '---------------------------------------------------'
    puts '%51s' % ["VERSIONEER GEM v#{Versioneer::GEM_VERSION}"]
    puts '---------------------------------------------------'
    puts 'Versioneer has calculated the following versions...'
    if File.exist?(Versioneer::Config::DEFAULT_FILE)
      puts "* Edit '#{Versioneer::Config::DEFAULT_FILE}' to modify project settings"
    else
      puts '* Run `versioneer init` to customize this project'
    end
    puts '* Try the --help option for additional commands'
    puts '---------------------------------------------------'
    locked = (v.respond_to? :lock_file and File.exist?(v.lock_file)) && '* LOCKED' || ''
    v.environment = 'development'
    puts "DEVELOPMENT -> #{v}    #{locked}"
    v.environment = 'production'
    puts " PRODUCTION -> #{v}    #{locked}"
  when 'print'
    puts v.to_s
  when 'lock', 'relock'
    v.unlock! if cmd == 'relock'
    v.lock!(ARGV.shift)
    unless cli_opts[:quiet]
      puts "+ #{Versioneer::Config::DEFAULT_LOCK}"
      puts "Version: #{v}"
    end
  when 'unlock'
    v.unlock!
    unless cli_opts[:quiet]
      puts "- #{Versioneer::Config::DEFAULT_LOCK}"
      puts "#{v.environment.capitalize} version now: #{v}"
    end
  when 'init'
    init_file = Versioneer::Config::DEFAULT_FILE
    if File.exist?(init_file)
      STDERR.puts("ERROR: Versioneer config file already exists.")
      exit(1)
    end
    File.open(init_file, 'w') do |file|
      puts "Generating config..." unless cli_opts[:quiet]
      [
          "# Versioneer Project Configuration",
          "# https://git.io/versioneer-help",
          "",
          "type: #{Versioneer::Config::DEFAULT_TYPE}",
          "bump_segment: #{v.bump_segment}",
          "prereleases:",
          "  - alpha",
          "  - beta",
          "  - rc",
      ].each do |line|
        file.write(line + "\n")
      end
      puts "+ #{File.basename(init_file)}" unless cli_opts[:quiet]
    end
  else
    puts opparser.help
end