Flutter aspectd (二)原始碼解析

老毛發表於2021-06-02

引導

在上一篇文章中,我們進行了apply patch檔案,那麼我們來看看apply的檔案,具體做了哪些事情。可以看到是在common.dart檔案做了更改,和新加了一個aspectd.dart檔案

common.dart檔案

該檔案所在目錄:

packages/flutter_tools/lib/build_system/targets/common.dart
複製程式碼

可以看到在build方法新增瞭如下程式碼:

 @override
  Future<void> build(Environment environment) async {
    // 這是原來的程式碼
    await buildImpl(environment);
    // 這是新增程式碼
    if (await AspectdHook.isAspectdEnabled()) {
        await AspectdHook().runBuildDillCommand(environment);
    }
  }
複製程式碼

AspectdHook.isAspectdEnabled()

上面程式碼呼叫了AspectdHook.isAspectdEnabled(),看看這裡面做了什麼

  static Future<bool> 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<Directory> 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<Directory> 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<bool> 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<String> commands = <String>[
        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<String> getDartSdkDependency(String aspectdDir) async {
    // 在aspectdDir下(即aspectd所在專案)執行pub get從而生成.package檔案
    final ProcessResult processResult = await globals.processManager.run(
        <String>[
          globals.fs.path.join(
              globals.artifacts.getArtifactPath(Artifact.engineDartSdkPath),
              'bin',
              'pub'),
          'get',
          '--verbosity=warning'
        ],
        workingDirectory: aspectdDir,
        environment: <String, String>{'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<void> 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<String, String> 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

相關文章