Skip to content

Commit 74f82ae

Browse files
committed
Fix generation with malformed quotes
1 parent dc3d021 commit 74f82ae

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎generate.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ cd vrchat_dart_generated
2222
dart pub get
2323
cd ..
2424

25+
# openapi-generator can emit invalid Dart like r'you\'re' (escapes in raw
26+
# strings). Fix that before formatting, which fails on unparseable files.
27+
cd vrchat_dart
28+
dart run tool/fix_raw_string_escapes.dart
29+
cd ..
30+
2531
# Format before patching since extra line breaks can break the patches
2632
dart format .
2733
cd vrchat_dart
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:io';
2+
3+
import 'package:path/path.dart' as path;
4+
5+
/// openapi-generator's dart-dio templates emit raw strings with `\'`, which is
6+
/// invalid Dart. Rewrite those as double-quoted raw strings before formatting.
7+
void main() {
8+
final root = Directory(path.join('..', 'vrchat_dart_generated'));
9+
for (final entity in root.listSync(recursive: true)) {
10+
if (entity is! File || !entity.path.endsWith('.dart')) continue;
11+
12+
final content = entity.readAsStringSync();
13+
final fixed = fixRawStringEscapes(content);
14+
if (fixed != content) {
15+
entity.writeAsStringSync(fixed);
16+
}
17+
}
18+
}
19+
20+
String fixRawStringEscapes(String source) {
21+
return source.replaceAllMapped(RegExp(r"r'((?:[^'\\]|\\.)*)'"), (match) {
22+
final inner = match[1]!;
23+
if (!inner.contains(r"\'")) return match[0]!;
24+
25+
final unescaped = inner.replaceAll(r"\'", "'");
26+
if (!unescaped.contains('"')) {
27+
return 'r"$unescaped"';
28+
}
29+
30+
final escaped = unescaped
31+
.replaceAll(r'\', r'\\')
32+
.replaceAll('"', r'\"')
33+
.replaceAll(r'$', r'\$');
34+
return '"$escaped"';
35+
});
36+
}

0 commit comments

Comments
 (0)