这是indexloc提供的服务,不要输入任何密码
Skip to content

Add support for --graph=file.html #329

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

Merged
merged 8 commits into from
Dec 17, 2021
Merged
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
25 changes: 2 additions & 23 deletions cli/internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

}
37 changes: 33 additions & 4 deletions cli/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strconv"
Expand All @@ -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"
)
Expand Down Expand Up @@ -613,9 +612,39 @@ func (c *RunCommand) Run(args []string) int {
Verbose: true,
DrawCycles: true,
}))
ext := filepath.Ext(runOptions.dotGraph)
if ext == ".html" {
f, err := os.Create(filepath.Join(cwd, runOptions.dotGraph))
if err != nil {
c.logError(c.Config.Logger, "", fmt.Errorf("error writing graph: %w", err))
return 1
}
defer f.Close()
f.WriteString(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Graph</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/viz.js@2.1.2-pre.1/viz.js"></script>
<script src="https://cdn.jsdelivr.net/npm/viz.js@2.1.2-pre.1/full.render.js"></script>
<script>`)
f.WriteString("const s = `" + graphString + "`.replace(/\\_\\_\\_ROOT\\_\\_\\_/g, \"Root\").replace(/\\[root\\]/g, \"\");new Viz().renderSVGElement(s).then(el => document.body.appendChild(el)).catch(e => console.error(e));")
f.WriteString(`
</script>
</body>
</html>`)
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" + path.Ext(runOptions.dotGraph)[1:], "-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 {
Expand Down Expand Up @@ -808,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
}

Expand Down
27 changes: 27 additions & 0 deletions cli/internal/util/browser/open.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
3 changes: 2 additions & 1 deletion docs/pages/docs/reference/command-line-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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-<timestamp>.jpg` is outputted.

If Graphviz is not installed, this command prints the dot graph to `stdout`.
Expand All @@ -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
```

<Callout>
Expand Down