-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_native.dart
64 lines (56 loc) · 2.07 KB
/
build_native.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
import 'dart:io';
import 'package:path/path.dart';
import 'package:rpi_i2c/rpi_i2c.dart' show findPkgRootDir;
const pkgName = 'rpi_i2c';
const buildScriptVersion = 2;
void main(List<String> args) {
var pkgRootDir = findPkgRootDir(Directory.current);
print('Building library in ${pkgRootDir.path}');
// Display the version of the rpi_gpio being built
final pubspecFile = File(join(pkgRootDir.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 = Directory(join(pkgRootDir.path, 'lib', 'src', 'native'));
final buildScriptFile = File(join(nativeDir.path, 'build_native'));
assertRunningOnRaspberryPi();
final buildResult = Process.runSync(buildScriptFile.path, []);
print(buildResult.stdout);
if (buildResult.exitCode != 0) {
print(buildResult.stderr);
print('Build failed: ${buildResult.exitCode}');
exit(buildResult.exitCode);
}
print('Build suceeded');
}
/// Parse the given content and return the version string
String parseVersion(String pubspec) {
var key = 'version:';
var start = pubspec.indexOf(key) + key.length;
var 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 this script is executing on the Raspberry Pi.
void assertRunningOnRaspberryPi() {
// Check for Windows 11 running on RPi
if (Platform.isWindows) {
// TODO detect typical install on RPi
print('Running Windows... hopefully on the Raspberry Pi...');
return;
}
if (Platform.isMacOS || Platform.isIOS) {
print('Not running on Raspberry Pi... skipping build');
throw 'Aborting build';
}
// Check for typical Raspbian install
if (Directory('/home/pi').existsSync()) return;
print('Typical setup not found... hopefully running on the Raspberry Pi...');
}