Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Swift registry support #1043

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/registry_example/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Import Shared settings
import %workspace%/../../shared.bazelrc

# Import CI settings.
import %workspace%/../../ci.bazelrc

# Try to import a local.rc file; typically, written by CI
try-import %workspace%/../../local.bazelrc

common --enable_bzlmod=false
35 changes: 35 additions & 0 deletions examples/registry_example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
load("@rules_swift_package_manager//swiftpkg:defs.bzl", "swift_update_packages")


# MARK: - Gazelle

# Ignore the Swift build folder
# gazelle:exclude .build

gazelle_binary(
name = "gazelle_bin",
languages = [
"@rules_swift_package_manager//gazelle",
],
)

swift_update_packages(
name = "swift_update_pkgs",
gazelle = ":gazelle_bin",
swift_deps_index = "swift_packages_index.json",
generate_swift_deps_for_workspace = True,
update_bzlmod_stanzas = False,
update_bzlmod_use_repo_names = False,
)

swift_binary(
name = "registry_example",
srcs = ["main.swift"],
module_name = "registry_example",
visibility = ["//visibility:public"],
deps = [
"@swiftpkg_apple.swift_collections//:Collections",
]
)
13 changes: 13 additions & 0 deletions examples/registry_example/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"pins" : [
{
"identity" : "apple.swift-collections",
"kind" : "registry",
"location" : "",
"state" : {
"version" : "1.1.0"
}
}
],
"version" : 2
}
10 changes: 10 additions & 0 deletions examples/registry_example/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "registry_example",
dependencies: [
.package(id: "apple.swift-collections", from: "1.1.0"),
]
)
1 change: 1 addition & 0 deletions examples/registry_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# registry Example
watt marked this conversation as resolved.
Show resolved Hide resolved
61 changes: 61 additions & 0 deletions examples/registry_example/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
workspace(name = "simple_example")

local_repository(
name = "rules_swift_package_manager",
path = "../..",
)

load("@rules_swift_package_manager//:deps.bzl", "swift_bazel_dependencies")

swift_bazel_dependencies()

load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")

bazel_starlib_dependencies()

# MARK: - Gazelle

# gazelle:repo bazel_gazelle

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@rules_swift_package_manager//:go_deps.bzl", "swift_bazel_go_dependencies")

# Declare Go dependencies before calling go_rules_dependencies.
swift_bazel_go_dependencies()

go_rules_dependencies()

go_register_toolchains(version = "1.21.1")

gazelle_dependencies()

# MARK: - Swift Toolchain

http_archive(
name = "build_bazel_rules_swift",
sha256 = "bb01097c7c7a1407f8ad49a1a0b1960655cf823c26ad2782d0b7d15b323838e2",
url = "https://github.com/bazelbuild/rules_swift/releases/download/1.18.0/rules_swift.1.18.0.tar.gz",
)

load(
"@build_bazel_rules_swift//swift:repositories.bzl",
"swift_rules_dependencies",
)

swift_rules_dependencies()

load(
"@build_bazel_rules_swift//swift:extras.bzl",
"swift_rules_extra_dependencies",
)

swift_rules_extra_dependencies()

# MARK: - Swift Dependencies

load("//:swift_deps.bzl", "swift_dependencies")

# gazelle:repository_macro swift_deps.bzl%swift_dependencies
swift_dependencies()
1 change: 1 addition & 0 deletions examples/registry_example/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank: Force bzlmod to strict mode
13 changes: 13 additions & 0 deletions examples/registry_example/do_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

# 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

# Ensure that it builds and tests pass
"${bazel}" test //...
6 changes: 6 additions & 0 deletions examples/registry_example/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Collections

var deque: Deque<String> = ["Ted", "Rebecca"]
deque.prepend("Keeley")
deque.append("Nathan")
print(deque) // ["Keeley", "Ted", "Rebecca", "Nathan"]
8 changes: 8 additions & 0 deletions examples/registry_example/swift_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_swift_package_manager//swiftpkg:defs.bzl", "swift_registry_package")

