Example #1
0
// removePackage removes package from local file system.
func removePackage(node *doc.Node) (*doc.Node, []string) {
	// Find package in GOPATH.
	paths := utils.GetGOPATH()
	for _, p := range paths {
		absPath := p + "/src/" + utils.GetProjectPath(node.ImportPath) + "/"
		if utils.IsExist(absPath) {
			fmt.Printf(fmt.Sprintf("%s\n", promptMsg["RemovePackage"]), node.ImportPath)
			// Remove files.
			os.RemoveAll(absPath)
			// Remove file in GOPATH/bin
			proName := utils.GetExecuteName(node.ImportPath)
			paths := utils.GetGOPATH()
			var gopath string

			for _, v := range paths {
				if utils.IsExist(v + "/bin/" + proName) {
					gopath = v // Don't need to find again.
					os.Remove(v + "/bin/" + proName)
				}
			}

			pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/" + node.ImportPath
			// Remove file in GOPATH/pkg
			if len(gopath) == 0 {
				for _, v := range paths {
					if utils.IsExist(v + pkgPath + "/") {
						gopath = v
					}
				}
			}

			os.RemoveAll(gopath + pkgPath + "/")
			os.Remove(gopath + pkgPath + ".a")
			return node, nil
		}
	}

	// Cannot find package.
	fmt.Printf(fmt.Sprintf("%s\n", promptMsg["PackageNotFound"]), node.ImportPath)
	return nil, nil
}
Example #2
0
func runBuild(cmd *Command, args []string) {
	// Check flags.
	num := checkFlags(cmd.Flags, config.AutoEnable.Build, args, printBuildPrompt)
	if num == -1 {
		return
	}
	args = args[num:]

	var cmdArgs []string
	cmdArgs = append(cmdArgs, "install")
	if cmdBuild.Flags["-v"] {
		cmdArgs = append(cmdArgs, "-v")
	}

	executeCommand("go", cmdArgs)

	// Find executable in GOPATH and copy to current directory.
	wd, _ := os.Getwd()
	proName := utils.GetExecuteName(wd)
	paths := utils.GetGOPATH()

	for _, v := range paths {
		if utils.IsExist(v + "/bin/" + proName) {
			if utils.IsExist(wd + "/" + proName) {
				err := os.Remove(wd + "/" + proName)
				if err != nil {
					fmt.Printf(fmt.Sprintf("ERROR: %s\n", promptMsg["RemoveFile"]), err)
					return
				}
			}
			err := os.Rename(v+"/bin/"+proName, wd+"/"+proName)
			if err == nil {
				fmt.Printf(fmt.Sprintf("%s\n", promptMsg["MovedFile"]), v, wd)
				// Check if need to run program.
				if cmdBuild.Flags["-r"] {
					cmdArgs = make([]string, 0)
					executeCommand(proName, cmdArgs)
				}
				return
			}

			fmt.Printf(fmt.Sprintf("%s\n", promptMsg["MoveFile"]), v, wd)
			break
		}
	}
}
Example #3
0
func runCheck(cmd *Command, args []string) {
	// Check flags.
	num := checkFlags(cmd.Flags, config.AutoEnable.Check, args, printCheckPrompt)
	if num == -1 {
		return
	}
	args = args[num:]

	wd, _ := os.Getwd()
	// Guess import path.
	gopath := utils.GetBestMatchGOPATH(wd) + "/src/"
	if len(wd) <= len(gopath) {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InvalidPath"]))
		return
	}

	importPath := wd[len(gopath):]
	imports, err := doc.CheckImports(wd+"/", importPath)
	if err != nil {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["CheckImports"]), err)
		return
	}

	if len(imports) == 0 {
		return
	}

	uninstallList := make([]string, 0)
	isInstalled := false
	// Check if dependencies have been installed.
	paths := utils.GetGOPATH()

	for _, v := range imports {
		// Make sure it doesn't belong to same project.
		if utils.GetProjectPath(v) != utils.GetProjectPath(importPath) {
			for _, p := range paths {
				if utils.IsExist(p + "/src/" + v + "/") {
					isInstalled = true
					break
				}
			}

			if !isInstalled {
				uninstallList = append(uninstallList, v)
			}
		}
	}

	// Check if need to install packages.
	if len(uninstallList) > 0 {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["MissingImports"]))
		for _, v := range uninstallList {
			fmt.Printf("%s\n", v)
		}
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["ContinueDownload"]))
		var option string
		fmt.Fscan(os.Stdin, &option)
		if strings.ToLower(option) != "y" {
			os.Exit(0)
		}

		installGOPATH = utils.GetBestMatchGOPATH(appPath)
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["DownloadPath"]), installGOPATH)
		// Generate temporary nodes.
		nodes := make([]*doc.Node, len(uninstallList))
		for i := range nodes {
			nodes[i] = new(doc.Node)
			nodes[i].ImportPath = uninstallList[i]
		}
		// Download packages.
		downloadPackages(nodes)

		// Install packages all together.
		var cmdArgs []string
		cmdArgs = append(cmdArgs, "install")
		cmdArgs = append(cmdArgs, "<blank>")

		paths := utils.GetGOPATH()
		pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/"
		for _, k := range uninstallList {
			// Delete old packages.
			for _, p := range paths {
				os.RemoveAll(p + pkgPath + k + "/")
				os.Remove(p + pkgPath + k + ".a")
			}
		}

		for _, k := range uninstallList {
			fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InstallStatus"]), k)
			cmdArgs[1] = k
			executeCommand("go", cmdArgs)
		}

		// Generate configure file.
	}
}
Example #4
0
func runInstall(cmd *Command, args []string) {
	// Check flags.
	num := checkFlags(cmd.Flags, config.AutoEnable.Install, args, printInstallPrompt)
	if num == -1 {
		return
	}
	args = args[num:]

	// Check length of arguments.
	if len(args) < 1 {
		fmt.Printf(fmt.Sprintf("%s\n", promptMsg["NoPackage"]))
		return
	}

	// Check version control tools.
	checkVCSTool()

	installGOPATH = utils.GetBestMatchGOPATH(appPath)
	fmt.Printf(fmt.Sprintf("%s\n", promptMsg["DownloadPath"]), installGOPATH)

	// Generate temporary nodes.
	nodes := make([]*doc.Node, len(args))
	for i := range nodes {
		nodes[i] = new(doc.Node)
		nodes[i].ImportPath = args[i]
	}
	// Download packages.
	downloadPackages(nodes)

	if !cmdInstall.Flags["-d"] && cmdInstall.Flags["-p"] {
		// Install packages all together.
		var cmdArgs []string
		cmdArgs = append(cmdArgs, "install")
		cmdArgs = append(cmdArgs, "<blank>")

		paths := utils.GetGOPATH()
		pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/"
		for k := range downloadCache {
			// Delete old packages.
			for _, p := range paths {
				os.RemoveAll(p + pkgPath + k + "/")
				os.Remove(p + pkgPath + k + ".a")
			}
		}

		for k := range downloadCache {
			fmt.Printf(fmt.Sprintf("%s\n", promptMsg["InstallStatus"]), k)
			cmdArgs[1] = k
			executeCommand("go", cmdArgs)
		}

		// Save local nodes to file.
		fw, err := os.Create(appPath + "data/nodes.json")
		if err != nil {
			fmt.Printf(fmt.Sprintf("ERROR: runInstall -> %s\n", promptMsg["OpenFile"]), err)
			return
		}
		defer fw.Close()
		fbytes, err := json.MarshalIndent(&localNodes, "", "\t")
		if err != nil {
			fmt.Printf(fmt.Sprintf("ERROR: runInstall -> %s\n", promptMsg["ParseJSON"]), err)
			return
		}
		fw.Write(fbytes)
	}
}