3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'package:dart_style/dart_style.dart' ;
6
+ import 'package:path/path.dart' as p;
6
7
import 'package:pub_semver/pub_semver.dart' ;
7
8
import 'package:yaml/yaml.dart' ;
8
9
10
+ class FunctionConfig {
11
+ /// The number of variable arguments
12
+ final int ? varArgs;
13
+
14
+ const FunctionConfig ({this .varArgs});
15
+ }
16
+
9
17
abstract interface class Config {
10
18
/// The name for the configuration
11
19
String ? get name;
@@ -28,13 +36,23 @@ abstract interface class Config {
28
36
/// The Dart Language Version to use
29
37
Version get languageVersion;
30
38
39
+ /// Function configuration
40
+ FunctionConfig ? get functions;
41
+
31
42
bool get singleFileOutput => input.length == 1 ;
32
43
33
- factory Config ({
34
- required List <String > input,
35
- required String output,
36
- required Version languageVersion,
37
- }) = ConfigImpl ._;
44
+ /// Include the following declarations when generating JS interop code
45
+ ///
46
+ /// This could be a plain name for a declaration, or a [RegExp] pattern
47
+ /// If empty, all declarations will be generated by default
48
+ List <String > get includedDeclarations;
49
+
50
+ factory Config (
51
+ {required List <String > input,
52
+ required String output,
53
+ required Version languageVersion,
54
+ FunctionConfig ? functions,
55
+ List <String > includedDeclarations}) = ConfigImpl ._;
38
56
}
39
57
40
58
class ConfigImpl implements Config {
@@ -59,14 +77,20 @@ class ConfigImpl implements Config {
59
77
@override
60
78
String ? preamble;
61
79
62
- ConfigImpl ._({
63
- required this .input,
64
- required this .output,
65
- required this .languageVersion,
66
- });
80
+ @override
81
+ FunctionConfig ? functions;
82
+
83
+ @override
84
+ List <String > includedDeclarations;
85
+
86
+ ConfigImpl ._(
87
+ {required this .input,
88
+ required this .output,
89
+ required this .languageVersion,
90
+ this .functions,
91
+ this .includedDeclarations = const []});
67
92
68
93
@override
69
- // TODO: implement singleFileOutput
70
94
bool get singleFileOutput => input.length == 1 ;
71
95
}
72
96
@@ -95,36 +119,58 @@ class YamlConfig implements Config {
95
119
@override
96
120
String ? preamble;
97
121
122
+ @override
123
+ FunctionConfig ? functions;
124
+
125
+ @override
126
+ List <String > includedDeclarations;
127
+
98
128
YamlConfig ._(
99
129
{required this .filename,
100
130
required this .input,
101
131
required this .output,
102
132
this .description,
103
133
this .name,
104
134
this .preamble,
135
+ this .functions,
136
+ this .includedDeclarations = const [],
105
137
String ? languageVersion})
106
138
: languageVersion = languageVersion == null
107
139
? DartFormatter .latestLanguageVersion
108
140
: Version .parse (languageVersion);
109
141
110
- factory YamlConfig .fromYaml (YamlMap yaml, {required String filename}) {
111
- List <String > input;
142
+ factory YamlConfig .fromYaml (YamlMap yaml,
143
+ {required String filename, List <String >? input, String ? output}) {
144
+ List <String > inputFiles;
112
145
final yamlInput = yaml['input' ];
113
146
if (yamlInput is YamlList ) {
114
- input = yamlInput.map ((y) => y is String ? y : y.toString ()).toList ();
147
+ inputFiles =
148
+ yamlInput.map ((y) => y is String ? y : y.toString ()).toList ();
115
149
} else if (yamlInput is String ) {
116
- input = [yamlInput];
150
+ inputFiles = [yamlInput];
151
+ } else if (input != null ) {
152
+ inputFiles = input;
117
153
} else {
118
154
throw TypeError ();
119
155
}
120
156
121
157
return YamlConfig ._(
122
158
filename: Uri .file (filename),
123
- input: input,
124
- output: yaml['output' ] as String ,
159
+ input: inputFiles
160
+ .map ((file) => p.join (p.dirname (filename), file))
161
+ .toList (),
162
+ output:
163
+ p.join (p.dirname (filename), (yaml['output' ] ?? output) as String ),
125
164
name: yaml['name' ] as String ? ,
126
165
description: yaml['description' ] as String ? ,
127
166
languageVersion: yaml['language_version' ] as String ? ,
128
- preamble: yaml['preamble' ] as String ? );
167
+ preamble: yaml['preamble' ] as String ? ,
168
+ // TODO: Can we consider using `json_serializable`?
169
+ functions: FunctionConfig (
170
+ varArgs: (yaml['functions' ] as YamlMap ? )? ['varargs' ] as int ? ),
171
+ includedDeclarations: (yaml['include' ] as YamlList ? )
172
+ ? .map <String >((node) => node.toString ())
173
+ .toList () ??
174
+ []);
129
175
}
130
176
}
0 commit comments