Flutter aspectd (二)源码解析

引导

在上一篇文章中,我们进行了apply patch文件,那么我们来看看apply的文件,具体做了哪些事情。可以看到是在common.dart文件做了更改,和新加了一个aspectd.dart文件

common.dart文件

该文件所在目录:

packages/flutter_tools/lib/build_system/targets/common.dart

可以看到在build方法新增了如下代码:

 @override
  Future build(Environment environment) async {
    // 这是原来的代码
    await buildImpl(environment);
    // 这是新增代码
    if (await AspectdHook.isAspectdEnabled()) {
        await AspectdHook().runBuildDillCommand(environment);
    }
  }

AspectdHook.isAspectdEnabled()

上面代码调用了AspectdHook.isAspectdEnabled(),看看这里面做了什么

  static Future isAspectdEnabled() async {
    final Directory currentDirectory = globals.fs.currentDirectory;
    
    // 获取到aspectd_impl对应的目录,详情见下面
    final Directory aspectdDirectory = getAspectdImplDirectory(currentDirectory);
    // 如果该目录不存在,返回false,不走aspectd逻辑
    if (!aspectdDirectory.existsSync()) {
      return false;
    }
    
    // 拿到aspectd_imple项目下的.packages文件,因为要取该文件,所以我们需要先执行 pub get
    final String aspectdImplPackagesPath = globals.fs.path.join(aspectdDirectory.absolute.path, '.packages');
    // 通过.package文件中的数据,得到aspectd目录,从而得到frontend_server.dart.snapshot所在的目录,具体见下方
    final Directory flutterFrontendServerDirectory = await getFlutterFrontendServerDirectory(aspectdImplPackagesPath);
    
    // 判断如果aspectd_impl项目不存在或frontend_server.dart.snapshot对应目录不存在 及 对应的文件不存在的话返回false
    if (!(aspectdDirectory.existsSync() &&
        flutterFrontendServerDirectory.existsSync() &&
        currentDirectory.absolute.path != aspectdDirectory.absolute.path &&
        globals.fs
            .file(globals.fs.path.join(aspectdDirectory.path, 'pubspec.yaml'))
            .existsSync() &&
        globals.fs
            .file(
            globals.fs.path.join(aspectdDirectory.path, '.packages'))
            .existsSync() &&
        globals.fs
            .file(globals.fs.path.join(
            aspectdDirectory.path, 'lib', aspectdImplPackageName + '.dart'))
            .existsSync())) {
      return false;
    }
    // 生成frontend_server.dart.snapshot,具体见下方
    return await checkAspectdFlutterFrontendServerSnapshot(aspectdImplPackagesPath);
  }

下面就是获取到aspectd_impl目录的具体方法

const String aspectdImplPackageRelPath = '..';
const String aspectdImplPackageName = 'aspectd_impl';

.
.
.

  static Directory getAspectdImplDirectory(Directory rootProjectDir) {
    return globals.fs.directory(globals.fs.path.normalize(globals.fs.path.join(
        rootProjectDir.path,
        aspectdImplPackageRelPath,
        aspectdImplPackageName)));
  }

获取aspectd对应的项目,及该项目下的flutter_frontend_server目录

 static Future getFlutterFrontendServerDirectory(
      String packagesPath) async {
      // 找到aspectd对应项目的路径后,添加具体flutter_frontend_server对应的路径
    return globals.fs.directory(globals.fs.path.join(
        (await getPackagePathFromConfig(packagesPath, 'aspectd')).absolute.path,
        'lib',
        'src',
        'flutter_frontend_server'));
  }
 static Future getPackagePathFromConfig(String packageConfigPath, String packageName) async {
    // 取出.package中的信息
    final PackageConfig packageConfig = await loadPackageConfigWithLogging(globals.fs.file(packageConfigPath),logger: globals.logger,);
    if ((packageConfig?.packages?.length ?? 0) > 0) {
      final Package aspectdPackage = packageConfig.packages.toList().firstWhere(
                // 找到我们要找的信息
              (Package element) => element.name == packageName,
          orElse: () => null);
        // 返回找到的路径
      return globals.fs.directory(aspectdPackage.root.toFilePath());
    }
    return null;
  }

生成frontend_server.dart.snapshot

const String frontendServerDartSnapshot = 'frontend_server.dart.snapshot';


