Class: Uas2Git::Migration

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Migration

Returns a new instance of Migration.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/uas2git/migration.rb', line 8

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

  if @options[:password].nil? then
    @options[:password] = ask('Enter password for ' + @options[:username] + '@' + @options[:host] + ': ') { |q| q.echo = false }
  end
end

Instance Method Details

#parse(args) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/uas2git/migration.rb', line 153

def parse(args)
  # Set up reasonable defaults for options.
  options = {}
  options[:host] = 'localhost'
  options[:username] = 'admin'
  options[:password] = nil

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

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

    opts.on('-H', '--host NAME', 'Unity Asset Server host') do |host|
      options[:host] = host
    end

    opts.on('-u', '--username NAME', 'Crendential for Unity Asset Server') do |username|
      options[:username] = username
    end

    opts.on('-p', '--password PASSWD') do |password|
      options[:password] = password
    end

    opts.separator ''

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end
  end

  @opts.parse! args
  options
end

#run!Object



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
62
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/uas2git/migration.rb', line 26

def run!
  ActiveRecord::Base.establish_connection(
      :adapter  => 'postgresql',
      :host     => @options[:host],
      :port     => '10733',
      :username => @options[:username],
      :password => @options[:password],
      :database => @project_name
  )

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

  # Import files
  oids = {}
  meta_oids = {}

  asset_versions = Uas2Git::Uas::Model::AssetVersion.joins(:type).where('assettype.description <> \'dir\'')
  Progress.start('Importing ' + asset_versions.count.to_s + ' files', asset_versions.count) do
    asset_versions.find_each do |asset_version|
      Progress.step do
        asset_version.contents.each do |contents|
          blob = ''

          asset_version.transaction do
            fd = asset_version.class.connection.raw_connection.lo_open(contents.stream, PG::INV_READ)

            loop do
              data = asset_version.class.connection.raw_connection.lo_read(fd, 65536)
              break if data == nil || data.empty?

              blob += data
            end

            asset_version.class.connection.raw_connection.lo_close(fd)
          end

          oid = repo.write(blob, :blob)

          if contents.tag == 'asset' then
            oids[asset_version.asset.serial] = {} if oids[asset_version.asset.serial].nil?
            oids[asset_version.asset.serial][asset_version.revision] = oid
          elsif contents.tag == 'asset.meta' then
            meta_oids[asset_version.asset.serial] = {} if meta_oids[asset_version.asset.serial].nil?
            meta_oids[asset_version.asset.serial][asset_version.revision] = oid
          end
        end
      end
    end
  end

  # Precalculating directory paths from changesets
  dirs = {}
  prev_changeset = nil

  Progress.start('Precalculating directory paths', Uas2Git::Uas::Model::Changeset.count) do
    Uas2Git::Uas::Model::Changeset.find_each do |changeset|
      Progress.step do
        dirs[changeset.serial] = prev_changeset ? dirs[prev_changeset.serial].clone : {}

        changeset.asset_versions.joins(:type).where('assettype.description = \'dir\'').find_each do |asset_version|
          if asset_version.parent then
            dirs[changeset.serial][asset_version.asset.serial] = dirs[changeset.serial][asset_version.parent.serial] + '/' + asset_version.name
          else
            dirs[changeset.serial][asset_version.asset.serial] = asset_version.name
          end
        end

        prev_changeset = changeset
      end
    end
  end

  # Importing changesets
  Progress.start('Importing ' + Uas2Git::Uas::Model::Changeset.count.to_s + ' changesets', Uas2Git::Uas::Model::Changeset.count) do
    Uas2Git::Uas::Model::Changeset.find_each do |changeset|
      Progress.step do
        index = Rugged::Index.new

        Uas2Git::Uas::Model::AssetVersion.joins(:type).where('assettype.description <> \'dir\'').where(
            'created_in <= :c AND assetversion.serial IN (SELECT assetversion FROM changesetcontents WHERE changesetcontents.changeset <= :c) AND revision = (SELECT MAX(revision) FROM assetversion AV2 WHERE AV2.asset = assetversion.asset AND AV2.created_in <= :c)',
            { :c => changeset.serial }
        ).find_each do |asset_version|
          if asset_version.parent then
            path = dirs[changeset.serial][asset_version.parent.serial] + '/' + asset_version.name
          else
            path = 'ProjectSettings/' + asset_version.name
          end

          if !path.start_with?('Trash/') then
            index.add(:path => path, :oid => oids[asset_version.asset.serial][asset_version.revision], :mode => 0100644)
            if meta_oids.has_key?(asset_version.asset.serial) then
              index.add(:path => path + '.meta', :oid => meta_oids[asset_version.asset.serial][asset_version.revision], :mode => 0100644)
            end
          end
        end

        author = {
            :name => changeset.creator.username,
            :email => '',
            :time => changeset.commit_time.nil? ? Time.now : changeset.commit_time
        }

        Rugged::Commit.create(repo, {
            :tree => index.write_tree(repo),
            :author => author,
            :committer => author,
            :message => changeset.description,
            :parents => repo.empty? ? [] : [ repo.head.target ].compact,
            :update_ref => 'HEAD'
        })
      end
    end
  end

  # 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



192
193
194
195
196
# File 'lib/uas2git/migration.rb', line 192

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