Run script as part of `tuist generate`

Hey there, I’m fairly new to Tuist so apologies if this question has an obvious answer (I’ve tried to search for one, but no luck).

What I’d like to happen is, when a developer on our team runs tuist generate on their machine, a script / some code is also kicked off that does a few things like:

  • Copying across our pre-push hook to the .git directory (it exists by default in a more easy-to-find location within our repo)
  • Installing the latest version of SwiftLint that we’re currently supporting

Is there a best practice way to achieve this?

The approach I’m currently investigating appears to work thus far, but it doesn’t “feel” quite right (though this may be my lack of experience with Tuist).

I’ve basically changed our Workspace.swift so, instead of:

let workspace = Workspace( ... )

It now reads:

let workspace = defaultWorkspace()

private func defaultWorkspace() -> Workspace {
    // Run some native Swift code to copy across pre-push hook and do other stuff.

    // Return original workspace here.
    return Workspace( ... )
}

This appears to work so far (I’ve not got to the meat of it yet though) but I wanted to reach out to the community to understand whether I’ve approached this in an entirely unusual way :sweat_smile:.

I’m aware I could wrap tuist generate in some other utility script and have developers use that to generate their project. I ideally don’t want to do this though because we’ve got a fairly large team and it would be a big muscle memory shift for something that gets run many times a day!

Thanks!

Hey :wave:

// Run some native Swift code to copy across pre-push hook and do other stuff.

We don’t recommend executing code in your manifests. These manifests get cached and running code like this makes them more non-deterministic.

Installing the latest version of SwiftLint that we’re currently supporting

We recommend using Mise to align your CLI versions. Not only for SwiftLint, but also for Tuist. See how you can install Tuist via Mise here.

  • Copying across our pre-push hook to the .git directory (it exists by default in a more easy-to-find location within our repo),

You can install hooks via Mise as well: Hooks | mise-en-place

That is, all your needs would be covered by encoding your tools and hooks in .mise.toml and developers will need to run mise install to set things up. Not only that, your team would benefit by having Tuist and SwiftLint versions synced.

2 Likes

That’s perfect - thanks for taking the time to explain @marekfort. We’re actually already using Mise to install Tuist so I’ll look into this a bit further!