@@ -14,83 +14,83 @@ import SourceControl
14
14
import TSCBasic
15
15
import TSCUtility
16
16
17
- /// A downloaded artifact managed by the workspace.
18
- public struct ManagedArtifact {
19
- /// The package reference.
20
- public let packageRef : PackageReference
21
-
22
- /// The name of the binary target the artifact corresponds to.
23
- public let targetName : String
24
-
25
- /// The source of the artifact (local or remote).
26
- public let source : Source
27
-
28
- /// The path of the artifact on disk
29
- public let path : AbsolutePath
30
-
31
- public init (
32
- packageRef: PackageReference ,
33
- targetName: String ,
34
- source: Source ,
35
- path: AbsolutePath
36
- ) {
37
- self . packageRef = packageRef
38
- self . targetName = targetName
39
- self . source = source
40
- self . path = path
41
- }
17
+ extension Workspace {
18
+ /// A downloaded artifact managed by the workspace.
19
+ public struct ManagedArtifact {
20
+ /// The package reference.
21
+ public let packageRef : PackageReference
22
+
23
+ /// The name of the binary target the artifact corresponds to.
24
+ public let targetName : String
25
+
26
+ /// The source of the artifact (local or remote).
27
+ public let source : Source
28
+
29
+ /// The path of the artifact on disk
30
+ public let path : AbsolutePath
31
+
32
+ public init (
33
+ packageRef: PackageReference ,
34
+ targetName: String ,
35
+ source: Source ,
36
+ path: AbsolutePath
37
+ ) {
38
+ self . packageRef = packageRef
39
+ self . targetName = targetName
40
+ self . source = source
41
+ self . path = path
42
+ }
42
43
43
- /// Create an artifact downloaded from a remote url.
44
- public static func remote(
45
- packageRef: PackageReference ,
46
- targetName: String ,
47
- url: String ,
48
- checksum: String ,
49
- path: AbsolutePath
50
- ) -> ManagedArtifact {
51
- return ManagedArtifact (
52
- packageRef: packageRef,
53
- targetName: targetName,
54
- source: . remote( url: url, checksum: checksum) ,
55
- path: path
56
- )
57
- }
44
+ /// Create an artifact downloaded from a remote url.
45
+ public static func remote(
46
+ packageRef: PackageReference ,
47
+ targetName: String ,
48
+ url: String ,
49
+ checksum: String ,
50
+ path: AbsolutePath
51
+ ) -> ManagedArtifact {
52
+ return ManagedArtifact (
53
+ packageRef: packageRef,
54
+ targetName: targetName,
55
+ source: . remote( url: url, checksum: checksum) ,
56
+ path: path
57
+ )
58
+ }
58
59
59
- /// Create an artifact present locally on the filesystem.
60
- public static func local(
61
- packageRef: PackageReference ,
62
- targetName: String ,
63
- path: AbsolutePath
64
- ) -> ManagedArtifact {
65
- return ManagedArtifact (
66
- packageRef: packageRef,
67
- targetName: targetName,
68
- source: . local,
69
- path: path
70
- )
71
- }
60
+ /// Create an artifact present locally on the filesystem.
61
+ public static func local(
62
+ packageRef: PackageReference ,
63
+ targetName: String ,
64
+ path: AbsolutePath
65
+ ) -> ManagedArtifact {
66
+ return ManagedArtifact (
67
+ packageRef: packageRef,
68
+ targetName: targetName,
69
+ source: . local,
70
+ path: path
71
+ )
72
+ }
72
73
73
- /// Represents the source of the artifact.
74
- public enum Source : Equatable {
74
+ /// Represents the source of the artifact.
75
+ public enum Source : Equatable {
75
76
76
- /// Represents a remote artifact, with the url it was downloaded from, its checksum, and its path relative to
77
- /// the workspace artifacts path.
78
- case remote( url: String , checksum: String )
77
+ /// Represents a remote artifact, with the url it was downloaded from, its checksum, and its path relative to
78
+ /// the workspace artifacts path.
79
+ case remote( url: String , checksum: String )
79
80
80
- /// Represents a locally available artifact, with its path relative to its package.
81
- case local
81
+ /// Represents a locally available artifact, with its path relative to its package.
82
+ case local
83
+ }
82
84
}
83
85
}
84
86
85
- // MARK: - CustomStringConvertible
86
-
87
- extension ManagedArtifact : CustomStringConvertible {
87
+ extension Workspace . ManagedArtifact : CustomStringConvertible {
88
88
public var description : String {
89
89
return " <ManagedArtifact: \( self . packageRef. name) . \( self . targetName) \( self . source) \( self . path) > "
90
90
}
91
91
}
92
92
93
- extension ManagedArtifact . Source : CustomStringConvertible {
93
+ extension Workspace . ManagedArtifact . Source : CustomStringConvertible {
94
94
public var description : String {
95
95
switch self {
96
96
case . local:
@@ -101,63 +101,59 @@ extension ManagedArtifact.Source: CustomStringConvertible {
101
101
}
102
102
}
103
103
104
- // MARK: -
105
-
106
- /// A collection of managed artifacts which have been downloaded.
107
- public final class ManagedArtifacts {
104
+ // MARK: - ManagedArtifacts
108
105
109
- /// A mapping from package url, to target name, to ManagedArtifact.
110
- private var artifactMap : [ String : [ String : ManagedArtifact ] ]
111
-
112
- internal var artifacts : AnyCollection < ManagedArtifact > {
113
- AnyCollection ( artifactMap. values. lazy. flatMap ( { $0. values } ) )
114
- }
106
+ extension Workspace {
107
+ /// A collection of managed artifacts which have been downloaded.
108
+ public final class ManagedArtifacts {
109
+ /// A mapping from package identity, to target name, to ManagedArtifact.
110
+ private var artifactMap : [ PackageIdentity : [ String : ManagedArtifact ] ]
115
111
116
- init ( artifactMap : [ String : [ String : ManagedArtifact ] ] = [ : ] ) {
117
- self . artifactMap = artifactMap
118
- }
112
+ internal var artifacts : AnyCollection < ManagedArtifact > {
113
+ AnyCollection ( self . artifactMap. values . lazy . flatMap { $0 . values } )
114
+ }
119
115
120
- public subscript( packageURL packageURL: String , targetName targetName: String ) -> ManagedArtifact ? {
121
- artifactMap [ packageURL] ? [ targetName]
122
- }
116
+ init ( _ artifacts: [ ManagedArtifact ] = [ ] ) {
117
+ let artifactsByPackagePath = Dictionary ( grouping: artifacts, by: { $0. packageRef. identity } )
118
+ self . artifactMap = artifactsByPackagePath. mapValues { artifacts in
119
+ Dictionary ( uniqueKeysWithValues: artifacts. map { ( $0. targetName, $0) } )
120
+ }
121
+ }
123
122
124
- public subscript( packageName packageName : String , targetName targetName: String ) -> ManagedArtifact ? {
125
- artifacts . first ( where : { $0 . packageRef . name == packageName && $0 . targetName == targetName } )
126
- }
123
+ public subscript( packageIdentity packageIdentity : PackageIdentity , targetName targetName: String ) -> ManagedArtifact ? {
124
+ self . artifactMap [ packageIdentity ] ? [ targetName]
125
+ }
127
126
128
- public func add( _ artifact: ManagedArtifact ) {
129
- artifactMap [ artifact. packageRef. location , default: [ : ] ] [ artifact. targetName] = artifact
130
- }
127
+ public func add( _ artifact: ManagedArtifact ) {
128
+ self . artifactMap [ artifact. packageRef. identity , default: [ : ] ] [ artifact. targetName] = artifact
129
+ }
131
130
132
- public func remove( packageURL: String , targetName: String ) {
133
- artifactMap [ packageURL] ? [ targetName] = nil
131
+ public func remove( packageIdentity: PackageIdentity , targetName: String ) {
132
+ self . artifactMap [ packageIdentity] ? [ targetName] = nil
133
+ }
134
134
}
135
135
}
136
136
137
- // MARK: - Collection
138
-
139
- extension ManagedArtifacts : Collection {
137
+ extension Workspace . ManagedArtifacts : Collection {
140
138
public var startIndex : AnyIndex {
141
- artifacts. startIndex
139
+ self . artifacts. startIndex
142
140
}
143
141
144
142
public var endIndex : AnyIndex {
145
- artifacts. endIndex
143
+ self . artifacts. endIndex
146
144
}
147
145
148
- public subscript( index: AnyIndex ) -> ManagedArtifact {
149
- artifacts [ index]
146
+ public subscript( index: AnyIndex ) -> Workspace . ManagedArtifact {
147
+ self . artifacts [ index]
150
148
}
151
149
152
150
public func index( after index: AnyIndex ) -> AnyIndex {
153
- artifacts. index ( after: index)
151
+ self . artifacts. index ( after: index)
154
152
}
155
153
}
156
154
157
- // MARK: - CustomStringConvertible
158
-
159
- extension ManagedArtifacts : CustomStringConvertible {
155
+ extension Workspace . ManagedArtifacts : CustomStringConvertible {
160
156
public var description : String {
161
- " <ManagedArtifacts: \( Array ( artifacts) ) > "
157
+ " <ManagedArtifacts: \( Array ( self . artifacts) ) > "
162
158
}
163
159
}
0 commit comments