-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse v1 and v2
Package.resolved
files (#34)
- 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
Showing
25 changed files
with
880 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def swift_dependencies(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
Oops, something went wrong.