compile method

IrDocument compile(
  1. ProjectAnalysis analysis, {
  2. Map<String, IrScreen> rendered = const {},
  3. Map<String, IrImageAsset> renderedImages = const {},
  4. Map<String, Object?>? renderedTheme,
})

Compiles every screen of analysis. Screens in rendered (by name, e.g. captured at runtime) are used as they are instead, and their renderedImages are embedded too.

Implementation

IrDocument compile(
  ProjectAnalysis analysis, {
  Map<String, IrScreen> rendered = const {},
  Map<String, IrImageAsset> renderedImages = const {},
  Map<String, Object?>? renderedTheme,
}) {
  _diagnostics.clear();
  _images.clear();
  _renderedTheme = renderedTheme;
  _themeInCode = true;
  assets ??= ProjectAssets(analysis.root);
  if (extractTheme) {
    final extractor = ThemeExtractor(eval);
    eval.theme = extractor.fromProject(analysis, brightness: brightness);
    _themeInCode = !extractor.diagnostics.any(
      (d) => d.message.startsWith('No MaterialApp found'),
    );
    _absorb(extractor.diagnostics);
  }
  final screens = [
    for (final w in analysis.screens)
      rendered[w.name] ?? compileScreen(w.name, w.tree!, w.source),
  ];

  IrDesignSystem? system;
  _painted.clear();
  for (final s in screens) {
    _collectTokens(s.root);
  }
  if (designSystem) {
    final components = ComponentExtractor(
      minUses: minComponentUses,
      sources: {for (final w in analysis.widgets) w.name: w.source},
    ).extract(screens);
    system = _designSystem(analysis, components);
  } else {
    void clear(IrNode n) {
      n.instance = null;
      if (n is IrFrame) n.children.forEach(clear);
    }

    for (final s in screens) {
      clear(s.root);
    }
  }

  return IrDocument(
    project: analysis.name,
    screens: screens,
    designSystem: system,
    images: {..._images, ...renderedImages},
    diagnostics: [
      for (final d in analysis.diagnostics) IrDiagnostic(IrSeverity.error, d),
      ..._diagnostics.values,
    ],
  );
}