Class: U3d::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/u3d/commands.rb

Overview

API for U3d, redirecting calls to class they concern

Class Method Summary collapse

Class Method Details

.credentials(args: [], options: {}) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/u3d/commands.rb', line 219

def credentials(args: [], options: {})
  action = args[0]
  raise "Please specify an action to perform, one of #{credentials_actions.join(',')}" unless action
  raise "Unknown action '#{action}'. Use one of #{credentials_actions.join(',')}" unless credentials_actions.include? action
  if action == 'add'
    U3dCore::Globals.use_keychain = true
    # credentials = U3dCore::Credentials.new(user: ENV['USER'])
    # credentials.login # ask password
    UI.error 'Invalid credentials' unless U3dCore::CommandExecutor.has_admin_privileges?
  elsif action == 'remove'
    U3dCore::Globals.use_keychain = true
    U3dCore::Credentials.new(user: ENV['USER']).forget_credentials(force: true)
  else
    U3dCore::Globals.use_keychain = true
    credentials = U3dCore::Credentials.new(user: ENV['USER'])
    U3dCore::Globals.(true) do
      if credentials.password.to_s.empty?
        UI.message "No credentials stored"
      else
        if U3dCore::CommandExecutor.has_admin_privileges?
          UI.success "Stored credentials are valid"
        else
          UI.error "Stored credentials are not valid"
        end
      end
    end
    # FIXME: return value
  end
end

.credentials_actionsObject



215
216
217
# File 'lib/u3d/commands.rb', line 215

def credentials_actions
  %w(add remove check)
end

.download(args: [], options: {}) ⇒ Object



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
# File 'lib/u3d/commands.rb', line 98

def download(args: [], options: {})
  version = args[0]
  UI.user_error!('Please specify a Unity version to download') unless version

  packages = packages_with_unity_first(options)

  os = U3dCore::Helper.operating_system
  cache = Cache.new(force_os: os)
  versions = cache[os.id2name]['versions']
  version = interpret_latest(version, versions)

  unless packages.include?('Unity')
    unity = check_unity_presence(version: version)
    return unless unity
    options[:installation_path] ||= unity.path if Helper.windows?
  end

  U3d::Globals.use_keychain = true if options[:keychain] && Helper.mac?

  unless options[:no_install]
    UI.important 'Root privileges are required'
    raise 'Could not get administrative privileges' unless U3dCore::CommandExecutor.has_admin_privileges?
  end

  files = []
  if os == :linux
    UI.important 'Option -a | --all not available for Linux' if options[:all]
    UI.important 'Option -p | --packages not available for Linux' if options[:packages]
    downloader = Downloader::LinuxDownloader
    files << ["Unity #{version}", downloader.download(version, versions), {}]
  else
    downloader = Downloader::MacDownloader if os == :mac
    downloader = Downloader::WindowsDownloader if os == :win
    if options[:all]
      files = downloader.download_all(version, versions)
    else
      packages.each do |package|
        result = downloader.download_specific(package, version, versions)
        files << [package, result[0], result[1]] unless result.nil?
      end
    end
  end

  return if options[:no_install]
  files.each do |name, file, info|
    UI.verbose "Installing #{name}#{info['mandatory'] ? ' (mandatory package)' : ''}, with file #{file}"
    Installer.install_module(file, version, installation_path: options[:installation_path], info: info)
  end
end

.list_available(options: {}) ⇒ Object



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
# File 'lib/u3d/commands.rb', line 52

def list_available(options: {})
  ver = options[:unity_version]
  os = options[:operating_system]
  rl = options[:release_level]
  if os
    os = os.to_sym
    oses = U3dCore::Helper.operating_systems
    raise "Specified OS (#{os}) isn't valid [#{oses.join(', ')}]" unless oses.include?(os)
  else
    os = U3dCore::Helper.operating_system
  end
  cache = Cache.new(force_os: os, force_refresh: options[:force])
  versions = {}

  return UI.error "Version #{ver} is not in cache" if ver && cache[os.id2name]['versions'][ver].nil?

  versions = if ver
               { ver => cache[os.id2name]['versions'][ver] }
             else
               cache[os.id2name]['versions']
             end

  vcomparators = versions.keys.map { |k| UnityVersionComparator.new(k) }
  if rl
    letter = release_letter_mapping["latest_#{rl}".to_sym]
    UI.message "Filtering available versions with release level '#{rl}' [letter '#{letter}']"
    vcomparators.select! { |vc| vc.version.parts[3] == letter }
  end
  sorted_keys = vcomparators.sort.map { |v| v.version.to_s }

  sorted_keys.each do |k|
    v = versions[k]
    UI.message "Version #{k}: " + v.to_s.cyan.underline
    next unless options[:packages]
    inif = nil
    begin
      inif = U3d::INIparser.load_ini(k, versions, os: os)
    rescue => e
      UI.error "Could not load packages for this version (#{e})"
    else
      UI.message 'Packages:'
      inif.keys.each { |pack| UI.message " - #{pack}" }
    end
  end
