forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_rewriter.dart
83 lines (76 loc) · 2.55 KB
/
sdk_rewriter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
final ArgParser argParser = ArgParser()
..addOption('output-dir')
..addOption('input-dir')
..addFlag('ui', defaultsTo: false)
..addFlag('engine', defaultsTo: false)
..addMultiOption('input');
const List<List<String>> uiPatterns = <List<String>>[
<String>['library ui;', 'library dart.ui;'],
<String>['part of ui;', 'part of dart.ui;'],
<String>[
r'''
import 'src/engine.dart' as engine;
''',
r'''
import 'dart:_engine' as engine;
'''
],
<String>[
r'''
export 'src/engine.dart' show persistedPictureFactory, houdiniPictureFactory, webOnlyInitializeEngine;
''',
r'''
export 'dart:_engine' show persistedPictureFactory, houdiniPictureFactory, webOnlyInitializeEngine;
''',
]
];
const List<List<String>> enginePatterns = <List<String>>[
<String>['library engine;', 'library dart._engine;'],
<String>['part of engine;', 'part of dart._engine;'],
<String>[
r'''
import '../ui.dart' as ui;
''',
r'''
import 'dart:ui' as ui;
'''
]
];
const List<List<String>> sharedPatterns = <List<String>>[
<String>["import 'package:meta/meta.dart';" ,''],
<String>['@required', ''],
<String>['@protected', ''],
<String>['@mustCallSuper', ''],
<String>['@immutable', ''],
<String>['@visibleForTesting', '']
];
// Rewrites the "package"-style web ui library into a dart:ui implementation.
// So far this only requires a replace of the library declarations.
void main(List<String> arguments) {
final ArgResults results = argParser.parse(arguments);
final Directory directory = Directory(results['output-dir']);
final String inputDirectoryPath = results['input-dir'];
for (String inputFilePath in results['input']) {
final File inputFile = File(inputFilePath);
final File outputFile = File(path.join(directory.path, inputFile.path.substring(inputDirectoryPath.length)))
..createSync(recursive: true);
String source = inputFile.readAsStringSync();
final List<List<String>> replacementPatterns = <List<String>>[];
replacementPatterns.addAll(sharedPatterns);
if (results['ui']) {
replacementPatterns.addAll(uiPatterns);
} else if (results['engine']) {
replacementPatterns.addAll(enginePatterns);
}
for (List<String> patterns in replacementPatterns) {
source = source.replaceAll(patterns.first, patterns.last);
}
outputFile.writeAsStringSync(source);
}
}