-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapp_darwin.go
More file actions
176 lines (142 loc) · 4.79 KB
/
app_darwin.go
File metadata and controls
176 lines (142 loc) · 4.79 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package app
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/ActiveState/cli/internal/assets"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/strutils"
)
const (
execFileSource = "exec.sh.tpl"
launchFileSource = "com.activestate.platform.app.plist.tpl"
iconFile = "icon.icns"
assetAppDir = "placeholder.app"
)
func (a *App) install() error {
// Create all of the necessary directories and files in a temporary directory
// Then move the temporary directory to the final location which for macOS will be the Applications directory
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-", a.Name))
if err != nil {
return errs.Wrap(err, "Could not create temporary directory")
}
defer os.RemoveAll(tmpDir)
appPath := a.Path()
tmpAppPath := filepath.Join(tmpDir, filepath.Base(appPath))
if err := fileutils.Mkdir(tmpAppPath); err != nil {
return errs.Wrap(err, "Could not create .app directory")
}
if err := fileutils.CopyFilesDirReader(assets.NewAssetsFS(), assetAppDir, tmpAppPath, assets.PlaceholderFileName); err != nil {
return errs.Wrap(err, "Could not copy files from assets")
}
if err := a.createIcon(tmpAppPath); err != nil {
return errs.Wrap(err, "Could not create icon")
}
if err := a.createExecFile(tmpAppPath); err != nil {
return errs.Wrap(err, "Could not create exec file")
}
if err := a.createInfoFile(tmpAppPath); err != nil {
return errs.Wrap(err, "Could not create info file")
}
installDir := filepath.Dir(appPath)
if err := fileutils.MkdirUnlessExists(installDir); err != nil {
return errs.Wrap(err, "Could not create app parent directory: %s", installDir)
}
if fileutils.DirExists(appPath) {
if err := os.RemoveAll(appPath); err != nil {
return errs.Wrap(err, "Could not remove existing app directory: %s", appPath)
}
}
if err := fileutils.CopyAndRenameFiles(tmpDir, installDir); err != nil {
return errs.Wrap(err, "Could not move .app to Applications directory")
}
return nil
}
func (a *App) Path() string {
installDir := a.Dir
if override := os.Getenv(constants.AppInstallDirOverrideEnvVarName); override != "" {
installDir = override
}
return filepath.Join(installDir, fmt.Sprintf("%s.app", a.Name))
}
func (a *App) createIcon(path string) error {
icon, err := assets.ReadFile(a.options.IconFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
if err = fileutils.WriteFile(filepath.Join(path, "Contents", "Resources", iconFile), icon); err != nil {
return errs.Wrap(err, "Could not write icon file")
}
return nil
}
func (a *App) createExecFile(base string) error {
path := filepath.Join(base, "Contents", "MacOS")
asset, err := assets.ReadFile(execFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
scriptFile := fmt.Sprintf("%s.sh", filepath.Base(a.Exec))
content, err := strutils.ParseTemplate(
string(asset),
map[string]interface{}{
"Exec": a.Exec,
"Args": strings.Join(a.Args, " "),
}, nil)
if err != nil {
return errs.Wrap(err, "Could not parse launch file source")
}
err = fileutils.WriteFile(filepath.Join(path, scriptFile), []byte(content))
if err != nil {
return errs.Wrap(err, "Could not write Info.plist file")
}
err = os.Chmod(filepath.Join(path, scriptFile), 0755)
if err != nil {
return errs.Wrap(err, "Could not make executable")
}
return nil
}
func (a *App) createInfoFile(base string) error {
path := filepath.Join(base, "Contents")
asset, err := assets.ReadFile(launchFileSource)
if err != nil {
return errs.Wrap(err, "Could not read asset")
}
scriptFile := fmt.Sprintf("%s.sh", filepath.Base(a.Exec))
content, err := strutils.ParseTemplate(
string(asset),
map[string]interface{}{
"Exec": scriptFile,
"Icon": a.options.IconFileName,
"HideDockIcon": a.options.MacHideDockIcon,
"IsGUIApp": a.options.IsGUIApp,
}, nil)
if err != nil {
return errs.Wrap(err, "Could not parse launch file source")
}
err = fileutils.WriteFile(filepath.Join(path, "Info.plist"), []byte(content))
if err != nil {
return errs.Wrap(err, "Could not write Info.plist file")
}
return nil
}
func (a *App) uninstall() error {
baseDir := a.Dir
if override := os.Getenv(constants.AppInstallDirOverrideEnvVarName); override != "" {
baseDir = override
}
installDir := filepath.Join(baseDir, fmt.Sprintf("%s.app", a.Name))
if !fileutils.DirExists(installDir) {
logging.Debug("Directory does not exist, nothing to do")
return nil
}
if err := os.RemoveAll(installDir); err != nil {
logging.Debug("Could not remove %s: %v", installDir, err)
return errs.Wrap(err, "Could not remove .app from Applications directory")
}
return nil
}