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

command-line: exe执行/当前执行目录统一为 tools.ExeDir(), tools.CurrentExecuteDir()

上级 676f077c
......@@ -40,7 +40,7 @@ func main() {
}
func termRun() {
wd, _ := os.Getwd()
wd := tools.CurrentExecuteDir()
cc := &command.Config{Wd: wd}
parser := flags.NewParser(cc, flags.HelpFlag|flags.PassDoubleDash)
if len(os.Args) < 2 {
......
......@@ -82,14 +82,11 @@ func compressCEF7za(proj *project.Project) (string, error) {
return outFilePath, nil
}
wd, err := os.Getwd()
if err != nil {
return "", err
}
wd := tools.CurrentExecuteDir()
defer func() {
os.Chdir(wd)
}()
err = os.Chdir(proj.FrameworkPath)
err := os.Chdir(proj.FrameworkPath)
if err != nil {
return "", err
}
......
......@@ -13,7 +13,7 @@ package project
import (
"encoding/json"
"github.com/energye/energy/v2/cmd/internal/consts"
"github.com/energye/golcl/energy/tools"
"github.com/energye/energy/v2/cmd/internal/tools"
"os"
"path/filepath"
"runtime"
......@@ -40,7 +40,8 @@ func (m *Project) setDefaults() {
m.Name = "energyapp"
}
if m.ProjectPath == "" {
m.ProjectPath, _ = os.Getwd()
// 设置当前执行目录为项目目录
m.ProjectPath = tools.CurrentExecuteDir()
}
if m.FrameworkPath == "" {
m.FrameworkPath = os.Getenv(consts.EnergyHomeKey)
......@@ -169,7 +170,8 @@ func parse(projectData []byte) (*Project, error) {
// NewProject 创建项目对象, 根据energy.json配置
func NewProject(projectPath string) (*Project, error) {
if projectPath == "" {
projectPath, _ = os.Getwd()
// 设置当前执行目录为项目目录
projectPath = tools.CurrentExecuteDir()
}
config := filepath.Join(projectPath, consts.EnergyProjectConfig)
rawBytes, err := os.ReadFile(config)
......
......@@ -25,6 +25,18 @@ import (
"text/template"
)
var (
exePath string
currentExecuteDir string
)
func init() {
// 执行文件所在目录
exePath, _ = filepath.Split(os.Args[0])
// 当前执行目录,在其它目录执行目标执行文件时,返回当前执行目录
currentExecuteDir, _ = os.Getwd()
}
// CommandExists 命令是否存在
func CommandExists(name string) bool {
_, err := exec.LookPath(name)
......@@ -191,3 +203,15 @@ func CheckCEFDir() bool {
}
return false
}
// ExePath
// 返回当前执行文件路径
func ExePath() string {
return exePath
}
// CurrentExecuteDir
// 返回当前执行目录
func CurrentExecuteDir() string {
return currentExecuteDir
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册