getCombinedAppBundle method

Future<Map?> getCombinedAppBundle()

Implementation

Future<Map?> getCombinedAppBundle() async {
  Map code = {};
  Map output = {};
  Map widgets = {};
  Map actions = {};

  try {
    // Get the manifest content
    final manifestContent =
        await rootBundle.loadString(path + '.manifest.json');
    final Map<String, dynamic> manifestMap = json.decode(manifestContent);

    // Process App Widgets
    try {
      if (manifestMap['widgets'] != null) {
        final List<Map<String, dynamic>> widgetsList =
            List<Map<String, dynamic>>.from(manifestMap['widgets']);

        for (var widgetItem in widgetsList) {
          try {
            // Load the widget content in YamlMap
            final widgetContent =
                await _readFile("widgets/${widgetItem["name"]}.yaml");
            if (widgetContent is YamlMap) {
              // Store the full YAML to preserve Import declarations
              widgets[widgetItem["name"]] = widgetContent;
            } else {
              debugPrint('Content in ${widgetItem["name"]} is not a YamlMap');
            }
          } catch (e) {
            // ignore error
          }
        }
      }
    } catch (e) {
      debugPrint('Error processing widgets: $e');
    }

    // Process App Scripts
    try {
      if (manifestMap['scripts'] != null) {
        final List<Map<String, dynamic>> scriptsList =
            List<Map<String, dynamic>>.from(manifestMap['scripts']);

        for (var script in scriptsList) {
          try {
            // Load the script content in string
            final scriptContent = await rootBundle
                .loadString("${path}scripts/${script["name"]}.js");
            code[script["name"]] = scriptContent;
          } catch (e) {
            // ignore error
          }
        }
      }
    } catch (e) {
      debugPrint('Error processing scripts: $e');
    }

    // Process App Actions (reusable global Actions)
    try {
      if (manifestMap['actions'] != null) {
        final List<Map<String, dynamic>> actionsList =
            List<Map<String, dynamic>>.from(manifestMap['actions']);

        for (var actionItem in actionsList) {
          try {
            final String actionName = actionItem["name"];
            final dynamic actionContent =
                await _readFile("actions/$actionName.yaml");

            if (actionContent is! Map) {
              debugPrint(
                  'Content in action $actionName is not a Map/YamlMap (${actionContent.runtimeType})');
              continue;
            }

            final YamlMap? actionDefinition =
                ActionScopeUtil.mergeActionFileContent(actionContent);
            if (actionDefinition == null) {
              debugPrint('Action root in $actionName is not a Map/YamlMap');
              continue;
            }

            actions[actionName] = actionDefinition;
          } catch (e) {
            // ignore error loading individual action
          }
        }
      }
    } catch (e) {
      debugPrint('Error processing actions: $e');
    }

    output[ResourceArtifactEntry.Widgets.name] = widgets;
    output[ResourceArtifactEntry.Scripts.name] = code;
    if (actions.isNotEmpty) {
      output[ResourceArtifactEntry.Actions.name] = actions;
    }

    return output;
  } catch (e) {
    return null;
  }
}