Class: Fastlane::Helper::UnityEditorHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb

Class Method Summary collapse

Class Method Details

.find_best_unity_editor_versionObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 33

def self.find_best_unity_editor_version
  installed_editors = Helper::UnityHubHelper.unity_hub_installed_editors

  unity_project_version = load_unity_project_version
  UI.important("Unity project uses version '#{unity_project_version}'")

  if installed_editors.key?(unity_project_version)
    UI.important("'#{unity_project_version}' is installed!")
    return installed_editors[unity_project_version][2]
  end

  fallback_editor_version = find_closest_unity_version(unity_project_version, installed_editors.keys)
  if fallback_editor_version == ""
    # TODO: offer to install appropriate editor via Hub?
    UI.user_error!("'#{unity_project_version}' not installed. No appropriate fallback found. Please install ‘#{unity_project_version}‘ or the latest '#{unity_project_version_no_patch}' manually via the Unity Hub.")
    return ""
  end

  UI.important("'#{unity_project_version}' not installed. Using '#{fallback_editor_version}' instead.")
  return installed_editors[fallback_editor_version][2]
end

.find_closest_unity_version(unity_version, other_versions) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 93

def self.find_closest_unity_version(unity_version, other_versions)
  # finds closest version by ignoring "patch"
  closest_version = ""
  version_no_patch_regex = /\d+\.\d+/
  unity_project_version_no_patch = unity_version.match(version_no_patch_regex)[0]
  other_versions_sorted = sort_unity_versions(other_versions)

  other_versions_sorted_no_patch = other_versions_sorted.map do |other_version|
    m = other_version.match(version_no_patch_regex)
    if m.length > 0
      m[0]
    end
  end

  other_versions_sorted_no_patch.each_with_index do |other_version_no_patch, index|
    if other_version_no_patch == unity_project_version_no_patch
      # since the list is sorted, we keep going to match the highest available path version
      closest_version = other_versions_sorted[index]
    end
  end

  return closest_version
end

.load_unity_project_package_manifestObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 68

def self.load_unity_project_package_manifest
  relative_path = if FastlaneCore::Helper.is_test?
                    "/tmp/fastlane/tests/fixtures/unity_project"
                  else
                    unity_project_path
                  end

  package_manifest_path = "#{relative_path}/Packages/manifest.json"
  return File.read(package_manifest_path)
end

.load_unity_project_versionObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 79

def self.load_unity_project_version
  relative_path = if FastlaneCore::Helper.is_test?
                    "/tmp/fastlane/tests/fixtures/unity_project"
                  else
                    unity_project_path
                  end

  project_version_txt_path = "#{relative_path}/ProjectSettings/ProjectVersion.txt"
  project_version_txt = File.read(project_version_txt_path)
  project_version_match = project_version_txt.scan(/.*: (\d+\.\d+\.\d+[abf]\d+).*/)
  project_version = project_version_match[0][0]
  return project_version
end

.sort_unity_versions(unity_versions) ⇒ Object



117
118
119
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 117

def self.sort_unity_versions(unity_versions)
  return unity_versions.sort_by { |v| Gem::Version.new(v) }
end

.unity_editor_pathObject



28
29
30
31
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 28

def self.unity_editor_path
  unity_binary_path = find_best_unity_editor_version
  return Helper::GenericHelper.shellify(unity_binary_path)
end

.unity_project_pathObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 16

def self.unity_project_path
  project_path = @project_path == "" ? @project_path_default : @project_path

  # we verify the path to the Unity project by looking for the 'manifest.json' file
  package_manifest_path = "#{project_path}/Packages/manifest.json"
  unless File.file?(package_manifest_path)
    UI.user_error!("Cannot find 'manifest.json' at '#{package_manifest_path}'. Make sure that the Unity project path is properly set.")
  end

  return project_path
end

.verify_exporter_packageObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/unity_exporter/helper/unity_editor_helper.rb', line 55

def self.verify_exporter_package
  # verifies that the UnityExporter package has been added to the Unity project
  # TODO Unity currently does not support commandline arguments for the Package Manager
  exporter_package_namespace = "io.armet.unity.buildexporter"
  included = load_unity_project_package_manifest.include?(exporter_package_namespace)
  # UI.message("exporter package part of Unity project: '#{included}'")
  unless included
    UI.user_error!("Package 'io.armet.unity.exporter' must be added to the Unity project.")
  end

  return included
end