getKnownRegions function

Future<(String, Set<String>)> getKnownRegions(
  1. String projectPath
)

Gets the known regions and source language from the Xcode project.

Implementation

Future<(String source, Set<String> regions)> getKnownRegions(
  String projectPath,
) async {
  final rubyScript =
      '''
require "xcodeproj"

project_path = "$projectPath"

begin
  project = Xcodeproj::Project.open(project_path)

  # Get development region (source language) - print it first
  dev_region = project.root_object.development_region
  puts dev_region

  # Get known regions
  known_regions = project.root_object.known_regions
  known_regions.each do |region|
    puts region
  end
rescue => e
  STDERR.puts "Error: " + e.message
  exit 1
end
''';

  final result = await Process.run('ruby', ['-e', rubyScript]);

  if (result.exitCode != 0) {
    Logger.error('Error getting known regions: ${result.stderr}');
    return ('en', <String>{});
  }

  // Parse output - first line is source language, rest are known regions
  final lines = result.stdout
      .toString()
      .trim()
      .split('\n')
      .where((line) => line.isNotEmpty)
      .map((line) => line.trim())
      .toList();

  if (lines.isEmpty) {
    return ('en', <String>{});
  }

  final source = lines.first;
  final regions = lines
      .skip(1) // Skip the first line (source language)
      .where(
        (region) => region != 'Base',
      ) // Filter out "Base" - not a real language
      .toSet();

  return (source, regions);
}