-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathprepare.js
More file actions
118 lines (108 loc) · 2.94 KB
/
prepare.js
File metadata and controls
118 lines (108 loc) · 2.94 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
// @ts-check
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Creates a Microsoft Windows Installer (TM) compatible version from the provided crate's semver version.
* `major.minor.patch.build`
*
* @see {@link https://tauri.app/reference/config/#version-1}
* @param {string} cargoFilePath
* @returns {Promise<string>}
*/
async function semverToWIXCompatibleVersion(cargoFilePath) {
const config = await fs.readFile(cargoFilePath, "utf-8");
const match = /version\s*=\s*"([\w.-]+)"/.exec(config);
if (!match)
throw new Error(
'Failed to extract version from "Cargo.toml". Have you removed the main crate version by accident?',
);
const ver = match[1];
const [core, buildOrPrerelease] = ver.includes("+")
? ver.split("+")
: ver.split("-");
const [major, minor, patch] = core.split(".");
let build = 0;
if (buildOrPrerelease) {
const numMatch = buildOrPrerelease.match(/\d+$/);
build = numMatch ? parseInt(numMatch[0], 10) : 0;
}
const wixVersion = `${major}.${minor}.${patch}${
build === 0 ? "" : `.${build}`
}`;
if (wixVersion !== ver)
console.log(`Using wix-compatible version ${ver} --> ${wixVersion}`);
return wixVersion;
}
/**
* Deeply merges two objects
*
* @param {Object} target
* @param {Object} source
* @returns {Object}
*/
function deepMerge(target, source) {
for (const key of Object.keys(source)) {
if (
source[key] instanceof Object &&
key in target &&
target[key] instanceof Object
) {
Object.assign(source[key], deepMerge(target[key], source[key]));
}
}
return { ...target, ...source };
}
/**
* Writes platform-specific tauri configs
*
* @param {NodeJS.Platform} platform
* @param {{} | undefined} configOptions
*/
export async function createTauriPlatformConfigs(
platform,
configOptions = undefined,
) {
const srcTauri = path.join(__dirname, "../src-tauri/");
let baseConfig = {};
let configFileName = null;
console.log(`Updating Platform (${platform}) Tauri config...`);
if (platform === "win32") {
configFileName = "tauri.windows.conf.json";
baseConfig = {
...baseConfig,
bundle: {
resources: {
"../../../target/ffmpeg/bin/*.dll": "./",
},
windows: {
wix: {
version: await semverToWIXCompatibleVersion(
path.join(srcTauri, "Cargo.toml"),
),
},
},
},
};
}
if (!configFileName) return;
const mergedConfig = configOptions
? deepMerge(baseConfig, configOptions)
: baseConfig;
await fs.writeFile(
`${srcTauri}/${configFileName}`,
JSON.stringify(mergedConfig, null, 2),
);
}
async function main() {
console.log("--- Preparing sidecars and configs...");
await createTauriPlatformConfigs(process.platform);
console.log("--- Preparation finished");
}
main().catch((err) => {
console.error("\n--- Preparation Failed");
console.error(err);
console.error("---");
});