Files
fx_cast/app/bin/build.js

445 lines
13 KiB
JavaScript

"use strict";
const fs = require("fs-extra");
const os = require("os");
const path = require("path");
const minimist = require("minimist");
const glob = require("glob");
const mustache = require("mustache");
const makensis = require("makensis");
const pkg = require("pkg");
const { spawnSync } = require("child_process");
const { __applicationName: applicationName
, __applicationVersion: applicationVersion } = require("../package.json");
const { __extensionId: extensionId } = require("../../ext/package.json");
const { executableName
, executablePath
, manifestName
, manifestPath
, pkgPlatform
, DIST_PATH
, WIN_REGISTRY_KEY } = require("./lib/paths");
// Command line args
const argv = minimist(process.argv.slice(2), {
boolean: [ "package" ]
, string: [ "platform", "arch", "packageType" ]
, default: {
platform: os.platform()
, arch: os.arch()
, package: false
// Linux package type (deb/rpm)
, packageType: "deb"
}
});
const ROOT_PATH = path.join(__dirname, "..");
const SRC_PATH = path.join(ROOT_PATH, "src");
const BUILD_PATH = path.join(ROOT_PATH, "build");
/**
* Shouldn't exist, but cleanup and re-create any existing
* build directories, just in case.
*/
fs.removeSync(BUILD_PATH);
fs.removeSync(DIST_PATH);
fs.ensureDirSync(BUILD_PATH);
fs.ensureDirSync(DIST_PATH, { recursive: true });
async function build () {
// Run tsc
spawnSync(`tsc --project ${ROOT_PATH} \
--outDir ${BUILD_PATH}`
, {
shell: true
, stdio: [ process.stdin, process.stdout, process.stderr ]
});
// Move tsc output to build dir
fs.moveSync(path.join(BUILD_PATH, "src"), BUILD_PATH);
// Copy other files
fs.copySync(SRC_PATH, BUILD_PATH, {
overwrite: true
, filter (src, dest) {
return !/.(js|ts)$/.test(src);
}
});
/**
* Native app manifest
* https://mdn.io/Native_manifests#Native_messaging_manifests
*/
const manifest = {
"name": applicationName
, "description": ""
, "type": "stdio"
, "allowed_extensions": [ extensionId ]
};
/**
* If packaging, add the installed executable path, otherwise
* add the path to the built executable in the dist folder.
*/
if (argv.package) {
// If packaging for windows, use win32 path helpers.
manifest.path = (argv.platform === "win32" ? path.win32 : path)
.join(executablePath[argv.platform]
, executableName[argv.platform]);
} else {
manifest.path = path.join(DIST_PATH, executableName[argv.platform]);
}
// Write app manifest
fs.writeFileSync(path.join(BUILD_PATH, manifestName)
, JSON.stringify(manifest, null, 4));
// Ensure file permissions are correct
for (const file of fs.readdirSync(BUILD_PATH)) {
fs.chmodSync(path.resolve(BUILD_PATH, file), 0o755);
}
// Need a minimal package.json for pkg.
const pkgManifest = {
bin: "main.js"
, pkg: {
/**
* Workaround for pkg asset detection
* https://github.com/thibauts/node-castv2/issues/46
*/
"assets": "../node_modules/castv2/lib/cast_channel.proto"
}
};
// Write pkg manifest
fs.writeFileSync(path.join(BUILD_PATH, "package.json")
, JSON.stringify(pkgManifest))
// Run pkg to create a single executable
await pkg.exec([
BUILD_PATH
, "--target", `${pkgPlatform[argv.platform]}-${argv.arch}`
, "--output", path.join(BUILD_PATH, executableName[argv.platform])
]);
/**
* If packaging, create an installer package and move it to
* dist, otherwise move the built executable and app manifest
* to dist.
*/
if (argv.package) {
const installerName = await packageApp(argv.platform);
if (installerName) {
// Move installer to dist
fs.moveSync(
path.join(BUILD_PATH, installerName)
, path.join(DIST_PATH, path.basename(installerName))
, { overwrite: true });
}
} else {
const builtExecutableName = executableName[argv.platform];
// Move executable and app manifest to dist
fs.moveSync(
path.join(BUILD_PATH, manifestName)
, path.join(DIST_PATH, manifestName)
, { overwrite: true });
fs.moveSync(
path.join(BUILD_PATH, builtExecutableName)
, path.join(DIST_PATH, builtExecutableName)
, { overwrite: true });
}
// Remove build directory
fs.removeSync(BUILD_PATH);
}
/**
* Takes a platform and returns the path of the created
* install package.
*/
function packageApp (platform) {
const packageFunctionArgs = [
executableName[platform] // platformExecutableName
, executablePath[platform] // platformExecutablePath
, manifestPath[platform] // platformManifestPath
];
switch (platform) {
case "win32": return packageWin32(...packageFunctionArgs);
case "darwin": return packageDarwin(...packageFunctionArgs);
case "linux":
/**
* Get manifest path from package type sub key for Linux
* platforms.
*/
packageFunctionArgs.push(
packageFunctionArgs.pop()[argv.packageType]);
switch (argv.packageType) {
case "deb": return packageLinuxDeb(...packageFunctionArgs);
case "rpm": return packageLinuxRpm(...packageFunctionArgs);
}
break;
default:
console.log("Cannot build installer package for this platform");
}
}
/**
* Builds a macOS Installer package.
*
* Creates a root directory with the installed file system
* structure for package files, bundles the postinstall
* script (packaging/mac/scripts/postinstall), then creates
* a component package.
* Distribution package is built from the component package
* and distribution file (packaging/mac/distribution.xml).
*
* Requires the pkgbuild and productbuild command line
* utilities. Only possible on macOS.
*/
function packageDarwin (
platformExecutableName
, platformExecutablePath
, platformManifestPath) {
const installerName = `${applicationName}.pkg`;
const componentName = `${applicationName}_component.pkg`;
const packagingDir = path.join(__dirname, "../packaging/mac/");
const packagingOutputDir = path.join(BUILD_PATH, "packaging");
// Create pkgbuild root
const rootPath = path.join(BUILD_PATH, "root");
const rootExecutablePath = path.join(rootPath, platformExecutablePath);
const rootManifestPath = path.join(rootPath, platformManifestPath);
// Create install locations
fs.ensureDirSync(rootExecutablePath, { recursive: true });
fs.ensureDirSync(rootManifestPath, { recursive: true });
// Move files to root
fs.moveSync(path.join(BUILD_PATH, platformExecutableName)
, path.join(rootExecutablePath, platformExecutableName));
fs.moveSync(path.join(BUILD_PATH, manifestName)
, path.join(rootManifestPath, manifestName));
// Copy static files to be processed
fs.copySync(packagingDir, packagingOutputDir);
const view = {
applicationName
, manifestName
, componentName
, packageId: `tf.matt.${applicationName}`
, executablePath: platformExecutablePath
, manifestPath: platformManifestPath
};
// Template paths
const templatePaths = [
path.join(packagingOutputDir, "scripts/postinstall")
, path.join(packagingOutputDir, "distribution.xml")
];
// Do templating on static files
for (const templatePath of templatePaths) {
const templateContent = fs.readFileSync(templatePath).toString();
fs.writeFileSync(templatePath, mustache.render(templateContent, view));
}
// Build component package
spawnSync(
`pkgbuild --root ${rootPath} `
+ `--identifier "tf.matt.${applicationName}" `
+ `--version "${applicationVersion}" `
+ `--scripts ${path.join(packagingOutputDir, "scripts")} `
+ `${path.join(BUILD_PATH, componentName)}`
, { shell: true });
// Distribution XML file
const distFilePath = path.join(packagingOutputDir, "distribution.xml");
// Build installer package
spawnSync(
`productbuild --distribution ${distFilePath} `
+ `--package-path ${BUILD_PATH} `
+ `${path.join(BUILD_PATH, installerName)}`
, { shell: true });
return installerName;
}
/**
* Builds a DEB package for Debian, Ubuntu, Mint, etc...
*
* Creates a root directory with the installed file system
* structure for package files, copies control file
* (packaging/linux/deb/DEBIAN/control) to root, then builds
* package from root.
* Requires the dpkg-deb command line utility.
*/
function packageLinuxDeb (
platformExecutableName
, platformExecutablePath
, platformManifestPath) {
const installerName = `${applicationName}.deb`;
// Create root
const rootPath = path.join(BUILD_PATH, "root");
const rootExecutablePath = path.join(rootPath, platformExecutablePath);
const rootManifestPath = path.join(rootPath
, platformManifestPath);
fs.ensureDirSync(rootExecutablePath, { recursive: true });
fs.ensureDirSync(rootManifestPath, { recursive: true });
// Move files to root
fs.moveSync(path.join(BUILD_PATH, platformExecutableName)
, path.join(rootExecutablePath, platformExecutableName));
fs.moveSync(path.join(BUILD_PATH, manifestName)
, path.join(rootManifestPath, manifestName));
const controlDir = path.join(__dirname, "../packaging/linux/deb/DEBIAN/");
const controlOutputDir = path.join(rootPath, path.basename(controlDir));
const controlFilePath = path.join(controlOutputDir, "control");
// Copy package info to root
fs.copySync(controlDir, controlOutputDir);
const view = {
// Debian package names can't contain underscores
packageName: applicationName.replace(/_/g, "-")
, applicationName
, applicationVersion
};
// Do templating on control file
fs.writeFileSync(controlFilePath
, mustache.render(
fs.readFileSync(controlFilePath).toString()
, view));
// Build .deb package
spawnSync(
`dpkg-deb --build ${rootPath} `
+ `${path.join(BUILD_PATH, installerName)}`
, { shell: true});
return installerName;
}
/**
* Builds an RPM package for Fedora, openSUSE, etc...
*
* Templates and uses the spec file
* (packaging/linux/rpm/package.spec) to build the package.
* Requires the rpmbuild command line utility.
*/
function packageLinuxRpm (
platformExecutableName
, platformExecutablePath
, platformManifestPath) {
const specPath = path.join(__dirname
, "../packaging/linux/rpm/package.spec");
const specOutputPath = path.join(BUILD_PATH, path.basename(specPath));
const view = {
packageName: applicationName
, applicationName
, applicationVersion
, executablePath: platformExecutablePath
, manifestPath: platformManifestPath
, executableName: platformExecutableName
, manifestName
};
fs.writeFileSync(specOutputPath
, mustache.render(
fs.readFileSync(specPath).toString()
, view));
// TODO: Use argv.arch
spawnSync(
`rpmbuild -bb ${specOutputPath} `
+ `--define "_distdir ${BUILD_PATH}" `
+ `--define "_rpmdir ${BUILD_PATH}" `
+ `--target=x86_64-linux`
, { shell: true });
return glob.sync("**/*.rpm", { cwd: BUILD_PATH })[0];
}
/**
* Builds a Windows installer.
*
* Uses NSIS to create a GUI installer with an installer
* script (packaging/win/installer.nsi). Requires the
* makensis command line utility.
*/
function packageWin32 (
platformExecutableName
, platformExecutablePath) {
const scriptPath = path.join(__dirname, "../packaging/win/installer.nsi");
const scriptOutputPath = path.join(BUILD_PATH, path.basename(scriptPath));
const outFile = "installer.exe";
const view = {
applicationName
, applicationVersion
, executableName: platformExecutableName
, executablePath: platformExecutablePath
, manifestName
, winRegistryKey: WIN_REGISTRY_KEY
, outFile
};
// Write templated script to build dir
fs.writeFileSync(scriptOutputPath
, mustache.render(
fs.readFileSync(scriptPath).toString()
, view));
const output = makensis.compileSync(scriptOutputPath);
if (output.status === 0) {
console.log(output.stdout);
} else {
console.error(output.stderr);
}
return outFile;
}
build().catch(e => {
console.log("Build failed", e);
process.exit(1);
});