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

     1  // Copyright 2013-2014 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/filepath"
    20  
    21  	"github.com/Unknwon/com"
    22  	"github.com/codegangsta/cli"
    23  
    24  	"github.com/gpmgo/gopm/doc"
    25  	"github.com/gpmgo/gopm/log"
    26  )
    27  
    28  var CmdInstall = cli.Command{
    29  	Name:  "install",
    30  	Usage: "link dependencies and go install",
    31  	Description: `Command install links dependencies according to gopmfile,
    32  and execute 'go install'
    33  
    34  gopm install
    35  gopm install <import path>
    36  
    37  If no argument is supplied, then gopmfile must be present`,
    38  	Action: runInstall,
    39  	Flags: []cli.Flag{
    40  		cli.BoolFlag{"pkg, p", "only install non-main packages"},
    41  		cli.BoolFlag{"verbose, v", "show process details"},
    42  	},
    43  }
    44  
    45  func runInstall(ctx *cli.Context) {
    46  	setup(ctx)
    47  
    48  	var target string
    49  	switch len(ctx.Args()) {
    50  	case 0:
    51  		if !com.IsFile(".gopmfile") {
    52  			break
    53  		}
    54  
    55  		gf := doc.NewGopmfile(".")
    56  		target = gf.MustValue("target", "path")
    57  	case 1:
    58  		target = ctx.Args()[0]
    59  	default:
    60  		log.Fatal("install", "Too many arguments")
    61  	}
    62  
    63  	// Get GOPATH.
    64  	installGopath = com.GetGOPATHs()[0]
    65  	if com.IsDir(installGopath) {
    66  		isHasGopath = true
    67  		log.Log("Indicated GOPATH: %s", installGopath)
    68  		installGopath += "/src"
    69  	} else {
    70  		if ctx.Bool("gopath") {
    71  			log.Error("get", "Invalid GOPATH path")
    72  			log.Error("", "GOPATH does not exist or is not a directory:")
    73  			log.Error("", "\t"+installGopath)
    74  			log.Help("Try 'go help gopath' to get more information")
    75  		} else {
    76  			// It's OK that no GOPATH setting
    77  			// when user does not specify to use.
    78  			log.Warn("No GOPATH setting available")
    79  		}
    80  	}
    81  
    82  	genNewGoPath(ctx, false)
    83  	defer os.RemoveAll(doc.VENDOR)
    84  
    85  	var installRepos []string
    86  	if ctx.Bool("pkg") {
    87  		curPath, _ := filepath.Abs(".")
    88  		installRepos = doc.GetAllImports([]string{curPath}, ".", ctx.Bool("example"), false)
    89  	} else {
    90  		if len(target) == 0 {
    91  			target = pkgName
    92  		}
    93  
    94  		installRepos = []string{target}
    95  	}
    96  
    97  	log.Trace("Installing...")
    98  
    99  	for _, repo := range installRepos {
   100  		cmdArgs := []string{"go", "install"}
   101  
   102  		if ctx.Bool("verbose") {
   103  			cmdArgs = append(cmdArgs, "-v")
   104  		}
   105  		cmdArgs = append(cmdArgs, repo)
   106  		err := execCmd(newGoPath, newCurPath, cmdArgs...)
   107  		if err != nil {
   108  			log.Error("install", "Fail to install program:")
   109  			log.Fatal("", "\t"+err.Error())
   110  		}
   111  	}
   112  
   113  	log.Success("SUCC", "install", "Command executed successfully!")
   114  }