-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix spm licenses gen #150
Merged
Merged
Fix spm licenses gen #150
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
45b3ffc
Add xcode project for testing.
yosshi4486 5ede04d
Add packages to test project.
yosshi4486 78785a7
Add failed test.
yosshi4486 deb8421
Test projectPath computed variable.
yosshi4486 67ea3d2
Add document codes.
yosshi4486 8e4e16a
Test package.resolved assertions.
yosshi4486 d3db461
Add SwiftPackageFileReader and its tests.
yosshi4486 cf1cf90
Refactor process method of LicensePlist type.
yosshi4486 11b04fb
Refactor test utils.
yosshi4486 f161ff8
Add xcworkspace file by executing "pod install".
yosshi4486 638f562
Test non updated Package.resolved
yosshi4486 f423594
Refactor test things.
yosshi4486 ef39090
Fix spm gen problem.
yosshi4486 f3ce46b
Add workaround document.
yosshi4486 d0bb647
Update README.
yosshi4486 f7f12fa
Exclude test projects directory from Package.swift
yosshi4486 90c2158
Ignore xcodeprojects from lint.
yosshi4486 7a55694
Adopt lint suggestions.
yosshi4486 0d3c023
Refactor long assertion text to resource file.
yosshi4486 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ Pods | |
!Pods*.swift | ||
test_result_dir | ||
/.history | ||
Tests/LicensePlistTests/XcodeProjects/**/xcuserdata |
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
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
22 changes: 22 additions & 0 deletions
22
Sources/LicensePlistCore/Entity/FileReader/FileReader.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,22 @@ | ||
// | ||
// FileReader.swift | ||
// LicensePlistCore | ||
// | ||
// Created by yosshi4486 on 2021/04/06. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An object that reads any file from the given path. | ||
protocol FileReader { | ||
|
||
/// The result parameter type of reading a file. | ||
associatedtype ResultType | ||
|
||
/// The path which an interested file located. | ||
var path: URL { get } | ||
|
||
/// Returns a concrete result by reading a file which the given path specifies. | ||
func read() throws -> ResultType | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
Sources/LicensePlistCore/Entity/FileReader/SwiftPackageFileReader.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,37 @@ | ||
// | ||
// SwiftPackageFileReader.swift | ||
// LicensePlistCore | ||
// | ||
// Created by yosshi4486 on 2021/04/06. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An object that reads a Package.swift or Package.resolved file. | ||
struct SwiftPackageFileReader: FileReader { | ||
|
||
struct FileReaderError: Swift.Error { | ||
let path: URL | ||
|
||
var localizedDescription: String? { | ||
return "Invalide Package.swift name: \(path.lastPathComponent)" | ||
} | ||
|
||
} | ||
|
||
typealias ResultType = String? | ||
|
||
let path: URL | ||
|
||
func read() throws -> String? { | ||
if path.lastPathComponent != Consts.packageName && path.lastPathComponent != "Package.resolved" { | ||
throw FileReaderError(path: path) | ||
} | ||
|
||
if let content = path.deletingPathExtension().appendingPathExtension("resolved").lp.read() { | ||
return content | ||
} | ||
return path.lp.read() | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
Sources/LicensePlistCore/Entity/FileReader/XcodeProjectFileReader.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,84 @@ | ||
// | ||
// XcodeProjectFileReader.swift | ||
// LicensePlistCore | ||
// | ||
// Created by yosshi4486 on 2021/04/06. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An object that reads a xcodeproj file. | ||
struct XcodeProjectFileReader: FileReader { | ||
|
||
typealias ResultType = String? | ||
|
||
let path: URL | ||
|
||
/// The path which specifies "xcodeproj" file. | ||
var projectPath: URL? { | ||
if path.lastPathComponent.contains("*") { | ||
// find first "xcodeproj" in directory | ||
return path.deletingLastPathComponent().lp.listDir().first { $0.pathExtension == Consts.xcodeprojExtension } | ||
} else { | ||
// use the specified path | ||
return path | ||
} | ||
} | ||
|
||
func read() throws -> String? { | ||
guard let validatedPath = projectPath else { return nil } | ||
|
||
if validatedPath.pathExtension != Consts.xcodeprojExtension { | ||
return nil | ||
} | ||
|
||
let xcodeprojPackageResolvedPath = validatedPath | ||
.appendingPathComponent("project.xcworkspace") | ||
.appendingPathComponent("xcshareddata") | ||
.appendingPathComponent("swiftpm") | ||
.appendingPathComponent("Package.resolved") | ||
|
||
let xcworkspacePackageResolvedPath = validatedPath | ||
.deletingPathExtension() | ||
.appendingPathExtension("xcworkspace") | ||
.appendingPathComponent("xcshareddata") | ||
.appendingPathComponent("swiftpm") | ||
.appendingPathComponent("Package.resolved") | ||
|
||
let defaultPath = xcworkspacePackageResolvedPath | ||
|
||
/* | ||
Xcode only update one Package.resolved that associated with workspace you work in. so, the files may be inconsistent at any time. | ||
This implementation compare modificationDate and use new one to avoid referring old Package.resolved. | ||
*/ | ||
switch (xcodeprojPackageResolvedPath.lp.isExists, xcworkspacePackageResolvedPath.lp.isExists) { | ||
case (true, true): | ||
guard | ||
let xcodeprojPackageResolvedModifiedDate = try xcodeprojPackageResolvedPath | ||
.resourceValues(forKeys: [.attributeModificationDateKey]) | ||
.attributeModificationDate, | ||
let xcworkspacePackageResolveModifiedDate = try xcworkspacePackageResolvedPath | ||
.resourceValues(forKeys: [.attributeModificationDateKey]) | ||
.attributeModificationDate | ||
else { | ||
return try SwiftPackageFileReader(path: defaultPath).read() | ||
} | ||
|
||
if xcworkspacePackageResolveModifiedDate >= xcodeprojPackageResolvedModifiedDate { | ||
return try SwiftPackageFileReader(path: xcworkspacePackageResolvedPath).read() | ||
} else { | ||
return try SwiftPackageFileReader(path: xcodeprojPackageResolvedPath).read() | ||
} | ||
|
||
case (true, false): | ||
return try SwiftPackageFileReader(path: xcodeprojPackageResolvedPath).read() | ||
|
||
case (false, true): | ||
return try SwiftPackageFileReader(path: xcworkspacePackageResolvedPath).read() | ||
|
||
case (false, false): | ||
return try SwiftPackageFileReader(path: defaultPath).read() | ||
} | ||
} | ||
|
||
} |
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
133 changes: 133 additions & 0 deletions
133
Tests/LicensePlistTests/Entity/FileReader/SwiftPackageFileReaderTests.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,133 @@ | ||
// | ||
// SwiftPackageFileReaderTests.swift | ||
// LicensePlistTests | ||
// | ||
// Created by yosshi4486 on 2021/04/06. | ||
// | ||
|
||
import XCTest | ||
@testable import LicensePlistCore | ||
|
||
class SwiftPackageFileReaderTests: XCTestCase { | ||
|
||
var fileURL: URL! | ||
|
||
var packageResolvedText: String { | ||
return #""" | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "APIKit", | ||
"repositoryURL": "https://github.com/ishkawa/APIKit.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "86d51ecee0bc0ebdb53fb69b11a24169a69097ba", | ||
"version": "4.1.0" | ||
} | ||
}, | ||
{ | ||
"package": "HeliumLogger", | ||
"repositoryURL": "https://github.com/IBM-Swift/HeliumLogger.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "146a36c2a91270e4213fa7d7e8192cd2e55d0ace", | ||
"version": "1.9.0" | ||
} | ||
}, | ||
{ | ||
"package": "LoggerAPI", | ||
"repositoryURL": "https://github.com/IBM-Swift/LoggerAPI.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "3357dd9526cdf9436fa63bb792b669e6efdc43da", | ||
"version": "1.9.0" | ||
} | ||
}, | ||
{ | ||
"package": "Result", | ||
"repositoryURL": "https://github.com/antitypical/Result.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "2ca499ba456795616fbc471561ff1d963e6ae160", | ||
"version": "4.1.0" | ||
} | ||
}, | ||
{ | ||
"package": "swift-argument-parser", | ||
"repositoryURL": "https://github.com/apple/swift-argument-parser.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "92646c0cdbaca076c8d3d0207891785b3379cbff", | ||
"version": "0.3.1" | ||
} | ||
}, | ||
{ | ||
"package": "HTMLEntities", | ||
"repositoryURL": "https://github.com/IBM-Swift/swift-html-entities.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "744c094976355aa96ca61b9b60ef0a38e979feb7", | ||
"version": "3.0.14" | ||
} | ||
}, | ||
{ | ||
"package": "swift-log", | ||
"repositoryURL": "https://github.com/apple/swift-log.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "eba9b323b5ba542c119ff17382a4ce737bcdc0b8", | ||
"version": "0.0.0" | ||
} | ||
}, | ||
{ | ||
"package": "Yaml", | ||
"repositoryURL": "https://github.com/behrang/YamlSwift.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "287f5cab7da0d92eb947b5fd8151b203ae04a9a3", | ||
"version": "3.4.4" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} | ||
"""# | ||
} | ||
|
||
override func setUpWithError() throws { | ||
fileURL = URL(fileURLWithPath: "\(TestUtil.testProjectsPath)/SwiftPackageManagerTestProject/SwiftPackageManagerTestProject.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved") | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
fileURL = nil | ||
} | ||
|
||
func testInvalidPath() throws { | ||
let invalidFilePath = fileURL.deletingLastPathComponent().appendingPathComponent("Podfile.lock") | ||
let reader = SwiftPackageFileReader(path: invalidFilePath) | ||
XCTAssertThrowsError(try reader.read()) | ||
} | ||
|
||
func testPackageSwift() throws { | ||
// Path for this package's Package.swift. | ||
let packageSwiftPath = TestUtil.sourceDir.appendingPathComponent("Package.swift").lp.fileURL | ||
let reader = SwiftPackageFileReader(path: packageSwiftPath) | ||
XCTAssertEqual( | ||
try reader.read()?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
packageResolvedText.trimmingCharacters(in: .whitespacesAndNewlines) | ||
) | ||
} | ||
|
||
func testPackageResolved() throws { | ||
// Path for this package's Package.resolved. | ||
let packageResolvedPath = TestUtil.sourceDir.appendingPathComponent("Package.resolved").lp.fileURL | ||
let reader = SwiftPackageFileReader(path: packageResolvedPath) | ||
XCTAssertEqual( | ||
try reader.read()?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
packageResolvedText.trimmingCharacters(in: .whitespacesAndNewlines) | ||
) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could exclude the test xcode-projects directory by specifying it in Package.swift.