-
Notifications
You must be signed in to change notification settings - Fork 309
/
macos.ts
207 lines (181 loc) · 6.86 KB
/
macos.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as path from 'path'
import * as _ from 'lodash'
import * as qq from 'qqjs'
import {Command, Flags} from '@oclif/core'
import {Interfaces} from '@oclif/core'
import * as Tarballs from '../../tarballs'
import {templateShortKey} from '../../upload-util'
type OclifConfig = {
macos?: {
identifier?: string;
sign?: string;
};
}
const noBundleConfiguration = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>
`
const scripts = {
preinstall: (config: Interfaces.Config, additionalCLI: string | undefined) => `#!/usr/bin/env bash
sudo rm -rf /usr/local/lib/${config.dirname}
sudo rm -rf /usr/local/${config.bin}
sudo rm -rf /usr/local/bin/${config.bin}
${additionalCLI ?
`sudo rm -rf /usr/local/${additionalCLI}
sudo rm -rf /usr/local/bin/${additionalCLI}` : ''}
`,
postinstall: (config: Interfaces.Config, additionalCLI: string | undefined) => `#!/usr/bin/env bash
set -x
sudo mkdir -p /usr/local/bin
sudo ln -sf /usr/local/lib/${config.dirname}/bin/${config.bin} /usr/local/bin/${config.bin}
${additionalCLI ? `sudo ln -sf /usr/local/lib/${config.dirname}/bin/${additionalCLI} /usr/local/bin/${additionalCLI}` : ''}
`,
uninstall: (config: Interfaces.Config, additionalCLI: string | undefined) => {
const packageIdentifier = (config.pjson.oclif as OclifConfig).macos!.identifier!
return `#!/usr/bin/env bash
#Parameters
DATE=\`date +%Y-%m-%d\`
TIME=\`date +%H:%M:%S\`
LOG_PREFIX="[$DATE $TIME]"
#Functions
log_info() {
echo "\${LOG_PREFIX}[INFO]" $1
}
log_warn() {
echo "\${LOG_PREFIX}[WARN]" $1
}
log_error() {
echo "\${LOG_PREFIX}[ERROR]" $1
}
#Check running user
if (( $EUID != 0 )); then
echo "Please run as root."
exit
fi
echo "Welcome to Application Uninstaller"
echo "The following packages will be REMOVED:"
echo " ${config.dirname}"
while [ "$1" != "-y" ]; do
read -p "Do you wish to continue [Y/n]?" answer
[[ $answer == "y" || $answer == "Y" || $answer == "" ]] && break
[[ $answer == "n" || $answer == "N" ]] && exit 0
echo "Please answer with 'y' or 'n'"
done
echo "Application uninstalling process started"
# remove link to shorcut file
find "/usr/local/bin/" -name "${config.bin}" | xargs rm
${additionalCLI ? `find "/usr/local/bin/" -name "${additionalCLI}" | xargs rm` : ''}
if [ $? -eq 0 ]
then
echo "[1/3] [DONE] Successfully deleted shortcut links"
else
echo "[1/3] [ERROR] Could not delete shortcut links" >&2
fi
#forget from pkgutil
pkgutil --forget "${packageIdentifier}" > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "[2/3] [DONE] Successfully deleted application informations"
else
echo "[2/3] [ERROR] Could not delete application informations" >&2
fi
#remove application source distribution
[ -e "/usr/local/lib/${config.dirname}" ] && rm -rf "/usr/local/lib/${config.dirname}"
#remove application data directory
[ -e "${config.dataDir}" ] && rm -rf "${config.dataDir}"
#remove application cache directory
[ -e "${config.cacheDir}" ] && rm -rf "${config.cacheDir}"
#remove application config directory
[ -e "${config.configDir}" ] && rm -rf "${config.configDir}"
if [ $? -eq 0 ]
then
echo "[3/3] [DONE] Successfully deleted application"
else
echo "[3/3] [ERROR] Could not delete application" >&2
fi
echo "Application uninstall process finished"
exit 0
`
},
}
export default class PackMacos extends Command {
static description = 'pack CLI into macOS .pkg'
static flags = {
root: Flags.string({
char: 'r',
description: 'path to oclif CLI root',
default: '.',
required: true,
}),
'additional-cli': Flags.string({
description: `an Oclif CLI other than the one listed in config.bin that should be made available to the user
the CLI should already exist in a directory named after the CLI that is the root of the tarball produced by "oclif pack:tarballs"`,
hidden: true,
}),
tarball: Flags.string({
char: 't',
description: 'optionally specify a path to a tarball already generated by NPM',
required: false,
}),
targets: Flags.string({
description: 'comma-separated targets to upload (e.g.: darwin-x64, darwin-arm64)',
}),
}
async run(): Promise<void> {
if (process.platform !== 'darwin') this.error('must be run from macos')
const {flags} = await this.parse(PackMacos)
const buildConfig = await Tarballs.buildConfig(flags.root, {targets: flags?.targets?.split(',')})
const {config} = buildConfig
const c = config.pjson.oclif as OclifConfig
if (!c.macos) this.error('package.json is missing an oclif.macos config')
if (!c.macos.identifier) this.error('package.json must have oclif.macos.identifier set')
const macos = c.macos
const packageIdentifier = macos.identifier
await Tarballs.build(buildConfig, {platform: 'darwin', pack: false, tarball: flags.tarball})
const scriptsDir = qq.join(buildConfig.tmp, 'macos/scripts')
await qq.emptyDir(buildConfig.dist('macos'))
const noBundleConfigurationPath = qq.join(buildConfig.tmp, 'macos/no-bundle.plist')
const build = async (arch: Interfaces.ArchTypes) => {
const templateKey = templateShortKey('macos', {bin: config.bin, version: config.version, sha: buildConfig.gitSha, arch})
const dist = buildConfig.dist(`macos/${templateKey}`)
const rootDir = buildConfig.workspace({platform: 'darwin', arch})
const writeNoBundleConfiguration = async () => {
await qq.write(noBundleConfigurationPath, noBundleConfiguration)
await qq.chmod(noBundleConfigurationPath, 0o755)
}
const writeScript = async (script: 'preinstall' | 'postinstall' | 'uninstall') => {
const path = script === 'uninstall' ? [rootDir, 'bin'] : [scriptsDir]
path.push(script)
await qq.write(path, scripts[script](config, flags['additional-cli']))
await qq.chmod(path, 0o755)
}
await writeNoBundleConfiguration();
await writeScript('preinstall')
await writeScript('postinstall')
await writeScript('uninstall')
/* eslint-disable array-element-newline */
const args = [
'--root', rootDir,
'--component-plist', noBundleConfigurationPath,
'--identifier', packageIdentifier,
'--version', config.version,
'--install-location', `/usr/local/lib/${config.dirname}`,
'--scripts', scriptsDir,
]
/* eslint-enable array-element-newline */
if (macos.sign) {
args.push('--sign', macos.sign)
} else this.debug('Skipping macOS pkg signing')
if (process.env.OSX_KEYCHAIN) args.push('--keychain', process.env.OSX_KEYCHAIN)
args.push(dist)
await qq.x('pkgbuild', args as string[])
}
const arches = _.uniq(buildConfig.targets
.filter(t => t.platform === 'darwin')
.map(t => t.arch))
// eslint-disable-next-line no-await-in-loop
for (const a of arches) await build(a)
}
}