-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add bundle * update imports * update workspace go get -u * add fieldalignment addresses #41 * update generator documentation manual fieldaligns bundle fix add tools refactor copygen location * fix fieldalignment: exit code 3 may indicate that the tool was unable to align everything refactor generator error messages * add removed fieldaligned comments * fix potential data race
- Loading branch information
1 parent
d3d85ae
commit 6ad3efa
Showing
59 changed files
with
18,669 additions
and
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ The repository contains a [README](/README.md), [Examples](/_examples/), [Code G | |
| shard | Sharding Manager. | | ||
| tools | Utility Tools. | | ||
|
||
_A [bundler](https://pkg.go.dev/golang.org/x/tools/cmd/bundle) is used to package the API into a `disgo` package (`disgo.go`)_. | ||
_A **bundler** is used to package the API into a `disgo` package (`disgo.go`)_. | ||
|
||
#### Structs | ||
|
||
|
@@ -52,9 +52,7 @@ Comments follow [Effective Go](https://golang.org/doc/effective_go#commentary) a | |
|
||
#### Static Code Analysis | ||
|
||
Disgo uses [golangci-lint](https://github.com/golangci/golangci-lint) in order to statically analyze code. You can install golangci-lint with `go install github.com/golangci/golangci-lint/cmd/[email protected]`. If you receive a `diff` error _(while running)_, you must add a `diff` tool in your PATH. There is one located in the `Git` bin. | ||
|
||
If you receive `File is not ... with -...`, use `golangci-lint run --disable-all --no-config -Egofmt --fix`. | ||
Disgo uses [golangci-lint](https://github.com/golangci/golangci-lint) in order to statically analyze code. You can install golangci-lint with `go install github.com/golangci/golangci-lint/cmd/[email protected]`. If you receive a `diff` error _(while running)_, you must add a `diff` tool in your PATH. There is one located in the `Git/bin` directory. | ||
|
||
| Directory | Command | Description | | ||
| :-------- | :---------------------------- | :------------------------------------------------- | | ||
|
@@ -64,6 +62,8 @@ If you receive `File is not ... with -...`, use `golangci-lint run --disable-all | |
| `shard` | `golangci-lint run ./shard` | Perform static code analysis on the Shard Manager. | | ||
| `tools` | `golangci-lint run ./tools` | Perform static code analysis on the Tools Module. | | ||
|
||
_If you receive `File is not ... with -...`, use `golangci-lint run --disable-all --no-config -Egofmt --fix` or ignore it._ | ||
|
||
#### Fieldalignment | ||
|
||
Disgo [fieldaligns](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment) bundled code to save memory. | ||
|
@@ -80,15 +80,11 @@ Integration tests are used to ensure functionality between the API Wrapper and D | |
|
||
#### Running Tests | ||
|
||
Use `go test` to run the tests in the current directory. Use `go test ./<dir>` to run tests in a given directory (from the current directory). Use [Github Action Workflow Files](/.github/workflows/) to find the correct test command for a module. | ||
Use `go test` to run the tests in the current directory. Use `go test ./<dir>` to run tests in a given directory (from the current directory). Use [Github Action Workflow Files](/.github/workflows/) to find the correct test command and environment variables for a module. | ||
|
||
# Roadmap | ||
|
||
Disgo is **STABLE**. Here are the steps required in order to complete it. | ||
|
||
1. Bundle Disgo (with `fieldalignment`) _[v10.0.0]_. | ||
|
||
The following additional features are being implemented. | ||
Disgo is **STABLE**. The following additional features are being implemented. | ||
|
||
1. Voice Connections ([UDP Decision](/_contribution/libraries/), [Audio Processing using Opus](https://discord.com/developers/docs/topics/voice-connections#encrypting-and-sending-voice)) | ||
2. [Sharding](https://github.com/switchupcb/disgo/issues/26) | ||
|
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
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
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,115 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/switchupcb/disgo/_gen/bundle/tools" | ||
) | ||
|
||
var ( | ||
showFieldaligned = flag.Bool("f", false, "Use -f to showcase the output from the fieldalignment tool.") | ||
) | ||
|
||
const ( | ||
exeDir = "_gen/bundle" | ||
bundlePath = "disgo.go" | ||
pkg = "package disgo" | ||
filemodewrite = 0644 | ||
) | ||
|
||
func main() { | ||
if err := check(); err != nil { | ||
fmt.Printf("%v", err) | ||
os.Exit(1) | ||
} | ||
|
||
flag.Parse() | ||
|
||
// disgo generation | ||
if err := generate(); err != nil { | ||
fmt.Printf("%v", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// check checks that the current working directory is `disgo/_gen/bundle`. | ||
func check() error { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Errorf("error getting the current working directory: %w", err) | ||
} | ||
|
||
cwdDir := filepath.Dir(cwd) | ||
base := filepath.Base(cwdDir) + "/" + filepath.Base(cwd) | ||
if base != exeDir && filepath.Base(filepath.Dir(cwdDir)) != "disgo" { | ||
return fmt.Errorf("This executable must be run from disgo/" + exeDir) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// generate generates the disgo bundle. | ||
func generate() error { | ||
if err := os.Chdir("../../"); err != nil { | ||
return fmt.Errorf("chdir: %w", err) | ||
} | ||
|
||
// clear the bundled file. | ||
bundle := `//go:generate bundle -o disgo.go -dst . -pkg disgo -prefix "" ./wrapper` | ||
cleared := strings.Join([]string{bundle, pkg}, "\n") | ||
if err := os.WriteFile(bundlePath, []byte(cleared), filemodewrite); err != nil { | ||
return fmt.Errorf("clear: %w", err) | ||
} | ||
|
||
bundlegen := exec.Command("go", "generate") | ||
std, err := bundlegen.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("bundle (go generate): %v", string(std)) | ||
} | ||
|
||
// fix the imports of the bundle. | ||
if err := tools.Imports(bundlePath); err != nil { | ||
return fmt.Errorf("imports: %w", err) | ||
} | ||
|
||
// fieldalign the bundle (until there is nothing else to fieldalign). | ||
var fieldalignmentOutput []byte | ||
for i := 0; i < 5; i++ { | ||
fieldalignment := exec.Command("fieldalignment", "-fix", bundlePath) | ||
std, err = fieldalignment.CombinedOutput() | ||
if err != nil && err.Error() == "exit status 3" { | ||
fieldalignmentOutput = append(fieldalignmentOutput, std...) | ||
fmt.Printf("WARNING (fieldalignment): %v\n", err) | ||
fmt.Println("running fieldalignment again...\n") | ||
|
||
continue | ||
|
||
} else if err != nil { | ||
return fmt.Errorf("fieldalignment: %v", err) | ||
} | ||
|
||
fieldalignmentOutput = append(fieldalignmentOutput, std...) | ||
|
||
if i == 4 { | ||
return fmt.Errorf("fieldalignment: more calls to fieldalignment were required.") | ||
} | ||
|
||
break | ||
} | ||
|
||
if *showFieldaligned { | ||
fmt.Println(string(fieldalignmentOutput)) | ||
} | ||
|
||
// add removed comments to the bundle. | ||
if err := tools.Replace(bundlePath); err != nil { | ||
return fmt.Errorf("replace: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,50 @@ | ||
package tools | ||
|
||
import ( | ||
"fmt" | ||
"go/format" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
filemodewrite = 0644 | ||
) | ||
|
||
var ( | ||
// skip represents a set of imports to remove. | ||
skip = map[string]bool{ | ||
`"encoding/json"`: true, | ||
} | ||
) | ||
|
||
// Imports fixes the imports of the bundler. | ||
func Imports(filepath string) error { | ||
data, err := os.ReadFile(filepath) | ||
if err != nil { | ||
return fmt.Errorf("error reading generated %v file: %w", filepath, err) | ||
} | ||
|
||
var output []byte | ||
|
||
content := string(data) | ||
for _, line := range strings.Split(content, "\n") { | ||
if skip[strings.TrimSpace(line)] { | ||
continue | ||
} | ||
|
||
output = append(output, []byte(line+"\n")...) | ||
} | ||
|
||
// gofmt | ||
fmtdata, err := format.Source(output) | ||
if err != nil { | ||
return fmt.Errorf("error formatting generated code: %w", err) | ||
} | ||
|
||
if err = os.WriteFile(filepath, fmtdata, filemodewrite); err != nil { | ||
return fmt.Errorf("error writing file: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.