-
Notifications
You must be signed in to change notification settings - Fork 3
/
Ohos.ts
147 lines (119 loc) · 5.54 KB
/
Ohos.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as fs from 'fs';
import * as path from 'path';
import { AppCommand, STAND_ALONE_URL, WEBGLRENDERMODEJS } from './appCommand';
import { FileUtils } from './FileUtils';
export abstract class BaseTools {
abstract excuteCreateApp(demension:string, folder: string, sdk: string, platform: string, type: number, url: string, name: string, app_name: string, package_name: string, outputPath: string): boolean;
abstract excuteRefreshRes(folder: string, url: string, appPath:string): boolean
checkURL(url: string): boolean {
return true;
}
get standAloneUrl(): string {
return STAND_ALONE_URL
}
}
export class OhosTools extends BaseTools {
/**缓存目录 */
static Cache_Path = 'entry/src/main/resources/rawfile/cache';
/**入口文件 */
static Enter_Source_File = 'entry/src/main/ets/MainAbility/MainAbility.ts';
/**入口正则表达式 */
//static indexJS: RegExp = new RegExp("(?<=laya\\.ConchNAPI_configSetURL\\(\\s*\\')(.*)(?=\\'\\))");
static indexJS: RegExp = new RegExp("laya\\.ConchNAPI_configSetURL\\(\\s*\\'.*\\'\\s*\\)");
static indexJSReplace:string="laya.ConchNAPI_configSetURL('${url}')";
/**本地化正则表达式 */
static localizable:RegExp=new RegExp("laya\\.ConchNAPI_setLocalizable\\(\\s*.*\\s*\\)");
static localizableReplace:string="laya.ConchNAPI_setLocalizable(${localizable})";
checkURL(url: string): boolean {
return true;
}
/**
*
* @param folder 项目目录 type为0时无效
* @param sdk 本地sdk目录 模板目录
* @param platform 平台 ohos
* @param type type为2是单机包,为1是网络包
* @param url 项目地址
* @param name 导出目录名称
* @param app_name app名称
* @param package_name 包名
* @param outputPath 导出项目存储路径
* @returns
*/
public excuteCreateApp(demension:string, folder: string, sdk: string, platform: string, type: number, url: string, name: string, app_name: string, package_name: string, outputPath: string): boolean {
if (type !== 2 && !this.checkURL(url)) {
return false;
}
if (type > 0 && !fs.existsSync(folder)) {
console.log('错误: 找不到目录 ' + folder);
return false;
}
let appPath = AppCommand.getAppPath(AppCommand.getNativePath(path.join(outputPath, name)), platform);
let srcPath = path.join(sdk, platform);
if (type === 2) {
url = STAND_ALONE_URL;
}
FileUtils.copyFolderRecursiveSync(srcPath, path.dirname(appPath));
if (type > 0) {
FileUtils.processDcc({
res: {
path: OhosTools.Cache_Path,
}
}, folder, url, appPath);
}
this.replaceUrl(url, appPath,type);
const cfgFile = path.join(appPath, 'AppScope/app.json5');
let configJSON = FileUtils.readJSON5Sync(cfgFile);
configJSON.app.bundleName = package_name;
FileUtils.writeJSONSync(cfgFile, configJSON, 4);
const packageJsonPath = path.join(appPath, 'oh-package.json5');
const packageJson = FileUtils.readJSONSync(packageJsonPath);
packageJson.name = app_name;
FileUtils.writeJSONSync(packageJsonPath, packageJson, 4);
const appScopeStringJSONPath = path.join(appPath, 'AppScope/resources/base/element/string.json');
const appScopeStringJSON = FileUtils.readJSONSync(appScopeStringJSONPath);
appScopeStringJSON.string.find((item: any) => item.name === 'app_name').value = app_name;
FileUtils.writeJSONSync(appScopeStringJSONPath, appScopeStringJSON, 2);
const stringJSONPath = path.join(appPath, 'entry/src/main/resources/base/element/string.json');
const stringJSON = FileUtils.readJSONSync(stringJSONPath);
stringJSON.string.find((item: any) => item.name === 'MainAbility_label').value = app_name;
FileUtils.writeJSONSync(stringJSONPath, stringJSON, 2);
let nativeJSONPath = AppCommand.getNativeJSONPath(path.join(outputPath, name));
let nativeJSON = { h5: folder ? folder : ''};
FileUtils.mkdirsSync( path.dirname(nativeJSONPath));
fs.writeFileSync( nativeJSONPath, JSON.stringify(nativeJSON));
if (demension === '3D') {
var configJsPath = path.join(appPath, 'entry/src/main/resources/rawfile/scripts/config.js');
var str = fs.readFileSync(configJsPath, 'utf-8');
str += "\n";
str += WEBGLRENDERMODEJS;
fs.writeFileSync(configJsPath, str);
}
return true;
}
excuteRefreshRes(folder: string, url: string, appPath: string): boolean {
FileUtils.processDcc({
res: {
path: OhosTools.Cache_Path,
}
}, folder, url, appPath);
return true;
}
/**
* 替换项目URL
* @param url
* @param appPath
* @param type
*/
private replaceUrl(url: string, appPath: string,type:number): void {
let enterSourceFile = path.join(appPath, OhosTools.Enter_Source_File);
let standAlone = type === 2 ? true : false;
if(standAlone){
url = this.standAloneUrl;
}
let rs = fs.readFileSync(enterSourceFile, 'utf-8');
rs = rs.replace(OhosTools.indexJS,OhosTools.indexJSReplace.replace("${url}",url));
rs =rs.replace(OhosTools.localizable, OhosTools.localizableReplace.replace("${localizable}", standAlone?'true':'false'));
fs.writeFileSync(enterSourceFile, rs);
}
}