Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dartgen] Add ability to customize format options for dartgen command #4412

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 tools/goctl/api/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {
dartCmdFlags.BoolVar(&dartgen.VarStringLegacy, "legacy")
dartCmdFlags.StringVar(&dartgen.VarStringHostname, "hostname")
dartCmdFlags.StringVar(&dartgen.VarStringScheme, "scheme")
dartCmdFlags.StringVar(&dartgen.VarStringFormatArgs, "format-args")

docCmdFlags.StringVar(&docgen.VarStringDir, "dir")
docCmdFlags.StringVar(&docgen.VarStringOutput, "o")
Expand Down
9 changes: 8 additions & 1 deletion tools/goctl/api/dartgen/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"os"
"os/exec"
"strings"
)

const dartExec = "dart"

func formatDir(dir string) error {
func formatDir(dir string, formatArgs string) error {
ok, err := dirctoryExists(dir)
if err != nil {
return err
Expand All @@ -25,6 +26,12 @@ func formatDir(dir string) error {
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr

// Add additional format arguments to `dart format` command.
// Eg: `dart format --line-length=120`
if len(formatArgs) > 0 {
cmd.Args = append(cmd.Args, strings.Split(formatArgs, " ")...)
}

return cmd.Run()
}

Expand Down
8 changes: 7 additions & 1 deletion tools/goctl/api/dartgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
VarStringHostname string
// VarStringSchema defines the scheme.
VarStringScheme string
// VarFormatArgs defines the format arguments, eg: "--line-length=120"
VarStringFormatArgs string
)

// DartCommand create dart network request code
Expand All @@ -30,6 +32,7 @@ func DartCommand(_ *cobra.Command, _ []string) error {
isLegacy := VarStringLegacy
hostname := VarStringHostname
scheme := VarStringScheme
formatArgs := VarStringFormatArgs
if len(apiFile) == 0 {
return errors.New("missing -api")
}
Expand All @@ -44,6 +47,9 @@ func DartCommand(_ *cobra.Command, _ []string) error {
fmt.Println("you could use '-scheme' flag to specify your server scheme")
scheme = "http"
}
if len(formatArgs) == 0 {
fmt.Println(`you could use '-format-args "--line-length=120"' flag to specify the dart format arguments`)
}

api, err := parser.Parse(apiFile)
if err != nil {
Expand All @@ -62,7 +68,7 @@ func DartCommand(_ *cobra.Command, _ []string) error {
logx.Must(genData(dir+"data/", api, isLegacy))
logx.Must(genApi(dir+"api/", api, isLegacy))
logx.Must(genVars(dir+"vars/", isLegacy, scheme, hostname))
if err := formatDir(dir); err != nil {
if err := formatDir(dir, formatArgs); err != nil {
logx.Errorf("failed to format, %v", err)
}
return nil
Expand Down