Skip to content

cgrindel/swift_gazelle_plugin

Repository files navigation

Swift Gazelle Plugin for Bazel

Build

This repository contains a Gazelle plugin used to generate rules_swift targets based upon your Swift source code..

Table of Contents

Quickstart

The following provides a quick introduction on how to set up and use the features in this repository. These instructions assume that you are using [Bazel modules] to load your external dependencies. If you are using Bazel's legacy external dependency management, we recommend using [Bazel's hybrid mode], then follow the steps in this quickstart guide.

1. Enable bzlmod

This repository supports [bzlmod].

common --enable_bzlmod

2. Configure your MODULE.bazel to use swift_gazelle_plugin.

Add a dependency on swift_gazelle_plugin.

bazel_dep(name = "swift_gazelle_plugin", version = "0.2.1")

3. Add Gazelle targets to BUILD.bazel at the root of your workspace.

Add the following to the BUILD.bazel file at the root of your workspace.

load("@gazelle//:def.bzl", "gazelle", "gazelle_binary")

# This declaration builds a Gazelle binary that incorporates all of the Gazelle
# plugins for the languages that you use in your workspace. In this example, we
# are only listing the Gazelle plugin for Swift from swift_gazelle_plugin.
gazelle_binary(
    name = "gazelle_bin",
    languages = [
        "@swift_gazelle_plugin//gazelle",
    ],
)

# This target updates the Bazel build files for your project. Run this target
# whenever you add or remove source files from your project.
gazelle(
    name = "update_build_files",
    gazelle = ":gazelle_bin",
)

4. Create or update Bazel build files for your project.

Generate/update the Bazel build files for your project by running the following:

bazel run //:update_build_files

5. Build and test your project.

Build and test your project.

bazel test //...

6. Check in MODULE.bazel.

  • The MODULE.bazel contains the declarations for your external dependencies.

10. Start coding

You are ready to start coding.

Tips and Tricks

The following are a few tips to consider as you work with your repository:

  • When you add or remove source files, run bazel run //:update_build_files. This will create/update the Bazel build files in your project. It is designed to be fast and unobtrusive.
  • If things do not appear to be working properly, run the following:
    • bazel run //:update_build_files
  • Do yourself a favor and create a Bazel target (e.g., //:tidy) that runs your repository maintenance targets (e.g., //:update_build_files, formatting utilities) in the proper order. If you are looking for an easy way to set this up, check out the //:tidy declaration in this repository and the documentation for the tidy macro.