From 3f0e68e15366dcd43cb3c79313224c12b153cf92 Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Wed, 4 Sep 2024 14:22:41 +0800 Subject: [PATCH] =?UTF-8?q?ohos=E6=8F=92=E4=BB=B6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=BA=90=E7=A0=81=E4=BE=9D=E8=B5=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../lib/src/ohos/application_package.dart | 26 +++++++---- .../flutter_tools/lib/src/ohos/hvigor.dart | 28 ++++++------ .../lib/src/ohos/ohos_plugins_manager.dart | 45 ++++++++++++++----- 3 files changed, 65 insertions(+), 34 deletions(-) diff --git a/packages/flutter_tools/lib/src/ohos/application_package.dart b/packages/flutter_tools/lib/src/ohos/application_package.dart index c2e2ab987a..b2e3b3ba69 100644 --- a/packages/flutter_tools/lib/src/ohos/application_package.dart +++ b/packages/flutter_tools/lib/src/ohos/application_package.dart @@ -252,21 +252,31 @@ class OhosModule { String flavor; static List fromOhosProject(OhosProject ohosProject) { - final File buildProfileFile = ohosProject.ohosRoot.childFile('build-profile.json5'); + final List result = []; + final File buildProfileFile = + ohosProject.ohosRoot.childFile('build-profile.json5'); if (!buildProfileFile.existsSync()) { - return []; + return result; } - final Map buildProfile = JSON5.parse(buildProfileFile.readAsStringSync()) as Map; + final Map buildProfile = JSON5 + .parse(buildProfileFile.readAsStringSync()) as Map; if (!buildProfile.containsKey('modules')) { - return []; + return result; } final List modules = buildProfile['modules'] as List; - return modules.map((dynamic e) { + for (final dynamic e in modules) { final Map module = e as Map; final String srcPath = module['srcPath'] as String; - return OhosModule.fromModulePath( - modulePath: globals.fs.path.join(ohosProject.ohosRoot.path, srcPath)); - }).toList(); + final String modulePath = + globals.fs.path.join(ohosProject.ohosRoot.path, srcPath); + if (globals.fs.isLinkSync(modulePath) || + globals.fs.isDirectorySync(modulePath)) { + result.add(OhosModule.fromModulePath( + modulePath: modulePath, + )); + } + } + return result; } static OhosModule fromModulePath({ diff --git a/packages/flutter_tools/lib/src/ohos/hvigor.dart b/packages/flutter_tools/lib/src/ohos/hvigor.dart index 4ac5107ae4..bfc859c318 100644 --- a/packages/flutter_tools/lib/src/ohos/hvigor.dart +++ b/packages/flutter_tools/lib/src/ohos/hvigor.dart @@ -507,19 +507,20 @@ class OhosHvigorBuilder implements OhosBuilder { final String hvigorwPath = getHvigorwPath(_ohosRootPath, checkMod: true); - /// 生成所有 plugin 的 har - await assembleHars(_processUtils, project, ohosBuildInfo, _logger); + if (useHarDependency(project.ohos)) { + /// 生成所有 plugin 的 har + await assembleHars(_processUtils, project, ohosBuildInfo, _logger); + await removePluginsModules(project); + await addFlutterModuleAndPluginsOverrides(project); + // ohosProject.deleteOhModulesCache(); + await ohpmInstall( + processUtils: _processUtils, + workingDirectory: _ohosRootPath, + logger: _logger, + ); + } await assembleHsps(_processUtils, project, ohosBuildInfo, _logger); - await removePluginsModules(project); - await addFlutterModuleAndPluginsOverrides(project); - // ohosProject.deleteOhModulesCache(); - await ohpmInstall( - processUtils: _processUtils, - workingDirectory: _ohosRootPath, - logger: _logger, - ); - /// invoke hvigow task generate hap file. final int errorCode = await assembleHap( processUtils: _processUtils, @@ -709,10 +710,8 @@ class OhosHvigorBuilder implements OhosBuilder { await flutterBuildPre(flutterProject, ohosBuildInfo, target); - if (_ohosProject.isRunWithModuleHar) { + if (_ohosProject.isRunWithModuleHar && useHarDependency(flutterProject.ohos)) { await assembleHars(_processUtils, flutterProject, ohosBuildInfo, _logger); - await assembleHsps(_processUtils, flutterProject, ohosBuildInfo, _logger); - /// har文件拷贝后,需要重新install // ohosProject.deleteOhModulesCache(); await ohpmInstall( @@ -720,6 +719,7 @@ class OhosHvigorBuilder implements OhosBuilder { workingDirectory: _ohosProject.mainModuleDirectory.path, logger: _logger); } + await assembleHsps(_processUtils, flutterProject, ohosBuildInfo, _logger); } String _moduleNameWithFlavor(List modules, String? flavor) { diff --git a/packages/flutter_tools/lib/src/ohos/ohos_plugins_manager.dart b/packages/flutter_tools/lib/src/ohos/ohos_plugins_manager.dart index 574df42646..dd732da413 100644 --- a/packages/flutter_tools/lib/src/ohos/ohos_plugins_manager.dart +++ b/packages/flutter_tools/lib/src/ohos/ohos_plugins_manager.dart @@ -16,6 +16,7 @@ import 'dart:convert'; import 'package:json5/json5.dart'; +import 'package:path/src/context.dart'; import '../base/common.dart'; import '../base/file_system.dart'; @@ -27,6 +28,16 @@ import '../plugins.dart'; import '../project.dart'; const String kUseAbsolutePathOfHar = 'useAbsolutePathOfHar'; +const String kUseHarDependency = 'useHarDependency'; + +bool useAbsolutePathOfHar(OhosProject ohos) { + return ohos.settings.values[kUseAbsolutePathOfHar] == 'true'; +} + +bool useHarDependency(OhosProject ohos) { + return !ohos.settings.values.containsKey(kUseHarDependency) || + ohos.settings.values[kUseHarDependency] == 'true'; +} /// 检查 ohos plugin 依赖 Future checkOhosPluginsDependencies(FlutterProject flutterProject) async { @@ -35,31 +46,39 @@ Future checkOhosPluginsDependencies(FlutterProject flutterProject) async { .toList(); final File packageFile = flutterProject.ohos.flutterModulePackageFile; if (!packageFile.existsSync()) { - globals.logger.printTrace('check if oh-package.json5 file:($packageFile) exist ?'); + globals.logger + .printTrace('check if oh-package.json5 file:($packageFile) exist ?'); return; } - final SettingsFile settings = flutterProject.ohos.settings; - final bool useAbsolutePathOfHar = settings.values[kUseAbsolutePathOfHar] == 'true'; - final String packageConfig = packageFile.readAsStringSync(); - final Map config = JSON5.parse(packageConfig) as Map; + final Map config = + JSON5.parse(packageConfig) as Map; final Map dependencies = config['dependencies'] as Map; final List removeList = []; + final Context path = globals.fs.path; for (final Plugin plugin in plugins) { for (final String key in dependencies.keys) { if (key.startsWith('@ohos') && key.contains(plugin.name)) { removeList.add(key); } } - final String absolutePath = globals.fs.path.join(flutterProject.ohos.ohosRoot.path, 'har/${plugin.name}.har'); - if (useAbsolutePathOfHar && flutterProject.isModule) { - dependencies[plugin.name] = 'file:$absolutePath'; + final String usePluginPath; + if (useHarDependency(flutterProject.ohos)) { + final String absPath = path.join( + flutterProject.ohos.ohosRoot.path, 'har/${plugin.name}.har'); + if (useAbsolutePathOfHar(flutterProject.ohos) && + flutterProject.isModule) { + usePluginPath = absPath; + } else { + usePluginPath = _relative(absPath, packageFile.parent.path); + } } else { - final String relativePath = _relative(absolutePath, globals.fs.path.dirname(packageFile.path)); - dependencies[plugin.name] = 'file:$relativePath'; + final String absPath = path.join(plugin.path, 'ohos'); + usePluginPath = _relative(absPath, packageFile.parent.path); } + dependencies[plugin.name] = 'file:$usePluginPath'; } for (final String key in removeList) { globals.printStatus( @@ -85,10 +104,12 @@ Future addPluginsModules(FlutterProject flutterProject) async { final String packageConfig = buildProfileFile.readAsStringSync(); final Map buildProfile = JSON5.parse(packageConfig) as Map; final List> modules = (buildProfile['modules'] as List).cast(); - final Map modulesMap = Map.fromEntries(modules.map((e) => MapEntry(e['name'] as String, e))); + final Map modulesMap = Map.fromEntries( + modules.map((Map e) => + MapEntry(e['name'] as String, e))); for (final Plugin plugin in plugins) { if (modulesMap.containsKey(plugin.name)) { - continue; + modules.remove(modulesMap[plugin.name]); } modules.add({ 'name': plugin.name, -- Gitee