Skip to content

Commit

Permalink
feat: parse v1 and v2 Package.resolved files (#34)
Browse files Browse the repository at this point in the history
- Rename `examples/MySwiftPackage` to `examples/pkg_manifest`.
- Implement `spreso` package with models for `Package.resolved` file
formats (v1, v2).

Related to #9
  • Loading branch information
cgrindel committed Nov 30, 2022
1 parent 51467aa commit b4ea343
Show file tree
Hide file tree
Showing 25 changed files with 880 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# To update these lines, execute
# `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages`
build --deleted_packages=examples/simple,examples/simple/Sources/MyDequeModule,examples/simple/Sources/PrintStuff,examples/simple/Tests/MyDequeModuleTests
query --deleted_packages=examples/simple,examples/simple/Sources/MyDequeModule,examples/simple/Sources/PrintStuff,examples/simple/Tests/MyDequeModuleTests
build --deleted_packages=examples/pkg_manifest,examples/simple,examples/simple/Sources/MyDequeModule,examples/simple/Sources/PrintStuff,examples/simple/Tests/MyDequeModuleTests
query --deleted_packages=examples/pkg_manifest,examples/simple,examples/simple/Sources/MyDequeModule,examples/simple/Sources/PrintStuff,examples/simple/Tests/MyDequeModuleTests

# Import Shared settings
import %workspace%/shared.bazelrc
Expand Down
7 changes: 4 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ tidy(
"@contrib_rules_bazel_integration_test//tools:update_deleted_packages",
":bzlformat_missing_pkgs_fix",
":update_all",
# Run go_mod_tidy before gazelle_update_repos
# Run go_mod_tidy before go_update_repos
":go_mod_tidy",
":gazelle_update_repos",
":go_update_repos",
":gazelle",
],
)
Expand All @@ -53,14 +53,15 @@ gazelle(
)

gazelle(
name = "gazelle_update_repos",
name = "go_update_repos",
args = [
"-from_file=go.mod",
"-to_macro=go_deps.bzl%swift_bazel_go_dependencies",
"-build_external=external",
"-prune",
],
command = "update-repos",
gazelle = ":gazelle_bin",
)

# MARK: - Golang
Expand Down
29 changes: 29 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ bzlformat_pkg(name = "bzlformat")
# repository.
# gazelle:exclude **

# MARK: - Simple Example

