Skip to content

Commit

Permalink
fix: don't prompt about studio after run (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
chase-crumbaugh authored Sep 12, 2024
1 parent dfbc935 commit 970f441
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"request": "launch",
"program": "main.go",
"cwd": "/tmp/studio-test-6840674974721771490",
"args": ["run", "--launch-studio"]
"args": ["run", "--watch"]
},
{
"name": "Debug CLI headless",
Expand Down
34 changes: 4 additions & 30 deletions cmd/quickstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ import (
"strings"

"github.com/pkg/browser"
"github.com/samber/lo"
"github.com/speakeasy-api/speakeasy/internal/charm/styles"
"github.com/speakeasy-api/speakeasy/internal/config"
"github.com/speakeasy-api/speakeasy/internal/env"
"github.com/speakeasy-api/speakeasy/internal/interactivity"
"github.com/speakeasy-api/speakeasy/internal/studio"
"github.com/speakeasy-api/speakeasy/internal/utils"
"golang.org/x/exp/maps"

"github.com/speakeasy-api/speakeasy/internal/charm/styles"
"github.com/speakeasy-api/speakeasy/internal/log"
"github.com/speakeasy-api/speakeasy/internal/model/flag"
"github.com/speakeasy-api/speakeasy/internal/studio"

"github.com/speakeasy-api/huh"
"github.com/speakeasy-api/speakeasy/internal/model"
Expand All @@ -31,7 +26,6 @@ import (
sdkGenConfig "github.com/speakeasy-api/sdk-gen-config"
"github.com/speakeasy-api/sdk-gen-config/workflow"
speakeasyErrors "github.com/speakeasy-api/speakeasy-core/errors"
"github.com/speakeasy-api/speakeasy-core/suggestions"
"github.com/speakeasy-api/speakeasy/internal/charm"
"github.com/speakeasy-api/speakeasy/internal/run"
"github.com/speakeasy-api/speakeasy/prompts"
Expand Down Expand Up @@ -367,28 +361,8 @@ func retryWithSampleSpec(ctx context.Context, workflowFile *workflow.Workflow, i
}

func shouldLaunchStudio(ctx context.Context, wf *run.Workflow, fromQuickstart bool) bool {
if len(wf.SourceResults) != 1 {
// Only one source at a time is supported in the studio at the moment
return false
}
sourceResult := maps.Values(wf.SourceResults)[0]

if !utils.IsInteractive() || env.IsGithubAction() {
return false
}

if sourceResult.LintResult == nil {
// No lint result indicates the spec wasn't even loaded successfully, the studio can't help with that
return false
}

// TODO: include more relevant diagnostics as we go!
numDiagnostics := lo.SumBy(maps.Values(sourceResult.Diagnosis), func(x []suggestions.Diagnostic) int {
return len(x)
})

if numDiagnostics == 0 {
// No interesting diagnostics to show in the studio
canLaunch, numDiagnostics := studio.CanLaunch(ctx, wf)
if !canLaunch {
return false
}

Expand Down
26 changes: 16 additions & 10 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type RunFlags struct {
Verbose bool `json:"verbose"`
RegistryTags []string `json:"registry-tags"`
SetVersion string `json:"set-version"`
LaunchStudio bool `json:"launch-studio"`
Watch bool `json:"watch"`
GitHub bool `json:"github"`
}

Expand Down Expand Up @@ -147,7 +147,8 @@ var runCmd = &model.ExecutableCommand[RunFlags]{
Description: "the manual version to apply to the generated SDK",
},
flag.BooleanFlag{
Name: "launch-studio",
Name: "watch",
Shorthand: "w",
Description: "launch the web studio for improving the quality of the generated SDK",
Required: false,
},
Expand Down Expand Up @@ -330,11 +331,7 @@ func runNonInteractive(ctx context.Context, flags RunFlags) error {

github.GenerateWorkflowSummary(ctx, workflow.RootStep)

if flags.LaunchStudio || shouldLaunchStudio(ctx, workflow, false) {
err = studio.LaunchStudio(ctx, workflow)
}

return err
return maybeLaunchStudio(ctx, workflow, flags)
}

func runInteractive(ctx context.Context, flags RunFlags) error {
Expand Down Expand Up @@ -398,9 +395,18 @@ func runInteractive(ctx context.Context, flags RunFlags) error {
workflow.PrintSuccessSummary(ctx)
}

if flags.LaunchStudio || shouldLaunchStudio(ctx, workflow, false) {
err = studio.LaunchStudio(ctx, workflow)
return maybeLaunchStudio(ctx, workflow, flags)
}

func maybeLaunchStudio(ctx context.Context, wf *run.Workflow, flags RunFlags) error {
canLaunch, numDiagnostics := studio.CanLaunch(ctx, wf)
if canLaunch && flags.Watch {
return studio.LaunchStudio(ctx, wf)
} else if numDiagnostics > 1 {
log.From(ctx).PrintfStyled(styles.Info, "\nWe've detected `%d` potential improvements for your SDK.\nGet automatic fixes in the Studio with `speakeasy run --watch`", numDiagnostics)
} else if numDiagnostics == 1 {
log.From(ctx).PrintfStyled(styles.Info, "\nWe've detected `1` potential improvement for your SDK.\nGet automatic fixes in the Studio with `speakeasy run --watch`")
}

return err
return nil
}
2 changes: 1 addition & 1 deletion internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (l Logger) Printf(format string, a ...any) {
}

func (l Logger) PrintfStyled(style lipgloss.Style, format string, a ...any) {
l.PrintlnUnstyled(style.Render(fmt.Sprintf(format, a...)))
l.Fprintln(l.writer, style.Render(fmt.Sprintf(format, a...)))
}

func (l Logger) Println(s string) {
Expand Down
2 changes: 1 addition & 1 deletion internal/run/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (w *Workflow) printTargetSuccessMessage(ctx context.Context) {
var additionalLines []string
for target, url := range w.SDKOverviewURLs {
link := links.Shorten(ctx, url)
additionalLines = append(additionalLines, styles.Success.Render(fmt.Sprintf("└─`%s` overview: %s", target, styles.Dimmed.Render(link))))
additionalLines = append(additionalLines, styles.Success.Render(fmt.Sprintf("└─`%s` overview: ", target))+styles.DimmedItalic.Render(link))
}

msg := fmt.Sprintf("%s\n%s\n", styles.Success.Render(heading), strings.Join(additionalLines, "\n"))
Expand Down
31 changes: 31 additions & 0 deletions internal/studio/launchStudio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/samber/lo"
"github.com/speakeasy-api/speakeasy-core/suggestions"
"github.com/speakeasy-api/speakeasy/internal/env"
"github.com/speakeasy-api/speakeasy/internal/utils"
"golang.org/x/exp/maps"
"net"
"net/http"
"os"
Expand All @@ -23,6 +28,32 @@ import (
"go.uber.org/zap"
)

// CanLaunch returns true if the studio can be launched, and the number of diagnostics
func CanLaunch(ctx context.Context, wf *run.Workflow) (bool, int) {
if len(wf.SourceResults) != 1 {
// Only one source at a time is supported in the studio at the moment
return false, 0
}

sourceResult := maps.Values(wf.SourceResults)[0]

if !utils.IsInteractive() || env.IsGithubAction() {
return false, 0
}

if sourceResult.LintResult == nil {
// No lint result indicates the spec wasn't even loaded successfully, the studio can't help with that
return false, 0
}

// TODO: include more relevant diagnostics as we go!
numDiagnostics := lo.SumBy(maps.Values(sourceResult.Diagnosis), func(x []suggestions.Diagnostic) int {
return len(x)
})

return numDiagnostics > 0, numDiagnostics
}

func LaunchStudio(ctx context.Context, workflow *run.Workflow) error {
secret, err := getOrCreateSecret()
if err != nil {
Expand Down

0 comments on commit 970f441

Please sign in to comment.