end

.list_installed(options: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/u3d/commands.rb', line 37

def list_installed(options: {})
  list = Installer.create.installed
  if list.empty?
    UI.important 'No Unity version installed'
    return
  end
  list.each do |u|
    UI.message "Version #{u.version}\t(#{u.path})"
    packages = u.packages
    next unless options[:packages] && packages && !packages.empty?
    UI.message 'Packages:'
    packages.each { |pack| UI.message " - #{pack}" }
  end
end

.local_analyze(args: []) ⇒ Object

Raises:

  • (ArgumentError)


249
250
251
252
253
254
255
256
257
# File 'lib/u3d/commands.rb', line 249

def local_analyze(args: [])
  raise ArgumentError, 'No files given' if args.empty?
  raise ArgumentError, "File #{args[0]} does not exist" unless File.exist? args[0]

  analyzer = LogAnalyzer.new
  File.open(args[0], 'r') do |f|
    f.readlines.each { |l| analyzer.parse_line l }
  end
end

.local_install(args: [], options: {}) ⇒ Object



148
149
150
151
152
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
# File 'lib/u3d/commands.rb', line 148

def local_install(args: [], options: {})
  UI.user_error!('Please specify a version') if args.empty?
  version = args[0]

  packages = packages_with_unity_first(options)

  unless packages.include?('Unity')
    unity = check_unity_presence(version: version)
    return unless unity
    options[:installation_path] ||= unity.path if Helper.windows?
  end

  U3d::Globals.use_keychain = true if options[:keychain] && Helper.mac?

  UI.important 'Root privileges are required'
  raise 'Could not get administrative privileges' unless U3dCore::CommandExecutor.has_admin_privileges?

  os = U3dCore::Helper.operating_system
  files = []
  if os == :linux
    UI.important 'Option -a | --all not available for Linux' if options[:all]
    UI.important 'Option -p | --packages not available for Linux' if options[:packages]
    downloader = Downloader::LinuxDownloader
    files << ["Unity #{version}", downloader.local_file(version), {}]
  else
    downloader = Downloader::MacDownloader if os == :mac
    downloader = Downloader::WindowsDownloader if os == :win
    if options[:all]
      files = downloader.all_local_files(version)
    else
      packages.each do |package|
        result = downloader.local_file(package, version)
        files << [package, result[0], result[1]] unless result.nil?
      end
    end
  end

  files.each do |name, file, info|
    UI.verbose "Installing #{name}#{info['mandatory'] ? ' (mandatory package)' : ''}, with file #{file}"
    Installer.install_module(file, version, installation_path: options[:installation_path], info: info)
  end
end

.release_letter_mappingObject



263
264
265
266
267
268
269
270
# File 'lib/u3d/commands.rb', line 263

def release_letter_mapping
  {
    latest: 'f',
    latest_stable: 'f',
    latest_beta: 'b',
    latest_patch: 'p'
  }
end

.release_levelsObject



259
260
261
# File 'lib/u3d/commands.rb', line 259

def release_levels
  [:stable, :beta, :patch]
end

.run(options: {}, run_args: []) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/u3d/commands.rb', line 191

def run(options: {}, run_args: [])
  version = options[:unity_version]

  runner = Runner.new
  pp = runner.find_projectpath_in_args(run_args)
  pp = Dir.pwd unless pp
  up = UnityProject.new(pp)

  unless version # fall back in project default if we are on a Unity project
    version = up.editor_version if up.exist?
    unless version
      UI.user_error!('Not sure which version of Unity to run. Are you in a project?')
    end
  end

  run_args = ['-projectpath', up.path] if run_args.empty? && up.exist?

  # we could
  # * support matching 5.3.6p3 if passed 5.3.6
  unity = Installer.create.installed.find { |u| u.version == version }
  UI.user_error! "Unity version '#{version}' not found" unless unity
  runner.run(unity, run_args, raw_logs: options[:raw_logs])
end