Hive
Code generation during tuist generate
GitHub issue · Open
What problem or need do you have?
I was trying to create a logger instance for each of my projects, without manually having to create the file for each of them, similar to how the bundle accessors are defined, something along the lines of:
import OSLog
extension Logger {
static let logger = Logger(subsystem: "io.tuist.subsystem", category: "io.tuist.targetName")
}
However, currently I am unable to do this natively. One of the solutions would be to create a fake resource file, specifically recognisable for a logger resource synthesizer, or I could create a plugin or target script that runs before I build the project. None of those solutions really appeal to me.
Potential solution
I would like to be able to leverage SwiftGen to create code in the Derived folder, similar to how resources are synthesised and the bundle accessors are created. I would think of this as a general Synthesizer, something that can generate code for us.
public protocol Synthesizer {}
public struct ResourceSynthesizer: Synthesizer {}
public struct StaticSynthesizer: Synthesizer {}
The protocol Synthesizer could be something we would like to have anyways, as in the future we might be able to use it to support different code generators.
The existing ResourceSynthesizer should only be minimally affected, depending on how elaborate the Synthesizer protocol is made.
Then at the call site, I would opt to upgrade the resourceSynthesizer parameter to just being a generic Synthesizer parameter.
Project(
name: "Generate Me",
synthesizers: [
ResourceSynthesizer(/* ... */),
StaticSynthesizer(/* ... */)
]
)
We should be able to support the helper functions like we do right now too.
extension Synthesizer where Self == ResourceSynthesizer {
public static func resource(_ synthesizer: Self) -> Synthesizer {}
}
extension Synthesizer where Self == StaticSynthesizer {
public static func `static`(_ synthesizer: Self) -> Synthesizer {}
}
Project(
name: "Generate Me",
synthesizers: [
.resource(.assets()),
.resource(.custom(name: "Lotties", parser: .json, extensions: ["lottie"])),
.static(.bundleAccessors),
.static(.custom(name: "Logger"))
]
)
macOS version
13.4.1
Tuist version
3.20.0
Xcode version
14.3.1
Investigate, reproduce, or fix this item in an isolated repository. Each Flight preserves its outcome and agent session.
Start a Flight and preserve its objective, outcome, and session here.
Hey folks! 👋🏼 We are limited in resources to execute something like this, but we’d be able to support it if anyone is interesting in contributing it. I recommend that we create an RFC with the API proposal, align, and then move it to execution phase.