Skip to content

Commit 498bfa0

Browse files
author
Eric Miller
committed
Add new project refresh mechanism
1 parent d57c21c commit 498bfa0

File tree

7 files changed

+264
-402
lines changed

7 files changed

+264
-402
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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() -> 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 XcodeProjectRoot = "../../iOS";
43+
private const string XcodeProjectName = "UnityBuildKit";
44+
private const string ClassesProjectPath = "Vendor/UBK/Classes";
45+
private const string LibrariesProjectPath = "Vendor/UBK/Libraries";
46+
private const string PbxFilePath = XcodeProjectName + ".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(XcodeProjectRoot, PbxFilePath);
63+
pbx.ReadFromFile(pbxPath);
64+
65+
ProcessUnityDirectory(
66+
pbx,
67+
Path.Combine(pathToBuiltProject, "Classes"),
68+
Path.Combine(XcodeProjectRoot, ClassesProjectPath),
69+
ClassesProjectPath);
70+
71+
ProcessUnityDirectory(
72+
pbx,
73+
Path.Combine(pathToBuiltProject, "Libraries"),
74+
Path.Combine(XcodeProjectRoot, 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(XcodeProjectName);
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+
}

Sources/UBKit/Models/UBKitError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public enum UBKitError: Error, CustomStringConvertible {
3232
case invalidXcodeProject(String)
3333
case missingGroup(String)
3434
case invalidUnityProject
35+
case missingUnityFile(String)
3536
case unableToCreateXcodeProjectGroup(String)
3637
case unableToSaveXcodeProject
3738
case waitTimedOut
@@ -57,6 +58,8 @@ public enum UBKitError: Error, CustomStringConvertible {
5758
return "Could not find \(str)"
5859
case .invalidUnityProject:
5960
return "Invalid Unity Project"
61+
case .missingUnityFile(let str):
62+
return "Missing Unity File: \(str)"
6063
case .unableToCreateXcodeProjectGroup(let str):
6164
return "Could not create Xcode Group: \(str)"
6265
case .unableToSaveXcodeProject:

Sources/UBKit/Models/UnityCommandLine.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ import Foundation
2828
*/
2929
struct UnityCommandLine {
3030
static let buildAction = "iOSBuilder.Perform"
31-
static let refreshAction = "iOSRefresher.Perform"
31+
static let refreshAction = "XcodeRefresher.Refresh"
3232

3333
struct Arguments {
3434
static let projectPath = "-projectPath"
3535
static let createProject = "-createProject"
3636
static let outputLocation = "-outputLocation"
37+
static let buildPath = "-buildPath"
3738
static let sceneName = "-sceneName"
3839
static let executeMethod = "-executeMethod"
3940
static let batchmode = "-batchmode"

0 commit comments

Comments
 (0)