-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JSON project description SPM compatibility
Swift Package Manager uses `target_dependencies` instead of `dependencies` to list module dependencies. This change also improves the robustness when decoding JSON project descriptions for targets that lack explicit sources or dependencies.
- Loading branch information
1 parent
6a01bb7
commit 201a6bb
Showing
6 changed files
with
415 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Sources/MockingbirdGenerator/Utilities/KeyedDecodingContainer+Array.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// KeyedDecodingContainer+Array.swift | ||
// MockingbirdGenerator | ||
// | ||
// Created by typealias on 7/22/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension KeyedDecodingContainer { | ||
/// Decodes a value of the given array type for the given key, if present. | ||
/// | ||
/// This method returns `nil` if the container does not have a value | ||
/// associated with `key`, or if the value is null. The difference between | ||
/// these states can be distinguished with a `contains(_:)` call. | ||
/// | ||
/// - parameter type: The type of value to decode. | ||
/// - parameter key: The key that the decoded value is associated with. | ||
/// - returns: A decoded value of the requested type, or `nil` if the | ||
/// `Decoder` does not have an entry associated with the given key, or if | ||
/// the value is a null value. | ||
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value | ||
/// is not convertible to the requested type. | ||
func decodeIfPresent<T: Decodable>( | ||
_ type: [T].Type, | ||
forKey key: KeyedDecodingContainer<K>.Key | ||
) throws -> [T]? { | ||
guard contains(key) else { | ||
return nil | ||
} | ||
|
||
do { | ||
var container = try nestedUnkeyedContainer(forKey: key) | ||
var buffer = [T]() | ||
buffer.reserveCapacity(container.count ?? 0) | ||
|
||
while !container.isAtEnd { | ||
buffer.append(try container.decode(T.self)) | ||
} | ||
return buffer | ||
} catch DecodingError.valueNotFound { | ||
return nil | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Tests/MockingbirdTests/Generator/Resources/generic-project-description.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"targets":[ | ||
{ | ||
"c99name":"FeatureTargetTests", | ||
"module_type":"SwiftTarget", | ||
"name":"FeatureTargetTests", | ||
"path":"Tests/FeatureTargetTests", | ||
"sources":[ | ||
"ModelTests.swift", | ||
"ControllerTests.swift", | ||
"ViewTests.swift" | ||
], | ||
"dependencies":[ | ||
"FeatureTarget" | ||
], | ||
"type":"test" | ||
}, | ||
{ | ||
"c99name":"FeatureTarget", | ||
"module_type":"SwiftTarget", | ||
"name":"FeatureTarget", | ||
"path":"Sources/FeatureTarget", | ||
"sources":[ | ||
"Models/Things.swift", | ||
"Controllers/MasterViewController.swift", | ||
"Controllers/DetailViewController.swift", | ||
"Views/DetailView.swift" | ||
], | ||
"dependencies":[], | ||
"type":"library" | ||
}, | ||
{ | ||
"c99name":"EmptyTarget", | ||
"module_type":"SwiftTarget", | ||
"name":"EmptyTarget", | ||
"path":"Sources/EmptyTarget", | ||
"type":"library" | ||
} | ||
] | ||
} |
96 changes: 96 additions & 0 deletions
96
Tests/MockingbirdTests/Generator/Resources/spm-project-description.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
{ | ||
"dependencies":[ | ||
{ | ||
"requirement":{ | ||
"local_package":null | ||
}, | ||
"url":"../path/to/local/package" | ||
}, | ||
{ | ||
"name":"Mockingbird", | ||
"requirement":{ | ||
"range":[ | ||
{ | ||
"lower_bound":"0.16.0", | ||
"upper_bound":"0.17.0" | ||
} | ||
] | ||
}, | ||
"url":"https://github.com/birdrides/mockingbird.git" | ||
} | ||
], | ||
"name":"FeaturePackage", | ||
"path":"/path/to/FeaturePackage", | ||
"platforms":[ | ||
{ | ||
"name":"ios", | ||
"version":"12.0" | ||
} | ||
], | ||
"products":[ | ||
{ | ||
"name":"Feature", | ||
"targets":[ | ||
"FeatureTarget" | ||
], | ||
"type":{ | ||
"library":[ | ||
"automatic" | ||
] | ||
} | ||
} | ||
], | ||
"targets":[ | ||
{ | ||
"c99name":"FeatureTargetTests", | ||
"module_type":"SwiftTarget", | ||
"name":"FeatureTargetTests", | ||
"path":"Tests/FeatureTargetTests", | ||
"sources":[ | ||
"ModelTests.swift", | ||
"ControllerTests.swift", | ||
"ViewTests.swift" | ||
], | ||
"target_dependencies":[ | ||
"FeatureTarget" | ||
], | ||
"type":"test" | ||
}, | ||
{ | ||
"c99name":"FeatureTarget", | ||
"module_type":"SwiftTarget", | ||
"name":"FeatureTarget", | ||
"path":"Sources/FeatureTarget", | ||
"product_memberships":[ | ||
"Feature" | ||
], | ||
"resources":[ | ||
{ | ||
"path":"/path/to/FeatureTarget.storyboard", | ||
"rule":"process" | ||
} | ||
], | ||
"sources":[ | ||
"Models/Things.swift", | ||
"Controllers/MasterViewController.swift", | ||
"Controllers/DetailViewController.swift", | ||
"Views/DetailView.swift" | ||
], | ||
"target_dependencies":[ | ||
|
||
], | ||
"type":"library" | ||
}, | ||
{ | ||
"c99name":"EmptyTarget", | ||
"module_type":"SwiftTarget", | ||
"name":"EmptyTarget", | ||
"path":"Sources/EmptyTarget", | ||
"product_memberships":[ | ||
"Feature" | ||
], | ||
"type":"library" | ||
} | ||
], | ||
"tools_version":"5.3" | ||
} |
Oops, something went wrong.