github.com/Xenoex/gopm@v0.6.5/cmd/build.go (about)

     1  // Copyright 2013 gopm authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"os"
    19  	"path"
    20  	"path/filepath"
    21  
    22  	"github.com/Unknwon/com"
    23  	"github.com/codegangsta/cli"
    24  
    25  	"github.com/gpmgo/gopm/doc"
    26  	"github.com/gpmgo/gopm/log"
    27  )
    28  
    29  var CmdBuild = cli.Command{
    30  	Name:  "build",
    31  	Usage: "link dependencies and go build",
    32  	Description: `Command build links dependencies according to gopmfile,
    33  and execute 'go build'
    34  
    35  gopm build <go build commands>`,
    36  	Action: runBuild,
    37  	Flags: []cli.Flag{
    38  		cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
    39  		cli.BoolFlag{"verbose, v", "show process details"},
    40  	},
    41  }
    42  
    43  func runBuild(ctx *cli.Context) {
    44  	setup(ctx)
    45  
    46  	// Get GOPATH.
    47  	installGopath = com.GetGOPATHs()[0]
    48  	if com.IsDir(installGopath) {
    49  		isHasGopath = true
    50  		log.Log("Indicated GOPATH: %s", installGopath)
    51  		installGopath += "/src"
    52  	}
    53  
    54  	defer os.RemoveAll(doc.VENDOR)
    55  	buildBinary(ctx, ctx.Args()...)
    56  
    57  	log.Success("SUCC", "build", "Command executed successfully!")
    58  }
    59  
    60  func buildBinary(ctx *cli.Context, args ...string) {
    61  	genNewGoPath(ctx, false)
    62  
    63  	log.Trace("Building...")
    64  
    65  	cmdArgs := []string{"go", "build"}
    66  	cmdArgs = append(cmdArgs, args...)
    67  	err := execCmd(newGoPath, newCurPath, cmdArgs...)
    68  	if err != nil {
    69  		log.Error("build", "fail to build program:")
    70  		log.Fatal("", "\t"+err.Error())
    71  	}
    72  
    73  	if isWindowsXP {
    74  		fName := path.Base(pkgName)
    75  		binName := fName + ".exe"
    76  		os.Remove(binName)
    77  		exePath := filepath.Join(curPath, doc.VENDOR, "src", pkgName, binName)
    78  		if com.IsFile(exePath) {
    79  			err = os.Rename(exePath, filepath.Join(curPath, binName))
    80  			if err != nil {
    81  				log.Error("build", "fail to move binary:")
    82  				log.Fatal("", "\t"+err.Error())
    83  			}
    84  		} else {
    85  			log.Warn("No binary generated")
    86  		}
    87  	}
    88  }