fea: update project

This commit is contained in:
2026-01-03 21:34:13 +08:00
parent 6ee2c90464
commit c28a0ffa8e
3 changed files with 16 additions and 46 deletions

View File

@@ -20,10 +20,6 @@ type project struct {
}
// GetExecutablePath 获取可执行文件的路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:41 2022/10/14
func (p *project) GetExecutablePath() (string, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
if err != nil {
@@ -33,19 +29,11 @@ func (p *project) GetExecutablePath() (string, error) {
}
// GetFileSystemSeparator 获取当前文件系统的路径分隔符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:51 2022/10/14
func (p *project) GetFileSystemSeparator() string {
return string(filepath.Separator)
}
// BuildPath 构建路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:57 2022/10/14
func (p *project) BuildPath(filePathList ...string) string {
projectRootPath, _ := p.GetExecutablePath()
if len(filePathList) == 0 {
@@ -53,49 +41,29 @@ func (p *project) BuildPath(filePathList ...string) string {
return projectRootPath
}
// 第一个特殊处理
if filePathList[0] == "." || len(filePathList[0]) == 0 {
filePathList[0] = projectRootPath
if strings.ToLower(runtime.GOOS) == "windows" {
// windows
if !strings.Contains(filePathList[0], ":") {
// 不包含 :, 是 相对路径,拼成绝对路径
filePathList[0] = filepath.Join(projectRootPath, filePathList[0])
}
} else {
if strings.ToLower(runtime.GOOS) == "windows" {
// windows
if strings.HasPrefix(filePathList[0], "."+p.GetFileSystemSeparator()) {
// 相对路径
filePathList[0] = strings.ReplaceAll(filePathList[0], ".", projectRootPath)
} else if !strings.Contains(filePathList[0], ":") {
// 不包含 :, 是 相对路径,拼成绝对路径
filePathList[0] = projectRootPath + p.GetFileSystemSeparator() + strings.TrimLeft(filePathList[0], p.GetFileSystemSeparator())
}
} else {
// unix/ linux
if strings.HasPrefix(filePathList[0], "."+p.GetFileSystemSeparator()) {
filePathList[0] = strings.ReplaceAll(filePathList[0], ".", projectRootPath)
} else if !strings.HasPrefix(filePathList[0], p.GetFileSystemSeparator()) {
filePathList[0] = projectRootPath + p.GetFileSystemSeparator() + filePathList[0]
}
// unix/ linux
if !strings.HasPrefix(filePathList[0], p.GetFileSystemSeparator()) {
// 不是绝对路径
filePathList[0] = filepath.Join(projectRootPath, filePathList[0])
}
}
filePathList[0] = strings.TrimRight(filePathList[0], p.GetFileSystemSeparator())
for idx := 1; idx < len(filePathList); idx++ {
filePathList[idx] = strings.Trim(filePathList[idx], p.GetFileSystemSeparator())
}
return strings.Join(filePathList, p.GetFileSystemSeparator())
return filepath.Join(filePathList...)
}
// GetProjectPath 获取项目目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:39 2023/1/13
func (p *project) GetProjectPath() string {
// GetWorkDir 获取工作目录
func (p *project) GetWorkDir() string {
dir, _ := os.Getwd()
return dir
}
// GetHomeDir 获取家目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:31 2023/4/18
func (p *project) GetHomeDir() string {
homePath, _ := homedir.Dir()
return homePath