sh_binary(
name = "simple_test_runner",
testonly = True,
Expand All @@ -36,6 +38,33 @@ bazel_integration_tests(
workspace_path = "simple",
)

# MARK: - Package Manifest example

sh_binary(
name = "pkg_manifest_test_runner",
testonly = True,
srcs = ["pkg_manifest_test_runner.sh"],
data = [
"@contrib_rules_bazel_integration_test//tools:create_scratch_dir",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
"@cgrindel_bazel_starlib//shlib/lib:assertions",
],
)

bazel_integration_tests(
name = "pkg_manifest_test",
bazel_versions = SUPPORTED_BAZEL_VERSIONS,
test_runner = ":pkg_manifest_test_runner",
workspace_files = integration_test_utils.glob_workspace_files("pkg_manifest") + [
"//:local_repository_files",
],
workspace_path = "pkg_manifest",
)

# MARK: - Test Suites

test_suite(
name = "smoke_integration_tests",
tags = integration_test_utils.DEFAULT_INTEGRATION_TEST_TAGS,
Expand Down
28 changes: 28 additions & 0 deletions examples/pkg_manifest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")

# MARK: - Gazelle

gazelle_binary(
name = "gazelle_bin",
languages = [
"@bazel_skylib//gazelle/bzl",
"@cgrindel_swift_bazel//gazelle",
],
)

# gazelle:prefix github.com/cgrindel/swift_bazel/examples/simple
gazelle(
name = "gazelle",
gazelle = ":gazelle_bin",
)

gazelle(
name = "swift_update_repos",
args = [
"-from_file=Package.resolved",
"-to_macro=swift_deps.bzl%swift_dependencies",
"-prune",
],
command = "update-repos",
gazelle = ":gazelle_bin",
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions examples/pkg_manifest/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
workspace(name = "pkg_manifest_example")

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

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

swift_bazel_dependencies()

# MARK: - Gazelle

# gazelle:repo bazel_gazelle

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@cgrindel_swift_bazel//:go_deps.bzl", "swift_bazel_go_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

# Declare Go dependencies before calling go_rules_dependencies.
swift_bazel_go_dependencies()

go_rules_dependencies()

go_register_toolchains(version = "1.19.1")

gazelle_dependencies()

# MARK: - Swift Toolchain

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

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

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

# gazelle:repository_macro swift_deps.bzl%swift_dependencies
swift_dependencies()

swift_rules_dependencies()

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

swift_rules_extra_dependencies()
2 changes: 2 additions & 0 deletions examples/pkg_manifest/swift_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def swift_dependencies():
pass
56 changes: 56 additions & 0 deletions examples/pkg_manifest_test_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -o errexit
# --- end runfiles.bash initialization v2 ---

# MARK - Locate Deps

assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
source "${assertions_sh}"

create_scratch_dir_sh_location=contrib_rules_bazel_integration_test/tools/create_scratch_dir.sh
create_scratch_dir_sh="$(rlocation "${create_scratch_dir_sh_location}")" || \
(echo >&2 "Failed to locate ${create_scratch_dir_sh_location}" && exit 1)

# MARK - Process Arguments

bazel="${BIT_BAZEL_BINARY:-}"
workspace_dir="${BIT_WORKSPACE_DIR:-}"

[[ -n "${bazel:-}" ]] || exit_with_msg "Must specify the location of the Bazel binary."
[[ -n "${workspace_dir:-}" ]] || exit_with_msg "Must specify the location of the workspace directory."

# MARK - Create Scratch Directory

scratch_dir="$("${create_scratch_dir_sh}" --workspace "${workspace_dir}")"
cd "${scratch_dir}"

# MARK - Test

# Dump Bazel info
bazel info

# Run gazelle
bazel run //:gazelle -- --gen_from_pkg_manifest

# Ensure that it builds
bazel test //...

# Run the target
output="$(bazel run //:MySwiftPackage)"
assert_match "Hello, World!" "${output}"

# Run the product alias
output="$(bazel run //:printstuff)"
assert_match "Hello, World!" "${output}"
13 changes: 12 additions & 1 deletion gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

# MARK: - Starlark

Expand All @@ -22,6 +22,7 @@ go_library(
"generate.go",
"lang.go",
"resolve.go",
"update_repos.go",
],
importpath = "github.com/cgrindel/swift_bazel/gazelle",
visibility = ["//visibility:public"],
Expand All @@ -41,3 +42,13 @@ go_library(
"@org_golang_x_exp//slices",
],
)

go_test(
name = "gazelle_test",
srcs = ["update_repos_test.go"],
deps = [
":gazelle",
"@bazel_gazelle//language:go_default_library",
"@com_github_stretchr_testify//assert",
],
)
30 changes: 30 additions & 0 deletions gazelle/internal/spreso/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "spreso",
srcs = [
"package_reference.go",
"pin.go",
"pin_store_v1.go",
"pin_store_v2.go",
],
importpath = "github.com/cgrindel/swift_bazel/gazelle/internal/spreso",
visibility = ["//gazelle:__subpackages__"],
)

go_test(
name = "spreso_test",
srcs = [
"package_reference_test.go",
"pin_store_v1_test.go",
"pin_store_v2_test.go",
"pin_test.go",
],
deps = [
":spreso",
"@com_github_stretchr_testify//assert",
],
)

bzlformat_pkg(name = "bzlformat")
43 changes: 43 additions & 0 deletions gazelle/internal/spreso/package_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spreso

import "encoding/json"

type PkgRefKind int

const (
UnknownPkgRefKind PkgRefKind = iota
RootPkgRefKind
FileSystemPkgRefKind
LocalSourceControlPkgRefKind
RemoteSourceControlPkgRefKind
RegistryPkgRefKind
)

func (prk *PkgRefKind) UnmarshalJSON(b []byte) error {
var jsonVal string
if err := json.Unmarshal(b, &jsonVal); err != nil {
return err
}
switch jsonVal {
case "root":
*prk = RootPkgRefKind
case "fileSystem":
*prk = FileSystemPkgRefKind
case "localSourceControl":
*prk = LocalSourceControlPkgRefKind
case "remoteSourceControl":
*prk = RemoteSourceControlPkgRefKind
case "registry":
*prk = RegistryPkgRefKind
}
return nil
}

// Represents serialized form of PackageModel.PackageReference
// https://github.com/apple/swift-package-manager/blob/main/Sources/PackageModel/PackageReference.swift
type PackageReference struct {
Identity string
Kind PkgRefKind
Location string
Name string
}
49 changes: 49 additions & 0 deletions gazelle/internal/spreso/package_reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package spreso_test

import (
"encoding/json"
"testing"

"github.com/cgrindel/swift_bazel/gazelle/internal/spreso"
"github.com/stretchr/testify/assert"
)

func strToPkgRefKind(val string) (spreso.PkgRefKind, error) {
kind := spreso.UnknownPkgRefKind
jsonVal, err := json.Marshal(val)
if err != nil {
return kind, err
}
if err := kind.UnmarshalJSON(jsonVal); err != nil {
return kind, err
}
return kind, nil
}

func TestPkgRefKindUnmarshalJSON(t *testing.T) {
t.Run("root", func(t *testing.T) {
actual, err := strToPkgRefKind("root")
assert.NoError(t, err)
assert.Equal(t, spreso.RootPkgRefKind, actual)
})
t.Run("fileSystem", func(t *testing.T) {
actual, err := strToPkgRefKind("fileSystem")
assert.NoError(t, err)
assert.Equal(t, spreso.FileSystemPkgRefKind, actual)
})
t.Run("localSourceControl", func(t *testing.T) {
actual, err := strToPkgRefKind("localSourceControl")
assert.NoError(t, err)
assert.Equal(t, spreso.LocalSourceControlPkgRefKind, actual)
})
t.Run("remoteSourceControl", func(t *testing.T) {
actual, err := strToPkgRefKind("remoteSourceControl")
assert.NoError(t, err)
assert.Equal(t, spreso.RemoteSourceControlPkgRefKind, actual)
})
t.Run("registry", func(t *testing.T) {
actual, err := strToPkgRefKind("registry")
assert.NoError(t, err)
assert.Equal(t, spreso.RegistryPkgRefKind, actual)
})
}
Loading

0 comments on commit b4ea343

Please sign in to comment.