Class: Uas2Git::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/uas2git/main.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Main

Returns a new instance of Main.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#parse(args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



95
96
97
98
99
# File 'lib/uas2git/main.rb', line 95

def show_help_message(msg)
  puts "Error starting script: #{msg}\n\n"
  puts @opts.help
  exit
end