energy-command-line, 增加全自动安装energy整个开发环境

This commit is contained in:
杨红岩 2023-09-04 22:55:26 +08:00
parent 11411c01f2
commit cd528e54e8
2 changed files with 169 additions and 5 deletions

View File

@ -11,6 +11,7 @@
package env
import (
"bytes"
"fmt"
"github.com/energye/energy/v2/cmd/internal/command"
"github.com/energye/energy/v2/cmd/internal/tools"
@ -54,7 +55,46 @@ func SetGoEnv(goRoot string) {
args = []string{"path", "%path%;%GOROOT%\\bin"}
cmd.Command("setx", args...)
} else {
//export GOROOT=/home/yanghy/app/go
//export GOCACHE=$GOROOT/go-build
//export GOBIN=$GOROOT/bin
//export PATH=$PATH:$GOBIN
//var exGoRoot = fmt.Sprintf("export GOROOT=%s", goRoot)
//var exGoCache = "export GOCACHE==$GOROOT/go-build"
//var exGoBin = "export GOBIN=$GOROOT/bin"
//var exPath = "export PATH=$PATH:$GOBIN"
//var envFiles []string
//if command.IsLinux {
// envFiles = []string{".profile", ".zshrc", ".bashrc"}
//} else if command.IsDarwin {
// envFiles = []string{".profile", ".zshrc", ".bash_profile"}
//}
//homeDir, err := homedir.Dir()
//if err != nil {
// println(err.Error())
// return
//}
//for _, file := range envFiles {
// var fp = path.Join(homeDir, file)
// cmd.Command("touch", fp)
// f, err := os.OpenFile(fp, os.O_RDWR|os.O_APPEND, 0666)
// if err == nil {
// var oldContent string
// if contentBytes, err := ioutil.ReadAll(f); err == nil {
// content := string(contentBytes)
// oldContent = content
// var lines = strings.Split(content, "\n")
// var exist = false
// for i := 0; i < len(lines); i++ {
// line := lines[i]
// if strings.Index(line, energyHomeKey) == 0 {
// content = strings.ReplaceAll(content, line, energyHome)
// exist = true
// }
// }
// }
// }
//}
}
println("\nHint: Reopen the cmd window for the Go command to take effect.")
}
@ -91,7 +131,11 @@ func SetEnergyHomeEnv(homePath string) {
} else if command.IsDarwin {
envFiles = []string{".profile", ".zshrc", ".bash_profile"}
}
homeDir, _ := homedir.Dir()
homeDir, err := homedir.Dir()
if err != nil {
println(err.Error())
return
}
for _, file := range envFiles {
var fp = path.Join(homeDir, file)
cmd.Command("touch", fp)
@ -133,6 +177,7 @@ func SetEnergyHomeEnv(homePath string) {
f.WriteString("\n")
f.WriteString(energyHome)
f.WriteString("\n")
f.Close()
}
} else {
f.Close()
@ -142,3 +187,122 @@ func SetEnergyHomeEnv(homePath string) {
}
println("\nHint: Reopen the cmd window to make the environment variables take effect.")
}
func setEnv(exs []string, exGoBinPath, bin string) {
cmd := toolsCommand.NewCMD()
cmd.MessageCallback = func(s []byte, e error) {
fmt.Println("CMD:", string(s), " error:", e)
}
defer cmd.Close()
var envFiles []string
if command.IsLinux {
envFiles = []string{".profile", ".zshrc", ".bashrc"}
} else if command.IsDarwin {
envFiles = []string{".profile", ".zshrc", ".bash_profile"}
}
homeDir, err := homedir.Dir()
// test
homeDir = "E:\\app"
envFiles = []string{".profile", ".zshrc", ".bashrc"}
if err != nil {
println(err.Error())
return
}
var isExport = func(line string) (string, bool) {
for _, ex := range exs {
exName := strings.Split(ex, "=")[0]
if strings.Index(line, exName) == 0 {
return ex, true
}
}
return "", false
}
var isExportPath = func(line string) bool {
if strings.Index(line, "export PATH") == 0 {
if strings.Contains(line, bin) {
return true
}
}
return false
}
for _, file := range envFiles {
var fp = path.Join(homeDir, file)
cmd.Command("touch", fp)
f, err := os.OpenFile(fp, os.O_RDWR|os.O_APPEND, 0666)
if err == nil {
var oldContent string
if contentBytes, err := ioutil.ReadAll(f); err == nil {
content := string(contentBytes)
oldContent = content
var newContent = new(bytes.Buffer)
var lines = strings.Split(content, "\n")
var exist = false
var gobin = false
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if line == "" {
continue
}
if exGoBinPath != "" && isExportPath(line) {
//是path并且有gobin
newContent.WriteString(exGoBinPath)
gobin = true
} else {
// 其它变量, 判断是否存在,如果存在替换, 否则将原来的添加进来
if ex, ok := isExport(line); ok {
newContent.WriteString(ex)
exist = true
} else {
newContent.WriteString(line)
}
}
newContent.WriteString("\n")
}
if exist {
// 如果path里没有gobin, 添加一个
if exGoBinPath != "" && !gobin {
newContent.WriteString(exGoBinPath)
newContent.WriteString("\n")
}
// 有就覆盖掉之前的, 要先关闭掉文件
if err := f.Close(); err == nil {
// 如果关闭文件流成功, 重新写入覆盖文件
var oldWrite = func() {
if f, err = os.OpenFile(fp, os.O_RDWR, 0666); err == nil {
f.WriteString(oldContent)
f.Close()
}
}
// 打开覆盖模式
if newOpenFile, err := os.OpenFile(fp, os.O_RDWR|os.O_TRUNC, 0666); err == nil {
// 写入,如果失败,把老的写入
if _, err := newOpenFile.Write(newContent.Bytes()); err == nil {
newOpenFile.Close()
} else {
newOpenFile.Close()
oldWrite()
}
} else {
oldWrite()
}
}
} else {
// 都没有全添加一次
for _, ex := range exs {
f.WriteString("\n")
f.WriteString(ex)
f.WriteString("\n")
}
// 如果path里没有gobin, 添加一个
if exGoBinPath != "" && !gobin {
f.WriteString(exGoBinPath)
f.WriteString("\n")
}
f.Close()
}
} else {
f.Close()
}
}
}
}

View File

@ -97,8 +97,8 @@ func installGolang(c *command.Config) string {
version := command.GolangDefaultVersion
gos := runtime.GOOS
arch := runtime.GOARCH
gos = "darwin"
arch = "amd64"
//gos = "darwin"
//arch = "amd64"
ext := exts[gos]
if !tools.IsExist(s) {
println("Directory does not exist. Creating directory.", s)
@ -131,7 +131,7 @@ func installGolang(c *command.Config) string {
// 使用 go 名字做为 go 安装目录
targetPath := filepath.Join(s, "go")
// 释放文件
if !command.IsWindows {
if command.IsWindows {
//zip
ExtractUnZip(savePath, targetPath)
} else {