File gem_build of Package obs-service-gem_build

#!/usr/bin/ruby

require 'optparse'
require 'ostruct'
require 'pathname'
require 'fileutils'
require 'open3'
require "rubygems/gem_runner"

# Structure to hold the parsed options
options = OpenStruct.new(
    platform: false,
    force: false,
    strict: false,
    output: false,
    path: false,
    verbose: false,
    quiet: false,
    config_file: false,
    backtrace: false,
    debug: false,
    norc: false,
    noop: false,
    outdir: false
)

# Create the parser
parser = OptionParser.new do |opts|
    opts.banner = "Generate an gem "
    opts.separator ""
    opts.separator "Specific options:"

    opts.on("--noop [ENABLE]", "do nothing") do |v|
        options.noop = (v == 'enable')
    end

    opts.on("--norc [ENABLE]", "avoid loading any .gemrc file") do |v|
        options.norc = (v == 'enable')
    end

    opts.on("--debug [ENABLE]", "turn on ruby debugging") do |v|
        options.debug = (v == 'enable')
    end

    opts.on("--backtrace [ENABLE]", "show stack backtrace on errors") do |v|
        options.backtrace = (v == 'enable')
    end

    opts.on("--config-file [FILE]", "use this config file instead of default") do |v|
        options.config_file = v
    end

    opts.on("--quiet [ENABLE]", "silence command progress meter") do |v|
        options.quiet = (v == 'enable')
    end

    opts.on("--verbose [ENABLE]", "set the verbose level of output") do |v|
        options.verbose = (v == 'enable')
    end

    opts.on("--path [FILE]", "run as if gem build was started in path instead of the current working directory") do |v|
        options.path = v
    end

    opts.on("--output [FILE]", "output gem with the given filename") do |v|
        options.output = v
    end

    opts.on("--outdir [FILE]", "open build service argument") do |v|
        options.outdir = v
    end

    opts.on("--strict [ENABLE]", "consider warnings as errors when validating the spec") do |v|
        options.strict = (v == 'enable')
    end

    opts.on("--force [ENABLE]", "skip validation of the spec") do |v|
        options.force = (v == 'enable')
    end

    opts.on("--platform [FILE]", "specify the platform of gem to build") do |v|
        options.norc = v
    end
end

parser.parse! ARGV

if options.noop
    exit(0)
end

args = []

outdir = Dir.pwd

if options.outdir
   outdir = options.outdir
end
outdir = File.absolute_path(outdir)
path = outdir

if options.platform
    args << '--platform'
    args << options.platform
end

if options.output
   args << "--output"
   args << options.output
end

if options.path
    glob_paths = Dir.glob(
        options.path
    )
    if glob_paths.any?
        path = File.absolute_path(glob_paths.first)
        args << '-C'
        args << path
    else
        puts "path not found"
    end
end

args << 'build'

if options.config_file
    glob_paths = Dir.glob(options.config_file)
    if glob_paths.any?
        absolute_path = File.absolute_path(glob_paths.first)
        args << '--config-file'
        args << absolute_path
    else
        puts "config file not found"
    end
end

if options.force
   args << '--force'
end

if options.strict
    args << '--strict'
end

if options.verbose
    args << '--verbose'
end

if options.quiet
    args << '--quiet'
end

if options.backtrace
    args << '--backtrace'
end

if options.backtrace
    args << '--debug'
end

if options.backtrace
    args << '--norc'
end

puts "Executing command: gem #{args.join(' ')}"

Gem::GemRunner.new.run args.clone

FileUtils.mv(Dir.glob(path + "/*.gem" ), outdir)
openSUSE Build Service is sponsored by