feat: 增加程序是否在IDE中运行的检测

This commit is contained in:
2026-01-05 10:42:13 +08:00
parent 1f6145405e
commit 3d76b1bcde

View File

@@ -36,8 +36,8 @@ func (p *project) GetFileSystemSeparator() string {
// BuildPath 构建路径
func (p *project) BuildPath(filePathList ...string) string {
projectRootPath, _ := p.GetExecutablePath()
if p.IsRunningWithGoRun() {
// go run 运行, 则工作目录为项目目录
if p.IsRunningWithGoRun() || p.IsInIDE() {
// go run 运行 或 者 ide 中运行, 则工作目录为项目目录
projectRootPath = p.GetWorkDir()
}
if len(filePathList) == 0 {
@@ -98,3 +98,34 @@ func (p *project) IsRunningWithGoRun() bool {
return false
}
// IsInIDE 检查当前程序是否在IDE中运行
func (p *project) IsInIDE() bool {
// 检查常见的 IDE环境变量
ideEnvVars := []string{
"PYCHARM_HOSTED", // PyCharm
"IDEA_INITIAL_DIRECTORY", // IntelliJ IDEA
"VSCODE_PID", // VS Code
"GOLAND", // GoLand
"PHPSTORM", // PhpStorm
}
for _, env := range ideEnvVars {
if os.Getenv(env) != "" {
return true
}
}
// 检查父进程
parentProcess := os.Getenv("_")
if parentProcess != "" {
ideProcesses := []string{"goland", "idea", "pycharm", "vscode", "code"}
for _, ide := range ideProcesses {
if strings.Contains(strings.ToLower(parentProcess), ide) {
return true
}
}
}
return false
}