提交 215fe347 编写于 作者: yanghye's avatar yanghye

energy command-line tools: install upx

上级 0cd874e5
......@@ -24,6 +24,10 @@ import (
//go:embed assets
var assets embed.FS
const (
UpxVersion = "4.1.0"
)
// AssetsPath 返回配置资源目录
func AssetsPath(projectData *project.Project, file string) string {
return filepath.ToSlash(filepath.Join(projectData.AssetsDir, file))
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build linux && amd64
// +build linux,amd64
package assets
import (
"embed"
"io/fs"
)
//go:embed upx/upx-linux-amd64
var upx embed.FS
func UpxBytes() (fs.File, error) {
return upx.Open("upx/upx-linux-amd64")
}
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build linux && arm64
// +build linux,arm64
package assets
import (
"embed"
"io/fs"
)
//go:embed upx/upx-linux-arm64
var upx embed.FS
func UpxBytes() (fs.File, error) {
return upx.Open("upx/upx-linux-arm64")
}
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build darwin || (windows && arm64)
// +build darwin windows,arm64
package assets
import (
"errors"
"io/fs"
)
func UpxBytes() (fs.File, error) {
return nil, errors.New("not support")
}
//----------------------------------------
//
// 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 && 386
// +build windows,386
package assets
import (
"embed"
"io/fs"
)
//go:embed upx/upx-windows-amd32.exe
var upx embed.FS
func UpxBytes() (fs.File, error) {
return upx.Open("upx/upx-windows-amd32.exe")
}
//----------------------------------------
//
// 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 && amd64
// +build windows,amd64
package assets
import (
"embed"
"io/fs"
)
//go:embed upx/upx-windows-amd64.exe
var upx embed.FS
func UpxBytes() (fs.File, error) {
return upx.Open("upx/upx-windows-amd64.exe")
}
此差异已折叠。
......@@ -46,6 +46,10 @@ const (
NSISHomeKey = "NSIS_HOME"
)
const (
UPXHomeKey = "UPX_HOME"
)
const (
CefKey = "cef"
LiblclKey = "liblcl"
......
......@@ -27,6 +27,32 @@ import (
"strings"
)
func SetUPXEnv(upxRoot string) {
upx := filepath.Join(upxRoot, "upx")
if !tools.IsExist(upx) {
println("\nError: Failed to set the UPX environment variable, not a correct UPX installation directory. ", upxRoot)
return
}
println("\nSetting UPX environment Variables to:", upxRoot)
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 exUpxRoot = fmt.Sprintf("export UPX_HOME=%s", upxRoot)
var exPath = "export PATH=$PATH:$UPX_HOME"
var exs = []string{exUpxRoot, exPath}
setPosixEnv(exs, exPath, "$UPX_HOME")
println("Hint: Reopen the cmd window for the upx command to take effect.")
}
func SetNSISEnv(nsisRoot string) {
}
......
......@@ -22,6 +22,18 @@ import (
"strings"
)
func SetUPXEnv(upxRoot string) {
upx := filepath.Join(upxRoot, "upx.exe")
if !tools.IsExist(upx) {
println("\nError: Failed to set the UPX environment variable, not a correct UPX installation directory. ", upxRoot)
return
}
println("\nSetting UPX environment Variables to:", upxRoot)
setWindowsEnv(consts.UPXHomeKey, upxRoot)
appendWindowsEnv("Path", "%UPX_HOME%")
println("Hint: Reopen the cmd window for the upx command to take effect.")
}
func SetNSISEnv(nsisRoot string) {
makensis := filepath.Join(nsisRoot, "makensis.exe")
if !tools.IsExist(makensis) {
......
......@@ -53,6 +53,8 @@ func Install(c *command.Config) {
cefFrameworkSuccessCallback func()
nsisRoot string
nsisSuccessCallback func()
upxRoot string
upxSuccessCallback func()
)
if len(willInstall) > 0 {
println("Following will be installed")
......@@ -75,6 +77,12 @@ func Install(c *command.Config) {
if nsisRoot != "" {
env.SetNSISEnv(nsisRoot)
}
// 安装upx, 内置, 仅windows, linux
upxRoot, upxSuccessCallback = installUPX(c)
// 设置upx环境变量
if upxRoot != "" {
env.SetUPXEnv(upxRoot)
}
// 安装CEF二进制框架
cefFrameworkRoot, cefFrameworkSuccessCallback = installCEFFramework(c)
// 设置 energy cef 环境变量
......@@ -82,7 +90,7 @@ func Install(c *command.Config) {
env.SetEnergyHomeEnv(cefFrameworkRoot)
}
// success 输出
if nsisSuccessCallback != nil || goSuccessCallback != nil || cefFrameworkSuccessCallback != nil {
if nsisSuccessCallback != nil || goSuccessCallback != nil || upxSuccessCallback != nil || cefFrameworkSuccessCallback != nil {
println("-----------------------------------------------------\n-----------------------------------------------------")
println("Hint: Reopen the cmd window for command to take effect.")
println("-----------------------------------------------------\n-----------------------------------------------------")
......@@ -93,6 +101,9 @@ func Install(c *command.Config) {
if goSuccessCallback != nil {
goSuccessCallback()
}
if upxSuccessCallback != nil {
upxSuccessCallback()
}
if cefFrameworkSuccessCallback != nil {
cefFrameworkSuccessCallback()
}
......@@ -130,9 +141,15 @@ func nsisIsInstall() bool {
}
func upxIsInstall() bool {
return consts.IsWindows
return (consts.IsWindows && runtime.GOARCH != "arm64") || (consts.IsLinux)
}
// 检查当前环境
// golang, nsis, cef, upx
// golang: all os
// nsis: windows
// cef: all os
// upx: windows amd64, 386, linux amd64, arm64
func checkInstallEnv(c *command.Config) (result []string) {
skip := c.Install.All
var check = func(chkInstall func() bool, name string, yes func()) {
......@@ -354,6 +371,7 @@ func ExtractUnZip(filePath, targetPath string, rmRootDir bool, files ...any) err
os.MkdirAll(fDir, 0755)
}
if targetFile, err := os.Create(targetFileName); err == nil {
defer targetFile.Close()
fmt.Println("extract file: ", path)
bar.SetCurrentValue(0)
writeFile(file, targetFile, info.Size(), func(totalLength, processLength int64) {
......@@ -361,7 +379,6 @@ func ExtractUnZip(filePath, targetPath string, rmRootDir bool, files ...any) err
})
bar.PrintBar(100)
bar.PrintEnd()
targetFile.Close()
return nil
} else {
println("createWriteFile", err.Error())
......
......@@ -10,8 +10,60 @@
package install
import "github.com/energye/energy/v2/cmd/internal/command"
import (
"fmt"
"github.com/energye/energy/v2/cmd/internal/assets"
"github.com/energye/energy/v2/cmd/internal/command"
"github.com/energye/energy/v2/cmd/internal/consts"
progressbar "github.com/energye/energy/v2/cmd/internal/progress-bar"
"github.com/energye/energy/v2/cmd/internal/tools"
"os"
"path/filepath"
"runtime"
)
func installUPX(c *command.Config) string {
return ""
func installUPX(c *command.Config) (string, func()) {
if !c.Install.IUPX {
return "", nil
}
if (consts.IsWindows && runtime.GOARCH != "arm64") || (consts.IsLinux) {
s := upxInstallPathName(c) // 安装目录
if !tools.IsExist(s) {
os.MkdirAll(s, 0755)
}
var upxName = "upx"
if consts.IsWindows {
upxName += ".exe"
}
targetFileName := filepath.Join(s, upxName) // 保存安装目录
if targetFile, err := os.Create(targetFileName); err == nil {
defer targetFile.Close()
fmt.Println("extract file: ", upxName)
bar := progressbar.NewBar(100)
bar.SetNotice("\t")
bar.HideRatio()
fs, err := assets.UpxBytes()
if err != nil {
println("UPX Installed Error:", err.Error())
return "", nil
}
stat, err := fs.Stat()
if err != nil {
println("UPX Installed Error:", err.Error())
return "", nil
}
writeFile(fs, targetFile, stat.Size(), func(totalLength, processLength int64) {
bar.PrintBar(int((float64(processLength) / float64(totalLength)) * 100))
})
bar.PrintBar(100)
bar.PrintEnd()
return s, func() {
println("UPX Installed Successfully Version:", assets.UpxVersion)
}
} else {
println("createWriteFile", err.Error())
return "", nil
}
}
return "", nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册