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

Remove need for -build-pkg by parsing the go.mod file #85

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -455,7 +453,6 @@ func (r *runCmd) do() error {
{
params := buildParams{
projectName: r.projectName.String(),
pkgName: r.buildPkg,
compiledOutputDir: r.outDir,
buildDir: r.outDir,
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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, " "))
}
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down