Class: UpmSupport::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/upm_support/actions.rb

Class Method Summary collapse

Class Method Details

.clean_empty_dirsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/upm_support/actions.rb', line 129

def self.clean_empty_dirs
  empty_dirs = Dir.glob("Assets/**/*").filter{|f| Dir.exist?(f)}.select{|d| Dir.empty?(d) }

  while empty_dirs.count > 0
    empty_dirs.each{|empty_dir| Dir.rmdir(empty_dir)}
    parent_empty_dirs = empty_dirs.map{|d| File.dirname(d)}.uniq
    while true do
        deleted = []
        parent_empty_dirs.each do |dir|
            metas = Dir.glob("#{dir}/*.meta")
            meta_and_non_metas = metas.map{|meta| [meta, File.join(File.dirname(meta), File.basename(meta, ".meta"))] }
            to_delete = meta_and_non_metas.select{|a| File.exist?(a[0]) and !File.exist?(a[1])}
            deleted.concat(to_delete)
            to_delete.each{|a| puts "Deleting #{a[0]}"; File.delete(a[0])}
        end
        break if deleted.count == 0
        sleep 0.1 # give it some time to delete things
    end
    empty_dirs = parent_empty_dirs.select{|d| Dir.glob("#{d}/**").count == 0}.uniq
  end
end

.migrate_dependencies_to_upm(dir, migrations, registry_uri, force_upgrade: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
62
63
64
65
66
# File 'lib/upm_support/actions.rb', line 9

def self.migrate_dependencies_to_upm(dir, migrations, registry_uri, force_upgrade: false)
  up = UnityProject.new(dir)
  raise "Unity Project to migrate is too old (#{up.version}), some features might break. Consider upgrading to at least #{up.minimum_supported} first" unless up.is_minimum_supported?

  unless File.exist?("#{dir}/Upfile.lock")
    puts "No Upfile.lock here. Exiting"
    exit(-1)
  end
  upfile_lock = UpfileLock.new("#{dir}/Upfile.lock")
  upm_packages = UPMPackages.new("#{dir}/Packages/manifest.json")

  changed = false
  registry = UpmRegistry.new(registry_uri)
  upfile_lock.dependencies.each do |uplift_package, version|
    next unless migrations.keys.include? uplift_package

    upm_package = migrations[uplift_package]

    replacement_version = nil
    if force_upgrade
      puts "Trying to upgrade #{uplift_package} to the latest UPM one"

      candidate_versions = registry.versions(upm_package)
      if candidate_versions.empty?
        puts 'None found'
        next
      end
      replacement_version = candidate_versions.last
    else
      puts "checking if #{uplift_package}@#{version} has an equivalent UPM one"

      versions = registry.versions(upm_package)
      candidate_versions = versions.filter { |v| v == version || v =~ /#{version}-exp./ }
      if candidate_versions.empty?
        puts 'None found'
        next
      end
      replacement_version = if candidate_versions.include? version
                              version
                            else
                              candidate_versions.last # assume in order of releases....
                            end
    end
    puts "Replacing #{uplift_package}@#{version} with #{upm_package}@#{replacement_version}"
    upfile_lock.remove_dependency(uplift_package, version)
    upm_packages.add_dependency(upm_package, replacement_version)
    changed = true
  end
  return unless changed

  dragonbox_registry_present = upm_packages.scoped_registries.map { |r| r['url'] }.include? registry_uri
  upm_packages.add_scoped_registry('DragonBox', registry_uri, ['com.dragonbox']) unless dragonbox_registry_present

  upm_packages.save
  upfile_lock.save
  command = "git diff #{upfile_lock.path} #{upm_packages.path}"
  puts `#{command}`
end

.remove_deleted_uplift_assets(dir) ⇒ Object



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
# File 'lib/upm_support/actions.rb', line 68

def self.remove_deleted_uplift_assets(dir)
  upbring_path = "#{dir}/UPackages/Upbring.xml"
  unless File.exist? upbring_path
    puts "No Uplift setup to deal with"
    return
  end
  upbring = Upbring.new(upbring_path)

  upfile_lock_path = "#{dir}/Upfile.lock"
  upfile_lock = nil
  upfile_lock = UpfileLock.new(upfile_lock_path) if File.exist?(upfile_lock_path)

  upbring.installed_packages.each do |ip|
    if upfile_lock
      puts "Searching for #{ip.name} #{ip.version}"
      dependency = upfile_lock.dependencies[ip.name]
      if dependency == ip.version
        puts "Dependency #{ip.name}@#{ip.version} still in Upfile.lock"
        next
      end
    end

    puts "Dependency #{ip.name}@#{ip.version} not in Upfile.lock anymore, cleaning up"
    is_with_guids = ip.install_specs.reject { |is| is.guid.nil? }
    is_with_guids.each do |is|
      # search for asset
      is_guid = is.guid
      command = "grep -r \"^guid: #{is_guid}\" '#{dir}/Assets' --include '*.meta' | cut -d ':' -f 1"
      # FIXME you can have multiple results here, and you should exclude those that are in git...
      metapath = `#{command}`.strip.split("\n").first

      # if found
      if metapath != ""
        path = File.join(File.dirname(metapath),File.basename(metapath, '.meta'))
        [path, metapath].each do |file|
          if !File.exist?(file)
            puts "ERROR: file #{file} doesn't exist"
            next
          end
          puts "Deleting #{file}"
          File.delete(file)
        end
      end

      upbring.remove_package(ip.name)
      upbring.save
    end

    is_with_paths = ip.install_specs.reject { |is| is.path.nil? }
    is_with_paths.each do |is|
      path = "#{dir}/#{is.path}"
      if Dir.exist? path
        puts "Deleting #{path} recursively"
        FileUtils.rm_r path
      end
    end
  end
  self.clean_empty_dirs
end