Sometimes ago I created Puma, which is a thin wrapper around Xcode commandline tools, for example xcodebuild
There’s lots of arguments to pass in xcodebuild, and there are many tasks like build, test and archive that all uses this command.
Use Options struct to encapsulate parameters
To avoid passing many parameters into a class, I tend to make an Options struct to encapsulate all passing parameters. I also use composition, where Build.Options and Test.Options contains Xcodebuild.Options
This ensures that the caller must provide all needed parameters, when you can compile you are ensured that all required parameters are provided.
This is OK, but a bit rigid in a way that there are many more parameters we can pass into xcodebuild command, so we must provide a way for user to alter or add more parameters.
Here is how to convert from Options to arguments to pass to our command. Because each parameter has different specifiers, like with double hyphens --flag=true, single hyphen -flag=true or just hyphen with a space between parameter key and value -flag true, we need to manually specify that, and concat them with string. Luckily, the order of parameters is not important
publicstructXcodebuild{ publicstructOptions{ /// build the workspace NAME publiclet workspace: String? /// build the project NAME publiclet project: String /// build the scheme NAME publiclet scheme: String /// use the build configuration NAME for building each target publiclet configuration: String /// use SDK as the name or path of the base SDK when building the project publiclet sdk: String? publiclet signing: Signing? publiclet usesModernBuildSystem: Bool publicinit( workspace: String? = nil, project: String, scheme: String, configuration: String = Configuration.debug, sdk: String? = Sdk.iPhoneSimulator, signing: Signing? = nil, usesModernBuildSystem: Bool = true) { self.workspace = workspace self.project = project self.scheme = scheme self.configuration = configuration self.sdk = sdk self.signing = signing self.usesModernBuildSystem = usesModernBuildSystem } } }