class Uas2Git::Main

Public Class Methods

new(args) click to toggle source
# File lib/uas2git/main.rb, line 9
def initialize(args)
  @options = parse(args)

  show_help_message('Missing PROJECT_NAME parameter') if args.empty?
  show_help_message('Too many arguments') if args.size > 1

  @project_name = args.first

  begin
    show_help_message('The repository must be empty') unless Rugged::Repository.new('.').empty?
  rescue
  end

end

Public Instance Methods

parse(args) click to toggle source
# File lib/uas2git/main.rb, line 63
def parse(args)
  # Set up reasonable defaults for options.
  options = {}
  options[:host] = 'localhost'
  options[:username] = 'admin'

  @opts = OptionParser.new do |opts|
    opts.banner = 'Usage: uas2git PROJECT_NAME [options]'

    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('-h HOSTNAME', 'Unity Asset Server host (default: "localhost")') do |host|
      options[:host] = host
    end

    opts.on('-U NAME', 'Unity Asset Server user name (default: "admin")') do |username|
      options[:username] = username
    end

    opts.separator ''

    opts.on_tail('--help', 'Show this message') do
      puts opts
      exit
    end
  end

  @opts.parse! args
  options
end
run!() click to toggle source
# File lib/uas2git/main.rb, line 24
def run!
  password = ask('Enter password for ' + @options[:username] + '@' + @options[:host] + ': ') { |q| q.echo = false }

  connection = PG::connect(
      :host     => @options[:host],
      :port     => '10733',
      :user     => @options[:username],
      :password => password,
      :dbname   => 'template1'
  )

  result = connection.exec_params("SELECT db_name($1)", [ @project_name ])

  ActiveRecord::Base.establish_connection(
      :adapter  => 'postgresql',
      :host     => @options[:host],
      :port     => '10733',
      :username => @options[:username],
      :password => password,
      :database => result[0]['db_name']
  )

  # Initialize a git repository
  repo = Progress.start('Initializing a git repository', 1) do
    Progress.step do
      Rugged::Repository.init_at('.')
    end
  end

  Migrator.new(repo).migrate!

  # Checking out the working copy
  Progress.start('Checking out the work tree', 1) do
    Progress.step do
      repo.reset('HEAD', :hard)
    end
  end
end
show_help_message(msg) click to toggle source
# File lib/uas2git/main.rb, line 95
def show_help_message(msg)
  puts "Error starting script: #{msg}\n\n"
  puts @opts.help
  exit
end