Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
efbc18e
Start adding repo rule
cgrindel Dec 1, 2022
3fe7115
Add tests for RepoNameFromPin
cgrindel Dec 1, 2022
8e0c8c2
Generating swift_package declarations.
cgrindel Dec 1, 2022
95775b7
Initial swiftpkg files
cgrindel Dec 1, 2022
b7c398b
Initial swift_package impl
cgrindel Dec 1, 2022
14a3279
Add swift_package to loads.
cgrindel Dec 1, 2022
d92d0b0
Save work cloning repos with swift_package.
cgrindel Dec 1, 2022
52ffc1f
Do not include shallow_since as required for canonical form.
cgrindel Dec 1, 2022
531678e
Retrieving the dump and desc in swift_package
cgrindel Dec 2, 2022
b79122c
Clean up
cgrindel Dec 2, 2022
3ecce40
Brought over repository_files, clang_files and modulemap_parser.
cgrindel Dec 2, 2022
95f59b8
Add test for RepoRuleFromPin
cgrindel Dec 2, 2022
b9d2cd8
Revert updates from gazelle plugin
cgrindel Dec 2, 2022
2210889
Brought over spm_versions.
cgrindel Dec 2, 2022
9a78f12
Ran tidy and manually fixed some missing deps
cgrindel Dec 2, 2022
149252e
Clean up
cgrindel Dec 2, 2022
34256ea
Add repository_utils.parsed_json_from_spm_command
cgrindel Dec 2, 2022
1b6efb4
Save dump and desc JSON
cgrindel Dec 2, 2022
3ed0e80
Fixed test issues
cgrindel Dec 2, 2022
e259ede
Get to green
cgrindel Dec 2, 2022
d08ab5f
Add tools_version
cgrindel Dec 2, 2022
43715de
Add platform
cgrindel Dec 2, 2022
dcaa3b8
Add dependency
cgrindel Dec 2, 2022
d65b05e
Add product
cgrindel Dec 2, 2022
71f40a2
Add targets
cgrindel Dec 2, 2022
f147c3c
Add documentation.
cgrindel Dec 2, 2022
c711b8a
Fix call to wrong function
cgrindel Dec 2, 2022
accf589
Clean up
cgrindel Dec 2, 2022
7ff648b
Add spreso to local_repository_files
cgrindel Dec 2, 2022
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ filegroup(
"//gazelle/internal/pathdistance:all_files",
"//gazelle/internal/spdesc:all_files",
"//gazelle/internal/spdump:all_files",
"//gazelle/internal/spreso:all_files",
"//gazelle/internal/stringslices:all_files",
"//gazelle/internal/swift:all_files",
"//gazelle/internal/swiftbin:all_files",
Expand Down
1 change: 1 addition & 0 deletions examples/pkg_manifest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ bzl_library(
name = "swift_deps",
srcs = ["swift_deps.bzl"],
visibility = ["//visibility:public"],
deps = ["@cgrindel_swift_bazel//swiftpkg:defs"],
)
1 change: 1 addition & 0 deletions gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go_library(
importpath = "github.com/cgrindel/swift_bazel/gazelle",
visibility = ["//visibility:public"],
deps = [
"//gazelle/internal/spreso",
"//gazelle/internal/stringslices",
"//gazelle/internal/swift",
"//gazelle/internal/swiftbin",
Expand Down
2 changes: 1 addition & 1 deletion gazelle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func findExternalDepsInManifest(mi *swift.ModuleIndex, pi *swiftpkg.PackageInfo)
// Create a map of Swift external dep identity and Bazel repo name
depIdentToBazelRepoName := make(map[string]string)
for _, d := range dump.Dependencies {
depIdentToBazelRepoName[d.Name], err = swift.RepoName(d.URL)
depIdentToBazelRepoName[d.Name], err = swift.RepoNameFromURL(d.URL)
if err != nil {
log.Fatalf("Failed to create repo name for %s: %w", d.Name, err)
}
Expand Down
8 changes: 8 additions & 0 deletions gazelle/internal/spreso/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ go_test(
)

bzlformat_pkg(name = "bzlformat")

# MARK: - Integration Test

filegroup(
name = "all_files",
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)
5 changes: 5 additions & 0 deletions gazelle/internal/spreso/package_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ type PackageReference struct {
Location string
Name string
}

func (pr *PackageReference) Remote() string {
// We probably want to use a different value based upon the Kind
return pr.Location
}
14 changes: 13 additions & 1 deletion gazelle/internal/spreso/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/cgrindel/swift_bazel/gazelle/internal/jsonutils"
"github.com/cgrindel/swift_bazel/gazelle/internal/jsonutils"
)

// Pin
Expand Down Expand Up @@ -76,6 +76,10 @@ func (bps *BranchPinState) PinStateType() PinStateType {
return BranchPinStateType
}

func (bps *BranchPinState) Commit() string {
return bps.Revision
}

// VersionPinState

type VersionPinState struct {
Expand All @@ -94,6 +98,10 @@ func (vps *VersionPinState) PinStateType() PinStateType {
return VersionPinStateType
}

func (vps *VersionPinState) Commit() string {
return vps.Revision
}

// RevisionPinState

type RevisionPinState struct {
Expand All @@ -107,3 +115,7 @@ func NewRevisionPinState(revision string) *RevisionPinState {
func (rps *RevisionPinState) PinStateType() PinStateType {
return RevisionPinStateType
}

func (rps *RevisionPinState) Commit() string {
return rps.Revision
}
4 changes: 4 additions & 0 deletions gazelle/internal/swift/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"module_name.go",
"module_type.go",
"repo_name.go",
"repo_rule_from_pin.go",
"rule_kinds.go",
"rules_common.go",
"rules_from_manifest.go",
Expand All @@ -25,6 +26,7 @@ go_library(
deps = [
"//gazelle/internal/pathdistance",
"//gazelle/internal/spdump",
"//gazelle/internal/spreso",
"//gazelle/internal/stringslices",
"//gazelle/internal/swiftpkg",
"@bazel_gazelle//config:go_default_library",
Expand All @@ -44,10 +46,12 @@ go_test(
"module_index_test.go",
"module_name_test.go",
"repo_name_test.go",
"repo_rule_from_pin_test.go",
"rule_kinds_test.go",
],
deps = [
":swift",
"//gazelle/internal/spreso",
"@bazel_gazelle//label:go_default_library",
"@bazel_gazelle//rule:go_default_library",
"@com_github_stretchr_testify//assert",
Expand Down
23 changes: 21 additions & 2 deletions gazelle/internal/swift/repo_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"path"
"strings"

"github.com/cgrindel/swift_bazel/gazelle/internal/spreso"
)

func RepoName(url string) (string, error) {
func RepoNameFromURL(url string) (string, error) {
if url == "" {
return "", fmt.Errorf("URL cannot be empty string")
}
Expand All @@ -17,7 +19,7 @@ func RepoName(url string) (string, error) {

// Normalize parts
for idx, p := range parts {
parts[idx] = strings.ReplaceAll(p, "-", "_")
parts[idx] = normalizeStrForRepoName(p)
}

// Remove the extension from the last part of the URL
Expand All @@ -30,3 +32,20 @@ func RepoName(url string) (string, error) {
// Put parts back together
return strings.Join(parts, "_"), nil
}

func normalizeStrForRepoName(v string) string {
return strings.ReplaceAll(v, "-", "_")
}

func RepoNameFromStr(v string) string {
return normalizeStrForRepoName(v)
}

func RepoNameFromPin(p *spreso.Pin) (string, error) {
switch p.PkgRef.Kind {
case spreso.RemoteSourceControlPkgRefKind:
return RepoNameFromURL(p.PkgRef.Location)
default:
return RepoNameFromStr(p.PkgRef.Identity), nil
}
}
41 changes: 37 additions & 4 deletions gazelle/internal/swift/repo_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,53 @@ package swift_test
import (
"testing"

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

func TestRepoName(t *testing.T) {
actual, err := swift.RepoName("https://github.com/nicklockwood/SwiftFormat.git")
func TestRepoNameFromURL(t *testing.T) {
actual, err := swift.RepoNameFromURL("https://github.com/nicklockwood/SwiftFormat.git")
assert.NoError(t, err)
assert.Equal(t, "nicklockwood_SwiftFormat", actual)

actual, err = swift.RepoName("https://github.com/nicklockwood/SwiftFormat")
actual, err = swift.RepoNameFromURL("https://github.com/nicklockwood/SwiftFormat")
assert.NoError(t, err)
assert.Equal(t, "nicklockwood_SwiftFormat", actual)

actual, err = swift.RepoName("")
actual, err = swift.RepoNameFromURL("")
assert.ErrorContains(t, err, "URL cannot be empty string")
assert.Equal(t, "", actual)
}

func TestRepoNameFromStr(t *testing.T) {
actual := swift.RepoNameFromStr("swift-argument-parser")
assert.Equal(t, "swift_argument_parser", actual)
}

func TestRepoNameFromPin(t *testing.T) {
t.Run("pin is remoteSourceControl", func(t *testing.T) {
p := &spreso.Pin{
PkgRef: &spreso.PackageReference{
Kind: spreso.RemoteSourceControlPkgRefKind,
Location: "https://github.com/apple/swift-argument-parser",
Identity: "swift-argument-parser",
},
}
actual, err := swift.RepoNameFromPin(p)
assert.NoError(t, err)
assert.Equal(t, "apple_swift_argument_parser", actual)
})
t.Run("pin is not remoteSourceControl", func(t *testing.T) {
p := &spreso.Pin{
PkgRef: &spreso.PackageReference{
Kind: spreso.LocalSourceControlPkgRefKind,
Location: "/path/to/repo",
Identity: "swift-argument-parser",
},
}
actual, err := swift.RepoNameFromPin(p)
assert.NoError(t, err)
assert.Equal(t, "swift_argument_parser", actual)
})
}
36 changes: 36 additions & 0 deletions gazelle/internal/swift/repo_rule_from_pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package swift

import (
"fmt"

"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/cgrindel/swift_bazel/gazelle/internal/spreso"
)

type commitProvider interface {
Commit() string
}

func RepoRuleFromPin(p *spreso.Pin) (*rule.Rule, error) {
repoName, err := RepoNameFromPin(p)
if err != nil {
return nil, err
}
cp, ok := p.State.(commitProvider)
if !ok {
return nil, fmt.Errorf("expected pin state to provide a commit hash %T", p.State)
}

r := rule.NewRule(SwiftPkgRuleKind, repoName)
r.SetAttr("commit", cp.Commit())
r.SetAttr("remote", p.PkgRef.Remote())

switch t := p.State.(type) {
case *spreso.VersionPinState:
r.AddComment(fmt.Sprintf("# version: %s", t.Version))
case *spreso.BranchPinState:
r.AddComment(fmt.Sprintf("# branch: %s", t.Name))
}

return r, nil
}
34 changes: 34 additions & 0 deletions gazelle/internal/swift/repo_rule_from_pin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package swift_test

import (
"testing"

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

func TestRepoRuleFromPin(t *testing.T) {
remote := "https://github.com/apple/swift-argument-parser"
version := "1.2.3"
revision := "12345"
p := &spreso.Pin{
PkgRef: &spreso.PackageReference{
Location: remote,
},
State: &spreso.VersionPinState{
Version: version,
Revision: revision,
},
}
actual, err := swift.RepoRuleFromPin(p)
assert.NoError(t, err)
assert.Equal(t, swift.SwiftPkgRuleKind, actual.Kind())
expectedName, err := swift.RepoNameFromPin(p)
assert.NoError(t, err)
assert.Equal(t, expectedName, actual.Name())
assert.Equal(t, remote, actual.AttrString("remote"))
assert.Equal(t, revision, actual.AttrString("commit"))
assert.Len(t, actual.Comments(), 1)
assert.Contains(t, actual.Comments()[0], "# version: 1.2.3")
}
9 changes: 6 additions & 3 deletions gazelle/internal/swift/rule_kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package swift

const (
LibraryRuleKind = "swift_library"
BinaryRuleKind = "swift_binary"
TestRuleKind = "swift_test"
)
BinaryRuleKind = "swift_binary"
TestRuleKind = "swift_test"

// Repository Rule
SwiftPkgRuleKind = "swift_package"
)

func IsSwiftRuleKind(ruleKind string) bool {
switch ruleKind {
Expand Down
6 changes: 6 additions & 0 deletions gazelle/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ var loads = []rule.LoadInfo{
swift.TestRuleKind,
},
},
{
Name: "@cgrindel_swift_bazel//swiftpkg:defs.bzl",
Symbols: []string{
swift.SwiftPkgRuleKind,
},
},
}

type swiftLang struct {
Expand Down
41 changes: 26 additions & 15 deletions gazelle/update_repos.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package gazelle

import (
"log"
"os"
"path/filepath"

"github.com/bazelbuild/bazel-gazelle/language"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/cgrindel/swift_bazel/gazelle/internal/spreso"
"github.com/cgrindel/swift_bazel/gazelle/internal/swift"
)

// language.RepoImporter Implementation
Expand All @@ -16,19 +19,27 @@ func (*swiftLang) CanImport(path string) bool {

func (*swiftLang) ImportRepos(args language.ImportReposArgs) language.ImportReposResult {
result := language.ImportReposResult{}
// DEBUG BEGIN
log.Printf("*** CHUCK: ImportRepos args: %+#v", args)
// DEBUG END
return result
}

// // language.RepoUpdate Implementation
// Read the Package.resolved file
b, err := os.ReadFile(args.Path)
if err != nil {
result.Error = err
return result
}
pins, err := spreso.NewPinsFromResolvedPackageJSON(b)
if err != nil {
result.Error = err
return result
}

result.Gen = make([]*rule.Rule, len(pins))
for idx, p := range pins {
result.Gen[idx], err = swift.RepoRuleFromPin(p)
if err != nil {
result.Error = err
return result
}
}

// // Update repository rules that provide named libraries
// func (*swiftLang) UpdateRepos(args language.UpdateReposArgs) language.UpdateReposResult {
// result := language.UpdateReposResult{}
// // DEBUG BEGIN
// log.Printf("*** CHUCK: UpdateRepos args: %+#v", args)
// // DEBUG END
// return result
// }
return result
}
11 changes: 11 additions & 0 deletions swiftpkg/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")

bzl_library(
name = "defs",
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
deps = ["//swiftpkg/internal:swift_package"],
)

bzlformat_pkg(name = "bzlformat")
5 changes: 5 additions & 0 deletions swiftpkg/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Defines the public API for `swiftpkg`"""

load("//swiftpkg/internal:swift_package.bzl", _swift_package = "swift_package")

swift_package = _swift_package
Loading