-
Notifications
You must be signed in to change notification settings - Fork 121
/
clean.go
96 lines (82 loc) · 2.52 KB
/
clean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
var jbProducts = []string{"idea", "clion", "phpstorm", "goland", "pycharm", "webstorm", "webide", "rider", "datagrip", "rubymine", "appcode", "dataspell", "gateway", "jetbrains_client", "jetbrainsclient", "studio", "devecostudio"}
func Clean() {
switch runtime.GOOS {
case "darwin":
homeDir, _ := os.UserHomeDir()
vmSHFile := ".jetbrains.vmoptions.sh"
myVmOptionsShellFile := homeDir + "/" + vmSHFile
_ = os.Remove(myVmOptionsShellFile)
for _, prd := range jbProducts {
envName := strings.ToUpper(prd) + "_VM_OPTIONS"
_ = os.Unsetenv(envName)
cmd := fmt.Sprintf("launchctl unsetenv %s", envName)
_ = exec.Command("sh", "-c", cmd).Run()
}
plistPath := homeDir + "/Library/LaunchAgents/jetbrains.vmoptions.plist"
_ = os.Remove(plistPath)
removeLineFromFile(homeDir+"/.profile", vmSHFile)
removeLineFromFile(homeDir+"/.bash_profile", vmSHFile)
removeLineFromFile(homeDir+"/.zshrc", vmSHFile)
case "windows":
isClean := true
for _, prd := range jbProducts {
envKey := strings.ToUpper(prd) + "_VM_OPTIONS"
if os.Getenv(envKey) != "" {
isClean = false
}
_ = os.Unsetenv(envKey)
}
if !isClean {
var cleanVBSPath = jetPath + "/win_clean.vbs"
cleanFileData, err := scriptFS.ReadFile("script/win_clean.vbs")
if err == nil {
os.WriteFile(cleanVBSPath, cleanFileData, 0644)
}
err = exec.Command("cmd.exe", "/c", cleanVBSPath).Run()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
}
}
case "linux":
homeDir, _ := os.UserHomeDir()
vmSHFile := ".jetbrains.vmoptions.sh"
myVmOptionsShellFile := homeDir + "/" + vmSHFile
_ = os.Remove(myVmOptionsShellFile)
for _, prd := range jbProducts {
envName := strings.ToUpper(prd) + "_VM_OPTIONS"
_ = os.Unsetenv(envName)
}
removeLineFromFile(homeDir+"/.profile", vmSHFile)
removeLineFromFile(homeDir+"/.bashrc", vmSHFile)
removeLineFromFile(homeDir+"/.zshrc", vmSHFile)
kdeEnvDir := homeDir + "/.config/plasma-workspace/env"
_ = os.Remove(kdeEnvDir + "/jetbrains.vmoptions.sh")
}
}
func removeLineFromFile(filePath string, lineToRemove string) {
input, err := os.ReadFile(filePath)
if err != nil {
//fmt.Println(err)
return
}
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, lineToRemove) {
lines = append(lines[:i], lines[i+1:]...)
}
}
output := strings.Join(lines, "\n")
err = os.WriteFile(filePath, []byte(output), 0644)
if err != nil {
fmt.Println(err)
return
}
}