Skip to content

Commit

Permalink
only list files
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Dec 14, 2023
1 parent 8e8f3bf commit ca75e94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
27 changes: 22 additions & 5 deletions compiler/cmd/coroc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime/debug"

Expand All @@ -17,9 +19,20 @@ USAGE:
OPTIONS:
-h, --help Show this help information
-l, --list List all files that would be compiled
-v, --version Show the compiler version
`

var (
showVersion bool
onlyListFiles bool
)

func boolFlag(ptr *bool, short, long string) {
flag.BoolVar(ptr, short, false, "")
flag.BoolVar(ptr, long, false, "")
}

func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
Expand All @@ -30,10 +43,8 @@ func main() {
func run() error {
flag.Usage = func() { println(usage[1:]) }

var showVersion bool
flag.BoolVar(&showVersion, "v", false, "")
flag.BoolVar(&showVersion, "version", false, "")

boolFlag(&showVersion, "v", "version")
boolFlag(&onlyListFiles, "l", "list")
flag.Parse()

if showVersion {
Expand All @@ -55,7 +66,13 @@ func run() error {
}
}

return compiler.Compile(path)
if onlyListFiles {
log.SetOutput(io.Discard)
}

return compiler.Compile(path,
compiler.OnlyListFiles(onlyListFiles),
)
}

func version() (version string) {
Expand Down
17 changes: 14 additions & 3 deletions compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ func Compile(path string, options ...Option) error {
return c.compile(path)
}

// Option configures the compiler.
type Option func(*compiler)

type compiler struct {
onlyListFiles bool

prog *ssa.Program
generics map[*ssa.Function][]*ssa.Function
coroutinePkg *packages.Package
Expand Down Expand Up @@ -183,6 +182,17 @@ func (c *compiler) compile(path string) error {
pkgColors[fn] = color
}

if c.onlyListFiles {
cwd, _ := os.Getwd()
for pkg := range colorsByPkg {
for _, filePath := range pkg.GoFiles {
relPath, _ := filepath.Rel(cwd, filePath)
fmt.Println(relPath)
}
}
return nil
}

// Before mutating packages, we need to ensure that packages exist in a
// location where mutations can be made safely (without affecting other
// builds).
Expand Down Expand Up @@ -219,6 +229,7 @@ func (c *compiler) compile(path string) error {
// Reject packages outside ./vendor.
return fmt.Errorf("cannot mutate package %s (%s) safely. Please vendor dependencies: go mod vendor", p.PkgPath, dir)
}

if len(needVendoring) > 0 {
log.Printf("vendoring GOROOT packages")
newRoot := filepath.Join(moduleDir, "goroot")
Expand Down
10 changes: 10 additions & 0 deletions compiler/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package compiler

// Option configures the compiler.
type Option func(*compiler)

func OnlyListFiles(enabled bool) Option {
return func(c *compiler) {
c.onlyListFiles = enabled
}
}

0 comments on commit ca75e94

Please sign in to comment.