Skip to content

Add ComponentDefinition.withInstallAction() copy helper #290

@bguidolim

Description

@bguidolim

Problem

ComponentDefinition has 10 stored properties. When code needs to create a modified copy with a different installAction, it must manually reconstruct the entire struct:

ComponentDefinition(
    id: component.id,
    displayName: component.displayName,
    description: component.description,
    type: component.type,
    packIdentifier: component.packIdentifier,
    dependencies: component.dependencies,
    isRequired: component.isRequired,
    hookRegistration: component.hookRegistration,
    installAction: .copyPackFile(source: source, destination: newDestination, fileType: fileType),
    supplementaryChecks: component.supplementaryChecks
)

If ComponentDefinition gains a new stored property, this code silently omits it (the custom init has defaults for some parameters). This is fragile.

Proposed Solution

Add a withInstallAction(_:) convenience method:

extension ComponentDefinition {
    func withInstallAction(_ action: ComponentInstallAction) -> ComponentDefinition {
        ComponentDefinition(
            id: id, displayName: displayName, description: description,
            type: type, packIdentifier: packIdentifier,
            dependencies: dependencies, isRequired: isRequired,
            hookRegistration: hookRegistration, installAction: action,
            supplementaryChecks: supplementaryChecks
        )
    }
}

This centralizes the copy logic so new properties only need to be added in one place.

Current usage

  • Sources/mcs/Install/DestinationCollisionResolver.swift (lines 62-73)

References

  • Sources/mcs/TechPack/Component.swiftComponentDefinition struct

Metadata

Metadata

Assignees

No one assigned

    Labels

    DRYCode duplication / DRY violationenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions