-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPluginInstaller.swift
More file actions
157 lines (118 loc) · 4.77 KB
/
PluginInstaller.swift
File metadata and controls
157 lines (118 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
97
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
//
// PluginInstaller.swift
// XcodeHelper
//
// Created by dhcdht on 2016/10/27.
// Copyright © 2016年 XH. All rights reserved.
//
import Cocoa
/// Xcode 插件安装器
class PluginInstaller: PackageInstaller {
override class var sharedInstance: PluginInstaller {
get {
struct Single {
static let instance = PluginInstaller()
}
return Single.instance
}
}
// MARK: - Override
override func downloadRelativePath() -> String {
return "Plug-ins"
}
override func pathForInstalledPackage(package: Package) -> String? {
var installName: String = ""
if let name = self.pbxprojInstallNameForPlugin(package: package) {
installName = name
} else {
if let name = package.name, let extensionName = package.extensionName {
installName = name.appending(extensionName)
} else {
// TODO:
}
}
let extensionName = installName.pathExtension
var pluginsInstallPath = self.pathForPluginsWithExtension(extensionName: extensionName)
pluginsInstallPath.appendPathComponent(installName)
return pluginsInstallPath
}
override func installPackage(package: Package, completion: @escaping InstallOrUpdateCompletionBlock) {
if let plugin = package as? Plugin {
self.buildPlugin(plugin: plugin, completion: completion)
} else {
// TODO: error
}
}
override func updatePackage(package: Package, completion: @escaping DownloadOrUpdateCompletionBlock) {
Git.updateRepository(localPath: self.pathForDownloadedPackage(package: package), revision: package.revision, completion: completion)
}
override func downloadPackage(package: Package, completion: @escaping DownloadOrUpdateCompletionBlock) {
if FileManager.default.fileExists(atPath: self.pathForDownloadedPackage(package: package)) {
try? FileManager.default.removeItem(atPath: self.pathForDownloadedPackage(package: package))
}
if let remotePath = package.remotePath {
Git.cloneRepository(remotePath: remotePath, localPath: self.pathForDownloadedPackage(package: package), completion: completion)
} else {
// TODO: error
}
}
// MARK: - Private
private func pbxprojInstallNameForPlugin(package: Package) -> String? {
var pbxprojPath = self.findXcodeprojPathForPackage(package: package)
pbxprojPath?.appendPathComponent("project.pbxproj")
let ret = PbxprojParser.xcpluginNameFromPbxproj(path: pbxprojPath)
return ret
}
private func pathForPluginsWithExtension(extensionName: String) -> String {
var lastPath = ""
switch extensionName {
case "ideplugin":
lastPath = "Library/Developer/Xcode/Plug-ins"
default:
lastPath = "Library/Application Support/Developer/Shared/Xcode/Plug-ins"
}
var ret = NSHomeDirectory()
ret.appendPathComponent(lastPath)
return ret
}
private func findXcodeprojPathForPackage(package: Package) -> String? {
var clonedDirectory = self.pathForDownloadedPackage(package: package)
guard let name = package.name else {
return nil
}
var xcodeProjFileName = name
xcodeProjFileName.appendPathExtension("xcodeproj")
var allXcodeProjFilename = [String]()
guard let enumerator = FileManager.default.enumerator(atPath: clonedDirectory) else {
return nil
}
while let directoryEntry = enumerator.nextObject() as? String {
guard let fileName = directoryEntry.pathComponents.last else {
// TODO:
return nil
}
if fileName == xcodeProjFileName {
clonedDirectory.appendPathComponent(directoryEntry)
return clonedDirectory
} else if fileName.pathExtension == "xcodeproj" {
allXcodeProjFilename.append(directoryEntry)
}
}
if allXcodeProjFilename.count == 1 {
clonedDirectory.appendPathComponent(allXcodeProjFilename[0])
return clonedDirectory
}
return nil
}
private func buildPlugin(plugin: Plugin, completion: @escaping InstallOrUpdateCompletionBlock) -> Void {
guard let xcodeProjPath = self.findXcodeprojPathForPackage(package: plugin) else {
// TODO: error
completion(nil)
return
}
let shell = Shell()
shell.executeCommand(command: "/usr/bin/xcodebuild", arguments: ["clean", "build", "-project", xcodeProjPath], completion: { (output, error) -> Void in
completion(error)
})
}
}