ruby Ruby脚本模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby脚本模板相关的知识,希望对你有一定的参考价值。

#!/usr/bin/ruby
#
# This script modifies or creates a gradle.properties file
# and injects Nexus credentials in it to be used to release
# artifacts to Nexus
#
# Usage:
#   nexus_gradle_properties.rb \
#     -u nexus_username \
#     -p nexus_password
#

require 'optparse'
require 'logger'

# Set up logger
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |severity, datetime, progname, msg|
  "#{severity}: #{msg}\n"
end
@logger.level = Logger::INFO

# Read in config file, modify contents, write it back
def main(options = parse_args)
  if !File.exists? gradle.properties
    create_file
  end
end

# Create gradle.pzroperties file
def create_file() {
  @logger.info 'No gradle.properties file found. Creating gradle.properties'

  gradle_properties <<-EOF
    nexus_username=${ARGV[0]}
    nexus_password=${ARGV[1]}
  EOF

  File.open('gradle.properties', 'w') {|f| f.write(gradle_properties)}
end

# Parse arguments
def parse_args()
  options = {}

  optparse = OptionParser.new do |opts|
    opts.banner <<-EOF
      Modifies or creates a gradle.properties file
      and injects Nexus credentials in it
      to be used to release artifacts to
      Nexus \nUsage: #{__FILE__} [options]"
    EOF

    opts.on("-u", "--nexus-username NEXUS_USERNAME", String, "Nexus username") do |f|
      options[:nexus_username] = u
    end
    opts.on("-p", "--nexus-password NEXUS_PASSWORD", String, "Nexus password") do |p|
      options[:nexus_password] = p
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  begin
    optparse.parse!

    if options.length < 2
      exit_with_error "Both the username and password for Nexus must be supplied\n\n#{optparse}"
    end
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    exit_with_error $!.to_s
  end

  options
end

def exit_with_error(message, status_code = 1)
  @logger.error message
  exit status_code
end

main

以上是关于ruby Ruby脚本模板的主要内容,如果未能解决你的问题,请参考以下文章

ruby Ruby脚本,看看是否用openssl编译了ruby

如何使用 ruby​​ 处理 mailchimp 模板?

ruby 在ruby脚本中使用gem

ruby 示例脚本em ruby

ruby 银行坐标卡的Ruby脚本

ruby CSV到JSON ruby​​脚本