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

tiltfile+cli: embed api stub files and add tilt dump api-stubs #5574

Merged
merged 8 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
38 changes: 38 additions & 0 deletions internal/cli/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
Expand All @@ -14,6 +15,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/tiltfile"
"github.com/tilt-dev/tilt/pkg/model"
)

Expand All @@ -31,6 +33,7 @@ and may change frequently.
`,
}

result.AddCommand(newDumpApiDocsCmd())
result.AddCommand(newDumpWebviewCmd())
result.AddCommand(newDumpEngineCmd())
result.AddCommand(newDumpLogStoreCmd())
Expand All @@ -41,6 +44,22 @@ and may change frequently.
return result
}

func newDumpApiDocsCmd() *cobra.Command {
c := &apiDocsCmd{}
cmd := &cobra.Command{
Use: "api-docs",
Short: "dump the Tiltfile api documentation stub files",
Long: `Dumps the api documentation stub files to the provided directory.

The api stub files define the builtin functions, modules, and types used in Tiltfiles.
`,
Run: c.run,
Args: cobra.NoArgs,
}
cmd.Flags().StringVar(&c.dir, "dir", ".", "The directory to dump to")
return cmd
}

func newDumpWebviewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "webview",
Expand Down Expand Up @@ -244,6 +263,25 @@ func dumpLogStore(cmd *cobra.Command, args []string) {
}
}

type apiDocsCmd struct {
dir string
}

func (a *apiDocsCmd) run(cmd *cobra.Command, args []string) {
stat, err := os.Stat(a.dir)
if err != nil || !stat.IsDir() {
cmdFail(fmt.Errorf("Provided name %v doesn't exist or isn't a directory", a.dir))
}
err = tiltfile.DumpApiStubs(a.dir, func(path string, e error) {
if e == nil {
fmt.Printf("wrote %s\n", filepath.Join(a.dir, path))
}
})
if err != nil {
cmdFail(fmt.Errorf("dump api-docs: %v", err))
}
}

func dumpJSON(reader io.Reader) error {
result, err := decodeJSON(reader)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions internal/tiltfile/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tiltfile

import (
"embed"
"io/fs"
"os"
"path/filepath"
)

//go:embed api/*.py api/config/*.py api/os/*.py api/shlex/*.py api/sys/*.py api/v1alpha1/*.py
nicksieger marked this conversation as resolved.
Show resolved Hide resolved
var api embed.FS

func ApiStubs() fs.FS {
return api
}

func WalkApiStubs(fn fs.WalkDirFunc) error {
return fs.WalkDir(ApiStubs(), "api", fn)
}

func DumpApiStubs(dir string, callback func(string, error)) error {
return WalkApiStubs(func(path string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
var err error
dest := filepath.Join(dir, path)
if d.IsDir() {
err = os.MkdirAll(dest, 0755)
} else {
var bytes []byte
bytes, err = api.ReadFile(path)
if err != nil {
return err
}
err = os.WriteFile(dest, bytes, 0644)
}
callback(path, err)
return err
})
}
Loading