5
5
import 'dart:io' ;
6
6
7
7
import 'package:args/args.dart' ;
8
- import 'package:package_config/package_config.dart' ;
9
-
10
8
import 'package:jni/src/build_util/build_util.dart' ;
9
+ import 'package:package_config/package_config.dart' ;
11
10
12
11
const jniNativeBuildDirective =
13
12
'# jni_native_build (Build with jni:setup. Do not delete this line.)' ;
14
13
15
14
// When changing this constant here, also change corresponding path in
16
15
// test/test_util.
17
- const _defaultRelativeBuildPath = " build/jni_libs" ;
16
+ const _defaultRelativeBuildPath = ' build/jni_libs' ;
18
17
19
- const _buildPath = " build-path" ;
20
- const _srcPath = " add-source" ;
18
+ const _buildPath = ' build-path' ;
19
+ const _srcPath = ' add-source' ;
21
20
const _packageName = 'add-package' ;
22
- const _verbose = " verbose" ;
23
- const _cmakeArgs = " cmake-args" ;
21
+ const _verbose = ' verbose' ;
22
+ const _cmakeArgs = ' cmake-args' ;
24
23
25
24
Future <void > runCommand (
26
25
String exec, List <String > args, String workingDir) async {
@@ -30,11 +29,11 @@ Future<void> runCommand(
30
29
current += Platform .pathSeparator;
31
30
}
32
31
if (workingDir.startsWith (current)) {
33
- workingDir.replaceFirst (current, "" );
32
+ workingDir.replaceFirst (current, '' );
34
33
}
35
34
36
35
final cmd = "$exec ${args .join (" " )}" ;
37
- stderr.writeln (" + [$workingDir ] $cmd " );
36
+ stderr.writeln (' + [$workingDir ] $cmd ' );
38
37
int status;
39
38
if (options.verbose) {
40
39
final process = await Process .start (
@@ -58,8 +57,8 @@ Future<void> runCommand(
58
57
var out = process.stdout;
59
58
var err = process.stderr;
60
59
if (stdout.supportsAnsiEscapes) {
61
- out = " $ansiRed $out $ansiDefault " ;
62
- err = " $ansiRed $err $ansiDefault " ;
60
+ out = ' $ansiRed $out $ansiDefault ' ;
61
+ err = ' $ansiRed $err $ansiDefault ' ;
63
62
}
64
63
stdout.writeln (out);
65
64
stderr.writeln (err);
@@ -72,11 +71,11 @@ Future<void> runCommand(
72
71
73
72
class Options {
74
73
Options (ArgResults arg)
75
- : buildPath = arg[_buildPath],
76
- sources = arg[_srcPath],
77
- packages = arg[_packageName],
78
- cmakeArgs = arg[_cmakeArgs],
79
- verbose = arg[_verbose] ?? false ;
74
+ : buildPath = arg[_buildPath] as String ? ,
75
+ sources = arg[_srcPath] as List < String > ,
76
+ packages = arg[_packageName] as List < String > ,
77
+ cmakeArgs = arg[_cmakeArgs] as List < String > ,
78
+ verbose = ( arg[_verbose] as bool ? ) ?? false ;
80
79
81
80
String ? buildPath;
82
81
List <String > sources;
@@ -100,11 +99,11 @@ void verboseLog(String msg) {
100
99
Future <String > findSources (String packageName, String subDirectory) async {
101
100
final packageConfig = await findPackageConfig (Directory .current);
102
101
if (packageConfig == null ) {
103
- throw UnsupportedError (" Please run from project root." );
102
+ throw UnsupportedError (' Please run from project root.' );
104
103
}
105
104
final package = packageConfig[packageName];
106
105
if (package == null ) {
107
- throw UnsupportedError (" Cannot find package: $packageName " );
106
+ throw UnsupportedError (' Cannot find package: $packageName ' );
108
107
}
109
108
return package.root.resolve (subDirectory).toFilePath ();
110
109
}
@@ -114,12 +113,12 @@ Future<String> findSources(String packageName, String subDirectory) async {
114
113
Future <Map <String , String >> findDependencySources () async {
115
114
final packageConfig = await findPackageConfig (Directory .current);
116
115
if (packageConfig == null ) {
117
- throw UnsupportedError (" Please run the command from project root." );
116
+ throw UnsupportedError (' Please run the command from project root.' );
118
117
}
119
118
final sources = < String , String > {};
120
119
for (var package in packageConfig.packages) {
121
- final src = package.root.resolve (" src/" );
122
- final cmakeLists = src.resolve (" CMakeLists.txt" );
120
+ final src = package.root.resolve (' src/' );
121
+ final cmakeLists = src.resolve (' CMakeLists.txt' );
123
122
final cmakeListsFile = File .fromUri (cmakeLists);
124
123
if (cmakeListsFile.existsSync ()) {
125
124
final firstLine = cmakeListsFile.readAsLinesSync ().first;
@@ -134,13 +133,13 @@ Future<Map<String, String>> findDependencySources() async {
134
133
/// Returns the name of file built using sources in [cDir]
135
134
String getTargetName (Directory cDir) {
136
135
for (final file in cDir.listSync (recursive: true )) {
137
- if (file.path.endsWith (".c" )) {
136
+ if (file.path.endsWith ('.c' )) {
138
137
final cFileName = file.uri.pathSegments.last;
139
- final librarySuffix = Platform .isWindows ? " dll" : "so" ;
138
+ final librarySuffix = Platform .isWindows ? ' dll' : 'so' ;
140
139
return cFileName.substring (0 , cFileName.length - 1 ) + librarySuffix;
141
140
}
142
141
}
143
- throw Exception (" Could not find a C file in ${cDir .path }" );
142
+ throw Exception (' Could not find a C file in ${cDir .path }' );
144
143
}
145
144
146
145
void main (List <String > arguments) async {
@@ -161,8 +160,8 @@ void main(List<String> arguments) async {
161
160
final rest = argResults.rest;
162
161
163
162
if (rest.isNotEmpty) {
164
- stderr.writeln (" one or more unrecognized arguments: $rest " );
165
- stderr.writeln (" usage: dart run jni:setup <options>" );
163
+ stderr.writeln (' one or more unrecognized arguments: $rest ' );
164
+ stderr.writeln (' usage: dart run jni:setup <options>' );
166
165
stderr.writeln (parser.usage);
167
166
exitCode = 1 ;
168
167
return ;
@@ -175,19 +174,19 @@ void main(List<String> arguments) async {
175
174
}
176
175
if (sources.isEmpty) {
177
176
final dependencySources = await findDependencySources ();
178
- stderr.writeln (" selecting source directories for dependencies: "
179
- " ${dependencySources .keys }" );
177
+ stderr.writeln (' selecting source directories for dependencies: '
178
+ ' ${dependencySources .keys }' );
180
179
sources.addAll (dependencySources.values);
181
180
} else {
182
- stderr.writeln (" selecting source directories: $sources " );
181
+ stderr.writeln (' selecting source directories: $sources ' );
183
182
}
184
183
if (sources.isEmpty) {
185
184
stderr.writeln ('No source paths to build!' );
186
185
exitCode = 1 ;
187
186
return ;
188
187
}
189
188
190
- final currentDirUri = Uri .directory ("." );
189
+ final currentDirUri = Uri .directory ('.' );
191
190
final buildPath = options.buildPath ??
192
191
currentDirUri.resolve (_defaultRelativeBuildPath).toFilePath ();
193
192
final buildDir = Directory (buildPath);
@@ -216,41 +215,41 @@ void main(List<String> arguments) async {
216
215
return ;
217
216
}
218
217
219
- verboseLog (" srcPath: $srcPath " );
220
- verboseLog (" buildPath: $buildPath " );
218
+ verboseLog (' srcPath: $srcPath ' );
219
+ verboseLog (' buildPath: $buildPath ' );
221
220
222
221
final targetFileUri = buildDir.uri.resolve (getTargetName (srcDir));
223
222
final targetFile = File .fromUri (targetFileUri);
224
223
if (! needsBuild (targetFile, srcDir)) {
225
- verboseLog (" Last modified of ${targetFile .path }: "
226
- " ${targetFile .lastModifiedSync ()}." );
224
+ verboseLog (' Last modified of ${targetFile .path }: '
225
+ ' ${targetFile .lastModifiedSync ()}.' );
227
226
stderr.writeln ('Target newer than source, skipping build.' );
228
227
continue ;
229
228
}
230
229
231
230
// Note: creating temp dir in .dart_tool/jni instead of SystemTemp
232
231
// because latter can fail tests on Windows CI, when system temp is on
233
232
// separate drive or something.
234
- final jniDirUri = Uri .directory (" .dart_tool" ).resolve (" jni" );
233
+ final jniDirUri = Uri .directory (' .dart_tool' ).resolve (' jni' );
235
234
final jniDir = Directory .fromUri (jniDirUri);
236
235
await jniDir.create (recursive: true );
237
- final tempDir = await jniDir.createTemp (" jni_native_build_" );
236
+ final tempDir = await jniDir.createTemp (' jni_native_build_' );
238
237
final cmakeArgs = < String > [];
239
238
cmakeArgs.addAll (options.cmakeArgs);
240
239
// Pass absolute path of srcDir because cmake command is run in temp dir
241
240
cmakeArgs.add (srcDir.absolute.path);
242
- await runCommand (" cmake" , cmakeArgs, tempDir.path);
243
- await runCommand (" cmake" , [" --build" , "." ], tempDir.path);
241
+ await runCommand (' cmake' , cmakeArgs, tempDir.path);
242
+ await runCommand (' cmake' , [' --build' , '.' ], tempDir.path);
244
243
final dllDirUri =
245
- Platform .isWindows ? tempDir.uri.resolve (" Debug" ) : tempDir.uri;
244
+ Platform .isWindows ? tempDir.uri.resolve (' Debug' ) : tempDir.uri;
246
245
final dllDir = Directory .fromUri (dllDirUri);
247
246
for (var entry in dllDir.listSync ()) {
248
247
verboseLog (entry.toString ());
249
248
final dllSuffix = Platform .isWindows
250
- ? " dll"
249
+ ? ' dll'
251
250
: Platform .isMacOS
252
- ? " dylib"
253
- : "so" ;
251
+ ? ' dylib'
252
+ : 'so' ;
254
253
if (entry.path.endsWith (dllSuffix)) {
255
254
final dllName = entry.uri.pathSegments.last;
256
255
final target = buildDir.uri.resolve (dllName);
0 commit comments