Skip to content
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

Add Yaml support #5

Merged
merged 15 commits into from
Aug 29, 2024
Merged

Add Yaml support #5

merged 15 commits into from
Aug 29, 2024

Conversation

albertodebortoli
Copy link
Member

The changes in this PR are not backward compatible, hence leading to a major version bump (v2).

  • Remove generate-packages command as the tool shouldn't make assumption on the environment. The generate-package command is enough and any logic on top should be implemented in the user space.
  • Rework generate-package command: the options are now clearer and the command doesn't expect the spec file to be named as the containing folder.
  • Add support for YAML format for both the dependencies and the specs files using jpsim/Yams.
  • Add tests for files in YAML format.
  • Use .copy in testTarget.resources which sorts some warnings.
  • Update README.md
  • Remove generated Example's Package.swift

@albertodebortoli albertodebortoli added this to the v2 milestone Aug 23, 2024
README.md.orig Outdated
@@ -0,0 +1,193 @@
# PackageGenerator

<<<<<<< HEAD

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like merge conflict file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️


let mappedDependencies: [Spec.RemoteDependency] = spec.remoteDependencies
.compactMap { remoteDependency -> Spec.RemoteDependency? in
guard let dependency = dependencies.dependencies.first(where: {
Copy link

@akuzminskyi akuzminskyi Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broken indent because guard in the .compactMap block. and return(line 40) below looks correct

return nil
}
return Spec.RemoteDependency(
name: dependency.name,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


struct Product: Decodable {
let name: String
let productType: String

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not replace String to ProductType, as a result you will remove custom decoding, and also it will be typesafe to use productType

The same in Target below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's beyond this PR but I'll take a look.


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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add the same for init(from decoder: Decoder) throws {


if those variable mean the same maybe better to use non-option enum?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same but I thought it was beyond the scope of this PR.

@albertodebortoli
Copy link
Member Author

Regarding the 2 comments on using Decodable better, I tried but it seems that the reason why it wasn't done before is that Stencil doesn't allow fetching properties from computed vars on objects passed in the context. I'm therefore keen in leaving it as it is.

Otherwise, this should work:

enum Ref: Decodable {
    case version(String)
    case revision(String)
    case branch(String)

    enum CodingKeys: String, CodingKey {
        case version
        case revision
        case branch
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        if let version = try container.decodeIfPresent(String.self, forKey: .version) {
            self = .version(version)
        } else if let revision = try container.decodeIfPresent(String.self, forKey: .revision) {
            self = .revision(revision)
        } else if let branch = try container.decodeIfPresent(String.self, forKey: .branch) {
            self = .branch(branch)
        } else {
            throw DecodingError.dataCorruptedError(forKey: CodingKeys.version, in: container, debugDescription: "Expected one of version, revision, or branch to be present.")
        }
    }
}
struct Dependency: Decodable {

    let name: String
    let url: String
    let ref: Ref

    enum CodingKeys: String, CodingKey {
        case name
        case url
    }

    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.decode(String.self, forKey: .url)
        self.ref = try Ref(from: decoder)
    }

    init(name: String, url: String, ref: Ref) {
        self.name = name
        self.url = url
        self.ref = ref
    }
}
struct Spec: Decodable {
    struct Product: Decodable {
        let name: String
        let productType: ProductType
        let targets: [String]

        var type: String {
            productType.rawValue
        }

        enum CodingKeys: CodingKey {
            case name
            case productType
            case targets
        }
    }
    ...
    
    struct RemoteDependency: Decodable {
        let name: String
        let url: String?
        let ref: Ref?

        var version: String? {
            if case let .version(value) = ref {
                return value
            }
            return nil
        }

        var revision: String? {
            if case let .revision(value) = ref {
                return value
            }
            return nil
        }

        var branch: String? {
            if case let .branch(value) = ref {
                return value
            }
            return nil
        }

        enum CodingKeys: CodingKey {
            case name
            case url
        }

        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.ref = (try? Ref(from: decoder)) ?? nil
        }

        init(name: String, url: String?, ref: Ref) {
            self.name = name
            self.url = url
            self.ref = ref
        }
    }
    ...

@albertodebortoli albertodebortoli merged commit bbd95d8 into main Aug 29, 2024
2 checks passed
@albertodebortoli albertodebortoli deleted the yaml-support branch August 29, 2024 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants