bldy.build/build@v0.0.0-20181002085557-d04b29acc6a7/skylark/files.go (about)

     1  package skylark
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"bldy.build/build/file"
     7  	"bldy.build/build/label"
     8  	"bldy.build/build/workspace"
     9  	"github.com/google/skylark"
    10  )
    11  
    12  // this is very busy
    13  func processFiles(ctx *context, ruleAttrs *skylark.Dict, kwargs []skylark.Tuple, ws workspace.Workspace, lbl label.Label) ([]string, error) {
    14  	files := []string{}
    15  	ctx.files = skylark.StringDict{}
    16  	err := WalkDict(ruleAttrs, func(kw skylark.Value, attr Attribute) error { // check the attributes
    17  		name := string(kw.(skylark.String))
    18  		arg := ctx.attrs[name]
    19  
    20  		switch x := attr.(type) {
    21  		case *labelAttr:
    22  			l, ok := arg.(label.Label)
    23  			if !ok {
    24  				return fmt.Errorf("attribute %q should be of type list consisting of strings", name)
    25  			}
    26  			if x.AllowFiles {
    27  				f := file.New(l, lbl, ws)
    28  				if f.Exists() {
    29  					files = append(files, f.Path())
    30  				}
    31  				ctx.files[name] = f
    32  			}
    33  		case *labelListAttr:
    34  			if x.AllowFiles {
    35  				lblList, ok := arg.(*skylark.List)
    36  				if !ok {
    37  					return fmt.Errorf("attribute %q should be of type list consisting of strings", name)
    38  				}
    39  				skyfiles := skylark.NewList([]skylark.Value{})
    40  
    41  				i := lblList.Iterate()
    42  				var p skylark.Value
    43  				for i.Next(&p) {
    44  					l, ok := p.(label.Label)
    45  					if !ok {
    46  						return fmt.Errorf("label list only allows strings not %Ts: %v", p.Type(), p)
    47  					}
    48  					f := file.New(l, lbl, ws)
    49  					if f.Exists() {
    50  						files = append(files, f.Path())
    51  					}
    52  
    53  					skyfiles.Append(f)
    54  				}
    55  				ctx.files[name] = skyfiles
    56  			}
    57  		}
    58  		return nil
    59  	})
    60  	return files, err
    61  }