Skip to content

Commit

Permalink
Merge pull request #3 from emans/feature/support_commit
Browse files Browse the repository at this point in the history
Support Revision and Branch attributes
  • Loading branch information
albertodebortoli authored Oct 23, 2023
2 parents 4576c47 + 6c91a49 commit b4a433d
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 13 deletions.
10 changes: 10 additions & 0 deletions Example/Config/RemoteDependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"name": "RemoteDependencyB",
"url": "https://github.com/DependencyB",
"version": "2.0.0"
},
{
"name": "RemoteDependencyC",
"url": "https://github.com/DependencyC",
"revision": "abcde1235kjh"
},
{
"name": "RemoteDependencyD",
"url": "https://github.com/DependencyC",
"branch": "master"
}
]
}
3 changes: 3 additions & 0 deletions Example/Packages/Example/Example.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
},
{
"name": "RemoteDependencyB"
},
{
"name": "RemoteDependencyC"
}
],
"targets": [
Expand Down
4 changes: 4 additions & 0 deletions Example/Packages/Example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ let package = Package(
url: "https://github.com/DependencyB",
exact: "2.0.0"
),
.package(
url: "https://github.com/DependencyC",
revision: "abcde1235kjh"
),
],
targets: [
.target(
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ The command `generate-package` requires the following arguments:
"name": "ViewInspector",
"url": "https://github.com/nalexn/ViewInspector",
"version": "0.9.2"
},
{
"name": "ViewInspector",
"url": "https://github.com/nalexn/ViewInspector",
"version": "0.9.2"
},
{
"name": "SnapshotTesting",
"url": "https://github.com/pointfreeco/swift-snapshot-testing",
"branch": "master"
},
{
"name": "Fastlane",
"url": "https://github.com/fastlane/fastlane.git",
"revision": "2c4f29fe161c5998e30000f96d23384fd0eebe90"
}
]
}
Expand Down Expand Up @@ -60,6 +75,12 @@ Packages should be contained in respective folders inside a packages folder and
"name": "ViewInspector",
"version": "1.2.3"
},
{
"name": "Fastlane"
},
{
"name": "SnapshotTesting"
},
],
"targets": [
{
Expand All @@ -68,6 +89,9 @@ Packages should be contained in respective folders inside a packages folder and
"dependencies": [
{
"name": "Alamofire"
},
{
"name": "Fastlane"
}
],
"sourcesPath": "Framework/Sources",
Expand All @@ -83,6 +107,9 @@ Packages should be contained in respective folders inside a packages folder and
},
{
"name": "ViewInspector"
},
{
"name": "SnapshotTesting"
}
],
"sourcesPath": "Tests/Sources",
Expand All @@ -92,7 +119,7 @@ Packages should be contained in respective folders inside a packages folder and
}
```

> Note that `PackageGenerator` will automatically retrieve `url` & `version` values for `remoteDependencies` from the `RemoteDependencies.json` file. If you need to override those values, you can set them in the package spec.
> Note that `PackageGenerator` will automatically retrieve `url` & ( `version` || `branch` || `revision` ) values for `remoteDependencies` from the `RemoteDependencies.json` file. If you need to override those values, you can set them in the package spec.
We provide a default Stencil template that `PackageGenerator` can work with.

Expand Down
4 changes: 3 additions & 1 deletion Sources/Core/SpecGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ final class SpecGenerator {
guard let dependency = dependencies.first(where: { $0.name == remoteDependency.name }) else { return nil }
return RemoteDependency(name: dependency.name,
url: remoteDependency.url ?? dependency.url,
version: remoteDependency.version ?? dependency.version)
version: remoteDependency.version ?? dependency.version,
revision: remoteDependency.revision ?? dependency.revision,
branch: remoteDependency.branch ?? dependency.branch)
}

return Spec(name: partialSpec.name,
Expand Down
16 changes: 15 additions & 1 deletion Sources/Models/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,19 @@ struct Dependencies: Decodable {
struct Dependency: Decodable {
let name: String
let url: String
let version: String
let version: String?
let revision: String?
let branch: String?

init(name: String, url: String, version: String?, revision: String?, branch: String?) {
guard version != nil || revision != nil || branch != nil else {
fatalError("You need to provide at least one of the following: version, revision or branch")
}

self.name = name
self.url = url
self.version = version
self.revision = revision
self.branch = branch
}
}
16 changes: 14 additions & 2 deletions Sources/Models/Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,36 @@ struct RemoteDependency: Decodable {
let name: String
let url: String?
let version: String?

let revision: String?
let branch: String?

enum CodingKeys: CodingKey {
case name
case url
case version
case revision
case branch
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.url = try container.decodeIfPresent(String.self, forKey: .url)
self.version = try container.decodeIfPresent(String.self, forKey: .version)
self.revision = try container.decodeIfPresent(String.self, forKey: .revision)
self.branch = try container.decodeIfPresent(String.self, forKey: .branch)
}

init(name: String, url: String, version: String) {
init(name: String, url: String, version: String?, revision: String?, branch: String?) {
guard version != nil || revision != nil || branch != nil else {
fatalError("You need to provide at least one of the following: version, revision or branch")
}

self.name = name
self.url = url
self.version = version
self.revision = revision
self.branch = branch
}
}

Expand Down
6 changes: 6 additions & 0 deletions Templates/Package.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ let package = Package(
{% for remote_dependency in remote_dependencies %}
.package(
url: "{{ remote_dependency.url }}",
{% if remote_dependency.version %}
exact: "{{ remote_dependency.version }}"
{% elif remote_dependency.revision %}
revision: "{{ remote_dependency.revision }}"
{% elif remote_dependency.branch %}
branch: "{{ remote_dependency.branch }}"
{% endif %}
),
{% endfor %}
],
Expand Down
26 changes: 18 additions & 8 deletions Tests/PackageGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import XCTest
final class PackageGeneratorTests: XCTestCase {

enum PackageType: String {
case revisionProduct = "RevisionProduct"
case branchProduct = "BranchProduct"
case singleProduct = "SingleProduct"
case multipleProducts = "MultipleProducts"
case customPlatforms = "CustomPlatforms"
Expand All @@ -14,23 +16,31 @@ final class PackageGeneratorTests: XCTestCase {
case executableProduct = "ExecutableProduct"
case plugins = "PluginProduct"
}

let resourcesFolder = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent("Resources")

lazy var packagesFolderUrl = resourcesFolder.appendingPathComponent("Packages")
lazy var dependenciesUrl = resourcesFolder.appendingPathComponent("TestRemoteDependencies.json")
lazy var templatePath = resourcesFolder.appendingPathComponent("Package.stencil")

func test_SingleProduct() throws {
try assertPackage(for: .singleProduct)
}


func test_RevisionProduct() throws {
try assertPackage(for: .revisionProduct)
}

func test_BranchProduct() throws {
try assertPackage(for: .branchProduct)
}

func test_MultipleProducts() throws {
try assertPackage(for: .multipleProducts)
}

func test_customPlatforms() throws {
try assertPackage(for: .customPlatforms)
}
Expand All @@ -50,20 +60,20 @@ final class PackageGeneratorTests: XCTestCase {
func test_pluginProduct() throws {
try assertPackage(for: .plugins)
}

private func assertPackage(for packageType: PackageType) throws {
let packageSpecUrl = resourcesFolder
.appendingPathComponent("Packages")
.appendingPathComponent(packageType.rawValue)
.appendingPathComponent(packageType.rawValue)
.appendingPathExtension("json")

let packageUrl = resourcesFolder
.appendingPathComponent("Packages")
.appendingPathComponent(packageType.rawValue)
.appendingPathComponent("\(packageType.rawValue)Package")
.appendingPathExtension("swift")

let specGenerator = SpecGenerator(dependenciesUrl: dependenciesUrl, packagesFolder: packagesFolderUrl)
let spec = try specGenerator.makeSpec(for: packageType.rawValue, specUrl: packageSpecUrl)
let templater = Templater(templatePath: templatePath.absoluteString)
Expand Down
6 changes: 6 additions & 0 deletions Tests/Resources/Package.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ let package = Package(
{% for remote_dependency in remote_dependencies %}
.package(
url: "{{ remote_dependency.url }}",
{% if remote_dependency.version %}
exact: "{{ remote_dependency.version }}"
{% elif remote_dependency.revision %}
revision: "{{ remote_dependency.revision }}"
{% elif remote_dependency.branch %}
branch: "{{ remote_dependency.branch }}"
{% endif %}
),
{% endfor %}
],
Expand Down
66 changes: 66 additions & 0 deletions Tests/Resources/Packages/BranchProduct/BranchProduct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "BranchProduct",
"products": [
{
"name": "BranchProduct",
"productType": "library",
"targets": [
"TargetA"
]
}
],
"localDependencies": [
{
"name": "LocalDependencyA",
"path": "../LocalDependencies"
}
],
"remoteDependencies": [
{
"name": "RemoteDependencyA"
},
{
"name": "RemoteDependencyB"
},
{
"name": "RemoteDependencyD"
}
],
"targets": [
{
"name": "TargetA",
"targetType": "target",
"dependencies": [
{
"name": "LocalDependencyA"
},
{
"name": "RemoteDependencyA"
},
{
"name": "RemoteDependencyB"
},
{
"name": "RemoteDependencyD"
}
],
"sourcesPath": "Framework/Sources",
"resourcesPath": "Resources"
},
{
"name": "TargetATests",
"targetType": "testTarget",
"dependencies": [
{
"name": "TargetA",
"isTarget": true
},
{
"name": "RemoteDependencyB"
}
],
"sourcesPath": "Tests/Sources",
"resourcesPath": "Resources"
}
]
}
Loading

0 comments on commit b4a433d

Please sign in to comment.