diff --git a/README.md b/README.md index 372e1d8..2a18565 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,7 @@ syntax. Pushup treats projects as their own self-contained Go module. The build process assumes this is the case by default. But it is possible to include a Pushup project as part of a parent Go module. See the the `-module` option to -`pushup new`, and the `-build-pkg` option to the `pushup run` command. +`pushup new`. ## Project directory structure diff --git a/example/README.md b/example/README.md index f7382e1..20cfd12 100644 --- a/example/README.md +++ b/example/README.md @@ -6,8 +6,5 @@ Pushup features and models how to write Pushup syntax. To run it, install the main Pushup app, cd to this directory, and type: ``` -pushup run -build-pkg github.com/adhocteam/pushup/example/build +pushup run ``` - -(Note that the need to type `-build-pkg ...` is temporary and will go -away soon.) diff --git a/go.mod b/go.mod index 54982a5..27b6e8e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/fsnotify/fsnotify v1.5.4 github.com/google/go-cmp v0.5.8 + golang.org/x/mod v0.7.0 golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f ) diff --git a/go.sum b/go.sum index 4c381d8..bd6188f 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwV github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 h1:8NSylCMxLW4JvserAndSgFL7aPli6A68yf0bYFTcWCM= golang.org/x/net v0.0.0-20220706163947-c90051bbdb60/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8= diff --git a/main.go b/main.go index 1eac358..a3eb626 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ import ( "unicode/utf8" "github.com/fsnotify/fsnotify" + "golang.org/x/mod/modfile" "golang.org/x/net/html" "golang.org/x/net/html/atom" "golang.org/x/sync/errgroup" @@ -240,7 +241,6 @@ func initVcs(projectDir string, vcs vcs) error { type buildCmd struct { projectName *regexString projectDir string - buildPkg string applyOptimizations bool parseOnly bool codeGenOnly bool @@ -258,7 +258,6 @@ type buildCmd struct { func setBuildFlags(flags *flag.FlagSet, b *buildCmd) { b.projectName = newRegexString(`^\w+`, "myproject") flags.Var(b.projectName, "project", "name of Pushup project") - flags.StringVar(&b.buildPkg, "build-pkg", "example/myproject/build", "name of package of compiled Pushup app") flags.BoolVar(&b.applyOptimizations, "O", false, "apply simple optimizations to the parse tree") flags.BoolVar(&b.parseOnly, "parse-only", false, "exit after dumping parse result") flags.BoolVar(&b.codeGenOnly, "codegen-only", false, "codegen only, don't compile") @@ -339,7 +338,6 @@ func (b *buildCmd) do() error { { params := buildParams{ projectName: b.projectName.String(), - pkgName: b.buildPkg, compiledOutputDir: b.outDir, buildDir: b.outDir, outFile: b.outFile, @@ -455,7 +453,6 @@ func (r *runCmd) do() error { { params := buildParams{ projectName: r.projectName.String(), - pkgName: r.buildPkg, compiledOutputDir: r.outDir, buildDir: r.outDir, } @@ -2160,7 +2157,6 @@ func copyFileFS(fsys fs.FS, dest string, src string) error { type buildParams struct { projectName string - pkgName string // path to directory with the compiled Pushup project code compiledOutputDir string buildDir string @@ -2171,6 +2167,19 @@ type buildParams struct { // buildProject builds the Go program made up of the user's compiled .up // files and .go code, as well as Pushup's library APIs. func buildProject(_ context.Context, b buildParams) error { + var pkgName string + { + goModContents, err := os.ReadFile("go.mod") + if err != nil { + return fmt.Errorf("could not read go.mod: %w", err) + } + f, err := modfile.Parse("go.mod", goModContents, nil) + if err != nil { + return fmt.Errorf("parsing go.mod file: %w", err) + } + pkgName = f.Module.Mod.Path + "/build" + } + mainExeDir := filepath.Join(b.compiledOutputDir, "cmd", b.projectName) if err := os.MkdirAll(mainExeDir, 0755); err != nil { return fmt.Errorf("making directory for command: %w", err) @@ -2181,7 +2190,7 @@ func buildProject(_ context.Context, b buildParams) error { if err != nil { return fmt.Errorf("creating main.go: %w", err) } - if err := t.Execute(f, map[string]any{"ProjectPkg": b.pkgName}); err != nil { + if err := t.Execute(f, map[string]any{"ProjectPkg": pkgName}); err != nil { return fmt.Errorf("executing main.go template: %w", err) } f.Close() @@ -2191,7 +2200,7 @@ func buildProject(_ context.Context, b buildParams) error { b.outFile = filepath.Join(b.buildDir, "bin", b.projectName) } - args := []string{"build", "-o", b.outFile, filepath.Join(b.pkgName, "cmd", b.projectName)} + args := []string{"build", "-o", b.outFile, filepath.Join(pkgName, "cmd", b.projectName)} if b.verbose { fmt.Printf("build command: go %s\n", strings.Join(args, " ")) } diff --git a/main_test.go b/main_test.go index 3a91b24..1d9057f 100644 --- a/main_test.go +++ b/main_test.go @@ -164,7 +164,7 @@ func TestPushup(t *testing.T) { var cmd *exec.Cmd g.Go(func() error { - cmd = exec.Command(pushup, "run", "-build-pkg", "github.com/adhocteam/pushup/build", "-page", pushupFile, "-unix-socket", socketPath) + cmd = exec.Command(pushup, "run", "-page", pushupFile, "-unix-socket", socketPath) sysProcAttr(cmd) var err error