feat: 增加程序是否在IDE中运行的检测
This commit is contained in:
35
project.go
35
project.go
@@ -36,8 +36,8 @@ func (p *project) GetFileSystemSeparator() string {
|
|||||||
// BuildPath 构建路径
|
// BuildPath 构建路径
|
||||||
func (p *project) BuildPath(filePathList ...string) string {
|
func (p *project) BuildPath(filePathList ...string) string {
|
||||||
projectRootPath, _ := p.GetExecutablePath()
|
projectRootPath, _ := p.GetExecutablePath()
|
||||||
if p.IsRunningWithGoRun() {
|
if p.IsRunningWithGoRun() || p.IsInIDE() {
|
||||||
// go run 运行, 则工作目录为项目目录
|
// go run 运行 或 者 ide 中运行, 则工作目录为项目目录
|
||||||
projectRootPath = p.GetWorkDir()
|
projectRootPath = p.GetWorkDir()
|
||||||
}
|
}
|
||||||
if len(filePathList) == 0 {
|
if len(filePathList) == 0 {
|
||||||
@@ -98,3 +98,34 @@ func (p *project) IsRunningWithGoRun() bool {
|
|||||||
|
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user