Skip to content

Commit

Permalink
feat: add task packMacPkg
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Sep 25, 2022
1 parent b824d40 commit b552dec
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/build/tasks/pack/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parallel } from 'gulp'
import { Exceptions } from '../../utils/exceptions'
import { packMacApp } from './mac'
import { packMac } from './mac'
import { packMsi } from './msi'
import { packPortable } from './portable'

Expand All @@ -13,7 +13,7 @@ const buildPack = () => {
case 'win32':
return parallel(packPortable, packMsi)
case 'darwin':
return parallel(packPortable, packMacApp)
return parallel(packPortable, packMac)
case 'linux':
return parallel(packPortable)
default:
Expand Down
58 changes: 55 additions & 3 deletions packages/build/tasks/pack/mac.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { parallel, series } from 'gulp'
import mkdirp from 'mkdirp'
import { promises as fs } from 'node:fs'
import { macAppPlist } from '../../templates'
import { macAppPlist, macPkgDistribution } from '../../templates'
import { koiVersion } from '../../utils/config'
import { dir } from '../../utils/path'
import { tryExec } from '../../utils/spawn'
import { exec, tryExec } from '../../utils/spawn'

const appPath = dir('buildMac', 'Koishi.app/')

export const packMacApp = async () => {
const appPath = dir('buildMac', 'Koishi.app/')
const appInfoPlistPath = dir('buildMac', 'Koishi.app/Contents/Info.plist')
const appMacosPath = dir('buildMac', 'Koishi.app/Contents/MacOS/')
const appResourcesPath = dir('buildMac', 'Koishi.app/Contents/Resources/')
Expand All @@ -21,10 +23,60 @@ export const packMacApp = async () => {
await fs.cp(dir('buildPortable'), appMacosPath, { recursive: true })
await fs.copyFile(dir('buildAssets', 'koishi.icns'), appIconPath)
await fs.writeFile(appInfoPlistPath, macAppPlist)
}

export const packMacDmg = async () => {
await tryExec('yarn', ['create-dmg', appPath, dir('buildMac'), '--overwrite'])
await fs.rename(
dir('buildMac', `Koishi ${koiVersion}.dmg`),
dir('dist', 'koishi.dmg')
)
}

export const packMacPkg = async () => {
const scriptsPath = dir('buildMac', 'scripts/')
const postinstallPath = dir('buildMac', 'scripts/postinstall')

await mkdirp(scriptsPath)

await fs.writeFile(dir('buildMac', 'distribution.xml'), macPkgDistribution)
await fs.writeFile(
postinstallPath,
`
#!/bin/bash
echo "Starting post-install process..."
xattr -d com.apple.quarantine /Applications/Koishi.app/ || true
echo "Post-install process finished."
`.trim()
)

await exec(
'pkgbuild',
[
'--identifier',
'chat.koishi.desktop',
'--component',
'Koishi.app',
'--scripts',
'scripts',
'--install-location',
'/Applications',
'koishi-app.pkg',
],
dir('buildMac')
)

await exec(
'productbuild',
[
'--distribution',
'distribution.xml',
'--package-path',
'.',
dir('dist', 'koishi.pkg'),
],
dir('buildMac')
)
}

export const packMac = series(packMacApp, parallel(packMacDmg, packMacPkg))
38 changes: 38 additions & 0 deletions packages/build/templates/mac/distribution.xml.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<installer-gui-script minSpecVersion="1">
<title>Koishi</title>
<options
allow-external-scripts="false"
customize="never"
require-scripts="true"
hostArchitectures="x86_64,arm64"
rootVolumeOnly="true"
/>
<domains
enable_anywhere="false"
enable_currentUserHome="false"
enable_localSystem="true"
/>
<choices-outline>
<line choice="chat.koishi.desktop" />
</choices-outline>
<choice
id="chat.koishi.desktop"
title="Koishi"
description="The Koishi app."
description-mime-type="text/plain"
visible="false"
>
<pkg-ref id="chat.koishi.desktop" />
</choice>
<pkg-ref id="chat.koishi.desktop">
<must-close>
<app id="chat.koishi.desktop" />
</must-close>
</pkg-ref>
<pkg-ref
id="chat.koishi.desktop"
version="{{koiSemver.major}}.{{koiSemver.minor}}.{{koiSemver.patch}}"
onConclusion="none"
>koishi-app.pkg</pkg-ref>
</installer-gui-script>
8 changes: 7 additions & 1 deletion packages/build/templates/mac/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Handlebars from 'handlebars'
import * as fs from 'node:fs'
import * as path from 'node:path'
import { koiVersion } from '../../utils/config'
import { koiSemver, koiVersion } from '../../utils/config'

export const macAppPlist = Handlebars.compile(
fs.readFileSync(path.join(__dirname, 'mac-app.plist.hbs')).toString('utf-8')
)({ koiVersion })

export const macPkgDistribution = Handlebars.compile(
fs
.readFileSync(path.join(__dirname, 'distribution.xml.hbs'))
.toString('utf-8')
)({ koiSemver })

0 comments on commit b552dec

Please sign in to comment.