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

feat(cli): update the graph arg behavior #1353

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 5 commits into from
Jun 21, 2022
Merged

feat(cli): update the graph arg behavior #1353

merged 5 commits into from
Jun 21, 2022

Conversation

tknickman
Copy link
Member

@tknickman tknickman commented Jun 8, 2022

Updates the behavior of the --graph CLI flag and fixes a few bugs.

This command will generate an svg, png, jpg, pdf, json, html, or other supported output formats of the current task graph.
The output file format defaults to jpg, but can be controlled by specifying the filename's extension.

If Graphviz is not installed, or no filename is provided, this command prints the dot graph to stdout

This PR also:

  1. Updates docs to reflect the current state of the --graph CLI flag
  2. Refactors the graph visualization code out of run.go
  3. Cleans up the file name of colors_cache (follow up from fix(cli): add colors back for cache hit log line #1346)

Fixes #1286

@vercel
Copy link

vercel bot commented Jun 8, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
turbo-site ⬜️ Ignored (Inspect) Jun 21, 2022 at 6:24PM (UTC)

Copy link
Contributor

@gsoltis gsoltis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, I think we need to do a little update on text displayed to users.

@tknickman
Copy link
Member Author

@gsoltis updated, thanks for the review!

@gsoltis gsoltis added the pr: fixship A PR which is approved with notes and does not require re-review. label Jun 21, 2022
Copy link
Contributor

@gsoltis gsoltis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one tiny nit, then ship it!

@tknickman tknickman added pr: automerge Kodiak will merge these automatically after checks pass and removed pr: fixship A PR which is approved with notes and does not require re-review. labels Jun 21, 2022
@kodiakhq kodiakhq bot merged commit 793dca0 into main Jun 21, 2022
@tknickman tknickman deleted the graph_cli_arg branch June 21, 2022 18:32
}

func (d *graphValue) Set(value string) error {
if value == _graphNoValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like an unfortunate hack because of NoOptDefVal appearing as a string in the usage text. Is it possible that we can use pflag.Flagset.Changed instead of this? (I'd much prefer for this to be some sort of sigil value that a user is incapable of inputting.)

(I haven't yet looked into how pflag works so I don't know what is available when.)

Comment on lines -908 to -963
func (r *run) generateDotGraph(taskGraph *dag.AcyclicGraph, outputFilename fs.AbsolutePath) error {
graphString := string(taskGraph.Dot(&dag.DotOpts{
Verbose: true,
DrawCycles: true,
}))
ext := outputFilename.Ext()
if ext == ".html" {
f, err := outputFilename.Create()
if err != nil {
return fmt.Errorf("error writing graph: %w", err)
}
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>`)
r.ui.Output("")
r.ui.Output(fmt.Sprintf("✔ Generated task graph in %s", ui.Bold(outputFilename.ToString())))
if ui.IsTTY {
if err := browser.OpenBrowser(outputFilename.ToString()); err != nil {
r.ui.Warn(color.New(color.FgYellow, color.Bold, color.ReverseVideo).Sprintf("failed to open browser. Please navigate to file://%v", filepath.ToSlash(outputFilename.ToString())))
}
}
return nil
}
hasDot := hasGraphViz()
if hasDot {
dotArgs := []string{"-T" + ext[1:], "-o", outputFilename.ToString()}
cmd := exec.Command("dot", dotArgs...)
cmd.Stdin = strings.NewReader(graphString)
if err := cmd.Run(); err != nil {
return fmt.Errorf("could not generate task graphfile %v: %w", outputFilename, err)
} else {
r.ui.Output("")
r.ui.Output(fmt.Sprintf("✔ Generated task graph in %s", ui.Bold(outputFilename.ToString())))
}
} else {
r.ui.Output("")
r.ui.Warn(color.New(color.FgYellow, color.Bold, color.ReverseVideo).Sprint(" WARNING ") + color.YellowString(" `turbo` uses Graphviz to generate an image of your\ngraph, but Graphviz isn't installed on this machine.\n\nYou can download Graphviz from https://graphviz.org/download.\n\nIn the meantime, you can use this string output with an\nonline Dot graph viewer."))
r.ui.Output("")
r.ui.Output(graphString)
}
return nil
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 The more things that get removed from run the better our lives will be. This is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: automerge Kodiak will merge these automatically after checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make --graph option print to stdout by default
3 participants