-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
env.go
140 lines (115 loc) · 3.01 KB
/
env.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package env
import (
"crypto/md5"
"encoding/hex"
"github.com/hashload/boss/consts"
"github.com/hashload/boss/msg"
"github.com/mitchellh/go-homedir"
"golang.org/x/sys/windows/registry"
"os"
"os/exec"
"path/filepath"
"strings"
)
var Global bool
var Internal = false
var GlobalConfiguration, _ = LoadConfiguration(GetBossHome())
func HashDelphiPath() string {
hasher := md5.New()
hasher.Write([]byte(strings.ToLower(GlobalConfiguration.DelphiPath)))
hashString := hex.EncodeToString(hasher.Sum(nil))
if Internal {
hashString = consts.BossInternalDir + hashString
}
return hashString
}
func GetInternalGlobalDir() string {
return filepath.Join(GetBossHome(), consts.FolderDependencies, consts.BossInternalDir+HashDelphiPath())
}
func getwd() string {
if Global {
return filepath.Join(GetBossHome(), consts.FolderDependencies, HashDelphiPath())
} else {
if dir, err := os.Getwd(); err != nil {
msg.Err("Error to get paths", err)
return ""
} else {
return dir
}
}
}
func GetCacheDir() string {
s := os.Getenv("BOSS_CACHE_DIR")
if s == "" {
s = filepath.Join(GetBossHome(), "cache")
}
return s
}
func GetBossHome() string {
cacheDir, e := homedir.Dir()
if e != nil {
msg.Err("Error to get cache paths", e)
}
cacheDir = filepath.FromSlash(cacheDir)
return filepath.Join(cacheDir, consts.FolderBossHome)
}
func GetBossFile() string {
return filepath.Join(GetCurrentDir(), consts.FilePackage)
}
func GetModulesDir() string {
return filepath.Join(GetCurrentDir(), consts.FolderDependencies)
}
func GetCurrentDir() string {
return getwd()
}
func GetGlobalBinPath() string {
return filepath.Join(GetBossHome(), consts.FolderDependencies, consts.BinFolder)
}
func GetDcc32Dir() string {
if GlobalConfiguration.DelphiPath != "" {
return GlobalConfiguration.DelphiPath
}
command := exec.Command("where", "dcc32")
output, err := command.Output()
if err != nil {
msg.Warn("dcc32 not found")
}
outputStr := strings.ReplaceAll(string(output), "\n", "")
outputStr = strings.ReplaceAll(outputStr, "\r", "")
outputStr = filepath.Dir(outputStr)
return outputStr
}
func HandleError(err error) {
if err != nil {
msg.Err(err.Error())
}
}
func HandleErrorFatal(err error) {
if err != nil {
msg.Die(err.Error())
}
}
func GetDelphiVersionFromRegisty() string {
delphiVersions, err := registry.OpenKey(registry.CURRENT_USER, `Software\Embarcadero\BDS\`,
registry.ALL_ACCESS)
if err != nil {
msg.Err("Cannot open registry to IDE version")
}
keyInfo, err := delphiVersions.Stat()
HandleError(err)
names, err := delphiVersions.ReadSubKeyNames(int(keyInfo.SubKeyCount))
HandleError(err)
for _, value := range names {
delphiInfo, err := registry.OpenKey(registry.CURRENT_USER, `Software\Embarcadero\BDS\`+value, registry.QUERY_VALUE)
HandleError(err)
appPath, _, err := delphiInfo.GetStringValue("App")
if os.IsNotExist(err) {
continue
}
HandleError(err)
if strings.HasPrefix(strings.ToLower(appPath), strings.ToLower(GlobalConfiguration.DelphiPath)) {
return value
}
}
return ""
}