From 54b69ca16e2b344adc02908c05e647dc007d3f5e Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 16 Dec 2021 16:43:29 -0500 Subject: [PATCH 1/6] Add support for `--graph=file.html` --- cli/internal/run/run.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 2439f7b4c963d..b28b7bebe2f6c 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -624,9 +624,32 @@ func (c *RunCommand) Run(args []string) int { Verbose: true, DrawCycles: true, })) - hasDot := hasGraphViz() + ext := path.Ext(runOptions.dotGraph)[1:] + if ext == ".html" { + f, err := os.Create(runOptions.dotGraph) + if err != nil { + log.Fatal(err) + } + f.WriteString(` + + + + Graph + + + + + + + `) + f.Close() + } + hasDot := hasGraphViz() if hasDot { - dotArgs := []string{"-T" + path.Ext(runOptions.dotGraph)[1:], "-o", runOptions.dotGraph} + dotArgs := []string{"-T" + ext, "-o", runOptions.dotGraph} cmd := exec.Command("dot", dotArgs...) cmd.Stdin = strings.NewReader(graphString) if err := cmd.Run(); err != nil { From 13294d805980870f5801bda529df2d99b46f7a66 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Fri, 17 Dec 2021 00:16:53 -0500 Subject: [PATCH 2/6] Fix formatting --- cli/internal/run/run.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 1f6c2b4bdfccb..2aad506d0a43c 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -612,13 +612,13 @@ func (c *RunCommand) Run(args []string) int { Verbose: true, DrawCycles: true, })) - ext := path.Ext(runOptions.dotGraph)[1:] - if ext == ".html" { - f, err := os.Create(runOptions.dotGraph) - if err != nil { - log.Fatal(err) - } - f.WriteString(` + ext := path.Ext(runOptions.dotGraph)[1:] + if ext == ".html" { + f, err := os.Create(runOptions.dotGraph) + if err != nil { + log.Fatal(err) + } + f.WriteString(` @@ -628,14 +628,14 @@ func (c *RunCommand) Run(args []string) int { `) - f.Close() - } - hasDot := hasGraphViz() + f.Close() + } + hasDot := hasGraphViz() if hasDot { dotArgs := []string{"-T" + ext, "-o", runOptions.dotGraph} cmd := exec.Command("dot", dotArgs...) From e58de98b376b14e41f219add6e166ce015184062 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Fri, 17 Dec 2021 08:35:04 -0500 Subject: [PATCH 3/6] Tweak html graph and automatically open in browser if tty --- cli/internal/login/login.go | 25 ++----------------------- cli/internal/run/run.go | 25 ++++++++++++++++--------- cli/internal/util/browser/open.go | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 32 deletions(-) create mode 100644 cli/internal/util/browser/open.go diff --git a/cli/internal/login/login.go b/cli/internal/login/login.go index d0a24067e0eae..65438cd67a267 100644 --- a/cli/internal/login/login.go +++ b/cli/internal/login/login.go @@ -3,16 +3,14 @@ package login import ( "context" "fmt" - "log" "net/http" "net/url" "os" - "os/exec" - "runtime" "strings" "turbo/internal/config" "turbo/internal/ui" "turbo/internal/util" + "turbo/internal/util/browser" "github.com/fatih/color" "github.com/hashicorp/go-hclog" @@ -53,7 +51,7 @@ func (c *LoginCommand) Run(args []string) int { loginUrl := fmt.Sprintf("%v/turborepo/token?redirect_uri=%v", c.Config.LoginUrl, redirectUrl) c.Ui.Info(util.Sprintf(">>> Opening browser to %v", c.Config.LoginUrl)) s := ui.NewSpinner(os.Stdout) - openbrowser(loginUrl) + browser.OpenBrowser(loginUrl) s.Start("Waiting for your authorization...") var query url.Values @@ -127,22 +125,3 @@ func (c *LoginCommand) logFatal(log hclog.Logger, prefix string, err error) { c.Ui.Error(fmt.Sprintf("%s%s%s", ui.ERROR_PREFIX, prefix, color.RedString(" %v", err))) os.Exit(1) } - -func openbrowser(url string) { - var err error - - switch runtime.GOOS { - case "linux": - err = exec.Command("xdg-open", url).Start() - case "windows": - err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() - case "darwin": - err = exec.Command("open", url).Start() - default: - err = fmt.Errorf("unsupported platform") - } - if err != nil { - log.Fatal(err) - } - -} diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 2aad506d0a43c..30286446fdab5 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -8,7 +8,6 @@ import ( "log" "os" "os/exec" - "path" "path/filepath" "sort" "strconv" @@ -24,13 +23,13 @@ import ( "turbo/internal/scm" "turbo/internal/ui" "turbo/internal/util" + "turbo/internal/util/browser" "github.com/pyr-sh/dag" "github.com/fatih/color" glob "github.com/gobwas/glob" "github.com/hashicorp/go-hclog" - "github.com/mattn/go-isatty" "github.com/mitchellh/cli" "github.com/pkg/errors" ) @@ -612,12 +611,15 @@ func (c *RunCommand) Run(args []string) int { Verbose: true, DrawCycles: true, })) - ext := path.Ext(runOptions.dotGraph)[1:] + ext := filepath.Ext(runOptions.dotGraph) + log.Println(ext) if ext == ".html" { - f, err := os.Create(runOptions.dotGraph) + f, err := os.Create(filepath.Join(cwd, runOptions.dotGraph)) if err != nil { - log.Fatal(err) + c.logError(c.Config.Logger, "", fmt.Errorf("error writing graph: %w", err)) + return 1 } + defer f.Close() f.WriteString(` @@ -628,16 +630,21 @@ func (c *RunCommand) Run(args []string) int { `) - f.Close() + c.Ui.Output("") + c.Ui.Output(fmt.Sprintf("✔ Generated task graph in %s", ui.Bold(runOptions.dotGraph))) + if ui.IsTTY { + browser.OpenBrowser(filepath.Join(cwd, runOptions.dotGraph)) + } + return 0 } hasDot := hasGraphViz() if hasDot { - dotArgs := []string{"-T" + ext, "-o", runOptions.dotGraph} + dotArgs := []string{"-T" + ext[1:], "-o", runOptions.dotGraph} cmd := exec.Command("dot", dotArgs...) cmd.Stdin = strings.NewReader(graphString) if err := cmd.Run(); err != nil { @@ -830,7 +837,7 @@ func parseRunArgs(args []string, cwd string) (*RunOptions, error) { } // Force streaming output in CI/CD non-interactive mode - if !isatty.IsTerminal(os.Stdout.Fd()) || os.Getenv("CI") != "" { + if !ui.IsTTY || ui.IsCI { runOptions.stream = true } diff --git a/cli/internal/util/browser/open.go b/cli/internal/util/browser/open.go new file mode 100644 index 0000000000000..291ec30f55c28 --- /dev/null +++ b/cli/internal/util/browser/open.go @@ -0,0 +1,27 @@ +package browser + +import ( + "fmt" + "log" + "os/exec" + "runtime" +) + +func OpenBrowser(url string) { + var err error + + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + if err != nil { + log.Fatal(err) + } + +} From 42583bd246914a5124e2a8356954be5a40cf1f4a Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Dec 2021 15:14:01 -0500 Subject: [PATCH 4/6] Remove println --- cli/internal/run/run.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 1001cb81372b5..d612c424783df 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -613,7 +613,6 @@ func (c *RunCommand) Run(args []string) int { DrawCycles: true, })) ext := filepath.Ext(runOptions.dotGraph) - log.Println(ext) if ext == ".html" { f, err := os.Create(filepath.Join(cwd, runOptions.dotGraph)) if err != nil { From ecb8b999e20663ad780e25f45f24dc0978d897a9 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Dec 2021 15:14:11 -0500 Subject: [PATCH 5/6] Update docs --- docs/pages/docs/reference/command-line-reference.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/docs/reference/command-line-reference.mdx b/docs/pages/docs/reference/command-line-reference.mdx index 0d555b977bce1..8135a77dab25d 100644 --- a/docs/pages/docs/reference/command-line-reference.mdx +++ b/docs/pages/docs/reference/command-line-reference.mdx @@ -93,6 +93,7 @@ turbo run build test lint --graph=my-graph.svg turbo run build test lint --graph=my-json-graph.json turbo run build test lint --graph=my-graph.pdf turbo run build test lint --graph=my-graph.png +turbo run build test lint --graph=my-graph.html ``` From ac2e0c7399c8e89d040f08cbfc071455918202cf Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Dec 2021 15:15:35 -0500 Subject: [PATCH 6/6] Add missing html option --- docs/pages/docs/reference/command-line-reference.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pages/docs/reference/command-line-reference.mdx b/docs/pages/docs/reference/command-line-reference.mdx index 8135a77dab25d..5e5be2cc7326d 100644 --- a/docs/pages/docs/reference/command-line-reference.mdx +++ b/docs/pages/docs/reference/command-line-reference.mdx @@ -82,7 +82,7 @@ Let's say you have packages A, B, C, and D where A depends on B and C depends on `type: string` -If [Graphviz](https://graphviz.org) is installed, this command will generate an svg, png, jpg, pdf, json, or [other supported output formats](https://graphviz.org/doc/info/output.html) of the current task graph. +If [Graphviz](https://graphviz.org) is installed, this command will generate an svg, png, jpg, pdf, json, html, or [other supported output formats](https://graphviz.org/doc/info/output.html) of the current task graph. The output file format can be controlled with the filename's extension if it is specified. If a filename is not specified, `graph-.jpg` is outputted. If Graphviz is not installed, this command prints the dot graph to `stdout`.