Skip to content

Commit

Permalink
Expose JSON codegen to external packages (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael authored Aug 18, 2023
1 parent 71b0624 commit 2bbb578
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
12 changes: 7 additions & 5 deletions cmd/mdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"path/filepath"
"strings"

"goa.design/goa/v3/codegen"
goacodegen "goa.design/goa/v3/codegen"

"goa.design/model/codegen"
"goa.design/model/mdl"
model "goa.design/model/pkg"
)
Expand All @@ -24,7 +26,7 @@ func main() {
out = genset.String("out", "design.json", "set path to generated JSON representation")

svrset = flag.NewFlagSet("serve", flag.ExitOnError)
dir = svrset.String("dir", codegen.Gendir, "set output directory used by editor to save SVG files")
dir = svrset.String("dir", goacodegen.Gendir, "set output directory used by editor to save SVG files")
port = svrset.Int("port", 8080, "set local HTTP port used to serve diagram editor")

devmode = os.Getenv("DEVMODE") == "1"
Expand Down Expand Up @@ -81,7 +83,7 @@ func main() {
fail(`missing PACKAGE argument, use "--help" for usage`)
}
var b []byte
b, err = gen(pkg, *debug)
b, err = codegen.JSON(pkg, *debug)
if err == nil {
err = os.WriteFile(*out, b, 0644)
}
Expand All @@ -108,7 +110,7 @@ func main() {

func serve(out, pkg string, port int, devmode, debug bool) error {
// Retrieve initial design and create server.
b, err := gen(pkg, debug)
b, err := codegen.JSON(pkg, debug)
if err != nil {
return err
}
Expand All @@ -120,7 +122,7 @@ func serve(out, pkg string, port int, devmode, debug bool) error {

// Update server whenever design changes on disk.
err = watch(pkg, func() {
b, err := gen(pkg, debug)
b, err := codegen.JSON(pkg, debug)
if err != nil {
fmt.Println("error parsing DSL:\n" + err.Error())
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/mdl/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func (s *Server) Serve(outDir string, devmode bool, port int) error {
http.Handle("/", http.FileServer(FS(false)))
}

http.HandleFunc("/data/model.json", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/data/model.json", func(w http.ResponseWriter, _ *http.Request) {
s.lock.Lock()
defer s.lock.Unlock()
_, _ = w.Write(s.design)
})

http.HandleFunc("/data/layout.json", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/data/layout.json", func(w http.ResponseWriter, _ *http.Request) {
s.lock.Lock()
defer s.lock.Unlock()

Expand Down
4 changes: 3 additions & 1 deletion cmd/mdl/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/jaschaephraim/lrserver"
"golang.org/x/tools/go/packages"

"goa.design/model/codegen"
)

// watch implements functionality to listen to changes in the model files
Expand Down Expand Up @@ -50,7 +52,7 @@ func watch(pkg string, reload func()) error {
for {
select {
case ev := <-watcher.Events:
if strings.HasPrefix(filepath.Base(ev.Name), tmpDirPrefix) {
if strings.HasPrefix(filepath.Base(ev.Name), codegen.TmpDirPrefix) {
continue
}

Expand Down
11 changes: 7 additions & 4 deletions cmd/mdl/gen.go → codegen/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package codegen

import (
"fmt"
Expand All @@ -10,9 +10,12 @@ import (
"golang.org/x/tools/go/packages"
)

const tmpDirPrefix = "mdl--"
// TmpDirPrefix is the prefix used to create temporary directories.
const TmpDirPrefix = "mdl--"

func gen(pkg string, debug bool) ([]byte, error) {
// JSON generates a JSON representation of the model described in pkg.
// pkg must be a valid Go package import path.
func JSON(pkg string, debug bool) ([]byte, error) {
// Validate package import path
if _, err := packages.Load(&packages.Config{Mode: packages.NeedName}, pkg); err != nil {
return nil, err
Expand All @@ -23,7 +26,7 @@ func gen(pkg string, debug bool) ([]byte, error) {
if err != nil {
cwd = "."
}
tmpDir, err := os.MkdirTemp(cwd, tmpDirPrefix)
tmpDir, err := os.MkdirTemp(cwd, TmpDirPrefix)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2bbb578

Please sign in to comment.