提交 6818ac18 编写于 作者: yanghye's avatar yanghye

energy-command-line, 优化命令 install .

上级 432e0e3b
upx energy-windows-32.exe
upx energy-windows-64.exe
upx energy-windowsarm-64.exe
upx energy-darwin-64
upx energy-darwinarm-64
upx energy-linux-64
upx energy-linuxarm-64
pause
\ No newline at end of file
......@@ -6,7 +6,10 @@
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----
//----------------------------------------
//go:build !windows
// +build !windows
package env
......@@ -24,78 +27,13 @@ import (
"strings"
)
var envPath *envToPath
type envToPath struct {
path []string
}
func init() {
if consts.IsWindows {
envPath = &envToPath{}
}
}
func (m *envToPath) add(value string) {
if m == nil {
return
}
m.path = append(m.path, value)
}
func (m *envToPath) toPath() string {
if m == nil {
return ""
}
return strings.Join(m.path, ";")
}
// SetToPath
// windows go 和 nsis 设置环境变量到path
func SetToPath() {
if envPath == nil {
return
}
pathValue, _ := os.LookupEnv("path")
cmd := toolsCommand.NewCMD()
defer cmd.Close()
cmd.IsPrint = false
var args = []string{"path", fmt.Sprintf("%s;%s", pathValue, envPath.toPath())}
cmd.Command("setx", args...)
}
func SetNSISEnv(nsisRoot string) {
makensisbin := filepath.Join(nsisRoot, "makensis.exe")
if !tools.IsExist(makensisbin) {
println("\nError: Failed to set the NSIS environment variable, not a correct NSIS installation directory. ", nsisRoot)
return
}
println("\nSetting NSIS environment Variables to:", nsisRoot)
cmd := toolsCommand.NewCMD()
cmd.IsPrint = false
cmd.MessageCallback = func(s []byte, e error) {
msg := strings.TrimSpace(string(s))
if msg != "" {
fmt.Println("CMD:", msg)
}
if e != nil {
fmt.Println("CMD Error:", e)
}
}
defer cmd.Close()
var args = []string{consts.NSISHomeKey, nsisRoot}
cmd.Command("setx", args...)
envPath.add("%NSIS_HOME%")
println("\nHint: Reopen the cmd window for the makensis command to take effect.")
}
func SetGoEnv(goRoot string) {
var goexe = "go"
if consts.IsWindows {
goexe += ".exe"
}
gobin := filepath.Join(goRoot, "bin", goexe)
if !tools.IsExist(gobin) {
goBin := filepath.Join(goRoot, "bin", "go")
if !tools.IsExist(goBin) {
println("\nError: Failed to set the Golang environment variable, not a correct Golang installation directory. ", goRoot)
return
}
......@@ -112,39 +50,22 @@ func SetGoEnv(goRoot string) {
}
}
defer cmd.Close()
if consts.IsWindows {
// setx
// GOROOT=/to/go/path
var args = []string{"GOROOT", goRoot}
cmd.Command("setx", args...)
// GOCACHE=%GOROOT%\go-build
args = []string{"GOCACHE", "%GOROOT%\\go-build"}
cmd.Command("setx", args...)
// GOBIN=%GOROOT%\bin
args = []string{"GOBIN", "%GOROOT%\\bin"}
cmd.Command("setx", args...)
// PATH=%GOROOT%\bin
envPath.add("%GOROOT%\\bin")
} 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 exs = []string{exGoRoot, exGoCache, exGoBin}
setPosixEnv(exs, exPath, "$GOBIN")
}
//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 exs = []string{exGoRoot, exGoCache, exGoBin}
setPosixEnv(exs, exPath, "$GOBIN")
println("\nHint: Reopen the cmd window for the Go command to take effect.")
}
func SetEnergyHomeEnv(homePath string) {
var cef string
if consts.IsWindows {
cef = "libcef.dll"
} else if consts.IsLinux {
if consts.IsLinux {
cef = "libcef.so"
} else if consts.IsDarwin {
cef = "cef_sandbox.a"
......@@ -167,14 +88,9 @@ func SetEnergyHomeEnv(homePath string) {
}
}
defer cmd.Close()
if consts.IsWindows {
var args = []string{"/c", "setx", consts.EnergyHomeKey, homePath}
cmd.Command("cmd.exe", args...)
} else {
var energyHome = fmt.Sprintf("export %s=%s", consts.EnergyHomeKey, homePath)
exs := []string{energyHome}
setPosixEnv(exs, "", "")
}
var energyHome = fmt.Sprintf("export %s=%s", consts.EnergyHomeKey, homePath)
exs := []string{energyHome}
setPosixEnv(exs, "", "")
println("\nHint: Reopen the cmd window to make the environment variables take effect.")
}
......@@ -198,9 +114,6 @@ func setPosixEnv(exs []string, binPath, bin string) {
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
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build windows
// +build windows
package env
import (
"fmt"
"github.com/energye/energy/v2/cmd/internal/consts"
"github.com/energye/energy/v2/cmd/internal/tools"
toolsCommand "github.com/energye/golcl/tools/command"
"path/filepath"
"strings"
)
var envkeys = make(map[string]string)
func SetNSISEnv(nsisRoot string) {
makensis := filepath.Join(nsisRoot, "makensis.exe")
if !tools.IsExist(makensis) {
println("\nError: Failed to set the NSIS environment variable, not a correct NSIS installation directory. ", nsisRoot)
return
}
println("\nSetting NSIS environment Variables to:", nsisRoot)
//regCurUser := tools.NewRegistryCurrentUser()
//defer regCurUser.Close()
//regCurUser.Set(consts.NSISHomeKey, nsisRoot)
//regCurUser.Append("Path", "%NSIS_HOME%")
//cmd
setWindowsEnv(consts.NSISHomeKey, nsisRoot)
appendWindowsEnv("Path", "%NSIS_HOME%")
println("Hint: Reopen the cmd window for the makensis command to take effect.")
}
func SetGoEnv(goRoot string) {
goBin := filepath.Join(goRoot, "bin", "go.exe")
if !tools.IsExist(goBin) {
println("\nError: Failed to set the Golang environment variable, not a correct Golang installation directory. ", goRoot)
return
}
println("\nSetting Golang environment Variables to:", goRoot)
//regCurUser := tools.NewRegistryCurrentUser()
//defer regCurUser.Close()
//regCurUser.Set("GOROOT", goRoot)
//regCurUser.Set("GOCACHE", "%GOROOT%\\go-build")
//regCurUser.Set("GOBIN", "%GOROOT%\\bin")
//regCurUser.Append("Path", "%GOBIN%")
// cmd
setWindowsEnv("GOROOT", goRoot)
setWindowsEnv("GOCACHE", "%GOROOT%\\go-build")
setWindowsEnv("GOBIN", "%GOROOT%\\bin")
appendWindowsEnv("Path", "%GOROOT%\\bin")
println("Hint: Reopen the cmd window for the Go command to take effect.")
}
func SetEnergyHomeEnv(homePath string) {
cefPath := filepath.Join(homePath, "libcef.dll")
if !tools.IsExist(cefPath) {
println("\nError: Setting ENERGY_HOME environment variable failed and is not a correct CEF Framework installation directory. ", homePath)
return
}
println("\nSetting ENERGY environment Variables [ENERGY_HOME] to", homePath)
//regCurUser := tools.NewRegistryCurrentUser()
//defer regCurUser.Close()
//regCurUser.Set(consts.EnergyHomeKey, homePath)
//cmd
setWindowsEnv(consts.EnergyHomeKey, homePath)
println("Hint: Reopen the cmd window to make the environment variables take effect.")
}
func setWindowsEnv(name, value string) {
envkeys[name] = value
cmd := toolsCommand.NewCMD()
cmd.IsPrint = false
cmd.Command("setx", name, value)
cmd.Close()
}
func appendWindowsEnv(name, value string) {
regCurUser := tools.NewRegistryCurrentUser()
defer regCurUser.Close()
oldValue, err := regCurUser.Read(name)
if err == nil {
// 可变变量替换完整路径
var fullValuePath = func(value []string) string {
for i, val := range value {
values := strings.Split(val, "\\")
for i, vab := range values {
vab = strings.TrimSpace(vab)
if vab[0] == '%' && vab[len(vab)-1] == '%' {
vab = vab[1 : len(vab)-1]
if v, err := regCurUser.Read(vab); err == nil {
values[i] = v
}
}
}
value[i] = strings.Join(values, "\\")
}
return strings.Join(value, ";")
}
// 转换完整路径
valueFull := fullValuePath([]string{value})
oldValueFull := fullValuePath(strings.Split(oldValue, ";"))
oldValues := strings.Split(oldValueFull, ";")
// 检测当前设置变量如果已存在就跳出
for _, oval := range oldValues {
if strings.TrimSpace(valueFull) == strings.TrimSpace(oval) {
return
}
}
cmd := toolsCommand.NewCMD()
cmd.IsPrint = false
cmd.Command("setx", name, fmt.Sprintf("%s;%s", oldValue, value))
cmd.Close()
} else {
// 没有就设置一个新的
setWindowsEnv(name, value)
}
}
......@@ -58,7 +58,7 @@ func Install(c *command.Config) {
for _, name := range willInstall {
println("\t", name)
}
println("Press Enter to start installation")
println("Press Enter to start installation...")
var s string
fmt.Scanln(&s)
}
......@@ -68,6 +68,7 @@ func Install(c *command.Config) {
cefFrameworkRoot, cefFrameworkSuccessCallback = installCEFFramework(c)
// 安装nsis安装包制作工具, 仅windows - amd64
nsisRoot, nsisSuccessCallback = installNSIS(c)
// 设置nsis环境变量
if nsisRoot != "" {
env.SetNSISEnv(nsisRoot)
......@@ -76,14 +77,14 @@ func Install(c *command.Config) {
if goRoot != "" {
env.SetGoEnv(goRoot)
}
// windows path 环境变量设置
env.SetToPath()
// 设置 energy cef 环境变量
if cefFrameworkRoot != "" {
env.SetEnergyHomeEnv(cefFrameworkRoot)
}
println("-----------------------------------------------------")
// success 输出
if nsisSuccessCallback != nil || goSuccessCallback != nil || cefFrameworkSuccessCallback != nil {
println("-----------------------------------------------------")
}
if nsisSuccessCallback != nil {
nsisSuccessCallback()
}
......
......@@ -236,7 +236,7 @@ func installCEFFramework(c *command.Config) (string, func()) {
}
}
return installPathName, func() {
println("\nCEF Installed Successfully Version:", c.Install.Version, liblclVersion)
println("CEF Installed Successfully Version:", c.Install.Version, liblclVersion)
if liblclModule == nil {
println("hint: liblcl module", liblclModuleName, `is not configured in the current version, You need to use built-in binary build. [go build -tags="tempdll"]`)
}
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build windows
// +build windows
package tools
import (
"fmt"
"golang.org/x/sys/windows/registry"
"strings"
)
type Registry struct {
read registry.Key
set registry.Key
}
func NewRegistryCurrentUser() *Registry {
read, err := registry.OpenKey(registry.CURRENT_USER, "Environment", registry.READ)
if err != nil {
fmt.Println("open read", err)
return nil
}
set, err := registry.OpenKey(registry.CURRENT_USER, "Environment", registry.SET_VALUE)
if err != nil {
fmt.Println("open set", err)
return nil
}
return &Registry{read: read, set: set}
}
// Read 读取
func (m *Registry) Read(name string) (string, error) {
v, _, err := m.read.GetStringValue(name)
return v, err
}
// Set 覆盖
func (m *Registry) Set(name, value string) error {
return m.set.SetExpandStringValue(name, value)
//return m.set.SetStringValue(name, value)
}
// Append 追加
func (m *Registry) Append(name, value string) error {
if v, err := m.Read(name); err == nil {
vals := strings.Split(v, ";")
for _, val := range vals {
val = strings.TrimSpace(val)
if val == value { // 如果已经存在,就不添加了
return nil
}
}
if v[len(v)-1] == ';' {
v += value
} else {
v += ";" + value
}
return m.Set(name, v)
} else {
return m.Set(name, value)
}
}
// DeleteValue 删除指定key name
func (m *Registry) DeleteValue(name string) error {
return m.set.DeleteValue(name)
}
// Close 关闭
func (m *Registry) Close() {
if m.read != 0 {
_ = m.read.Close()
}
if m.set != 0 {
_ = m.set.Close()
}
}
......@@ -9,7 +9,7 @@ require (
github.com/jessevdk/go-flags v1.5.0
github.com/json-iterator/go v1.1.12
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f
golang.org/x/sys v0.12.0
)
require (
......
......@@ -24,5 +24,5 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c h1:coVla7zpsycc+kA9NXpcvv2E4I7+ii6L5hZO2S6C3kw=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册