github.com/vulppine/fotoDen@v0.3.0/tool/build.go (about)

     1  package tool
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  // BuildFile represents a fotoDen build file.
    12  type BuildFile struct {
    13  	Name     string   `yaml:"name"`
    14  	Dir      string   `yaml:"dir"`
    15  	Desc     string   `yaml:"desc"`
    16  	Type     string   `yaml:"type"`
    17  	Thumb    string   `yaml:"thumb"`
    18  	Static   bool     `yaml:"static"`
    19  	ImageDir string   `yaml:"imageDir"`
    20  	Images   []string `yaml:"images,flow"`
    21  	Options  struct {
    22  		Copy     bool `yaml:"copy"`
    23  		Sort     bool `yaml:"sort"`
    24  		Meta     bool `yaml:"metadata"`
    25  		Gensizes bool `yaml:"generateSizes"`
    26  	} `yaml:"imageOptions,flow"`
    27  	Subfolders []*BuildFile `yaml:"subfolders,flow"`
    28  }
    29  
    30  // OpenYAML opens a build file into a new BuildFile object.
    31  func (b *BuildFile) OpenBuildYAML(file string) error {
    32  	f, err := ioutil.ReadFile(file)
    33  	if checkError(err) {
    34  		return err
    35  	}
    36  
    37  	err = yaml.Unmarshal(f, &b)
    38  	if checkError(err) {
    39  		return err
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  // BuildFromYAML is the entry point to the fotoDen
    46  // website build system. It takes a YAML file,
    47  // with the correct structure, and creates a website
    48  // in the given folder.
    49  func (b *BuildFile) Build(folder string) error {
    50  	verbose(fmt.Sprint(b))
    51  	switch b.Type {
    52  	case "folder":
    53  		genopts := GeneratorOptions{
    54  			Static: b.Static,
    55  		}
    56  		if b.Dir == "" {
    57  			b.Dir = b.Name
    58  		}
    59  		err := GenerateFolder(
    60  			FolderMeta{
    61  				Name: b.Name,
    62  				Desc: b.Desc,
    63  			},
    64  			filepath.Join(folder, b.Dir),
    65  			genopts,
    66  		)
    67  		if checkError(err) {
    68  			return err
    69  		}
    70  	case "album":
    71  		genopts := GeneratorOptions{
    72  			ImageGen: true,
    73  			Copy:     b.Options.Copy,
    74  			Sort:     b.Options.Sort,
    75  			Meta:     b.Options.Meta,
    76  			Static:   b.Static,
    77  			Gensizes: b.Options.Gensizes,
    78  		}
    79  		if b.Dir == "" {
    80  			b.Dir = b.Name
    81  		}
    82  		if b.ImageDir != "" {
    83  			genopts.Source = b.ImageDir
    84  			err := GenerateFolder(
    85  				FolderMeta{
    86  					Name: b.Name,
    87  					Desc: b.Desc,
    88  				},
    89  				filepath.Join(folder, b.Dir),
    90  				genopts,
    91  			)
    92  			if checkError(err) {
    93  				return err
    94  			}
    95  
    96  			InsertImage(folder, "append", Genoptions, b.Images...)
    97  		} else {
    98  			err := GenerateFolder(
    99  				FolderMeta{
   100  					Name: b.Name,
   101  					Desc: b.Desc,
   102  				},
   103  				filepath.Join(folder, b.Dir),
   104  				genopts,
   105  			)
   106  			if checkError(err) {
   107  				return err
   108  			}
   109  
   110  			InsertImage(folder, "append", Genoptions, b.Images...)
   111  		}
   112  	}
   113  
   114  	for _, f := range b.Subfolders {
   115  		if f.Dir == "" {
   116  			f.Dir = f.Name
   117  		}
   118  
   119  		if !filepath.IsAbs(b.Dir) {
   120  			verbose("filepath not absolute, joining and locating")
   121  			b.Dir, _ = filepath.Abs(filepath.Join(folder, b.Dir))
   122  		}
   123  
   124  		err := f.Build(b.Dir)
   125  		if checkError(err) {
   126  			return err
   127  		}
   128  	}
   129  
   130  	return nil
   131  }