-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_lib.dart
98 lines (86 loc) · 3.32 KB
/
build_lib.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart';
const pkgName = 'rpi_serial';
const buildScriptVersion = 2;
main(List<String> args) {
// Locate the Dart SDK
File dartVm = new File(Platform.executable);
print('Dart VM... ${dartVm.path}');
if (!dartVm.isAbsolute) {
dartVm = new File.fromUri(Directory.current.uri.resolve(dartVm.path));
print('Dart VM... ${dartVm.path}');
}
abortIf(!dartVm.isAbsolute, 'Failed to find absolute path to Dart VM');
// Locate Dart SDK
final dartSdk = dartVm.parent.parent;
print('Dart SDK... ${dartSdk.path}');
// Locate dart_api.h
final headerPath = 'include/dart_api.h';
final headerFile = new File.fromUri(dartSdk.uri.resolve(headerPath));
abortIf(!headerFile.existsSync(), 'Failed to find $headerPath');
// Run pub list to determine the location of the GPIO package being used
final pub = new File.fromUri(dartSdk.uri.resolve('bin/pub'));
String pubOut = Process.runSync(pub.path, ['list-package-dirs']).stdout;
Map<String, dynamic> pubResult = jsonDecode(pubOut);
assertNoPubListError(pubResult);
String dirName = pubResult['packages'][pkgName];
final pkgDir = new Directory(dirName);
print('Building library in ${pkgDir.path}');
// Display the version of the rpi_gpio being built
final pubspecFile = new File(join(pkgDir.path, '..', 'pubspec.yaml'));
abortIf(!pubspecFile.existsSync(), 'Failed to find ${pubspecFile.path}');
final pubspec = pubspecFile.readAsStringSync();
print('$pkgName version ${parseVersion(pubspec)}');
// Build the native library
final nativeDir = new Directory(join(pkgDir.path, 'src', 'native'));
final buildScriptFile = new File(join(nativeDir.path, 'build_lib'));
assertRunningOnRaspberryPi();
final buildResult = Process.runSync(
buildScriptFile.path, [buildScriptVersion.toString(), dartSdk.path]);
print(buildResult.stdout);
print(buildResult.stderr);
if (buildResult.exitCode != 0) exit(buildResult.exitCode);
}
/// Parse the given content and return the version string
String parseVersion(String pubspec) {
var key = 'version:';
int start = pubspec.indexOf(key) + key.length;
int end = pubspec.indexOf('\n', start);
return pubspec.substring(start, end).trim();
}
/// Abort if the specified condition is true.
void abortIf(bool condition, String message) {
if (condition) {
print(message);
throw 'Aborting build';
}
}
/// Assert that the given pub list result does not indicate an error
void assertNoPubListError(Map<String, dynamic> pubResult) {
var error = pubResult['error'];
if (error == null) {
Map<String, dynamic> packages = pubResult['packages'];
if (packages != null) {
var rpiGpio = packages[pkgName];
if (rpiGpio != null) {
return;
}
print('Cannot find $pkgName in pub list result');
print('Must run this script on app referencing $pkgName package');
throw 'Aborting build';
}
print('Cannot find packages in pub list result');
throw 'Aborting build';
}
print(error);
print('Must run this script from directory containing pubspec.yaml file');
throw 'Aborting build';
}
/// Assert that this script is executing on the Raspberry Pi.
assertRunningOnRaspberryPi() {
if (!new Directory('/home/pi').existsSync()) {
print('Not running on Raspberry Pi... skipping build');
throw 'Aborting build';
}
}