static Future checkAspectdFlutterFrontendServerSnapshot(
      String packagesPath) async {
      // 获取到frontend_server.dart.snapshot对应上级目录,及文件对应路径
    final Directory flutterFrontendServerDirectory = await getFlutterFrontendServerDirectory(packagesPath);
    final String aspectdFlutterFrontendServerSnapshot = globals.fs.path.join(flutterFrontendServerDirectory.absolute.path,frontendServerDartSnapshot);
    
    // 获取到系统的frontend_server.dart.snapshot对应的路径
    final String defaultFlutterFrontendServerSnapshot = globals.artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk);
    
    // 如果frontend_server.dart.snapshot不存在,那么进行创建
    if (!globals.fs.file(aspectdFlutterFrontendServerSnapshot).existsSync()) {
    
        // 在getDartSdkDependency中执行pub get以便获取到aspectd对应项目中的.package,从而能得到dartSdkDir,详情见下方
      final String dartSdkDir = await getDartSdkDependency((await getPackagePathFromConfig(packagesPath, 'aspectd')).absolute.path);

        // 获取到flutter_frontend_server文件夹下的package_config.json
      final String frontendServerPackageConfigJsonFile = '${flutterFrontendServerDirectory.absolute.path}/package_config.json';
        // 获取到flutter_frontend_server文件夹下的rebased_package_config.json,一开始是不存在的,下面会往里面放东西
      final String rebasedFrontendServerPackageConfigJsonFile = '${flutterFrontendServerDirectory.absolute.path}/rebased_package_config.json';
      // 读取package_config.json中数据
      String frontendServerPackageConfigJson = globals.fs.file(frontendServerPackageConfigJsonFile).readAsStringSync();
      // 把上面读取到的数据中的../../../third_party/dart/替换为真是的dartSdkDir目录,即上面得到的kernel目录
      frontendServerPackageConfigJson = frontendServerPackageConfigJson.replaceAll('../../../third_party/dart/', dartSdkDir);
      // 将上面替换后的数据写到rebased_package_config.json文件中
      globals.fs.file(rebasedFrontendServerPackageConfigJsonFile).writeAsStringSync(frontendServerPackageConfigJson);

        // 准备生成frontend_server.dart.sanpshot文件对应的命令
      final List commands = [
        globals.artifacts.getArtifactPath(Artifact.engineDartBinary),
        '--deterministic',
        '--packages=$rebasedFrontendServerPackageConfigJsonFile',
        '--snapshot=$aspectdFlutterFrontendServerSnapshot',
        '--snapshot-kind=kernel',
        '${flutterFrontendServerDirectory.absolute.path}/starter.dart'
      ];
      // 执行命令生成frontend_server.dart.snapshot
      final ProcessResult processResult =await globals.processManager.run(commands);
      // 删除上面拷贝的那一份rebased_package_config.json文件(已经生成frontend_server.dart.server,所以不需要了)
      globals.fs.file(rebasedFrontendServerPackageConfigJsonFile).deleteSync();
      
      //异常判断
      if (processResult.exitCode != 0 || globals.fs.file(aspectdFlutterFrontendServerSnapshot).existsSync() ==false) {
        throwToolExit('Aspectd unexpected error: ${processResult.stderr.toString()}');
      }
    }
    
    // 查看系统中的frontend_server.dart.snapshot是否存在,存在的话删除掉
    if (globals.fs.file(defaultFlutterFrontendServerSnapshot).existsSync()) {
      globals.fs.file(defaultFlutterFrontendServerSnapshot).deleteSync();
    }
    // 把刚才生成的文件拷贝到系统
    globals.fs.file(aspectdFlutterFrontendServerSnapshot).copySync(defaultFlutterFrontendServerSnapshot);
    return true;
  }
  static Future getDartSdkDependency(String aspectdDir) async {
    // 在aspectdDir下(即aspectd所在项目)执行pub get从而生成.package文件
    final ProcessResult processResult = await globals.processManager.run(
        [
          globals.fs.path.join(
              globals.artifacts.getArtifactPath(Artifact.engineDartSdkPath),
              'bin',
              'pub'),
          'get',
          '--verbosity=warning'
        ],
        workingDirectory: aspectdDir,
        environment: {'FLUTTER_ROOT': Cache.flutterRoot});
        
    // 异常卡控
    if (processResult.exitCode != 0) {
      throwToolExit(
          'Aspectd unexpected error: ${processResult.stderr.toString()}');
    }
    
    // 根据.package文件找到kernel对应的具体目录,也就是dart sdk的目录
    final Directory kernelDir = await getPackagePathFromConfig(
        globals.fs.path.join(aspectdDir, '.packages'), 'kernel');
    return kernelDir.parent.parent.uri.toString();
  }