def swift_dependencies():
swift_registry_package(
name = "swiftpkg_apple.swift_collections",
dependencies_index = "@//:swift_packages_index.json",
url = "https://artifactory.global.square/artifactory/api/swift/swift-test/apple/swift-collections/1.1.0.zip",
brentleyjones marked this conversation as resolved.
Show resolved Hide resolved
)
154 changes: 154 additions & 0 deletions examples/registry_example/swift_packages_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"direct_dep_identities": [
"apple.swift-collections"
],
"modules": [
{
"name": "BitCollections",
"c99name": "BitCollections",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:BitCollections.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"BitCollections",
"Collections"
]
},
{
"name": "Collections",
"c99name": "Collections",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:Collections.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"Collections"
]
},
{
"name": "DequeModule",
"c99name": "DequeModule",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:DequeModule.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"DequeModule",
"Collections"
]
},
{
"name": "HashTreeCollections",
"c99name": "HashTreeCollections",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:HashTreeCollections.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"HashTreeCollections",
"Collections"
]
},
{
"name": "HeapModule",
"c99name": "HeapModule",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:HeapModule.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"HeapModule",
"Collections"
]
},
{
"name": "OrderedCollections",
"c99name": "OrderedCollections",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:OrderedCollections.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"OrderedCollections",
"Collections"
]
},
{
"name": "_CollectionsUtilities",
"c99name": "_CollectionsUtilities",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:_CollectionsUtilities.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"BitCollections",
"DequeModule",
"HashTreeCollections",
"HeapModule",
"OrderedCollections",
"_RopeModule",
"Collections"
]
},
{
"name": "_RopeModule",
"c99name": "_RopeModule",
"src_type": "swift",
"label": "@swiftpkg_apple.swift_collections//:_RopeModule.rspm",
"package_identity": "apple.swift-collections",
"product_memberships": [
"_RopeModule",
"Collections"
]
}
],
"products": [
{
"identity": "apple.swift-collections",
"name": "BitCollections",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:BitCollections"
},
{
"identity": "apple.swift-collections",
"name": "Collections",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:Collections"
},
{
"identity": "apple.swift-collections",
"name": "DequeModule",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:DequeModule"
},
{
"identity": "apple.swift-collections",
"name": "HashTreeCollections",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:HashTreeCollections"
},
{
"identity": "apple.swift-collections",
"name": "HeapModule",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:HeapModule"
},
{
"identity": "apple.swift-collections",
"name": "OrderedCollections",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:OrderedCollections"
},
{
"identity": "apple.swift-collections",
"name": "_RopeModule",
"type": "library",
"label": "@swiftpkg_apple.swift_collections//:_RopeModule"
}
],
"packages": [
{
"name": "swiftpkg_apple.swift_collections",
"identity": "apple.swift-collections",
"remote": {
"commit": "",
"remote": "",
"version": "1.1.0"
}
}
]
}
23 changes: 23 additions & 0 deletions gazelle/internal/spdump/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (
type Dependency struct {
SourceControl *SourceControl `json:"sourceControl"`
FileSystem *FileSystem `json:"fileSystem"`
Registry *Registry `json:"registry"`
}

// Identity returns the value that identifies the external dependency in the manifest.
func (d *Dependency) Identity() string {
if d.SourceControl != nil {
return d.SourceControl.Identity
}
if d.Registry != nil {
return d.Registry.Identity
}
return ""
}

Expand Down Expand Up @@ -167,3 +171,22 @@ func (fs *FileSystem) UnmarshalJSON(b []byte) error {
fs.Path = rawFS.Path
return nil
}

type reg struct {
Identity string
}

type Registry struct {
watt marked this conversation as resolved.
Show resolved Hide resolved
Identity string
}

func (r *Registry) UnmarshalJSON(b []byte) error {
var raw []*reg
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
rawRegistry := raw[0]
r.Identity = rawRegistry.Identity
return nil
}
Loading
Loading