Skip to content

Commit ab99e94

Browse files
RudeisniceNicolas Utzinger
andauthored
Fix to issue nativephp/laravel #318 + General cleanup and DRYied Publish command (#97)
* Fix mac -> win crossbuild + DRY cleanup * php.js and electron-builder.js cleanup * updated build commands with arch and uncommented npm update in native:build command * added comments * Unbroken previous change for removing publish command * revert to original package.json name property * wip * updated php.js to allow target architecture * PR clean up * changes for --no-interaction option * remove dd and var name change --------- Co-authored-by: Nicolas Utzinger <[email protected]>
1 parent 42993ea commit ab99e94

File tree

6 files changed

+192
-147
lines changed

6 files changed

+192
-147
lines changed

resources/js/electron-builder.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ if (isDarwin) {
3232

3333
let updaterConfig = {};
3434

35+
// We wouldn't need these since its not representing the target platform
3536
console.log("Arch: ", process.arch)
3637
console.log("Platform: ", process.platform)
3738
try {
@@ -107,10 +108,9 @@ if (isBuilding) {
107108
console.log(join(process.env.APP_PATH, 'dist'));
108109
console.log('=====================');
109110

110-
// We'll use the default PHP binary here, as we can cross-compile for all platforms
111-
const phpBinary = join(process.env.APP_PATH, 'vendor', 'nativephp', 'electron', 'resources', 'js', 'resources', 'php', 'php');
112111
const artisanPath = join(appPath, 'artisan');
113-
execSync(`${phpBinary} ${artisanPath} native:minify ${appPath}`);
112+
// We'll use the default PATH PHP binary here, as we can cross-compile for all platforms. This shouldn't be changed.
113+
execSync(`php ${artisanPath} native:minify ${appPath}`);
114114
} catch (e) {
115115
console.error('=====================');
116116
console.error('Error copying app to resources');

resources/js/package.json

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
{
2-
"name": "NativePHP",
3-
"version": "1.0.0",
4-
"description": "A NativePHP Electron application",
5-
"main": "./out/main/index.js",
6-
"author": "NativePHP",
7-
"homepage": "https://nativephp.com",
8-
"scripts": {
9-
"format": "prettier --write .",
10-
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
11-
"start": "electron-vite preview",
12-
"dev": "cross-env node php.js && electron-vite dev --watch",
13-
"build": "electron-vite build",
14-
"postinstall": "node ./node_modules/electron-builder/cli.js install-app-deps",
15-
"publish:win": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p always --win --config",
16-
"publish:mac": "cross-env npm run publish:mac-arm && cross-env npm run publish:mac-x86",
17-
"publish:mac-arm": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p always --mac --config --arm64 -p always",
18-
"publish:mac-x86": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p always --mac --config --x64 -p always",
19-
"publish:linux": "cross-env npm run publish:linux-x64",
20-
"publish:linux-x64": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js --linux --config --x64 -p always",
21-
"build:all": "cross-env npm run build:mac && cross-env npm run build:win && cross-env npm run build:linux",
22-
"build:win": "cross-env node php.js --win && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --win --config",
23-
"build:mac": "cross-env npm run build:mac-arm && cross-env npm run build:mac-x86",
24-
"build:mac-arm": "cross-env node php.js --arm64 --mac && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --mac --config --arm64",
25-
"build:mac-x86": "cross-env node php.js --mac && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --mac --config --x64",
26-
"build:linux": "cross-env npm run build:linux-x64",
27-
"build:linux-x64": "cross-env node php.js --linux && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --linux --config --x64"
28-
},
29-
"dependencies": {
30-
"@electron-toolkit/preload": "^1.0.3",
31-
"@electron-toolkit/utils": "^1.0.2",
32-
"@electron/remote": "^2.0.9",
33-
"@nativephp/electron-plugin": "^0.3.1",
34-
"yauzl": "^3.1.3"
35-
},
36-
"devDependencies": {
37-
"@electron/notarize": "^1.2.3",
38-
"@rushstack/eslint-patch": "^1.2.0",
39-
"@vitejs/plugin-vue": "^4.1.0",
40-
"@vue/eslint-config-prettier": "^7.1.0",
41-
"cross-env": "^7.0.3",
42-
"electron": "^25.0",
43-
"electron-builder": "^24.5.1",
44-
"electron-chromedriver": "^25.0",
45-
"electron-vite": "^1.0.24",
46-
"eslint": "^8.36.0",
47-
"eslint-plugin-vue": "^9.10.0",
48-
"less": "^4.1.3",
49-
"prettier": "^2.8.7",
50-
"vite": "^4.2.1",
51-
"vue": "^3.2.47"
52-
}
2+
"name": "NativePHP",
3+
"version": "1.0.0",
4+
"description": "A NativePHP Electron application",
5+
"main": "./out/main/index.js",
6+
"author": "NativePHP",
7+
"homepage": "https://nativephp.com",
8+
"scripts": {
9+
"format": "prettier --write .",
10+
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
11+
"start": "electron-vite preview",
12+
"dev": "cross-env node php.js && electron-vite dev --watch",
13+
"build": "electron-vite build",
14+
"postinstall": "node ./node_modules/electron-builder/cli.js install-app-deps",
15+
"publish:win": "cross-env npm run publish:win-x64",
16+
"publish:win-x64": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p always --win --config --x64",
17+
"publish:mac": "cross-env npm run publish:mac-arm && cross-env npm run publish:mac-x86",
18+
"publish:mac-arm": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p always --mac --config --arm64 -p always",
19+
"publish:mac-x86": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p always --mac --config --x64 -p always",
20+
"publish:linux": "cross-env npm run publish:linux-x64",
21+
"publish:linux-x64": "cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js --linux --config --x64 -p always",
22+
"build:all": "cross-env npm run build:mac && cross-env npm run build:win && cross-env npm run build:linux",
23+
"build:win": "cross-env npm run build:win-x64",
24+
"build:win-x64": "cross-env node php.js --win --x64 && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --win --config --x64",
25+
"build:mac": "cross-env npm run build:mac-arm64 && cross-env npm run build:mac-x86",
26+
"build:mac-arm64": "cross-env node php.js --mac --arm64 && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --mac --config --arm64",
27+
"build:mac-x86": "cross-env node php.js --mac --x86 && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --mac --config --x64",
28+
"build:linux": "cross-env npm run build:linux-x64",
29+
"build:linux-x64": "cross-env node php.js --linux --x64 && cross-env npm run build && cross-env node ./node_modules/electron-builder/cli.js -p never --linux --config --x64"
30+
},
31+
"dependencies": {
32+
"@electron-toolkit/preload": "^1.0.3",
33+
"@electron-toolkit/utils": "^1.0.2",
34+
"@electron/remote": "^2.0.9",
35+
"@nativephp/electron-plugin": "^0.3.1",
36+
"yauzl": "^3.1.3"
37+
},
38+
"devDependencies": {
39+
"@electron/notarize": "^1.2.3",
40+
"@rushstack/eslint-patch": "^1.2.0",
41+
"@vitejs/plugin-vue": "^4.1.0",
42+
"@vue/eslint-config-prettier": "^7.1.0",
43+
"cross-env": "^7.0.3",
44+
"electron": "^25.0",
45+
"electron-builder": "^24.5.1",
46+
"electron-chromedriver": "^25.0",
47+
"electron-vite": "^1.0.24",
48+
"eslint": "^8.36.0",
49+
"eslint-plugin-vue": "^9.10.0",
50+
"less": "^4.1.3",
51+
"prettier": "^2.8.7",
52+
"vite": "^4.2.1",
53+
"vue": "^3.2.47"
54+
}
5355
}

resources/js/php.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,63 @@
1+
const { log } = require("console");
12
const fs = require("fs");
23
const {copySync, removeSync, existsSync, ensureDirSync} = require("fs-extra");
34
const {join} = require("path");
5+
const { exit } = require("process");
46
const unzip = require("yauzl");
57

6-
const isBuilding = process.env.NATIVEPHP_BUILDING;
8+
const isBuilding = Boolean(process.env.NATIVEPHP_BUILDING);
79
const phpBinaryPath = process.env.NATIVEPHP_PHP_BINARY_PATH;
810
const phpVersion = process.env.NATIVEPHP_PHP_BINARY_VERSION;
911
const certificatePath = process.env.NATIVEPHP_CERTIFICATE_FILE_PATH;
1012

1113
// Differentiates for Serving and Building
12-
const isArm64 = isBuilding ? process.argv.includes('--arm64') : process.platform.includes('arm64') ;
14+
const isArm64 = isBuilding ? process.argv.includes('--arm64') : process.arch.includes('arm64');
1315
const isWindows = isBuilding ? process.argv.includes('--win') : process.platform.includes('win32');
1416
const isLinux = isBuilding ? process.argv.includes('--linux') : process.platform.includes('linux');
1517
const isDarwin = isBuilding ? process.argv.includes('--mac') : process.platform.includes('darwin');
1618

17-
let targetOs;
18-
let binaryArch = 'x64';
19-
let phpBinaryFilename = 'php';
19+
// false because string mapping is done in is{OS} checks
20+
const platform = {
21+
os: false,
22+
arch: false,
23+
phpBinary: 'php'
24+
};
2025

2126
if (isWindows) {
22-
targetOs = 'win';
23-
phpBinaryFilename += '.exe';
27+
platform.os = 'win';
28+
platform.phpBinary += '.exe';
2429
}
30+
2531
if (isLinux) {
26-
targetOs = 'linux';
32+
platform.os = 'linux';
2733
}
28-
// Use of isDarwin
34+
2935
if (isDarwin) {
30-
targetOs = 'mac';
31-
binaryArch = 'x86';
36+
platform.os = 'mac';
37+
platform.arch = 'x86';
3238
}
39+
3340
if (isArm64) {
34-
binaryArch = 'arm64';
41+
platform.arch = 'arm64';
42+
}
43+
44+
// isBuilding overwrites platform to the desired architecture
45+
if (isBuilding) {
46+
// Only one will be used by the configured build commands in package.json
47+
platform.arch = process.argv.includes('--x64') ? 'x64' : platform.arch;
48+
platform.arch = process.argv.includes('--x86') ? 'x86' : platform.arch;
49+
platform.arch = process.argv.includes('--arm64') ? 'arm64' : platform.arch;
3550
}
3651

3752
const phpVersionZip = 'php-' + phpVersion + '.zip';
38-
const binarySrcDir = join(phpBinaryPath, targetOs, binaryArch, phpVersionZip);
53+
const binarySrcDir = join(phpBinaryPath, platform.os, platform.arch, phpVersionZip);
3954
const binaryDestDir = join(__dirname, 'resources/php');
4055

4156
console.log('Binary Source: ', binarySrcDir);
42-
console.log('Binary Filename: ', phpBinaryFilename);
57+
console.log('Binary Filename: ', platform.phpBinary);
4358
console.log('PHP version: ' + phpVersion);
4459

45-
if (phpBinaryPath) {
60+
if (platform.phpBinary) {
4661
try {
4762
console.log('Unzipping PHP binary from ' + binarySrcDir + ' to ' + binaryDestDir);
4863
removeSync(binaryDestDir);
@@ -57,7 +72,7 @@ if (phpBinaryPath) {
5772
zipfile.openReadStream(entry, function (err, readStream) {
5873
if (err) throw err;
5974

60-
const binaryPath = join(binaryDestDir, phpBinaryFilename);
75+
const binaryPath = join(binaryDestDir, platform.phpBinary);
6176
const writeStream = fs.createWriteStream(binaryPath);
6277

6378
readStream.pipe(writeStream);

src/Commands/BuildCommand.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,58 @@
77
use Illuminate\Support\Str;
88
use Native\Electron\Concerns\LocatesPhpBinary;
99
use Native\Electron\Facades\Updater;
10-
11-
use function Laravel\Prompts\select;
10+
use Native\Electron\Traits\OsAndArch;
1211

1312
class BuildCommand extends Command
1413
{
1514
use LocatesPhpBinary;
15+
use OsAndArch;
16+
17+
protected $signature = 'native:build
18+
{os? : The operating system to build for (all, linux, mac, win)}
19+
{arch? : The Processor Architecture to build for (x64, x86, arm64)}
20+
{--publish : to publish the app}';
1621

17-
protected $signature = 'native:build {os? : The operating system to build for (all, linux, mac, win)}';
22+
protected $availableOs = ['win', 'linux', 'mac', 'all'];
1823

1924
public function handle(): void
2025
{
2126
$this->info('Build NativePHP app…');
2227

28+
Process::path(__DIR__.'/../../resources/js/')
29+
->env($this->getEnvironmentVariables())
30+
->forever()
31+
->run('npm update', function (string $type, string $output) {
32+
echo $output;
33+
});
34+
2335
Process::path(base_path())
2436
->run('composer install --no-dev', function (string $type, string $output) {
2537
echo $output;
2638
});
2739

28-
if (! $os = $this->argument('os')) {
29-
$os = select(
30-
label: 'Please select the operating system to build for',
31-
options: ['win', 'linux', 'mac', 'all'],
32-
default: $this->getDefaultOs(),
33-
);
40+
// Added checks for correct input for os and arch
41+
$os = $this->selectOs($this->argument('os'));
42+
43+
$buildCommand = 'build';
44+
if ($os != 'all') {
45+
$arch = $this->selectArchitectureForOs($os, $this->argument('arch'));
46+
47+
$os .= $arch != 'all' ? "-{$arch}": '';
48+
49+
// Wether to publish the app or not
50+
if ($publish = ($this->option('publish'))){
51+
$buildCommand = 'publish';
52+
}
3453
}
54+
$this->info((($publish ?? false) ? "Publishing" : 'Building') . " for {$os}");
55+
3556

3657
Process::path(__DIR__.'/../../resources/js/')
3758
->env($this->getEnvironmentVariables())
3859
->forever()
3960
->tty(PHP_OS_FAMILY != 'Windows' && ! $this->option('no-interaction'))
40-
->run("npm run build:{$os}", function (string $type, string $output) {
61+
->run("npm run {$buildCommand}:{$os}", function (string $type, string $output) {
4162
echo $output;
4263
});
4364
}
@@ -63,13 +84,4 @@ protected function getEnvironmentVariables(): array
6384
);
6485
}
6586

66-
protected function getDefaultOs(): string
67-
{
68-
return match (PHP_OS_FAMILY) {
69-
'Windows' => 'win',
70-
'Darwin' => 'mac',
71-
'Linux' => 'linux',
72-
default => 'all',
73-
};
74-
}
7587
}

src/Commands/PublishCommand.php

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,32 @@
33
namespace Native\Electron\Commands;
44

55
use Illuminate\Console\Command;
6-
use Illuminate\Support\Facades\Process;
7-
use Illuminate\Support\Str;
6+
use Illuminate\Support\Facades\Artisan;
87
use Native\Electron\Concerns\LocatesPhpBinary;
9-
use Native\Electron\Facades\Updater;
8+
use Native\Electron\Traits\OsAndArch;
109

1110
use function Laravel\Prompts\select;
1211

1312
class PublishCommand extends Command
1413
{
1514
use LocatesPhpBinary;
15+
use OsAndArch;
1616

17-
protected $signature = 'native:publish {os? : The operating system to build for (linux, mac, win)}';
17+
protected $signature = 'native:publish
18+
{os? : The operating system to build for (linux, mac, win)}
19+
{arch? : The Processor Architecture to build for (x64, x86, arm64)}';
20+
21+
22+
protected array $availableOs = ['win', 'linux', 'mac'];
1823

1924
public function handle(): void
2025
{
2126
$this->info('Building and publishing NativePHP app…');
2227

23-
Process::path(__DIR__.'/../../resources/js/')
24-
->env($this->getEnvironmentVariables())
25-
->forever()
26-
->run('npm update', function (string $type, string $output) {
27-
echo $output;
28-
});
29-
30-
Process::path(base_path())
31-
->run('composer install --no-dev', function (string $type, string $output) {
32-
echo $output;
33-
});
28+
$os = $this->selectOs($this->argument('os'));
3429

35-
if (! $os = $this->argument('os')) {
36-
$os = select(
37-
label: 'Please select the operating system to build for',
38-
options: ['win', 'linux', 'mac', 'all'],
39-
default: $this->getDefaultOs(),
40-
);
41-
}
42-
43-
Process::path(__DIR__.'/../../resources/js/')
44-
->env($this->getEnvironmentVariables())
45-
->forever()
46-
->tty(PHP_OS_FAMILY != 'Windows' && ! $this->option('no-interaction'))
47-
->run("npm run publish:{$os}", function (string $type, string $output) {
48-
echo $output;
49-
});
50-
}
30+
$arch = $this->selectArchitectureForOs($os, $this->argument('arch'));
5131

52-
protected function getEnvironmentVariables(): array
53-
{
54-
return array_merge(
55-
[
56-
'APP_PATH' => base_path(),
57-
'NATIVEPHP_BUILDING' => true,
58-
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
59-
'NATIVEPHP_PHP_BINARY_PATH' => base_path($this->phpBinaryPath()),
60-
'NATIVEPHP_CERTIFICATE_FILE_PATH' => base_path($this->binaryPackageDirectory().'cacert.pem'),
61-
'NATIVEPHP_APP_NAME' => config('app.name'),
62-
'NATIVEPHP_APP_ID' => config('nativephp.app_id'),
63-
'NATIVEPHP_APP_VERSION' => config('nativephp.version'),
64-
'NATIVEPHP_APP_FILENAME' => Str::slug(config('app.name')),
65-
'NATIVEPHP_UPDATER_CONFIG' => json_encode(Updater::builderOptions()),
66-
],
67-
Updater::environmentVariables(),
68-
);
69-
}
70-
71-
protected function getDefaultOs(): string
72-
{
73-
return match (PHP_OS_FAMILY) {
74-
'Windows' => 'win',
75-
'Darwin' => 'mac',
76-
'Linux' => 'linux',
77-
default => 'all',
78-
};
32+
Artisan::call("native:build", ['os'=>$os, 'arch' => $arch, '--publish' => true], $this->output);
7933
}
8034
}

0 commit comments

Comments
 (0)