@@ -12,6 +12,9 @@ import (
1212 "github.com/hephbuild/heph/utils/ads"
1313 "github.com/hephbuild/heph/utils/sets"
1414 "github.com/hephbuild/heph/worker2/poolwait"
15+ "os"
16+ "path/filepath"
17+ "strings"
1518)
1619
1720var errHasExprDep = errors .New ("has expr, bailing out" )
@@ -262,3 +265,64 @@ func GenerateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, t
262265
263266 return generateRRs (ctx , e .Graph , m , targs , opts , false )
264267}
268+
269+ func GenerateRRsFromVerb (ctx context.Context , cwd , verb , arg string , e * scheduler.Scheduler , opts targetrun.RequestOpts , plain , gen bool ) (targetrun.Requests , error ) {
270+ var pkgPath string
271+ if filepath .IsAbs (arg ) {
272+ pkgPath = arg
273+ } else {
274+ pkgPath = filepath .Join (cwd , arg )
275+ }
276+
277+ targ := ""
278+
279+ info , err := os .Stat (pkgPath )
280+ if err != nil {
281+ return nil , err
282+ }
283+ if ! info .IsDir () {
284+ targ = filepath .Base (pkgPath )
285+ pkgPath = filepath .Dir (pkgPath )
286+ }
287+
288+ pkg , err := filepath .Rel (e .Root .Root .Abs (), pkgPath )
289+ if err != nil {
290+ return nil , err
291+ }
292+ if strings .Contains (pkg , ".." ) {
293+ return nil , fmt .Errorf ("not in repo" )
294+ }
295+ if pkg == "." {
296+ pkg = ""
297+ }
298+
299+ matcher := specs.TargetAddr {
300+ Package : pkg ,
301+ Name : verb ,
302+ }
303+
304+ for {
305+ log .Debugf ("Attempting to find target %v, arg: %v " , matcher , targ )
306+
307+ rrs , err := GenerateRRs (ctx , e , matcher , []string {targ }, opts , plain , gen )
308+ if err != nil {
309+ var nferr specs.TargetNotFoundErr
310+ if errors .As (err , & nferr ) && nferr .String == matcher .String () {
311+ if matcher .Package == "" {
312+ return nil , fmt .Errorf ("not target found for to %v in %v" , verb , pkgPath )
313+ }
314+
315+ targ = filepath .Join (filepath .Base (matcher .Package ), targ )
316+ matcher .Package = filepath .Dir (matcher .Package )
317+ if matcher .Package == "." {
318+ matcher .Package = ""
319+ }
320+ continue
321+ }
322+
323+ return nil , err
324+ }
325+
326+ return rrs , nil
327+ }
328+ }
0 commit comments