generate method

Future<bool> generate()

Generates theme files from Figma variables.

This method:

  1. Creates the output directory
  2. Fetches variables from Figma
  3. Processes and categorizes variables
  4. Generates Flutter theme files

Returns true if generation was successful, false otherwise.

Implementation

Future<bool> generate() async {
  try {
    // Create output directory
    final dir = Directory(_outputDir);
    if (!dir.existsSync()) {
      dir.createSync(recursive: true);
    }

    // Fetch variables
    final data = await _client.getVariables();
    final collections =
        data['meta']['variableCollections'] as Map<String, dynamic>;
    final variables = data['variables'] as Map<String, dynamic>;

    if (_verbose) {
      Logger.debug('Found ${collections.length} variable collections');
      Logger.debug('Found ${variables.length} variables');
    }

    // Process variables by type
    final colors = <String, dynamic>{};
    final typography = <String, dynamic>{};
    final spacing = <String, dynamic>{};

    variables.forEach((id, variable) {
      final name = variable['name'] as String;
      final value = variable['valuesByMode'];
      final type = variable['resolvedType'] as String;

      if (type == 'COLOR') {
        colors[name] = value;
      } else if (type == 'FLOAT' || type == 'NUMBER') {
        if (name.toLowerCase().contains('spacing')) {
          spacing[name] = value;
        }
      } else if (type == 'STRING' && name.toLowerCase().contains('font')) {
        typography[name] = value;
      }
    });

    // Generate theme files
    _generateColorFile(colors);
    _generateTypographyFile(typography);
    _generateSpacingFile(spacing);
    _generateThemeFile();

    Logger.success('Theme generation completed successfully');
    return true;
  } catch (e) {
    Logger.error('Failed to generate theme: $e');
    return false;
  }
}