-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse output of
swift package dump-package
(#19)
- Add `.swiftformat` and `.swiftlint.yml` to the repo. - Update `.gitignore` with Swift-specific entries. - Add `MySwiftPackage` example. - Implement `jsonutils` package to help with parsing Swift package dump JSON. - Implement `swiftpkg` package to parse the Swift package dump JSON.
- Loading branch information
Showing
27 changed files
with
1,071 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
# Ignore Bazel symlinks | ||
bazel-* | ||
|
||
# Ignore Swift and Xcode stuff | ||
.DS_Store | ||
**/.build | ||
**/Packages | ||
**/*.xcodeproj | ||
**/xcuserdata/ | ||
**/DerivedData/ | ||
**/.swiftpm/config/registries.json | ||
**/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
**/.netrc |
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,11 @@ | ||
# For information on the rules, see | ||
# https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md | ||
|
||
--allman false | ||
--indent 2 | ||
--semicolons never | ||
--stripunusedargs always | ||
--maxwidth 100 | ||
--wraparguments before-first | ||
--wrapparameters before-first | ||
--wrapcollections before-first |
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,11 @@ | ||
# List the rules by running "swiftlint rules" | ||
disabled_rules: | ||
- trailing_comma | ||
excluded: | ||
- .build | ||
identifier_name: | ||
excluded: | ||
- id | ||
- db | ||
nesting: | ||
type_level: 2 |
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,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-argument-parser", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-argument-parser", | ||
"state" : { | ||
"revision" : "fddd1c00396eed152c45a46bea9f47b98e59301d", | ||
"version" : "1.2.0" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
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,27 @@ | ||
// swift-tools-version: 5.7 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MySwiftPackage", | ||
platforms: [.macOS(.v10_15)], | ||
products: [ | ||
.executable(name: "printstuff", targets: ["MySwiftPackage"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "MySwiftPackage", | ||
dependencies: [ | ||
.product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
] | ||
), | ||
.testTarget( | ||
name: "MySwiftPackageTests", | ||
dependencies: ["MySwiftPackage"] | ||
), | ||
] | ||
) |
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,3 @@ | ||
# MySwiftPackage | ||
|
||
A description of this package. |
10 changes: 10 additions & 0 deletions
10
examples/MySwiftPackage/Sources/MySwiftPackage/MySwiftPackage.swift
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,10 @@ | ||
import ArgumentParser | ||
|
||
@main | ||
struct MySwiftPackage: AsyncParsableCommand { | ||
public private(set) var text = "Hello, World!" | ||
|
||
mutating func main() async throws { | ||
print(MySwiftPackage().text) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/MySwiftPackage/Tests/MySwiftPackageTests/MySwiftPackageTests.swift
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,11 @@ | ||
import XCTest | ||
@testable import MySwiftPackage | ||
|
||
final class MySwiftPackageTests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
XCTAssertEqual(MySwiftPackage().text, "Hello, World!") | ||
} | ||
} |
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("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "jsonutils", | ||
srcs = [ | ||
"errors.go", | ||
"json_map.go", | ||
"json_slice.go", | ||
], | ||
importpath = "github.com/cgrindel/swift_bazel/gazelle/internal/jsonutils", | ||
visibility = ["//gazelle:__subpackages__"], | ||
) | ||
|
||
go_test( | ||
name = "jsonutils_test", | ||
srcs = [ | ||
"errors_test.go", | ||
"json_map_test.go", | ||
"json_slice_test.go", | ||
], | ||
deps = [ | ||
":jsonutils", | ||
"@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,123 @@ | ||
package jsonutils | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type mapKey struct { | ||
Key string | ||
} | ||
|
||
type sliceIndex struct { | ||
Index int | ||
} | ||
|
||
type unexpectedType struct { | ||
ExpectedType string | ||
ActualType string | ||
} | ||
|
||
// MissingKeyError | ||
|
||
type MissingKeyError struct { | ||
mapKey | ||
} | ||
|
||
func NewMissingKeyError(key string) *MissingKeyError { | ||
return &MissingKeyError{ | ||
mapKey{Key: key}, | ||
} | ||
} | ||
|
||
func (e *MissingKeyError) Error() string { | ||
return fmt.Sprintf("key '%v' not found", e.Key) | ||
} | ||
|
||
// KeyTypeError | ||
|
||
type KeyTypeError struct { | ||
mapKey | ||
unexpectedType | ||
} | ||
|
||
func NewKeyTypeError(key, expectedType string, actual any) *KeyTypeError { | ||
return &KeyTypeError{ | ||
mapKey{Key: key}, | ||
unexpectedType{ | ||
ExpectedType: expectedType, | ||
ActualType: fmt.Sprintf("%T", actual), | ||
}, | ||
} | ||
} | ||
|
||
func (e *KeyTypeError) Error() string { | ||
return fmt.Sprintf( | ||
"key '%s' expected to be type '%s', but was type '%s'", | ||
e.Key, e.ExpectedType, e.ActualType) | ||
} | ||
|
||
// MarshalKeyError | ||
|
||
type KeyError struct { | ||
Err error | ||
mapKey | ||
} | ||
|
||
func NewKeyError(k string, err error) *KeyError { | ||
mk := mapKey{Key: k} | ||
return &KeyError{ | ||
Err: err, | ||
mapKey: mk, | ||
} | ||
} | ||
|
||
func (e *KeyError) Error() string { | ||
return fmt.Sprintf("error occurred processing '%v', %v", e.Key, e.Err) | ||
} | ||
|
||
func (e *KeyError) Unwrap() error { | ||
return e.Err | ||
} | ||
|
||
// IndexTypeError | ||
|
||
type IndexTypeError struct { | ||
sliceIndex | ||
unexpectedType | ||
} | ||
|
||
func NewIndexTypeError(index int, expectedType string, actual any) *IndexTypeError { | ||
return &IndexTypeError{ | ||
sliceIndex{Index: index}, | ||
unexpectedType{ | ||
ExpectedType: expectedType, | ||
ActualType: fmt.Sprintf("%T", actual), | ||
}, | ||
} | ||
} | ||
|
||
func (e *IndexTypeError) Error() string { | ||
return fmt.Sprintf( | ||
"index %d expected to be type '%s', but was type '%s'", | ||
e.Index, e.ExpectedType, e.ActualType) | ||
} | ||
|
||
// IndexOutOfBoundsError | ||
|
||
type IndexOutOfBoundsError struct { | ||
sliceIndex | ||
ActualLen int | ||
} | ||
|
||
func NewIndexOutOfBoundsError(idx, actualLen int) *IndexOutOfBoundsError { | ||
return &IndexOutOfBoundsError{ | ||
sliceIndex: sliceIndex{Index: idx}, | ||
ActualLen: actualLen, | ||
} | ||
} | ||
|
||
func (e *IndexOutOfBoundsError) Error() string { | ||
return fmt.Sprintf( | ||
"index %d out of bounds for slice with length %d", | ||
e.Index, e.ActualLen) | ||
} |
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,45 @@ | ||
package jsonutils_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cgrindel/swift_bazel/gazelle/internal/jsonutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMissingKeyError(t *testing.T) { | ||
key := "foo" | ||
mke := jsonutils.NewMissingKeyError(key) | ||
assert.Equal(t, key, mke.Key) | ||
assert.Implements(t, (*error)(nil), mke) | ||
} | ||
|
||
func TestKeyTypeError(t *testing.T) { | ||
key := "foo" | ||
expectedType := "string" | ||
kte := jsonutils.NewKeyTypeError(key, expectedType, 123) | ||
assert.Equal(t, key, kte.Key) | ||
assert.Equal(t, expectedType, kte.ExpectedType) | ||
assert.Equal(t, "int", kte.ActualType) | ||
assert.Implements(t, (*error)(nil), kte) | ||
} | ||
|
||
func TestKeyError(t *testing.T) { | ||
key := "foo" | ||
oerr := fmt.Errorf("original error") | ||
ke := jsonutils.NewKeyError(key, oerr) | ||
assert.Equal(t, ke.Key, key) | ||
assert.Equal(t, ke.Err, oerr) | ||
assert.Implements(t, (*error)(nil), ke) | ||
} | ||
|
||
func TestIndexTypeError(t *testing.T) { | ||
index := 3 | ||
expectedType := "string" | ||
ite := jsonutils.NewIndexTypeError(index, expectedType, 123) | ||
assert.Equal(t, index, ite.Index) | ||
assert.Equal(t, expectedType, ite.ExpectedType) | ||
assert.Equal(t, "int", ite.ActualType) | ||
assert.Implements(t, (*error)(nil), ite) | ||
} |
Oops, something went wrong.