This repository has been archived on 2021-02-20. You can view files and clone it, but cannot push or open issues or pull requests.
shapez.io/gulp/standalone.js

236 lines
9.3 KiB
JavaScript
Raw Normal View History

require("colors");
2020-05-09 16:45:23 +02:00
const packager = require("electron-packager");
const path = require("path");
const { getVersion } = require("./buildutils");
2020-05-09 16:45:23 +02:00
const fs = require("fs");
const fse = require("fs-extra");
const buildutils = require("./buildutils");
2020-05-09 16:45:23 +02:00
const execSync = require("child_process").execSync;
function gulptasksStandalone($, gulp) {
2020-06-17 15:10:14 +02:00
const electronBaseDir = path.join(__dirname, "..", "electron");
2020-05-09 16:45:23 +02:00
2020-06-17 15:10:14 +02:00
const tempDestDir = path.join(__dirname, "..", "tmp_standalone_files");
2020-05-09 16:45:23 +02:00
const tempDestBuildDir = path.join(tempDestDir, "built");
gulp.task("standalone.prepare.cleanup", () => {
2020-06-13 17:59:25 +02:00
return gulp.src(tempDestDir, { read: false, allowEmpty: true }).pipe($.clean({ force: true }));
2020-05-09 16:45:23 +02:00
});
gulp.task("standalone.prepare.copyPrefab", () => {
// const requiredFiles = $.glob.sync("../electron/");
const requiredFiles = [
path.join(electronBaseDir, "lib", "**", "*.node"),
path.join(electronBaseDir, "node_modules", "**", "*.*"),
path.join(electronBaseDir, "node_modules", "**", ".*"),
path.join(electronBaseDir, "favicon*"),
// fails on platforms which support symlinks
// https://github.com/gulpjs/gulp/issues/1427
// path.join(electronBaseDir, "node_modules", "**", "*"),
2020-05-09 16:45:23 +02:00
];
return gulp.src(requiredFiles, { base: electronBaseDir }).pipe(gulp.dest(tempDestBuildDir));
});
2020-06-13 17:59:25 +02:00
gulp.task("standalone.prepare.writePackageJson", cb => {
2020-05-09 16:45:23 +02:00
fs.writeFileSync(
path.join(tempDestBuildDir, "package.json"),
JSON.stringify(
{
devDependencies: {
2020-05-21 13:05:21 +02:00
electron: "6.1.12",
2020-05-09 16:45:23 +02:00
},
},
null,
4
)
);
2020-06-13 17:59:25 +02:00
cb();
2020-05-09 16:45:23 +02:00
});
gulp.task("standalone.prepareVDF", cb => {
const hash = buildutils.getRevision();
const steampipeDir = path.join(__dirname, "steampipe", "scripts");
const templateContents = fs
.readFileSync(path.join(steampipeDir, "app.vdf.template"), { encoding: "utf-8" })
.toString();
const convertedContents = templateContents.replace("$DESC$", "Commit " + hash);
fs.writeFileSync(path.join(steampipeDir, "app.vdf"), convertedContents);
cb();
});
2020-05-09 16:45:23 +02:00
gulp.task("standalone.prepare.minifyCode", () => {
return gulp.src(path.join(electronBaseDir, "*.js")).pipe(gulp.dest(tempDestBuildDir));
2020-05-09 16:45:23 +02:00
});
gulp.task("standalone.prepare.copyGamefiles", () => {
2020-05-21 13:05:21 +02:00
return gulp.src("../build/**/*.*", { base: "../build" }).pipe(gulp.dest(tempDestBuildDir));
2020-05-09 16:45:23 +02:00
});
2020-06-13 17:59:25 +02:00
gulp.task("standalone.killRunningInstances", cb => {
2020-05-09 16:45:23 +02:00
try {
execSync("taskkill /F /IM shapezio.exe");
} catch (ex) {
console.warn("Failed to kill running instances, maybe none are up.");
}
2020-06-13 17:59:25 +02:00
cb();
2020-05-09 16:45:23 +02:00
});
gulp.task(
"standalone.prepare",
2020-06-13 17:59:25 +02:00
gulp.series(
2020-05-09 16:45:23 +02:00
"standalone.killRunningInstances",
"standalone.prepare.cleanup",
"standalone.prepare.copyPrefab",
"standalone.prepare.writePackageJson",
"standalone.prepare.minifyCode",
"standalone.prepare.copyGamefiles"
)
);
/**
*
* @param {'win32'|'linux'|'darwin'} platform
* @param {'x64'|'ia32'} arch
* @param {function():void} cb
* @param {boolean=} isRelease
2020-05-09 16:45:23 +02:00
*/
function packageStandalone(platform, arch, cb, isRelease = true) {
2020-05-30 20:25:46 +02:00
const tomlFile = fs.readFileSync(path.join(__dirname, ".itch.toml"));
2020-05-09 16:45:23 +02:00
packager({
dir: tempDestBuildDir,
2020-05-21 13:05:21 +02:00
appCopyright: "Tobias Springer",
appVersion: getVersion(),
2020-05-09 16:45:23 +02:00
buildVersion: "1.0.0",
arch,
platform,
asar: true,
executableName: "shapezio",
icon: path.join(electronBaseDir, "favicon"),
2020-05-21 13:36:08 +02:00
name: "shapez.io-standalone",
2020-05-09 16:45:23 +02:00
out: tempDestDir,
overwrite: true,
appBundleId: "io.shapez.standalone",
appCategoryType: "public.app-category.games",
...(isRelease &&
platform === "darwin" && {
osxSign: {
"identity": process.env.SHAPEZ_CLI_APPLE_CERT_NAME,
"hardened-runtime": true,
"hardenedRuntime": true,
"entitlements": "entitlements.plist",
"entitlements-inherit": "entitlements.plist",
"signature-flags": "library",
},
osxNotarize: {
appleId: process.env.SHAPEZ_CLI_APPLE_ID,
appleIdPassword: "@keychain:SHAPEZ_CLI_APPLE_ID",
},
}),
2020-05-09 16:45:23 +02:00
}).then(
appPaths => {
console.log("Packages created:", appPaths);
appPaths.forEach(appPath => {
if (!fs.existsSync(appPath)) {
console.error("Bad app path gotten:", appPath);
return;
}
fs.writeFileSync(
path.join(appPath, "LICENSE"),
fs.readFileSync(path.join(__dirname, "..", "LICENSE"))
);
2020-05-30 20:25:46 +02:00
fs.writeFileSync(path.join(appPath, ".itch.toml"), tomlFile);
2020-07-06 15:29:17 +02:00
if (platform === "linux") {
2020-07-06 08:14:44 +02:00
fs.writeFileSync(
path.join(appPath, "play.sh"),
'#!/usr/bin/env bash\n./shapezio --no-sandbox "$@"\n'
);
2020-05-30 20:25:46 +02:00
fs.chmodSync(path.join(appPath, "play.sh"), 0o775);
}
if (process.platform === "win32" && platform === "darwin") {
console.warn(
"Cross-building for macOS on Windows: dereferencing symlinks.\n".red +
"This will nearly double app size and make code signature invalid. Sorry!\n"
.red.bold +
"For more information, see " +
"https://github.com/electron/electron-packager/issues/71".underline
);
// Clear up framework folders
2020-07-06 15:29:17 +02:00
fs.writeFileSync(
path.join(appPath, "play.sh"),
'#!/usr/bin/env bash\n./shapez.io-standalone.app/Contents/MacOS/shapezio --no-sandbox "$@"\n'
);
fs.chmodSync(path.join(appPath, "play.sh"), 0o775);
fs.chmodSync(
path.join(appPath, "shapez.io-standalone.app", "Contents", "MacOS", "shapezio"),
0o775
);
const finalPath = path.join(appPath, "shapez.io-standalone.app");
const frameworksDir = path.join(finalPath, "Contents", "Frameworks");
const frameworkFolders = fs
.readdirSync(frameworksDir)
.filter(fname => fname.endsWith(".framework"));
for (let i = 0; i < frameworkFolders.length; ++i) {
const folderName = frameworkFolders[i];
const frameworkFolder = path.join(frameworksDir, folderName);
console.log(" -> ", frameworkFolder);
const filesToDelete = fs
.readdirSync(frameworkFolder)
.filter(fname => fname.toLowerCase() !== "versions");
filesToDelete.forEach(fname => {
console.log(" -> Deleting", fname);
fs.unlinkSync(path.join(frameworkFolder, fname));
});
const frameworkSourceDir = path.join(frameworkFolder, "Versions", "A");
fse.copySync(frameworkSourceDir, frameworkFolder);
}
}
2020-05-09 16:45:23 +02:00
});
cb();
},
err => {
console.error("Packaging error:", err);
cb();
}
);
}
gulp.task("standalone.package.prod.win64", cb => packageStandalone("win32", "x64", cb));
gulp.task("standalone.package.prod.win32", cb => packageStandalone("win32", "ia32", cb));
gulp.task("standalone.package.prod.linux64", cb => packageStandalone("linux", "x64", cb));
gulp.task("standalone.package.prod.linux32", cb => packageStandalone("linux", "ia32", cb));
gulp.task("standalone.package.prod.darwin64", cb => packageStandalone("darwin", "x64", cb));
gulp.task("standalone.package.prod.darwin64.unsigned", cb =>
packageStandalone("darwin", "x64", cb, false)
);
2020-05-09 16:45:23 +02:00
gulp.task(
"standalone.package.prod",
2020-06-13 17:59:25 +02:00
gulp.series(
"standalone.prepare",
gulp.parallel(
"standalone.package.prod.win64",
"standalone.package.prod.linux64",
"standalone.package.prod.darwin64"
)
)
2020-05-09 16:45:23 +02:00
);
}
module.exports = { gulptasksStandalone };