Skip to content

Commit 9ba9e82

Browse files
authored
Merge pull request #3 from handsomecode/refresh
Add Refresh task
2 parents 48a6f0f + 1d6ff1d commit 9ba9e82

File tree

10 files changed

+396
-110
lines changed

10 files changed

+396
-110
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ DerivedData/
1717
!default.perspectivev3
1818
xcuserdata/
1919
.DS_Store
20-
iOS/
21-
Unity/
20+
/iOS/
21+
/Unity/
2222
ubconfig.json
2323
refreshprojects.swift
2424

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// UnityEditorScript.swift
3+
//
4+
// Copyright (c) 2017 Handsome
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
24+
import Foundation
25+
26+
extension File {
27+
28+
class func unityBuildScriptFile() -> Data? {
29+
let file = """
30+
using System.Collections;
31+
using System.IO;
32+
using UnityEngine;
33+
using UnityEditor;
34+
using UnityEditor.SceneManagement;
35+
using UnityEngine.SceneManagement;
36+
37+
public class iOSBuilder: MonoBehaviour {
38+
public static void Perform () {
39+
var outputLocation = GetArg ("-outputLocation");
40+
var sceneName = GetArg ("-sceneName");
41+
var scenePath = "Assets/Scenes/" + sceneName + ".unity";
42+
43+
if (!File.Exists (scenePath)) {
44+
Scene scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
45+
EditorSceneManager.SaveScene(scene, scenePath);
46+
}
47+
48+
BuildPlayerOptions playerOptions = new BuildPlayerOptions ();
49+
playerOptions.scenes = new[] { scenePath };
50+
playerOptions.locationPathName = outputLocation;
51+
playerOptions.target = BuildTarget.iOS;
52+
playerOptions.options = BuildOptions.None;
53+
BuildPipeline.BuildPlayer (playerOptions);
54+
}
55+
56+
private static string GetArg (string name) {
57+
var args = System.Environment.GetCommandLineArgs ();
58+
for (int i = 0; i < args.Length; i++) {
59+
if (args [i] == name && args.Length > i + 1) {
60+
return args [i + 1];
61+
}
62+
}
63+
return null;
64+
}
65+
}
66+
""".data(using: .utf8)
67+
return file
68+
}
69+
}

Sources/UBKit/Models/Unity.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Unity.swift
3+
//
4+
// Copyright (c) 2017 Handsome
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
24+
import Foundation
25+
26+
/**
27+
A helper class for interacting with the Unity command line.
28+
*/
29+
struct Unity {
30+
static let buildAction = "iOSBuilder.Perform"
31+
32+
struct Arguments {
33+
static let projectPath = "-projectPath"
34+
static let createProject = "-createProject"
35+
static let outputLocation = "-outputLocation"
36+
static let sceneName = "-sceneName"
37+
static let executeMethod = "-executeMethod"
38+
static let batchmode = "-batchmode"
39+
static let quit = "-quit"
40+
}
41+
}

Sources/UBKit/UBKit.swift

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,30 @@ public final class UBKit {
4444
return
4545
}
4646

47+
let workingPath = fileManager.currentDirectoryPath
48+
do {
49+
guard let fileData = fileManager.contents(atPath: workingPath.appending("/ubconfig.json")) else {
50+
completion(UBKitError.invalidFolder(workingPath.appending("/ubconfig.json")))
51+
return
52+
}
53+
if let configJSON = try JSONSerialization.jsonObject(with: fileData, options: .allowFragments) as? [String : String] {
54+
guard let config = Config(json: configJSON) else {
55+
completion(UBKitError.invalidConfigFile)
56+
return
57+
}
58+
kitManager = UBKitManager(config: config)
59+
}
60+
61+
} catch {
62+
completion(error)
63+
return
64+
}
65+
4766
switch arg {
4867
case .generate:
49-
generate(completion)
68+
generate(manager: kitManager, completion)
5069
case .refresh:
51-
refresh(completion)
70+
refresh(manager: kitManager, completion)
5271
}
5372
}
5473

@@ -69,27 +88,8 @@ private extension UBKit {
6988
case generate, refresh
7089
}
7190

