// Concatenates all the files from the input channel // and passes them to output channel with the given name. func Concat(c *slurp.C, name string) slurp.Stage { return func(files <-chan slurp.File, out chan<- slurp.File) { var ( size int64 bigfile = new(bytes.Buffer) ) for f := range files { c.Infof("Adding %s to %s", f.Path, name) n, err := bigfile.ReadFrom(f) if err != nil { c.Error(err) return } bigfile.WriteRune('\n') size += n + 1 f.Close() } fi := slurp.FileInfo{} fi.SetSize(size) fi.SetName(name) out <- slurp.File{ Reader: bigfile, Dir: "", Path: name, FileInfo: fi, } } }
// A build stage creates a new build and adds all the files coming through the channel to // the Build and returns the result of Build as a File on the output channel. func Build(c *slurp.C, config Config) slurp.Stage { return func(in <-chan slurp.File, out chan<- slurp.File) { b := cache{config, make(map[string]*bytes.Buffer)} for file := range in { path, _ := filepath.Rel(file.Dir, file.Path) path = filepath.ToSlash(path) c.Infof("Adding %s", path) buff := new(bytes.Buffer) _, err := buff.ReadFrom(file) if err != nil { c.Error(err) } b.Files[path] = buff file.Close() //Close files AFTER we have build our package. } buff := new(bytes.Buffer) err := cacheTemplate.Execute(buff, b) if err != nil { c.Error(err) return } sf := slurp.File{ Reader: buff, Path: b.Name, } sf.FileInfo.SetName(b.Name) sf.FileInfo.SetSize(int64(buff.Len())) out <- sf } }
//For The Glory of Debugging. func List(c *slurp.C) slurp.Stage { return func(files <-chan slurp.File, out chan<- slurp.File) { for f := range files { s, err := f.Stat() if err != nil { c.Error("Can't get File Stat name.") } else { c.Infof("slurp.File: %+v Name: %s", f, s.Name()) } out <- f } } }