From 7a740085efeadb563e4efdcd9307af82c0ed9a1a Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sun, 4 Sep 2022 12:12:48 +0200 Subject: [PATCH] spec_templates: Allow includes in routes files --- packages/spec_templates/bin/generate.dart | 109 ++++++++++++++-------- 1 file changed, 72 insertions(+), 37 deletions(-) diff --git a/packages/spec_templates/bin/generate.dart b/packages/spec_templates/bin/generate.dart index 029cdc1d..1d84ab5f 100644 --- a/packages/spec_templates/bin/generate.dart +++ b/packages/spec_templates/bin/generate.dart @@ -55,43 +55,8 @@ Future main(final List args) async { 'routes.php', ); } - final routesPhpLines = File(routesPhpPath).readAsStringSync().split('\n'); - - final reg = RegExp('^(\t| )*\\/\\/', multiLine: true); - final routesPhpLinesResult = []; - for (var i = 0; i < routesPhpLines.length; i++) { - final line = routesPhpLines[i]; - if (!reg.hasMatch(line)) { - routesPhpLinesResult.add(line); - } - } - - var routesPhp = routesPhpLinesResult.join('\n'); - if (routesPhp.contains('registerRoutes')) { - routesPhp = RegExp(r'registerRoutes\(\$this, (\[[^;]*)\);').firstMatch(routesPhp)!.group(1)!; - } else if (routesPhp.contains('return [')) { - routesPhp = RegExp(r'return (\[[^;]*);').firstMatch(routesPhp)!.group(1)!; - } else { - throw Exception('Unsupported routes.php format'); - } - - final phpFile = File(p.join(tmpDirectory.path, '$id.php')); - final resultFile = File(p.join(tmpDirectory.path, '$id.json')); - phpFile.writeAsStringSync( - ''' - -''', - ); - final result = await Process.run('php', [phpFile.path]); - if (result.exitCode != 0) { - throw Exception('Failed to run php: ${result.stderr}'); - } - final routes = json.decode(resultFile.readAsStringSync()) as Map; + final routes = await _parseRoutesFile(tmpDirectory, routesPhpPath); final paths = {}; @@ -105,7 +70,7 @@ fclose(\$fp); final ocsBasePath = '/ocs/v1.php$routesBasePath'; for (final k in routes.keys) { - for (final Map route in routes[k]) { + for (final route in routes[k]!) { final name = route['name'] as String; var url = route['url'] as String; // ignore: avoid_dynamic_calls @@ -415,3 +380,73 @@ List _getMethodParameters( return result; } + +Future>>> _parseRoutesFile( + final Directory tmpDirectory, + final String path, +) async { + final content = File(path).readAsStringSync(); + + late String routes; + if (content.contains('registerRoutes')) { + routes = RegExp(r'registerRoutes\(\$this, (\[[^;]*)\);').firstMatch(content)!.group(1)!; + } else if (content.contains('return [')) { + routes = RegExp(r'return (\[[^;]*);').firstMatch(content)!.group(1)!; + } else if (content.contains('return array_merge_recursive(')) { + final includes = RegExp(r"include\(__DIR__ . '/([^']*)'\),") + .allMatches(RegExp(r'return array_merge_recursive\(\n(.*)\n\);', dotAll: true).firstMatch(content)!.group(1)!) + .map((final match) => match.group(1)!) + .toList(); + + final out = >>{}; + for (final include in includes) { + final routes = await _parseRoutesFile(tmpDirectory, p.join(File(path).parent.path, include)); + for (final key in routes.keys) { + if (!out.containsKey(key)) { + out[key] = []; + } + out[key]!.addAll(routes[key]!); + } + } + + return out; + } else { + throw Exception('Unsupported routes format'); + } + + final allowedVariables = [ + 'requirements', + 'requirementsWithToken', + 'requirementsWithMessageId', + ]; + final variables = RegExp('^(\\\$(${allowedVariables.join('|')}) =[^;]*;)\$', multiLine: true) + .allMatches(content) + .map((final match) => match.group(1)!) + .toList(); + + final phpFile = File(p.join(tmpDirectory.path, p.basename(path))); + final jsonFile = File(p.join(tmpDirectory.path, p.basename(path).replaceAll('.php', '.json'))); + + phpFile.writeAsStringSync( + ''' + +''', + ); + final result = await Process.run('php', [phpFile.path]); + if (result.exitCode != 0) { + throw Exception('Failed to run php: ${result.stderr}'); + } + + return (json.decode(jsonFile.readAsStringSync()) as Map).map( + (final key, final value) => MapEntry>>( + key, + (value as List).map((final a) => a as Map).toList(), + ), + ); +}