Skip to content

Commit

Permalink
feat: add swift_package_tool generated targets (#1307)
Browse files Browse the repository at this point in the history
This adds a `swift_package_tool` rule which will be generated by the
`swift_deps` bzlmod extension. These targets can be run using `bazel
run` to update/resolve the `Package.swift`.

For example:

```sh
cd examples/interesting_deps

bazel run @swift_package//:update
bazel run @swift_package//:resolve
bazel run @swift_package//:update -- GeoSwift
```
  • Loading branch information
luispadron authored Oct 25, 2024
1 parent 2770623 commit 1ffa4b1
Show file tree
Hide file tree
Showing 31 changed files with 420 additions and 3 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ development inside a Bazel workspace.
* [Quickstart](#quickstart)
* [1. Enable bzlmod](#1-enable-bzlmod)
* [2. Configure your `MODULE.bazel` to use rules_swift_package_manager.](#2-configure-your-modulebazel-to-use-rules_swift_package_manager)
* [(Optional) Use `swift_package` repository for updating packages](#optional-use-swift_package-repository-for-updating-packages)
* [(Optional) Enable `swift_deps_info` generation for the Gazelle plugin](#optional-enable-swift_deps_info-generation-for-the-gazelle-plugin)
* [3. Create a minimal `Package.swift` file.](#3-create-a-minimal-packageswift-file)
* [4. Run `swift package update`](#4-run-swift-package-update)
Expand Down Expand Up @@ -121,6 +122,54 @@ NOTE: Some Swift package manager features (e.g., resources) use rules from [rule
dependency for `rules_swift_package_manager`. However, you do not need to declare it unless you use
any of the rules in your project.

#### (Optional) Use `swift_package` repository for updating packages

The `swift_deps` module extension will by default generate a `swift_package` repository which can be used to execute `swift package` commands.
This is useful if you'd like to control the flags and behavior of `swift package`, as well as for using the correct `swift` binary according to the Bazel configured toolchain.

For example, to resolve the `Package.swift` file:

```sh
bazel run @swift_package//:resolve
```

To update packages to their latest supported version:

```sh
bazel run @swift_package//:update
```

Both targets support passing arguments as well, so for example, you could update a single package:

```sh
bazel run @swift_package//:update -- MyPackage
```

These targets will update the `Package.resolved` file defined in `swift_deps.from_package`.
The targets come with default flags applied to enable the best Bazel compatibility, if you wish to configure it further, you can do so with `configure_swift_package`:

```starlark
# MODULE.bazel

swift_deps.configure_swift_package(
build_path = "spm-build",
cache_path = "spm-cache",
dependency_caching = "false",
manifest_cache = "none",
manifest_caching = "false",
)
```

If you do not want to use the `swift_package` repository you can disable it in the `swift_deps.from_package` call:

```starlark
swift_deps.from_package(
declare_swift_package = False, # <=== Disable the `swift_package` repository
resolved = "//:Package.resolved",
swift = "//:Package.swift",
)
```

#### (Optional) Enable `swift_deps_info` generation for the Gazelle plugin

If you will be using the Gazelle plugin for Swift, you will need to enable the generation of
Expand Down
21 changes: 20 additions & 1 deletion docs/bzlmod_extensions_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ On this page:
swift_deps = use_extension("@rules_swift_package_manager//:extensions.bzl", "swift_deps")
swift_deps.configure_package(<a href="#swift_deps.configure_package-name">name</a>, <a href="#swift_deps.configure_package-init_submodules">init_submodules</a>, <a href="#swift_deps.configure_package-patch_args">patch_args</a>, <a href="#swift_deps.configure_package-patch_cmds">patch_cmds</a>, <a href="#swift_deps.configure_package-patch_cmds_win">patch_cmds_win</a>,
<a href="#swift_deps.configure_package-patch_tool">patch_tool</a>, <a href="#swift_deps.configure_package-patches">patches</a>, <a href="#swift_deps.configure_package-recursive_init_submodules">recursive_init_submodules</a>)
swift_deps.from_package(<a href="#swift_deps.from_package-declare_swift_deps_info">declare_swift_deps_info</a>, <a href="#swift_deps.from_package-resolved">resolved</a>, <a href="#swift_deps.from_package-swift">swift</a>)
swift_deps.configure_swift_package(<a href="#swift_deps.configure_swift_package-build_path">build_path</a>, <a href="#swift_deps.configure_swift_package-cache_path">cache_path</a>, <a href="#swift_deps.configure_swift_package-dependency_caching">dependency_caching</a>, <a href="#swift_deps.configure_swift_package-manifest_cache">manifest_cache</a>,
<a href="#swift_deps.configure_swift_package-manifest_caching">manifest_caching</a>)
swift_deps.from_package(<a href="#swift_deps.from_package-declare_swift_deps_info">declare_swift_deps_info</a>, <a href="#swift_deps.from_package-declare_swift_package">declare_swift_package</a>, <a href="#swift_deps.from_package-resolved">resolved</a>, <a href="#swift_deps.from_package-swift">swift</a>)
</pre>


Expand All @@ -42,6 +44,22 @@ Used to add or override settings for a particular Swift package.
| <a id="swift_deps.configure_package-patches"></a>patches | A list of files that are to be applied as patches after extracting the archive. By default, it uses the Bazel-native patch implementation which doesn't support fuzz match and binary patch, but Bazel will fall back to use patch command line tool if `patch_tool` attribute is specified or there are arguments other than `-p` in `patch_args` attribute. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="swift_deps.configure_package-recursive_init_submodules"></a>recursive_init_submodules | Whether to clone submodules recursively in the repository. | Boolean | optional | `True` |

<a id="swift_deps.configure_swift_package"></a>

### configure_swift_package

Used to configure the flags used when running the `swift package` binary.

**Attributes**

| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="swift_deps.configure_swift_package-build_path"></a>build_path | The relative path within the runfiles tree for the Swift Package Manager build directory. | String | optional | `".build"` |
| <a id="swift_deps.configure_swift_package-cache_path"></a>cache_path | The relative path within the runfiles tree for the shared Swift Package Manager cache directory. | String | optional | `".cache"` |
| <a id="swift_deps.configure_swift_package-dependency_caching"></a>dependency_caching | Whether to enable the dependency cache. | String | optional | `"true"` |
| <a id="swift_deps.configure_swift_package-manifest_cache"></a>manifest_cache | Caching mode of Package.swift manifests (shared: shared cache, local: package's build directory, none: disabled) | String | optional | `"shared"` |
| <a id="swift_deps.configure_swift_package-manifest_caching"></a>manifest_caching | Whether to enable build manifest caching. | String | optional | `"true"` |

<a id="swift_deps.from_package"></a>

### from_package
Expand All @@ -53,6 +71,7 @@ Load Swift packages from `Package.swift` and `Package.resolved` files.
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="swift_deps.from_package-declare_swift_deps_info"></a>declare_swift_deps_info | Declare a `swift_deps_info` repository that is used by external tooling (e.g. Swift Gazelle plugin). | Boolean | optional | `False` |
| <a id="swift_deps.from_package-declare_swift_package"></a>declare_swift_package | Declare a `swift_package_tool` repository named `swift_package` which defines two targets: `update` and `resolve`. These targets run can be used to run the `swift package` binary in a Bazel context. The flags used when running the underlying `swift package` can be configured using the `configure_swift_package` tag.<br><br>They can be `bazel run` to update/resolve the `resolved` file:<br><br><pre><code>bazel run @swift_package//:update&#10;bazel run @swift_package//:resolve</code></pre> | Boolean | optional | `True` |
| <a id="swift_deps.from_package-resolved"></a>resolved | A `Package.resolved`. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="swift_deps.from_package-swift"></a>swift | A `Package.swift`. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |

Expand Down
1 change: 1 addition & 0 deletions examples/firebase_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_firebase_ios_sdk",
"swiftpkg_reachability.swift",
)
3 changes: 3 additions & 0 deletions examples/google_maps_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ swift_deps = use_extension(
"@rules_swift_package_manager//:extensions.bzl",
"swift_deps",
)

# Example showing that `swift_package` repo can be disabled.
swift_deps.from_package(
declare_swift_deps_info = True,
declare_swift_package = False,
resolved = "//:Package.resolved",
swift = "//:Package.swift",
)
Expand Down
12 changes: 12 additions & 0 deletions examples/interesting_deps/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,21 @@ swift_deps.from_package(
resolved = "//:Package.resolved",
swift = "//:Package.swift",
)

# Example showing customization of the `swift_package` repo tool.
# To resolve: `bazel run @swift_package//:resolve`
# To update: `bazel run @swift_package//:update`
swift_deps.configure_swift_package(
build_path = "spm-build",
cache_path = "spm-cache",
dependency_caching = "false",
manifest_cache = "none",
manifest_caching = "false",
)
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_cocoalumberjack",
"swiftpkg_geoswift",
"swiftpkg_libwebp_xcode",
Expand Down
5 changes: 4 additions & 1 deletion examples/interesting_deps/do_test
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

set -o errexit -o nounset -o pipefail

# Use the Bazel binary specified by the integration test. Otherise, fall back
# Use the Bazel binary specified by the integration test. Otherise, fall back
# to bazel.
bazel="${BIT_BAZEL_BINARY:-bazel}"

# Generate Swift external deps and update build files
"${bazel}" run //:tidy

# Test resolving the package via the `swift_package` repo.
"${bazel}" run @swift_package//:resolve

# Ensure that it builds and tests pass
"${bazel}" test //...
1 change: 1 addition & 0 deletions examples/ios_sim/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ swift_deps.configure_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_swift_markdown",
"swiftpkg_swift_nio",
)
1 change: 1 addition & 0 deletions examples/lottie_ios_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_lottie_spm",
)
1 change: 1 addition & 0 deletions examples/messagekit_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_kingfisher",
"swiftpkg_messagekit",
)
1 change: 1 addition & 0 deletions examples/nimble_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_nimble",
"swiftpkg_quick",
)
1 change: 1 addition & 0 deletions examples/objc_code/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_trustkit",
)
1 change: 1 addition & 0 deletions examples/phone_number_kit/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_phonenumberkit",
)
1 change: 1 addition & 0 deletions examples/pkg_manifest_minimal/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_my_local_package",
"swiftpkg_notthatamazingmodule",
"swiftpkg_swift_argument_parser",
Expand Down
1 change: 1 addition & 0 deletions examples/resources_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ swift_deps.from_package(
)
use_repo(
swift_deps,
"swift_package",
"swiftpkg_another_package_with_resources",
"swiftpkg_app_lovin_sdk",
"swiftpkg_googlesignin_ios",
Expand Down
1 change: 1 addition & 0 deletions examples/shake_ios_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_shake_ios",
)
1 change: 1 addition & 0 deletions examples/snapkit_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_snapkit",
)
1 change: 1 addition & 0 deletions examples/soto_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_soto",
)
1 change: 1 addition & 0 deletions examples/stripe_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_stripe_ios",
)
1 change: 1 addition & 0 deletions examples/symlink_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_empty_framework",
)
1 change: 1 addition & 0 deletions examples/tca_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_swift_composable_architecture",
)
1 change: 1 addition & 0 deletions examples/vapor_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ swift_deps.from_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_fluent",
"swiftpkg_fluent_sqlite_driver",
"swiftpkg_vapor",
Expand Down
1 change: 1 addition & 0 deletions examples/xcmetrics_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ swift_deps.configure_package(
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_xcmetrics",
)
1 change: 1 addition & 0 deletions swiftpkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bzl_library(
"//swiftpkg/internal:swift_deps_index",
"//swiftpkg/internal:swift_deps_info",
"//swiftpkg/internal:swift_package",
"//swiftpkg/internal:swift_package_tool",
],
)

Expand Down
3 changes: 3 additions & 0 deletions swiftpkg/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ bzl_library(
"//swiftpkg/internal:bazel_repo_names",
"//swiftpkg/internal:local_swift_package",
"//swiftpkg/internal:pkginfos",
"//swiftpkg/internal:repository_utils",
"//swiftpkg/internal:swift_deps_info",
"//swiftpkg/internal:swift_package",
"//swiftpkg/internal:swift_package_tool",
"//swiftpkg/internal:swift_package_tool_repo",
],
)
Loading

0 comments on commit 1ffa4b1

Please sign in to comment.