github.com/thetechnoweenie/graven@v1.0.2/commands/build.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/cbegin/graven/buildtool"
     8  	"github.com/cbegin/graven/domain"
     9  	"github.com/cbegin/graven/util"
    10  	"github.com/hashicorp/go-multierror"
    11  	"github.com/urfave/cli"
    12  )
    13  
    14  var BuildCommand = cli.Command{
    15  	Name:   "build",
    16  	Usage:  "Builds the current project",
    17  	Action: build,
    18  }
    19  
    20  func build(c *cli.Context) error {
    21  	if err := clean(c); err != nil {
    22  		return err
    23  	}
    24  
    25  	project, err := domain.FindProject()
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	if err := writeVersionFile(project); err != nil {
    31  		fmt.Println(err)
    32  	}
    33  
    34  	var merr error
    35  	for _, artifact := range project.Artifacts {
    36  		a := artifact
    37  		for _, target := range artifact.Targets {
    38  			t := target
    39  			err := buildTarget(project, &a, &t)
    40  			if err != nil {
    41  				merr = multierror.Append(merr, err)
    42  			}
    43  		}
    44  	}
    45  
    46  	return merr
    47  }
    48  
    49  func buildTarget(project *domain.Project, artifact *domain.Artifact, target *domain.Target) error {
    50  	classifiedPath := project.TargetPath(artifact.Classifier)
    51  	if _, err := os.Stat(classifiedPath); os.IsNotExist(err) {
    52  		os.Mkdir(classifiedPath, 0755)
    53  	}
    54  
    55  	for _, resource := range append(project.Resources, artifact.Resources...) {
    56  		resourcePath := project.ProjectPath(resource)
    57  		baseProjectPath := project.ProjectPath()
    58  		if len(resourcePath[len(baseProjectPath):]) < 1 {
    59  			return fmt.Errorf("Resource path cannot be the entire project folder: %s", resourcePath)
    60  		}
    61  		err := util.CopyDir(resourcePath, classifiedPath)
    62  		if err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	// TODO - Make this configurable
    68  	var buildTool builder.BuildTool = &builder.GoBuildTool{}
    69  	return buildTool.Build(classifiedPath, project, artifact, target)
    70  }