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

native-pack-tool support send message to builder #16202

Closed
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
8 changes: 7 additions & 1 deletion packages/native-pack-tool/source/base/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import { cchelper, Paths } from "../utils";
import { CocosProjectTasks } from './cocosProjectTypes';
import { gzipSync } from 'zlib';
import { EventEmitter } from 'events';
const globby = require('globby');
const xxtea = require('xxtea-node');

Expand Down Expand Up @@ -68,7 +69,7 @@ export interface INativePlatformOptions {
init: (params: CocosParams<Object>) => void;
}

export abstract class NativePackTool {
export abstract class NativePackTool extends EventEmitter {
// 传入的打包参数
params!: CocosParams<Object>;
// 收集初始化的一些路径信息
Expand Down Expand Up @@ -535,6 +536,11 @@ export abstract class NativePackTool {
}
}

// 平台统一更新日志消息
protected handleMessage(message: string) {
this.emit('update-message', message);
}

abstract create(): Promise<boolean>;
generate?(): Promise<boolean>;
make?(): Promise<boolean>;
Expand Down
16 changes: 16 additions & 0 deletions packages/native-pack-tool/source/base/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class NativePackToolManager {

private PackToolMap: Record<string, NativePackTool> = {};

private updateMessage: Function | null = null;

private getPackTool(platform: string): NativePackTool {
const handler = this.PackToolMap[platform];
if (!handler) {
Expand Down Expand Up @@ -53,6 +55,12 @@ export class NativePackToolManager {
if (!tool.make) {
return false;
}
// 监听平台执行时的日志信息
tool.on('update-message', (message: string) => {
if (this.updateMessage) {
this.updateMessage(message);
}
});
await tool.make();
return true;
}
Expand All @@ -65,6 +73,14 @@ export class NativePackToolManager {
await tool.run();
return true;
}

setUpdateMessage(func: Function) {
this.updateMessage = func;
}

getUpdateMessage(): Function | null {
return this.updateMessage;
}
}

export const nativePackToolMg = new NativePackToolManager();
3 changes: 3 additions & 0 deletions packages/native-pack-tool/source/platforms/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ export class AndroidPackTool extends NativePackTool {
const originDir = process.cwd();
try {
process.chdir(projDir);
// cchelper 需要先设置监听消息的方法,才能够在执行 runCmake 时传递消息
cchelper.handleMessage = this.handleMessage.bind(this);
await cchelper.runCmd(gradle, [buildMode], false, projDir);
} catch (e) {
throw e;
} finally {
// popd
process.chdir(originDir);
cchelper.handleMessage = null;
}


Expand Down
3 changes: 3 additions & 0 deletions packages/native-pack-tool/source/platforms/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export class WindowsPackTool extends NativePackTool {

async make() {
const nativePrjDir = this.paths.nativePrjDir;
// toolHelper 需要先设置监听消息的方法,才能够在执行 runCmake 时传递消息
toolHelper.handleMessage = this.handleMessage.bind(this);
await toolHelper.runCmake(['--build', `"${cchelper.fixPath(nativePrjDir)}"`, '--config', this.params.debug ? 'Debug' : 'Release', '--', '-verbosity:quiet']);
toolHelper.handleMessage = null;
return true;
}

Expand Down
11 changes: 9 additions & 2 deletions packages/native-pack-tool/source/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ export const EXT_LIST = ['.js', '.ccc', '.ccd', '.jsg', '.jsc'];

export class cchelper {

static handleMessage: Function | null = null;

static replaceEnvVariables(str: string): string {
return str.replace(/\$\{([^}]*)\}/g, (_, n) => process.env[n] === undefined ? _ : process.env[n]!)
.replace(/(~)/g, (_, n) => process.env.HOME!);
}


static fixPath(p: string): string {
p = this.replaceEnvVariables(p);
if (os.platform() === 'win32') {
Expand Down Expand Up @@ -297,7 +298,6 @@ export class cchelper {
await fs.writeFile(filepath, newContent);
}


static exactValueFromFile(regexp: RegExp, filename: string, idx: number): string | undefined {
if (!(fs.existsSync(filename))) {
console.error(`file ${filename} not exist!`);
Expand Down Expand Up @@ -326,6 +326,8 @@ export class cchelper {
if (!slient) {
cp.stdout.on(`data`, (chunk) => {
console.log(`[runCmd ${cmd}] ${chunk}`);
// TODO: 此处是在 cchelper 里示例,需要自定义消息过滤
this.handleMessage && this.handleMessage(chunk);
});
cp.stderr.on(`data`, (chunk) => {
console.log(`[runCmd ${cmd} - error] ${chunk}`);
Expand Down Expand Up @@ -423,6 +425,8 @@ export class cchelper {
}

export const toolHelper = {
handleMessage: null as ((msg: string) => void) | null,

getXcodeMajorVerion(): number {
try {
const output = execSync('xcrun xcodebuild -version').toString('utf8');
Expand Down Expand Up @@ -487,6 +491,9 @@ export const toolHelper = {
console.log(`[cmake-warn] ${msg}`);
} else {
console.log(`[cmake] ${msg}`);
// TODO: 此处是在 toolHelper 里示例
// 自定义过滤日志信息, 但目前 cmake 并没有抛出什么信息
toolHelper.handleMessage && toolHelper.handleMessage(msg);
}
});
cp.stderr.on('data', (data: any) => {
Expand Down
Loading