Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement / address build command papercuts #153

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 27 additions & 82 deletions resources/js/electron-builder.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import os from 'os';
import { join } from 'path';
import { mkdtempSync } from 'fs';
import { exec } from 'child_process';

// Workaround for CommonJS module
import fs_extra from 'fs-extra';
const { copySync, removeSync, writeJsonSync } = fs_extra;

const isBuilding = process.env.NATIVEPHP_BUILDING;
const appUrl = process.env.APP_URL;
const appId = process.env.NATIVEPHP_APP_ID;
const appName = process.env.NATIVEPHP_APP_NAME;
const isBuilding = process.env.NATIVEPHP_BUILDING;
const appAuthor = process.env.NATIVEPHP_APP_AUTHOR;
const fileName = process.env.NATIVEPHP_APP_FILENAME;
const appVersion = process.env.NATIVEPHP_APP_VERSION;
const appUrl = process.env.APP_URL;
const appAuthor = process.env.NATIVEPHP_APP_AUTHOR;
const deepLinkProtocol = process.env.NATIVEPHP_DEEPLINK_SCHEME;

// Since we do not copy the php executable here, we only need these for building
Expand All @@ -25,20 +20,18 @@ let targetOs;
if (isWindows) {
targetOs = 'win';
}

if (isLinux) {
targetOs = 'linux';
}
// Use of isDarwin

if (isDarwin) {
targetOs = 'mac';
}


let updaterConfig = {};

// We wouldn't need these since its not representing the target platform
console.log("Arch: ", process.arch)
console.log("Platform: ", process.platform)
try {
updaterConfig = process.env.NATIVEPHP_UPDATER_CONFIG;
updaterConfig = JSON.parse(updaterConfig);
Expand All @@ -47,82 +40,13 @@ try {
}

if (isBuilding) {

console.log();
console.log('===================================================================');
console.log(' Building for ' + targetOs);
console.log('===================================================================');
console.log();
console.log('Updater config', updaterConfig);
console.log();

try {
const appPath = join(import.meta.dirname, 'resources', 'app');

removeSync(appPath);

// As we can't copy into a subdirectory of ourself we need to copy to a temp directory
let tmpDir = mkdtempSync(join(os.tmpdir(), 'nativephp'));

copySync(process.env.APP_PATH, tmpDir, {
overwrite: true,
dereference: true,
filter: (src, dest) => {
let skip = [
// Skip .git and Dev directories
join(process.env.APP_PATH, '.git'),
join(process.env.APP_PATH, 'docker'),
join(process.env.APP_PATH, 'packages'),

// Only needed for local testing
join(process.env.APP_PATH, 'vendor', 'nativephp', 'electron', 'vendor'),
join(process.env.APP_PATH, 'vendor', 'nativephp', 'laravel', 'vendor'),

join(process.env.APP_PATH, 'vendor', 'nativephp', 'php-bin'),
join(process.env.APP_PATH, 'vendor', 'nativephp', 'electron', 'bin'),
join(process.env.APP_PATH, 'vendor', 'nativephp', 'electron', 'resources'),
join(process.env.APP_PATH, 'node_modules'),
join(process.env.APP_PATH, 'dist'),
];

let shouldSkip = false;
skip.forEach((path) => {
if (src.indexOf(path) === 0) {
shouldSkip = true;
}
});

return !shouldSkip;
}
});

copySync(tmpDir, appPath);

// Electron build removes empty folders, so we have to create dummy files
// dotfiles unfortunately don't work.
writeJsonSync(join(appPath, 'storage', 'framework', 'cache', '_native.json'), {})
writeJsonSync(join(appPath, 'storage', 'framework', 'sessions', '_native.json'), {})
writeJsonSync(join(appPath, 'storage', 'framework', 'testing', '_native.json'), {})
writeJsonSync(join(appPath, 'storage', 'framework', 'views', '_native.json'), {})
writeJsonSync(join(appPath, 'storage', 'app', 'public', '_native.json'), {})
writeJsonSync(join(appPath, 'storage', 'logs', '_native.json'), {})

removeSync(tmpDir);

console.log();
console.log('Copied app to resources');
console.log(join(process.env.APP_PATH, 'dist'));
console.log();
console.log('===================================================================');
console.log(' Starting build...');
console.log();
} catch (e) {
console.error();
console.error('Error copying app into build environment');
console.error(e);
console.error();
}

}

export default {
Expand All @@ -142,6 +66,27 @@ export default {
asarUnpack: [
'resources/**',
],
beforePack: async (context) => {
let arch = {
1: 'x64',
3: 'arm64'
}[context.arch];

// If mac is not arm, use 32bits (x86)
// This is how this works now according to package.json scripts,
// TODO: Check this. Not sure why mac x64 needs a php binary for x86? @simonhamp
if(targetOs === 'mac' && arch === 'x64') {
arch = 'x86'
}

if(arch === undefined) {
console.error('Cannot build PHP for unsupported architecture');
process.exit(1);
}

console.log(` • building php binary - exec php.js --${targetOs} --${arch}`);
exec(`node php.js --${targetOs} --${arch}`);
},
afterSign: 'build/notarize.js',
win: {
executableName: fileName,
Expand Down
Loading
Loading