Skip to content

Commit 1ac340e

Browse files
committed
fix build
1 parent bbea49e commit 1ac340e

4 files changed

Lines changed: 90 additions & 52 deletions

File tree

‎scripts/build-app.sh‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,80 @@ else
211211
echo " (AppWindowAdapter.ets not found, skipping)"
212212
fi
213213

214+
# --- Patch WebAbilityStage.ets for startup timing fix ------------------------
215+
216+
echo "==> Patching web_engine WebAbilityStage for SetContextPaths timing ..."
217+
218+
WEB_ABILITY_STAGE="${WEB_ENGINE_DIR}/src/main/ets/application/WebAbilityStage.ets"
219+
if [ -f "${WEB_ABILITY_STAGE}" ]; then
220+
python3 -c "
221+
import sys
222+
223+
with open('${WEB_ABILITY_STAGE}', 'r') as f:
224+
content = f.read()
225+
226+
original = content
227+
228+
# 1. Add synchronous DI init + SetContextPaths to onCreate(), before
229+
# runTaskAsync() is called. This ensures the native module has the
230+
# HarmonyOS directory paths BEFORE the Electron runtime starts and
231+
# tries to call app.getPath('appData').
232+
old_oncreate = ''' onCreate(): void {
233+
LogUtil.info(TAG, '[ohoswindow] in WebAbilityStage onCreate.');
234+
this.runTaskAsync();
235+
}'''
236+
237+
new_oncreate = ''' onCreate(): void {
238+
LogUtil.info(TAG, '[ohoswindow] in WebAbilityStage onCreate.');
239+
// Initialize DI container and set context paths synchronously,
240+
// before the Electron runtime starts. This ensures app.getPath()
241+
// works when the Electron main process loads app-props.js.
242+
// (Moved from runTaskAsync to fix race condition on slower devices.)
243+
if (!GlobalThisHelper.isLaunched()) {
244+
let appContext = this.context.getApplicationContext();
245+
GlobalThisHelper.appInit(new CommonDependencyProvider(appContext));
246+
}
247+
JsBindingUtils.SetContextPaths();
248+
this.runTaskAsync();
249+
}'''
250+
251+
content = content.replace(old_oncreate, new_oncreate)
252+
253+
# 2. Remove the appInit block from runTaskAsync() (now done in onCreate)
254+
old_appinit = ''' if (!GlobalThisHelper.isLaunched()) {
255+
let appContext = this.context.getApplicationContext();
256+
GlobalThisHelper.appInit(new CommonDependencyProvider(appContext));
257+
}
258+
import'''
259+
260+
new_appinit = ''' import'''
261+
262+
content = content.replace(old_appinit, new_appinit)
263+
264+
# 3. Remove SetContextPaths from the async import().then() callback
265+
# (now done synchronously in onCreate)
266+
old_setpaths = ''' this.nativeThemeAdapter = Inject.get(NativeThemeAdapter);
267+
JsBindingUtils.SetContextPaths();
268+
}).catch'''
269+
270+
new_setpaths = ''' this.nativeThemeAdapter = Inject.get(NativeThemeAdapter);
271+
}).catch'''
272+
273+
content = content.replace(old_setpaths, new_setpaths)
274+
275+
if content == original:
276+
print(' WARNING: WebAbilityStage.ets pattern not found — file may already be patched or structure changed')
277+
sys.exit(0)
278+
279+
with open('${WEB_ABILITY_STAGE}', 'w') as f:
280+
f.write(content)
281+
282+
print(' WebAbilityStage patched: SetContextPaths moved to synchronous onCreate()')
283+
"
284+
else
285+
echo " (WebAbilityStage.ets not found, skipping)"
286+
fi
287+
214288
# --- Check signing materials ------------------------------------------------
215289

216290
echo "==> Checking signing materials ..."

‎src/app/common/app-props.js‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,20 @@
11
/**
2-
* app path
2+
* app path — HarmonyOS only.
3+
*
4+
* On HarmonyOS, appData (filesDir) serves as both the app data directory
5+
* and the home directory. There is no separate home concept or portable mode.
36
*/
47
const { app } = require('electron')
58
const { resolve } = require('path')
69
const constants = require('./runtime-constants')
7-
const installSrc = require('../lib/install-src')
810

9-
function getDataPath () {
10-
const defaultValue = {
11-
appPath: app.getPath('appData'),
12-
isPortable: false
13-
}
14-
if (!constants.isWin) {
15-
return defaultValue
16-
}
17-
const exePath = app.getPath('exe').replace('\\electerm.exe', '')
18-
const p = exePath + '\\' + 'electerm'
19-
if (
20-
installSrc === 'win-x64-portable.tar.gz' ||
21-
require('fs').existsSync(
22-
p
23-
)
24-
) {
25-
return {
26-
appPath: exePath,
27-
exePath,
28-
isPortable: true
29-
}
30-
}
31-
return {
32-
...defaultValue,
33-
exePath
34-
}
35-
}
11+
const appDataPath = app.getPath('appData')
3612

3713
module.exports = {
38-
...getDataPath(),
39-
sshKeysPath: resolve(
40-
app.getPath('home'),
41-
'.ssh'
42-
),
14+
appPath: appDataPath,
15+
isPortable: false,
16+
exePath: '',
17+
sshKeysPath: resolve(appDataPath, '.ssh'),
18+
homeOrTmp: appDataPath,
4319
...constants
4420
}

‎src/app/common/runtime-constants.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,5 @@ module.exports = {
5151
minWindowHeight: 400,
5252
defaultLang: 'en_us',
5353
tempDir: require('os').tmpdir(),
54-
homeOrTmp: os.homedir() || os.tmpdir(),
5554
packInfo: require(isDev ? '../../../package.json' : '../package.json')
5655
}

‎src/app/lib/single-instance.js‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
/**
2-
* Single instance lock with socket-based IPC fallback
3-
* for Electron versions where additionalData doesn't work (e.g., Electron 22)
2+
* Single instance lock with socket-based IPC
43
*/
54

65
const net = require('net')
76
const fs = require('fs')
87
const path = require('path')
8+
const os = require('os')
99
const { app } = require('electron')
1010
const globalState = require('./glob-state')
1111

12-
// Get socket path based on platform
13-
function getSocketPath () {
14-
const appName = app.getName()
15-
if (process.platform === 'win32') {
16-
return `\\\\.\\pipe\\${appName}-instance-lock`
17-
}
18-
// Unix socket
19-
const tmpDir = app.getPath('temp')
20-
return path.join(tmpDir, `${appName}-instance.sock`)
21-
}
22-
23-
const socketPath = getSocketPath()
12+
const socketPath = path.join(os.tmpdir(), `${app.getName()}-instance.sock`)
2413

25-
// Clean up stale socket file on Unix
14+
// Clean up stale socket file
2615
function cleanupSocket () {
27-
if (process.platform !== 'win32' && fs.existsSync(socketPath)) {
16+
if (fs.existsSync(socketPath)) {
2817
try {
2918
fs.unlinkSync(socketPath)
3019
} catch (e) {
@@ -94,7 +83,7 @@ function sendToFirstInstance (data) {
9483
}
9584

9685
/**
97-
* Handle second instance connection with socket fallback
86+
* Handle second instance connection
9887
* @param {Object} progs - Parsed command line options
9988
* @returns {Promise<boolean>} - True if this is the primary instance
10089
*/

0 commit comments

Comments
 (0)