|
| 1 | +// |
| 2 | +// UnityProjectScript.swift |
| 3 | +// UnityBuildKitPackageDescription |
| 4 | +// |
| 5 | +// Copyright (c) 2017 Handsome |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | + |
| 25 | +import Foundation |
| 26 | + |
| 27 | +extension File { |
| 28 | + |
| 29 | + class func unityProjectScriptFile(projectName: String, iOSProjectPath: String) -> Data? { |
| 30 | + let file = """ |
| 31 | + using System.Linq; |
| 32 | + using System.Collections.Generic; |
| 33 | + using System.IO; |
| 34 | + using System.Text; |
| 35 | + using UnityEngine; |
| 36 | + using UnityEditor; |
| 37 | + using UnityEditor.Callbacks; |
| 38 | + using UnityEditor.iOS.Xcode; |
| 39 | +
|
| 40 | + public class XcodeRefresher { |
| 41 | +
|
| 42 | + private const string iOSProjectRoot = \"\(iOSProjectPath)\"; |
| 43 | + private const string iOSProjectName = \"\(projectName)\"; |
| 44 | + private const string ClassesProjectPath = "Vendor/UBK/Classes"; |
| 45 | + private const string LibrariesProjectPath = "Vendor/UBK/Libraries"; |
| 46 | + private const string PbxFilePath = iOSProjectName + ".xcodeproj/project.pbxproj"; |
| 47 | +
|
| 48 | + public static void Refresh() { |
| 49 | + var pathToBuiltProject = GetArg ("-buildPath"); |
| 50 | + UpdateUnityProjectFiles (pathToBuiltProject); |
| 51 | + } |
| 52 | +
|
| 53 | + #if UNITY_IOS |
| 54 | + [PostProcessBuild] |
| 55 | + public static void OnPostBuild(BuildTarget target, string pathToBuiltProject) { |
| 56 | + UpdateUnityProjectFiles(pathToBuiltProject); |
| 57 | + } |
| 58 | + #endif |
| 59 | +
|
| 60 | + private static void UpdateUnityProjectFiles(string pathToBuiltProject) { |
| 61 | + var pbx = new PBXProject(); |
| 62 | + var pbxPath = Path.Combine(iOSProjectRoot, PbxFilePath); |
| 63 | + pbx.ReadFromFile(pbxPath); |
| 64 | +
|
| 65 | + ProcessUnityDirectory( |
| 66 | + pbx, |
| 67 | + Path.Combine(pathToBuiltProject, "Classes"), |
| 68 | + Path.Combine(iOSProjectRoot, ClassesProjectPath), |
| 69 | + ClassesProjectPath); |
| 70 | +
|
| 71 | + ProcessUnityDirectory( |
| 72 | + pbx, |
| 73 | + Path.Combine(pathToBuiltProject, "Libraries"), |
| 74 | + Path.Combine(iOSProjectRoot, LibrariesProjectPath), |
| 75 | + LibrariesProjectPath); |
| 76 | +
|
| 77 | + pbx.WriteToFile(pbxPath); |
| 78 | + } |
| 79 | +
|
| 80 | + /// <summary> |
| 81 | + /// Update pbx project file by adding src files and removing extra files that |
| 82 | + /// exists in dest but not in src any more. |
| 83 | + /// |
| 84 | + /// This method only updates the pbx project file. It does not copy or delete |
| 85 | + /// files in Swift Xcode project. The Swift Xcode project will do copy and delete |
| 86 | + /// during build, and it should copy files if contents are different, regardless |
| 87 | + /// of the file time. |
| 88 | + /// </summary> |
| 89 | + /// <param name="pbx">The pbx project.</param> |
| 90 | + /// <param name="src">The directory where Unity project is built.</param> |
| 91 | + /// <param name="dest">The directory of the Swift Xcode project where the |
| 92 | + /// Unity project is embedded into.</param> |
| 93 | + /// <param name="projectPathPrefix">The prefix of project path in Swift Xcode |
| 94 | + /// project for Unity code files. E.g. "DempApp/Unity/Classes" for all files |
| 95 | + /// under Classes folder from Unity iOS build output.</param> |
| 96 | + private static void ProcessUnityDirectory(PBXProject pbx, string src, string dest, string projectPathPrefix) { |
| 97 | + var targetGuid = pbx.TargetGuidByName(iOSProjectName); |
| 98 | +
|
| 99 | + string[] newFiles, extraFiles; |
| 100 | + CompareDirectories(src, dest, out newFiles, out extraFiles); |
| 101 | +
|
| 102 | + foreach (var f in newFiles) { |
| 103 | + var projPath = Path.Combine(projectPathPrefix, f); |
| 104 | + if (!pbx.ContainsFileByProjectPath(projPath)) { |
| 105 | + var guid = pbx.AddFile(Path.Combine(src, f), projPath, PBXSourceTree.Absolute); |
| 106 | + pbx.AddFileToBuild(targetGuid, guid); |
| 107 | +
|
| 108 | + Debug.LogFormat("Added file to pbx: '{0}'", projPath); |
| 109 | + } |
| 110 | + } |
| 111 | +
|
| 112 | + foreach (var f in extraFiles) { |
| 113 | + var projPath = Path.Combine(projectPathPrefix, f); |
| 114 | + if (pbx.ContainsFileByProjectPath(projPath)) { |
| 115 | + var guid = pbx.FindFileGuidByProjectPath(projPath); |
| 116 | + pbx.RemoveFile(guid); |
| 117 | +
|
| 118 | + Debug.LogFormat("Removed file from pbx: '{0}'", projPath); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +
|
| 123 | + /// <summary> |
| 124 | + /// Compares the directories. Returns files that exists in src and |
| 125 | + /// extra files that exists in dest but not in src any more. |
| 126 | + /// </summary> |
| 127 | + private static void CompareDirectories(string src, string dest, out string[] srcFiles, out string[] extraFiles) { |
| 128 | + srcFiles = GetFilesRelativePath(src); |
| 129 | +
|
| 130 | + var destFiles = GetFilesRelativePath(dest); |
| 131 | + var extraFilesSet = new HashSet<string>(destFiles); |
| 132 | +
|
| 133 | + extraFilesSet.ExceptWith(srcFiles); |
| 134 | + extraFiles = extraFilesSet.ToArray(); |
| 135 | + } |
| 136 | +
|
| 137 | + private static string[] GetFilesRelativePath(string directory) { |
| 138 | + var results = new List<string>(); |
| 139 | +
|
| 140 | + if (Directory.Exists(directory)) { |
| 141 | + foreach (var path in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)) { |
| 142 | + var relative = path.Substring(directory.Length).TrimStart('/'); |
| 143 | + results.Add(relative); |
| 144 | + } |
| 145 | + } |
| 146 | +
|
| 147 | + return results.ToArray(); |
| 148 | + } |
| 149 | +
|
| 150 | + private static string GetArg(string name) { |
| 151 | + var args = System.Environment.GetCommandLineArgs(); |
| 152 | + for (int i = 0; i < args.Length; i++) { |
| 153 | + if (args [i] == name && args.Length > i + 1) { |
| 154 | + return args [i + 1]; |
| 155 | + } |
| 156 | + } |
| 157 | + return null; |
| 158 | + } |
| 159 | + } |
| 160 | + """.data(using: .utf8) |
| 161 | + return file |
| 162 | + } |
| 163 | +} |
0 commit comments