Skip to content

Commit

Permalink
WIP verb
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Apr 28, 2024
1 parent 57c1fdc commit 15f5953
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
64 changes: 64 additions & 0 deletions bootstrap/rrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/hephbuild/heph/utils/ads"
"github.com/hephbuild/heph/utils/sets"
"github.com/hephbuild/heph/worker2/poolwait"
"os"
"path/filepath"
"strings"
)

var errHasExprDep = errors.New("has expr, bailing out")
Expand Down Expand Up @@ -262,3 +265,64 @@ func GenerateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, t

return generateRRs(ctx, e.Graph, m, targs, opts, false)
}

func GenerateRRsFromVerb(ctx context.Context, cwd, verb, arg string, e *scheduler.Scheduler, opts targetrun.RequestOpts, plain, gen bool) (targetrun.Requests, error) {
var pkgPath string
if filepath.IsAbs(arg) {
pkgPath = arg
} else {
pkgPath = filepath.Join(cwd, arg)
}

targ := ""

info, err := os.Stat(pkgPath)
if err != nil {
return nil, err
}
if !info.IsDir() {
targ = filepath.Base(pkgPath)
pkgPath = filepath.Dir(pkgPath)
}

pkg, err := filepath.Rel(e.Root.Root.Abs(), pkgPath)
if err != nil {
return nil, err
}
if strings.Contains(pkg, "..") {
return nil, fmt.Errorf("not in repo")
}
if pkg == "." {
pkg = ""
}

matcher := specs.TargetAddr{
Package: pkg,
Name: verb,
}

for {
log.Debugf("Attempting to find target %v, arg: %v ", matcher, targ)

rrs, err := GenerateRRs(ctx, e, matcher, []string{targ}, opts, plain, gen)
if err != nil {
var nferr specs.TargetNotFoundErr
if errors.As(err, &nferr) && nferr.String == matcher.String() {
if matcher.Package == "" {
return nil, fmt.Errorf("not target found for to %v in %v", verb, pkgPath)
}

targ = filepath.Join(filepath.Base(matcher.Package), targ)
matcher.Package = filepath.Dir(matcher.Package)
if matcher.Package == "." {
matcher.Package = ""
}
continue
}

return nil, err
}

return rrs, nil
}
}
37 changes: 37 additions & 0 deletions cmd/heph/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ var rootCmd = &cobra.Command{
Version: utils.Version,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ArbitraryArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return cmd.PersistentPreRunE(cmd, args)
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
lvl, err := log.ParseLevel(*logLevel)
if err != nil {
Expand Down Expand Up @@ -186,6 +190,39 @@ var rootCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

if len(args) > 0 {
verb := args[0]
arg := ""
if len(args) > 1 {
arg = args[1]
}

bs, err := schedulerInit(ctx, func(bootstrap.BaseBootstrap) error {
return bootstrap.BlockReadStdin(args)
})
if err != nil {
return err
}

rrs, err := bootstrap.GenerateRRsFromVerb(ctx, bs.Cwd, verb, arg, bs.Scheduler, getRROpts(), *plain, !*noGen)
if err != nil {
return err
}

for _, req := range rrs {
log.Info(req.Target.Addr, req.Args)
}

err = bootstrap.Run(ctx, bs.Scheduler, rrs, getRunOpts(), true)
if err != nil {
return err
}

return nil
}

err := cmd.Help()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion x/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

target(
name = "noop",
run = "echo ran",
run = "echo $@; echo ran",
cache = False,
)

Expand Down

0 comments on commit 15f5953

Please sign in to comment.