2022-01-07 16:52:02 +01:00
|
|
|
import {resolveLibs} from "./RollupConfig.js"
|
2022-03-02 18:21:01 +01:00
|
|
|
import {nativeDepWorkaroundPlugin} from "./RollupPlugins.js"
|
2019-09-13 13:49:11 +02:00
|
|
|
import nodeResolve from "@rollup/plugin-node-resolve"
|
|
|
|
|
import fs from "fs"
|
2022-01-12 14:43:01 +01:00
|
|
|
import path, {dirname} from "path"
|
2019-09-13 13:49:11 +02:00
|
|
|
import {rollup} from "rollup"
|
|
|
|
|
import {terser} from "rollup-plugin-terser"
|
|
|
|
|
import commonjs from "@rollup/plugin-commonjs"
|
|
|
|
|
import electronBuilder from "electron-builder"
|
2022-03-02 18:21:01 +01:00
|
|
|
import generatePackageJson from "./electron-package-json-template.js"
|
2019-09-13 13:49:11 +02:00
|
|
|
import {create as createEnv, preludeEnvPlugin} from "./env.js"
|
2021-06-17 14:33:18 +02:00
|
|
|
import cp from 'child_process'
|
|
|
|
|
import util from 'util'
|
2022-01-07 16:52:02 +01:00
|
|
|
import typescript from "@rollup/plugin-typescript"
|
2022-02-21 17:48:12 +01:00
|
|
|
import {keytarNativePlugin, sqliteNativeBannerPlugin} from "./nativeLibraryRollupPlugin.js"
|
2022-01-12 14:43:01 +01:00
|
|
|
import {fileURLToPath} from "url"
|
2018-09-24 11:56:41 +02:00
|
|
|
|
2021-08-11 17:53:48 +02:00
|
|
|
const exec = util.promisify(cp.exec)
|
2022-01-12 14:43:01 +01:00
|
|
|
const buildSrc = dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
const projectRoot = path.resolve(path.join(buildSrc, ".."))
|
2019-09-13 13:49:11 +02:00
|
|
|
|
2022-02-21 15:17:37 +01:00
|
|
|
/**
|
|
|
|
|
* @param dirname directory this was called from
|
|
|
|
|
* @param version application version that gets built
|
2022-03-02 18:21:01 +01:00
|
|
|
* @param platform: {"linux"|"win32"|"darwin"} - Canonical platform name of the desktop target to be built
|
2022-02-21 15:17:37 +01:00
|
|
|
* @param updateUrl where the client should pull its updates from, if any
|
|
|
|
|
* @param nameSuffix suffix used to distinguish test-, prod- or snapshot builds on the same machine
|
|
|
|
|
* @param notarize for the MacOs notarization feature
|
|
|
|
|
* @param outDir where copy the finished artifacts
|
|
|
|
|
* @param unpacked output desktop client without packing it into an installer
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
export async function buildDesktop(
|
|
|
|
|
{
|
|
|
|
|
dirname,
|
|
|
|
|
version,
|
2022-03-02 18:21:01 +01:00
|
|
|
platform,
|
2022-02-21 15:17:37 +01:00
|
|
|
updateUrl,
|
|
|
|
|
nameSuffix,
|
|
|
|
|
notarize,
|
|
|
|
|
outDir,
|
|
|
|
|
unpacked,
|
|
|
|
|
}
|
|
|
|
|
) {
|
2019-09-13 13:49:11 +02:00
|
|
|
// The idea is that we
|
|
|
|
|
// - build desktop code into build/dist/desktop
|
|
|
|
|
// - package the whole dist directory into the app
|
|
|
|
|
// - move installers out of the dist into build/desktop-whatever
|
|
|
|
|
// - cleanup dist directory
|
|
|
|
|
// It's messy
|
2022-02-21 15:17:37 +01:00
|
|
|
|
2022-03-02 18:21:01 +01:00
|
|
|
console.log(`Building ${platform} desktop client for v${version}`)
|
|
|
|
|
const updateSubDir = `desktop${nameSuffix}`
|
2019-09-13 13:49:11 +02:00
|
|
|
const distDir = path.join(dirname, "build", "dist")
|
2022-03-02 18:21:01 +01:00
|
|
|
outDir = path.join(outDir ?? path.join(distDir, ".."), updateSubDir)
|
2019-09-13 13:49:11 +02:00
|
|
|
await fs.promises.mkdir(outDir, {recursive: true})
|
|
|
|
|
|
2019-11-07 16:08:49 +01:00
|
|
|
|
2019-09-13 13:49:11 +02:00
|
|
|
// We need to get the right build of native dependencies. There's a tool called node-gyp which can build for different architectures
|
|
|
|
|
// and downloads everything it needs. Usually dependencies build themselves in post-install script.
|
|
|
|
|
// Currently we have keytar which avoids building itself if possible and only build
|
2018-12-13 17:59:47 +01:00
|
|
|
console.log("Updating electron-builder config...")
|
2022-03-02 18:21:01 +01:00
|
|
|
const content = generatePackageJson({
|
2019-09-13 13:49:11 +02:00
|
|
|
nameSuffix,
|
|
|
|
|
version,
|
|
|
|
|
updateUrl,
|
2020-04-07 10:32:03 +02:00
|
|
|
iconPath: path.join(dirname, "/resources/desktop-icons/logo-solo-red.png"),
|
2019-09-13 13:49:11 +02:00
|
|
|
notarize,
|
|
|
|
|
unpacked,
|
2021-03-16 11:16:48 +01:00
|
|
|
sign: (process.env.DEBUG_SIGN && updateUrl !== "") || !!process.env.JENKINS,
|
2020-04-07 10:32:03 +02:00
|
|
|
})
|
2019-09-13 13:49:11 +02:00
|
|
|
console.log("updateUrl is", updateUrl)
|
|
|
|
|
await fs.promises.writeFile("./build/dist/package.json", JSON.stringify(content), 'utf-8')
|
2022-03-02 18:21:01 +01:00
|
|
|
if (platform === "win32") await getMapirs(distDir)
|
2021-06-17 14:33:18 +02:00
|
|
|
|
2019-09-13 13:49:11 +02:00
|
|
|
// prepare files
|
|
|
|
|
try {
|
2021-06-17 14:33:18 +02:00
|
|
|
await fs.promises.rm(path.join(distDir, "..", updateSubDir), {recursive: true})
|
2019-09-13 13:49:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
if (e.code !== 'ENOENT') {
|
|
|
|
|
throw e
|
2018-12-13 17:59:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2019-09-13 13:49:11 +02:00
|
|
|
console.log("Bundling desktop client")
|
2022-03-02 18:21:01 +01:00
|
|
|
await rollupDesktop(dirname, path.join(distDir, "desktop"), version, platform)
|
2018-12-13 17:59:47 +01:00
|
|
|
|
2019-09-13 13:49:11 +02:00
|
|
|
console.log("Starting installer build...")
|
2021-08-11 17:53:48 +02:00
|
|
|
if (process.platform.startsWith("darwin")) {
|
|
|
|
|
// dmg-license is required by electron to build the mac installer
|
|
|
|
|
// We can't put dmg-license as a dependency in package.json because
|
|
|
|
|
// it will cause npm install to fail if you do it in linux or windows
|
|
|
|
|
// We could install it in mac and then it will be in package-lock.json
|
|
|
|
|
// but then we will have to be vigilant that it doesn't get removed ever
|
|
|
|
|
await exec("npm install dmg-license")
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 13:49:11 +02:00
|
|
|
// package for linux, win, mac
|
|
|
|
|
await electronBuilder.build({
|
|
|
|
|
_: ['build'],
|
2022-03-02 18:21:01 +01:00
|
|
|
win: platform === "win32" ? [] : undefined,
|
|
|
|
|
mac: platform === "darwin" ? [] : undefined,
|
|
|
|
|
linux: platform === "linux" ? [] : undefined,
|
2021-02-23 08:36:38 +01:00
|
|
|
publish: 'always',
|
2019-09-13 13:49:11 +02:00
|
|
|
project: distDir
|
|
|
|
|
})
|
|
|
|
|
console.log("Move output to ", outDir)
|
|
|
|
|
await fs.promises.mkdir(outDir, {recursive: true})
|
|
|
|
|
await Promise.all(
|
|
|
|
|
fs.readdirSync(path.join(distDir, '/installers'))
|
2021-04-06 14:43:41 +02:00
|
|
|
.filter((file => file.startsWith(content.name) || file.endsWith('.yml') || file.endsWith("-unpacked")))
|
|
|
|
|
.map(file => fs.promises.rename(
|
2022-01-07 16:52:02 +01:00
|
|
|
path.join(distDir, '/installers/', file),
|
|
|
|
|
path.join(outDir, file)
|
2021-05-19 13:53:01 +02:00
|
|
|
)
|
|
|
|
|
)
|
2019-09-13 13:49:11 +02:00
|
|
|
)
|
|
|
|
|
await Promise.all([
|
2021-06-17 14:33:18 +02:00
|
|
|
fs.promises.rm(path.join(distDir, '/installers/'), {recursive: true}),
|
|
|
|
|
fs.promises.rm(path.join(distDir, '/node_modules/'), {recursive: true}),
|
2019-09-13 13:49:11 +02:00
|
|
|
fs.promises.unlink(path.join(distDir, '/package.json')),
|
|
|
|
|
fs.promises.unlink(path.join(distDir, '/package-lock.json'),),
|
|
|
|
|
])
|
|
|
|
|
}
|
2018-12-13 17:59:47 +01:00
|
|
|
|
2022-03-02 18:21:01 +01:00
|
|
|
async function rollupDesktop(dirname, outDir, version, platform) {
|
2019-09-13 13:49:11 +02:00
|
|
|
const mainBundle = await rollup({
|
2022-01-07 16:52:02 +01:00
|
|
|
input: path.join(dirname, "src/desktop/DesktopMain.ts"),
|
2021-02-23 08:36:38 +01:00
|
|
|
preserveEntrySignatures: false,
|
2019-09-13 13:49:11 +02:00
|
|
|
plugins: [
|
2022-01-07 16:52:02 +01:00
|
|
|
typescript({
|
|
|
|
|
tsconfig: "tsconfig.json",
|
|
|
|
|
outDir,
|
|
|
|
|
}),
|
2019-09-13 13:49:11 +02:00
|
|
|
resolveLibs(),
|
|
|
|
|
nativeDepWorkaroundPlugin(),
|
2022-02-21 17:48:12 +01:00
|
|
|
keytarNativePlugin({
|
|
|
|
|
rootDir: projectRoot,
|
2022-03-02 18:21:01 +01:00
|
|
|
platform
|
2022-02-21 17:48:12 +01:00
|
|
|
}),
|
2019-09-13 13:49:11 +02:00
|
|
|
nodeResolve({preferBuiltins: true}),
|
2021-05-07 10:28:22 +02:00
|
|
|
// requireReturnsDefault: "preferred" is needed in order to correclty generate a wrapper for the native keytar module
|
2021-05-03 14:03:52 +02:00
|
|
|
commonjs({
|
|
|
|
|
exclude: "src/**",
|
|
|
|
|
requireReturnsDefault: "preferred",
|
2022-01-12 14:43:01 +01:00
|
|
|
ignoreDynamicRequires: true,
|
2021-05-03 14:03:52 +02:00
|
|
|
}),
|
2021-01-29 13:56:41 +01:00
|
|
|
terser(),
|
2022-01-12 14:43:01 +01:00
|
|
|
preludeEnvPlugin(createEnv({staticUrl: null, version, mode: "Desktop", dist: true})),
|
|
|
|
|
sqliteNativeBannerPlugin(
|
|
|
|
|
{
|
|
|
|
|
environment: "electron",
|
|
|
|
|
rootDir: projectRoot,
|
2022-03-02 18:21:01 +01:00
|
|
|
dstPath: "./build/dist/desktop/better_sqlite3.node",
|
2022-02-10 16:32:47 +01:00
|
|
|
// Relative to the source file from which the .node file is loaded
|
|
|
|
|
// In our case it will be desktop/DesktopMain.js, which is located in the same directory
|
|
|
|
|
// This depends on the changes we made in our own fork of better_sqlite3
|
|
|
|
|
nativeBindingPath: "./better_sqlite3.node",
|
2022-03-02 18:21:01 +01:00
|
|
|
platform,
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
),
|
2019-09-13 13:49:11 +02:00
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
await mainBundle.write({sourcemap: true, format: "commonjs", dir: outDir})
|
2021-02-23 08:36:38 +01:00
|
|
|
await fs.promises.copyFile(path.join(dirname, "src/desktop/preload.js"), path.join(outDir, "preload.js"))
|
2022-02-23 11:55:07 +01:00
|
|
|
await fs.promises.copyFile(path.join(dirname, "src/desktop/preload-webdialog.js"), path.join(outDir, "preload-webdialog.js"))
|
2021-06-17 14:33:18 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:50:46 +02:00
|
|
|
|
2021-09-30 10:27:02 +02:00
|
|
|
/**
|
|
|
|
|
* get the DLL that's needed for the windows client to handle "Send as Mail..." context
|
|
|
|
|
* menu actions.
|
|
|
|
|
* Tries to get a locally built version before delegating to downloadLatestMapirs
|
|
|
|
|
* @param distDir the directory to put the DLL
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
2021-09-09 15:50:46 +02:00
|
|
|
async function getMapirs(distDir) {
|
|
|
|
|
const dllName = "mapirs.dll"
|
|
|
|
|
const dllSrc = process.platform === "win32"
|
2021-09-28 16:10:39 +02:00
|
|
|
? path.join('../mapirs/target/x86_64-pc-windows-msvc/release', dllName)
|
2021-09-09 15:50:46 +02:00
|
|
|
: path.join('../mapirs/target/x86_64-pc-windows-gnu/release', dllName)
|
|
|
|
|
const dllTrg = path.join(distDir, dllName)
|
|
|
|
|
console.log("trying to copy", dllName, "from", dllSrc, "to", dllTrg)
|
|
|
|
|
try {
|
|
|
|
|
await fs.promises.copyFile(dllSrc, dllTrg)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("no local", dllName, "found, using release from github")
|
|
|
|
|
await downloadLatestMapirs(dllName, dllTrg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 10:27:02 +02:00
|
|
|
/**
|
|
|
|
|
* get the latest mapirs.dll release from github.
|
|
|
|
|
* @param dllName {string} name of the file that should be downloaded from the latest release
|
|
|
|
|
* @param dllTrg {string} path to put the downloaded file
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
2021-09-09 15:50:46 +02:00
|
|
|
async function downloadLatestMapirs(dllName, dllTrg) {
|
|
|
|
|
const {Octokit} = await import("@octokit/rest")
|
|
|
|
|
const octokit = new Octokit();
|
|
|
|
|
const opts = {
|
|
|
|
|
owner: "tutao",
|
|
|
|
|
repo: "mapirs"
|
|
|
|
|
}
|
|
|
|
|
const res = await octokit.request('GET /repos/{owner}/{repo}/releases/latest', opts)
|
|
|
|
|
const asset_id = res.data.assets.find(a => a.name.startsWith(dllName)).id
|
|
|
|
|
const asset = await octokit.repos.getReleaseAsset(Object.assign(opts, {
|
|
|
|
|
asset_id,
|
|
|
|
|
headers: {
|
|
|
|
|
"Accept": "application/octet-stream"
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
await fs.promises.writeFile(dllTrg, Buffer.from(asset.data))
|
2019-09-13 13:49:11 +02:00
|
|
|
}
|