72-
func generate(_ completion: @escaping ((Error?) -> Void)) {
73-
let workingPath = fileManager.currentDirectoryPath
74-
do {
75-
guard let fileData = fileManager.contents(atPath: workingPath.appending("/ubconfig.json")) else {
76-
completion(UBKitError.invalidFolder(workingPath.appending("/ubconfig.json")))
77-
return
78-
}
79-
if let configJSON = try JSONSerialization.jsonObject(with: fileData, options: .allowFragments) as? [String : String] {
80-
guard let config = Config(json: configJSON) else {
81-
completion(UBKitError.invalidConfigFile)
82-
return
83-
}
84-
kitManager = UBKitManager(config: config)
85-
}
86-
87-
} catch {
88-
completion(error)
89-
return
90-
}
91-
92-
let taskResult = kitManager.performTasks()
91+
func generate(manager: UBKitManager, _ completion: @escaping ((Error?) -> Void)) {
92+
let taskResult = manager.performGenerateTasks()
9393
guard taskResult == .success else {
9494
completion(taskResult.error)
9595
return
@@ -102,7 +102,12 @@ private extension UBKit {
102102
completion(nil)
103103
}
104104

105-
func refresh(_ completion: @escaping ((Error?) -> Void)) {
105+
func refresh(manager: UBKitManager, _ completion: @escaping ((Error?) -> Void)) {
106+
let taskResult = manager.performRefreshTasks()
107+
guard taskResult == .success else {
108+
completion(taskResult.error)
109+
return
110+
}
106111
print("\n----------")
107112
print("👍 Successfully refreshed your iOS project")
108113
completion(nil)

Sources/UBKit/UBKitManager.swift

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class UBKitManager {
4242
self.xcodeProjectYMLFileName = "project.yml"
4343
}
4444

45-
func performTasks() -> Result {
45+
func performGenerateTasks() -> Result {
4646
print("\n----------")
4747
print("Creating iOS Project")
4848
print("----------")
@@ -60,23 +60,39 @@ class UBKitManager {
6060
}
6161

6262
print("\n----------")
63-
print("Initializing projects")
63+
print("Copying Unity Files")
6464
print("----------")
65-
let refreshResult = refreshProjects()
65+
let copyFilesResult = copyUnityFiles(refresh: false)
66+
guard copyFilesResult == .success else {
67+
return copyFilesResult
68+
}
69+
70+
return .success
71+
}
72+
73+
func performRefreshTasks() -> Result {
74+
print("\n----------")
75+
print("Refreshing Xcode project")
76+
print("----------")
77+
let refreshResult = refreshXcodeProject()
6678
guard refreshResult == .success else {
6779
return refreshResult
6880
}
6981

7082
print("\n----------")
7183
print("Copying Unity Files")
7284
print("----------")
73-
let copyFilesResult = copyUnityFiles()
85+
let copyFilesResult = copyUnityFiles(refresh: true)
7486
guard copyFilesResult == .success else {
7587
return copyFilesResult
7688
}
7789

7890
return .success
7991
}
92+
}
93+
94+
// MARK: - Generate
95+
private extension UBKitManager {
8096

8197
func createiOSProject() -> Result {
8298
let xcodeProject = XcodeProject(
@@ -92,26 +108,36 @@ class UBKitManager {
92108
let unityProject = UnityProject(
93109
projectName: config.projectName,
94110
workingPath: workingUnityFolderPath,
95-
unityAppPath: config.unityPath
96-
)
97-
return unityProject.create()
98-
}
99-
100-
func refreshProjects() -> Result {
101-
let refreshser = ProjectRefresher(
102-
projectName: config.projectName,
103-
workingPath: workingFolderPath,
111+
unityAppPath: config.unityPath,
104112
sceneName: config.unitySceneName
105113
)
106-
return refreshser.refresh()
114+
return unityProject.create()
107115
}
108116

109-
func copyUnityFiles() -> Result {
117+
func copyUnityFiles(refresh: Bool) -> Result {
110118
let fileCopier = FileCopier(
111119
config: config,
112120
workingPath: workingUnityFolderPath,
113121
xcodeProjectPath: workingiOSFolderPath
114122
)
115-
return fileCopier.copyFiles()
123+
return fileCopier.copyFiles(refresh: refresh)
124+
}
125+
126+
func refreshProjects() -> Result {
127+
let refresher = ProjectRefresher(config: config, workingPath: workingUnityFolderPath)
128+
return refresher.refresh()
129+
}
130+
}
131+
132+
// MARK: - Refresh
133+
private extension UBKitManager {
134+
135+
func refreshXcodeProject() -> Result {
136+
let refreshResult = refreshProjects()
137+
guard refreshResult == .success else {
138+
return refreshResult
139+
}
140+
141+
return .success
116142
}
117143
}

0 commit comments

Comments
 (0)