综合,在调用isAspectdEnabled方法的时候,做了2件事情:1.判断是否存在aspectd_impl和aspectd项目,不存在就不走aspectd这一套,防止其他项目有问题,2.生成frontend_server.dart.snapshot文件,并替换掉系统中的该文件。

frontend_server.dart.snapshot的作用:把我们的dart代码编译成app.dill

AspectdHook().runBuildDillCommand(environment)

上面只是做了一些准备工作,之后就是真正的将dart代码编译成dill

Future runBuildDillCommand(Environment environment) async {

    // 把系统的当前指向目录切换到aspectd_impl目录下方
    final Directory aspectdDir = getAspectdImplDirectory(globals.fs.currentDirectory);
    final Directory previousDirectory = globals.fs.currentDirectory;
    globals.fs.currentDirectory = aspectdDir;

    // 指定产物所在的目录,及编译工作所在的目录
    String relativeDir = environment.outputDir.absolute.path.substring(environment.projectDir.absolute.path.length +  1);
    final String outputDir = globals.fs.path.join(aspectdDir.path, relativeDir);
    final String buildDir =globals.fs.path.join(aspectdDir.path, '.dart_tool', 'flutter_build');

    // 指定要编译的dart文件,这里是aspectd_impl.dart,改文件起到了承上启下的作用,能把要插入的代码和我们写的代码串到一起,见下方
    final Map defines = environment.defines;
    defines[kTargetFile] = globals.fs.path.join(aspectdDir.path, 'lib', aspectdImplPackageName + '.dart');

    // 准备编译环境
    final Environment auxEnvironment = Environment(
        projectDir: aspectdDir,
        outputDir: globals.fs.directory(outputDir),
        cacheDir: environment.cacheDir,
        flutterRootDir: environment.flutterRootDir,
        fileSystem: environment.fileSystem,
        logger: environment.logger,
        artifacts: environment.artifacts,
        processManager: environment.processManager,
        engineVersion: environment.engineVersion,
        buildDir: globals.fs.directory(buildDir),
        defines: defines,
        inputs: environment.inputs);
    const KernelSnapshot auxKernelSnapshot = KernelSnapshot();
    
    // 进行编译,获得产物
    final CompilerOutput compilerOutput = await auxKernelSnapshot.buildImpl(auxEnvironment);

    // 把生成的产物拷贝到我们写的项目的.dart_tool/flutter_build/对应目录下(因为上方生成的app.dill产物是在aspectd_impl项目中)
    final String aspectdDill = compilerOutput.outputFilename;
    final File originalDillFile = globals.fs.file(globals.fs.path.join(environment.buildDir.absolute.path, 'app.dill'));
    // 这里是把我们写的项目中存在的app.dill进行了备份
    if (originalDillFile.existsSync()) {
      originalDillFile.renameSync(originalDillFile.absolute.path + '.bak');
    }
    // 具体的拷贝app.dill方法
    globals.fs.file(aspectdDill).copySync(originalDillFile.absolute.path);
    globals.fs.currentDirectory = previousDirectory;
  }
import 'package:sensors_demo/main.dart' as app;

// 下面就是导入的要插入的代码,它里面的注解能够使它虽没被引用,依然能参与编译
import 'sensorsdata_aop_impl.dart';
import 'sa_autotrack.dart';

// 这里调用的是我们写的代码中的main方法,所以生成的app.dill中包含aspect_impl及我们写的代码
void main()=> app.main();

总结

aspectd中都做了哪些事情:

  1. 判断当前执行项目的上一级中是否有aspect_impl项目,有的话就走aspectd逻辑。
  2. 生成frontend_server.dart.snapshot文件,并替换flutter sdk中对应的该文件。aspectd源码中的flutter_frontend_server文件下的就是对应做这个事情的。(frontend_server.dart.snapshot是用来把dart代码编译成dill)
  3. 把我们写的代码及要插入的代码一起编译成app.dill。我们写的代码是通过aspect_impl项目中的main方法调用了我们项目中的main方法。而插入的代码是通过注解实现的,在frontend_server.dart.snapshot将dart编译成app.dill过程中,会把注解转换为具体代码插入到抽象语法树(AST)中。涉及到的就是aspectd源码中的transformer中的文件。这也是为什么要用自己生成的frontend_server.dart.snapshot文件替换系统的该文件,因为aspectd生成的frontend_server.dart.snapshot中能够把注解转换为具体代码插入到AST中,从而最后生成的app.dill中是包含所有的代码。

为了好理解,写的有些啰嗦,悟性好的直接看阿里提供的图:

image

你可能感兴趣的:(Flutter aspectd (二)源码解析)