Skip to content

Commit 15de455

Browse files
committed
wip: it works, but need some minor cleaning/refactoring/testing
1 parent bb7b06a commit 15de455

File tree

3 files changed

+135
-11
lines changed

3 files changed

+135
-11
lines changed

resources/js/electron-plugin/dist/server/php.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
};
1010
import { mkdirSync, statSync, writeFileSync, existsSync } from 'fs';
1111
import fs_extra from 'fs-extra';
12-
const { copySync } = fs_extra;
12+
const { copySync, mkdirpSync } = fs_extra;
1313
import Store from 'electron-store';
1414
import { promisify } from 'util';
1515
import { join } from 'path';
1616
import { app } from 'electron';
17-
import { execFile, spawn } from 'child_process';
17+
import { execFile, spawn, spawnSync } from 'child_process';
1818
import state from "./state.js";
1919
import getPort, { portNumbers } from 'get-port';
2020
const storagePath = join(app.getPath('userData'), 'storage');
2121
const databasePath = join(app.getPath('userData'), 'database');
2222
const databaseFile = join(databasePath, 'database.sqlite');
23+
const bootstrapCache = join(app.getPath('userData'), 'bootstrap', 'cache');
2324
const argumentEnv = getArgumentEnv();
2425
const appPath = getAppPath();
26+
mkdirpSync(bootstrapCache);
27+
function runningProdVersion() {
28+
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'));
29+
}
2530
function runningSecureBuild() {
2631
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'));
2732
}
@@ -89,6 +94,22 @@ function callPhp(args, options, phpIniSettings = {}) {
8994
env: Object.assign(Object.assign({}, process.env), options.env),
9095
});
9196
}
97+
function callPhpSync(args, options, phpIniSettings = {}) {
98+
if (args[0] === 'artisan' && runningSecureBuild()) {
99+
args.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
100+
}
101+
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);
102+
Object.keys(iniSettings).forEach(key => {
103+
args.unshift('-d', `${key}=${iniSettings[key]}`);
104+
});
105+
if (parseInt(process.env.SHELL_VERBOSITY) > 0) {
106+
console.log('Calling PHP', state.php, args);
107+
}
108+
return spawnSync(state.php, args, {
109+
cwd: options.cwd,
110+
env: Object.assign(Object.assign({}, process.env), options.env)
111+
});
112+
}
92113
function getArgumentEnv() {
93114
const envArgs = process.argv.filter(arg => arg.startsWith('--env.'));
94115
const env = {};
@@ -134,7 +155,7 @@ function getPath(name) {
134155
}
135156
}
136157
function getDefaultEnvironmentVariables(secret, apiPort) {
137-
return {
158+
let variables = {
138159
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
139160
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
140161
LARAVEL_STORAGE_PATH: storagePath,
@@ -154,6 +175,14 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
154175
NATIVEPHP_VIDEOS_PATH: getPath('videos'),
155176
NATIVEPHP_RECENT_PATH: getPath('recent'),
156177
};
178+
if (runningProdVersion()) {
179+
variables.APP_SERVICES_CACHE = join(bootstrapCache, 'services.php');
180+
variables.APP_PACKAGES_CACHE = join(bootstrapCache, 'packages.php');
181+
variables.APP_CONFIG_CACHE = join(bootstrapCache, 'config.php');
182+
variables.APP_ROUTES_CACHE = join(bootstrapCache, 'routes.php');
183+
variables.APP_EVENTS_CACHE = join(bootstrapCache, 'events.php');
184+
}
185+
return variables;
157186
}
158187
function getDefaultPhpIniSettings() {
159188
return {
@@ -175,11 +204,15 @@ function serveApp(secret, apiPort, phpIniSettings) {
175204
};
176205
const store = new Store();
177206
if (!runningSecureBuild()) {
178-
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
207+
callPhpSync(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
208+
}
209+
if (runningProdVersion()) {
210+
console.log('Caching view and routes...');
211+
callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings);
179212
}
180213
if (shouldMigrateDatabase(store)) {
181214
console.log('Migrating database...');
182-
callPhp(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
215+
callPhpSync(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
183216
store.set('migrated_version', app.getVersion());
184217
}
185218
if (process.env.NODE_ENV === 'development') {
@@ -199,6 +232,9 @@ function serveApp(secret, apiPort, phpIniSettings) {
199232
}, phpIniSettings);
200233
const portRegex = /Development Server \(.*:([0-9]+)\) started/gm;
201234
phpServer.stdout.on('data', (data) => {
235+
if (parseInt(process.env.SHELL_VERBOSITY) > 0) {
236+
console.log(data.toString());
237+
}
202238
});
203239
phpServer.stderr.on('data', (data) => {
204240
const error = data.toString();

resources/js/electron-plugin/src/server/php.ts

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
import {mkdirSync, statSync, writeFileSync, existsSync} from 'fs'
22
import fs_extra from 'fs-extra';
33

4-
const {copySync} = fs_extra;
4+
const {copySync, mkdirpSync} = fs_extra;
55

66
import Store from 'electron-store'
77
import {promisify} from 'util'
88
import {join} from 'path'
99
import {app} from 'electron'
10-
import {execFile, spawn} from 'child_process'
10+
import {execFile, spawn, spawnSync} from 'child_process'
1111
import state from "./state.js";
1212
import getPort, {portNumbers} from 'get-port';
1313
import {ProcessResult} from "./ProcessResult.js";
1414

1515
const storagePath = join(app.getPath('userData'), 'storage')
1616
const databasePath = join(app.getPath('userData'), 'database')
1717
const databaseFile = join(databasePath, 'database.sqlite')
18+
const bootstrapCache = join(app.getPath('userData'), 'bootstrap', 'cache')
1819
const argumentEnv = getArgumentEnv();
1920
const appPath = getAppPath();
2021

22+
mkdirpSync(bootstrapCache);
23+
24+
function runningProdVersion() {
25+
//TODO: Check if the app is running the production version
26+
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'))
27+
}
28+
2129
function runningSecureBuild() {
2230
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'))
2331
}
@@ -105,6 +113,35 @@ function callPhp(args, options, phpIniSettings = {}) {
105113
);
106114
}
107115

116+
function callPhpSync(args, options, phpIniSettings = {}) {
117+
118+
if (args[0] === 'artisan' && runningSecureBuild()) {
119+
args.unshift(join(appPath, 'build', '__nativephp_app_bundle'));
120+
}
121+
122+
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);
123+
124+
Object.keys(iniSettings).forEach(key => {
125+
args.unshift('-d', `${key}=${iniSettings[key]}`);
126+
});
127+
128+
if (parseInt(process.env.SHELL_VERBOSITY) > 0) {
129+
console.log('Calling PHP', state.php, args);
130+
}
131+
132+
return spawnSync(
133+
state.php,
134+
args,
135+
{
136+
cwd: options.cwd,
137+
env: {
138+
...process.env,
139+
...options.env
140+
}
141+
}
142+
);
143+
}
144+
108145
function getArgumentEnv() {
109146
const envArgs = process.argv.filter(arg => arg.startsWith('--env.'));
110147

@@ -164,8 +201,37 @@ function getPath(name: string) {
164201
}
165202
}
166203

167-
function getDefaultEnvironmentVariables(secret, apiPort) {
168-
return {
204+
// Define an interface for the environment variables
205+
interface EnvironmentVariables {
206+
APP_ENV: string;
207+
APP_DEBUG: string;
208+
LARAVEL_STORAGE_PATH: string;
209+
NATIVEPHP_STORAGE_PATH: string;
210+
NATIVEPHP_DATABASE_PATH: string;
211+
NATIVEPHP_API_URL: string;
212+
NATIVEPHP_RUNNING: string;
213+
NATIVEPHP_SECRET: string;
214+
NATIVEPHP_USER_HOME_PATH: string;
215+
NATIVEPHP_APP_DATA_PATH: string;
216+
NATIVEPHP_USER_DATA_PATH: string;
217+
NATIVEPHP_DESKTOP_PATH: string;
218+
NATIVEPHP_DOCUMENTS_PATH: string;
219+
NATIVEPHP_DOWNLOADS_PATH: string;
220+
NATIVEPHP_MUSIC_PATH: string;
221+
NATIVEPHP_PICTURES_PATH: string;
222+
NATIVEPHP_VIDEOS_PATH: string;
223+
NATIVEPHP_RECENT_PATH: string;
224+
// Cache variables
225+
APP_SERVICES_CACHE?: string;
226+
APP_PACKAGES_CACHE?: string;
227+
APP_CONFIG_CACHE?: string;
228+
APP_ROUTES_CACHE?: string;
229+
APP_EVENTS_CACHE?: string;
230+
}
231+
232+
function getDefaultEnvironmentVariables(secret, apiPort): EnvironmentVariables {
233+
// Base variables with string values (no null values)
234+
let variables: EnvironmentVariables = {
169235
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
170236
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
171237
LARAVEL_STORAGE_PATH: storagePath,
@@ -185,6 +251,17 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
185251
NATIVEPHP_VIDEOS_PATH: getPath('videos'),
186252
NATIVEPHP_RECENT_PATH: getPath('recent'),
187253
};
254+
255+
// Only add cache paths if in production mode
256+
if(runningProdVersion()) {
257+
variables.APP_SERVICES_CACHE = join(bootstrapCache, 'services.php');
258+
variables.APP_PACKAGES_CACHE = join(bootstrapCache, 'packages.php');
259+
variables.APP_CONFIG_CACHE = join(bootstrapCache, 'config.php');
260+
variables.APP_ROUTES_CACHE = join(bootstrapCache, 'routes.php');
261+
variables.APP_EVENTS_CACHE = join(bootstrapCache, 'events.php');
262+
}
263+
264+
return variables;
188265
}
189266

190267
function getDefaultPhpIniSettings() {
@@ -217,13 +294,21 @@ function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
217294
// Make sure the storage path is linked - as people can move the app around, we
218295
// need to run this every time the app starts
219296
if (!runningSecureBuild()) {
220-
callPhp(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings)
297+
callPhpSync(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings)
298+
}
299+
300+
// Cache the project
301+
if (runningProdVersion()) {
302+
console.log('Caching view and routes...');
303+
// TODO: once per version
304+
callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings)
221305
}
222306

223307
// Migrate the database
224308
if (shouldMigrateDatabase(store)) {
225309
console.log('Migrating database...')
226-
callPhp(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings)
310+
callPhpSync(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings)
311+
// TODO: fail if callPhp fails and don't store migrated version
227312
store.set('migrated_version', app.getVersion())
228313
}
229314

src/Traits/CopiesBundleToBuildDirectory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function copyBundleToBuildDirectory(): bool
2525
$this->line('From: '.realpath(dirname($this->sourcePath(self::$bundlePath))));
2626
$this->line('To: '.realpath(dirname($this->buildPath(self::$bundlePath))));
2727

28+
// TODO: copy only the files that you need.
29+
$this->copyToBuildDirectory();
2830
$filesToCopy = [
2931
self::$bundlePath,
3032
'.env',
@@ -33,6 +35,7 @@ public function copyBundleToBuildDirectory(): bool
3335
foreach ($filesToCopy as $file) {
3436
$filesystem->copy($this->sourcePath($file), $this->buildPath($file), true);
3537
}
38+
// $this->keepRequiredDirectories();
3639

3740
return true;
3841
}

0 commit comments

Comments
 (0)