github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/pkg/project/run.go (about)

     1  package project
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/tooploox/oya/pkg/oyafile"
     7  	"github.com/tooploox/oya/pkg/task"
     8  	"github.com/tooploox/oya/pkg/template"
     9  )
    10  
    11  func (p *Project) Run(workDir string, taskName task.Name, recurse, useChangeset bool,
    12  	args []string, scope template.Scope, stdout, stderr io.Writer) error {
    13  
    14  	values, err := p.values()
    15  	if err != nil {
    16  		return err
    17  	}
    18  	scope = scope.Merge(values)
    19  
    20  	targets, err := p.ListTargets(workDir, recurse, useChangeset)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	if len(targets) == 0 {
    26  		return nil
    27  	}
    28  
    29  	dependencies, err := p.Deps()
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	foundAtLeastOneTask := false
    35  	for _, o := range targets {
    36  		err = o.Build(dependencies)
    37  		if err != nil {
    38  			return err
    39  		}
    40  		found, err := o.RunTask(taskName, args, scope, stdout, stderr)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		if found {
    45  			foundAtLeastOneTask = found
    46  		}
    47  	}
    48  	if !foundAtLeastOneTask {
    49  		return ErrNoTask{
    50  			Task: taskName,
    51  		}
    52  	}
    53  	return nil
    54  }
    55  
    56  func (p *Project) ListTargets(workDir string, recurse, useChangeset bool) ([]*oyafile.Oyafile, error) {
    57  	if useChangeset {
    58  		changes, err := p.Changeset(workDir)
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  
    63  		if len(changes) == 0 {
    64  			return nil, nil
    65  		}
    66  
    67  		if !recurse {
    68  			return p.oneTargetIn(workDir)
    69  		} else {
    70  			return changes, nil
    71  		}
    72  	} else {
    73  		if !recurse {
    74  			return p.oneTargetIn(workDir)
    75  		} else {
    76  			return p.List(workDir)
    77  		}
    78  	}
    79  }
    80  
    81  func (p *Project) values() (template.Scope, error) {
    82  	o, found, err := p.rawOyafileIn(p.RootDir)
    83  	if err != nil {
    84  		return template.Scope{}, err
    85  	}
    86  	if !found {
    87  		return template.Scope{}, ErrNoOyafile{Path: p.RootDir}
    88  	}
    89  	project, _, err := o.Project()
    90  	if err != nil {
    91  		return template.Scope{}, err
    92  	}
    93  	return template.Scope{
    94  		"Project": project,
    95  	}, nil
    96  }
    97  
    98  func (p *Project) oneTargetIn(dir string) ([]*oyafile.Oyafile, error) {
    99  	o, found, err := p.oyafileIn(dir)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	if !found {
   104  		return nil, ErrNoOyafile{Path: dir}
   105  	}
   106  	return []*oyafile.Oyafile{o}, nil
   107  }