5
5
import Foundation
6
6
7
7
public final class Carting {
8
-
8
+
9
9
enum Keys {
10
- static let defaultScriptName = " Carthage "
11
10
static let carthageScript = " \" /usr/local/bin/carthage copy-frameworks \" "
12
11
}
13
-
12
+
14
13
private let arguments : [ String ]
15
-
14
+
16
15
private let projectService = ProjectService ( )
17
-
16
+ private let frameworkInformationService = FrameworkInformationService ( )
17
+
18
18
public init ( arguments: [ String ] = CommandLine . arguments) {
19
19
self . arguments = arguments
20
20
}
21
-
21
+
22
22
public func run( ) throws {
23
+ guard let arguments = Arguments ( arguments: self . arguments) else {
24
+ print ( " ❌ Wrong arguments " )
25
+ print ( Arguments . description)
26
+ return
27
+ }
28
+
29
+ switch arguments. command {
30
+ case . help:
31
+ print ( Arguments . description)
32
+ case let . script( name: name) :
33
+ try updateScript ( withName: name)
34
+ case . list:
35
+ frameworkInformationService. printFrameworksInformation ( )
36
+ }
37
+ }
38
+
39
+ private func updateScript( withName scriptName: String ) throws {
23
40
let project = try projectService. project ( )
24
-
25
- let carthageScriptName = arguments. count > 1 ? arguments [ 1 ] : Keys . defaultScriptName
26
-
41
+
27
42
var projectHasBeenUpdated = false
28
-
43
+
29
44
try project. targets. forEach { target in
30
45
let frameworkBuildPhase = target. body. buildPhases. first { $0. name == " Frameworks " }
31
46
let frameworkScript = project. frameworkScripts. first { $0. identifier == frameworkBuildPhase? . identifier }
32
47
guard let script = frameworkScript else {
33
48
return
34
49
}
35
- let carthageFrameworkNames = try projectService. frameworkNames ( )
36
- let linkedCarthageFrameworkNames = script. body. files
37
- . filter { carthageFrameworkNames. contains ( $0. name) }
50
+ let linkedCarthageDynamicFrameworkNames = try frameworkInformationService. frameworksInformation ( )
51
+ . filter { information in
52
+ information. linking == . dynamic && script. body. files. contains { $0. name == information. name }
53
+ }
38
54
. map { $0. name }
39
-
40
- let carthageBuildPhase = target. body. buildPhases. first { $0. name == carthageScriptName }
55
+
56
+ let carthageBuildPhase = target. body. buildPhases. first { $0. name == scriptName }
41
57
let carthageScript = project. scripts. first { $0. identifier == carthageBuildPhase? . identifier }
42
-
43
- let inputPathsString = projectService. pathsString ( forFrameworkNames: linkedCarthageFrameworkNames ,
58
+
59
+ let inputPathsString = projectService. pathsString ( forFrameworkNames: linkedCarthageDynamicFrameworkNames ,
44
60
type: . input)
45
- let outputPathsString = projectService. pathsString ( forFrameworkNames: linkedCarthageFrameworkNames ,
61
+ let outputPathsString = projectService. pathsString ( forFrameworkNames: linkedCarthageDynamicFrameworkNames ,
46
62
type: . output)
47
-
63
+
48
64
if let carthage = carthageScript {
49
65
var scriptHasBeenUpdated = false
50
66
if carthage. body. inputPaths != inputPathsString {
51
- carthage. body. inputPaths = inputPathsString
67
+ carthage. body. inputPaths = inputPathsString
52
68
scriptHasBeenUpdated = true
53
69
}
54
70
if carthage. body. outputPaths != outputPathsString {
@@ -61,25 +77,25 @@ public final class Carting {
61
77
}
62
78
if scriptHasBeenUpdated {
63
79
projectHasBeenUpdated = true
64
- print ( " ✅ Script \" \( carthageScriptName ) \" in target \" \( target. name) \" was successfully updated. " )
80
+ print ( " ✅ Script \" \( scriptName ) \" in target \" \( target. name) \" was successfully updated. " )
65
81
}
66
82
}
67
- else if linkedCarthageFrameworkNames . count > 0 {
83
+ else if linkedCarthageDynamicFrameworkNames . count > 0 {
68
84
let body = ScriptBody ( inputPaths: inputPathsString,
69
- name: carthageScriptName ,
85
+ name: scriptName ,
70
86
outputPaths: outputPathsString,
71
87
shellScript: Keys . carthageScript)
72
-
88
+
73
89
let identifier = String . randomAlphaNumericString ( length: 24 )
74
- let script = Script ( identifier: identifier, name: carthageScriptName , body: body)
75
- let buildPhase = BuildPhase ( identifier: identifier, name: carthageScriptName )
90
+ let script = Script ( identifier: identifier, name: scriptName , body: body)
91
+ let buildPhase = BuildPhase ( identifier: identifier, name: scriptName )
76
92
project. scripts. append ( script)
77
93
target. body. buildPhases. append ( buildPhase)
78
- print ( " ✅ Script \( carthageScriptName ) was successfully added to \( target. name) target. " )
94
+ print ( " ✅ Script \( scriptName ) was successfully added to \( target. name) target. " )
79
95
projectHasBeenUpdated = true
80
96
}
81
97
}
82
-
98
+
83
99
try projectService. update ( project)
84
100
if !projectHasBeenUpdated {
85
101
print ( " 🤷♂️ Nothing to update. " )
@@ -92,7 +108,7 @@ enum MainError: Swift.Error {
92
108
}
93
109
94
110
extension MainError : LocalizedError {
95
-
111
+
96
112
var errorDescription : String ? {
97
113
switch self {
98
114
case . noScript( name: let name) : return " Can't find script with name \( name) "
0 commit comments