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

WIP verb #32

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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
Loading