Ejemplo n.º 1
0
// GetVcsName checks whether dirPath has .git .hg .svn else return ""
func GetVcsName(dirPath string) string {
	switch {
	case base.IsExist(path.Join(dirPath, ".git")):
		return "git"
	case base.IsExist(path.Join(dirPath, ".hg")):
		return "hg"
	case base.IsExist(path.Join(dirPath, ".svn")):
		return "svn"
	}
	return ""
}
Ejemplo n.º 2
0
// LoadConfig loads gopm global configuration.
func LoadConfig() (err error) {
	if !base.IsExist(ConfigFile) {
		os.MkdirAll(path.Dir(ConfigFile), os.ModePerm)
		if _, err = os.Create(ConfigFile); err != nil {
			return fmt.Errorf("fail to create config file: %v", err)
		}
	}

	Cfg, err = goconfig.LoadConfigFile(ConfigFile)
	if err != nil {
		return fmt.Errorf("fail to load config file: %v", err)
	}

	HttpProxy = Cfg.MustValue("settings", "HTTP_PROXY")
	return nil
}
Ejemplo n.º 3
0
// IsExistGopath returns true if package exists in GOPATH.
func (n *Node) IsExistGopath() bool {
	return base.IsExist(n.InstallGopath)
}
Ejemplo n.º 4
0
// IsExist returns true if package exists in local repository.
func (n *Node) IsExist() bool {
	return base.IsExist(n.InstallPath)
}
Ejemplo n.º 5
0
Archivo: bin.go Proyecto: swan-go/gopm
func runBin(ctx *cli.Context) {
	if err := setup(ctx); err != nil {
		errors.SetError(err)
		return
	}

	if len(ctx.Args()) != 1 {
		errors.SetError(fmt.Errorf("Incorrect number of arguments for command: should have 1"))
		return
	}

	// Check if given directory exists if specified.
	if ctx.IsSet("dir") && !base.IsDir(ctx.String("dir")) {
		errors.SetError(fmt.Errorf("Indicated path does not exist or not a directory"))
		return
	}

	// Backup exsited .vendor.
	if base.IsExist(setting.VENDOR) {
		os.Rename(setting.VENDOR, setting.VENDOR+".bak")
		defer func() {
			os.Rename(setting.VENDOR+".bak", setting.VENDOR)
		}()
	}

	// Parse package version.
	info := ctx.Args().First()
	pkgPath := info
	n := doc.NewNode(pkgPath, doc.BRANCH, "", "", true)
	if i := strings.Index(info, "@"); i > -1 {
		pkgPath = info[:i]
		var err error
		tp, val, dwn, err := validPkgInfo(info[i+1:])
		if err != nil {
			errors.SetError(err)
			return
		}
		n = doc.NewNode(pkgPath, tp, val, dwn, !ctx.Bool("download"))
	}

	// Check package name.
	if !strings.Contains(pkgPath, "/") {
		tmpPath, err := setting.GetPkgFullPath(pkgPath)
		if err != nil {
			errors.SetError(err)
			return
		}
		if tmpPath != pkgPath {
			n = doc.NewNode(tmpPath, n.Type, n.Value, n.DownloadURL, n.IsGetDeps)
		}
	}

	if err := downloadPackages(".", ctx, []*doc.Node{n}); err != nil {
		errors.SetError(err)
		return
	}

	// Check if previous steps were successful.
	if !n.IsExist() {
		errors.SetError(fmt.Errorf("Download steps weren't successful"))
		return
	}

	tmpVendor := path.Join("vendor", path.Base(n.RootPath))
	os.RemoveAll(tmpVendor)
	os.RemoveAll(setting.VENDOR)
	defer func() {
		os.RemoveAll(tmpVendor)
		os.RemoveAll(setting.VENDOR)
	}()

	// FIXME: should use .gopm/temp path.
	if err := autoLink(n.InstallPath, tmpVendor); err != nil {
		errors.SetError(fmt.Errorf("Fail to link slef: %v", err))
		return
	}

	os.Chdir(tmpVendor)
	oldWorkDir := setting.WorkDir
	setting.WorkDir = path.Join(setting.WorkDir, tmpVendor)
	if !setting.Debug {
		defer func() {
			os.Chdir(oldWorkDir)
			os.RemoveAll("vendor")
			os.RemoveAll(setting.VENDOR)
		}()
	}

	// if err := buildBinary(ctx); err != nil {
	// 	errors.SetError(err)
	// 	return
	// }

	if err := linkVendors(ctx, n.ImportPath); err != nil {
		errors.SetError(err)
		return
	}

	log.Info("Installing...")

	cmdArgs := []string{"go", "install"}
	if ctx.Bool("verbose") {
		cmdArgs = append(cmdArgs, "-v")
	}
	if len(ctx.String("tags")) > 0 {
		cmdArgs = append(cmdArgs, "-tags")
		cmdArgs = append(cmdArgs, ctx.String("tags"))
	}
	cmdArgs = append(cmdArgs, n.ImportPath)
	if err := execCmd(setting.DefaultVendor, setting.WorkDir, cmdArgs...); err != nil {
		errors.SetError(fmt.Errorf("fail to run program: %v", err))
		return
	}

	gf, _, err := parseGopmfile(setting.GOPMFILE)
	if err != nil {
		errors.SetError(err)
		return
	}

	// Because build command moved binary to root path.
	binName := path.Base(n.ImportPath)
	binPath := path.Join(setting.DefaultVendor, "bin", path.Base(n.ImportPath))
	if runtime.GOOS == "windows" {
		binName += ".exe"
	}

	// Move binary to given directory.
	movePath := oldWorkDir
	if ctx.IsSet("dir") {
		movePath = ctx.String("dir")
	} else if base.IsGoTool(n.ImportPath) {
		movePath = path.Join(runtime.GOROOT(), "pkg/tool", runtime.GOOS+"_"+runtime.GOARCH)
		if !base.IsExist(binPath) {
			log.Info("Command executed successfully!")
			fmt.Println("Binary has been built into: " + movePath)
			return
		}
	}

	if !base.IsFile(binPath) {
		errors.SetError(fmt.Errorf("Previous steps weren't successful or the project does not contain main package"))
		return
	}

	if base.IsExist(path.Join(movePath, binName)) {
		if err := os.Remove(path.Join(movePath, binName)); err != nil {
			log.Warn("Cannot remove binary in work directory: %v", err)
		}
	}

	if err := os.Rename(binPath, movePath+"/"+binName); err != nil {
		errors.SetError(fmt.Errorf("Fail to move binary: %v", err))
		return
	}
	os.Chmod(movePath+"/"+binName, os.ModePerm)

	includes := strings.Split(gf.MustValue("res", "include"), "|")
	if len(includes) > 0 {
		log.Info("Copying resources to %s", movePath)
		for _, include := range includes {
			if base.IsDir(include) {
				os.RemoveAll(path.Join(movePath, include))
				if err := base.CopyDir(include, filepath.Join(movePath, include)); err != nil {
					errors.AppendError(errors.NewErrCopyResource(include))
				}
			}
		}
	}

	log.Info("Command executed successfully!")
	fmt.Println("Binary has been built into: " + movePath)
}
Ejemplo n.º 6
0
Archivo: run.go Proyecto: swan-go/gopm
func linkVendors(ctx *cli.Context, optTarget string) error {
	gfPath := path.Join(setting.WorkDir, setting.GOPMFILE)
	gf, target, err := parseGopmfile(gfPath)
	if err != nil {
		return fmt.Errorf("fail to parse gopmfile: %v", err)
	}
	if len(optTarget) > 0 {
		target = optTarget
	}
	rootPath := doc.GetRootPath(target)

	// TODO: local support.

	// Make link of self.
	log.Debug("Linking %s...", rootPath)
	from := setting.WorkDir
	to := path.Join(setting.DefaultVendorSrc, rootPath)
	if setting.Debug {
		log.Debug("Linking from %s to %s", from, to)
	}
	if err := autoLink(from, to); err != nil {
		return fmt.Errorf("fail to link self: %v", err)
	}

	// Check and loads dependency packages.
	log.Debug("Loading dependencies...")
	imports, err := doc.ListImports(target, rootPath, setting.DefaultVendor, setting.WorkDir, ctx.String("tags"), ctx.Bool("test"))
	if err != nil {
		return fmt.Errorf("fail to list imports: %v", err)
	}

	stack := make([]*doc.Pkg, len(imports))
	for i, name := range imports {
		name := doc.GetRootPath(name)
		tp, val, _, err := validPkgInfo(gf.MustValue("deps", name))
		if err != nil {
			return fmt.Errorf("fail to validate package(%s): %v", name, err)
		}

		stack[i] = doc.NewPkg(name, tp, val)
	}

	// FIXME: at least link once, need a set
	lastIdx := len(stack) - 1
	for lastIdx >= 0 {
		pkg := stack[lastIdx]
		linkPath := path.Join(setting.DefaultVendorSrc, pkg.RootPath)
		if setting.Debug {
			log.Debug("Import path: %s", pkg.ImportPath)
			log.Debug("Linking path: %s", linkPath)
		}
		if base.IsExist(linkPath) {
			stack = stack[:lastIdx]
			lastIdx = len(stack) - 1
			continue
		}

		if pkg.IsEmptyVal() && setting.HasGOPATHSetting &&
			base.IsExist(path.Join(setting.InstallGopath, pkg.RootPath)) {
			stack = stack[:lastIdx]
			lastIdx = len(stack) - 1
			continue
		}

		venderPath := path.Join(setting.InstallRepoPath, pkg.RootPath+pkg.ValSuffix())
		if !base.IsExist(venderPath) && pkg.RootPath != "C" {
			//import "C" is cgo
			return fmt.Errorf("package not installed: %s", pkg.RootPath+pkg.VerSuffix())
		}

		log.Debug("Linking %s...", pkg.RootPath+pkg.ValSuffix())
		if err := autoLink(venderPath, linkPath); err != nil {
			return fmt.Errorf("fail to link dependency(%s): %v", pkg.RootPath, err)
		}
		stack = stack[:lastIdx]

		gf, target, err := parseGopmfile(path.Join(linkPath, setting.GOPMFILE))
		if err != nil {
			return fmt.Errorf("fail to parse gopmfile(%s): %v", linkPath, err)
		}
		// parseGopmfile only returns right target when parse work directory.
		target = pkg.RootPath
		rootPath := target
		imports, err := doc.ListImports(target, rootPath, setting.DefaultVendor, linkPath, ctx.String("tags"), ctx.Bool("test"))
		if err != nil {
			errors.SetError(err)
		}
		for _, name := range imports {
			if name == "C" {
				continue
			}
			name := doc.GetRootPath(name)
			tp, val, _, err := validPkgInfo(gf.MustValue("deps", name))
			if err != nil {
				return fmt.Errorf("fail to validate package(%s): %v", name, err)
			}

			stack = append(stack, doc.NewPkg(name, tp, val))
		}
		lastIdx = len(stack) - 1
	}
	return nil
}