Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
use package_config to find sources during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-hegde committed Jul 17, 2022
1 parent 849901e commit 36fee46
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
51 changes: 43 additions & 8 deletions jni/bin/setup.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:package_config/package_config.dart';

const _buildDir = "build-dir";
const _srcDir = "source-dir";
Expand Down Expand Up @@ -53,6 +54,22 @@ void log(String msg) {
}
}

/// tries to find package:jni's source folder in pub cache
/// if not possible, returns null.
Future<String?> findSources() async {
final packageConfig = await findPackageConfig(Directory.current);
if (packageConfig == null) {
return null;
}
final packages = packageConfig.packages;
for (var package in packages) {
if (package.name == 'jni') {
return package.root.resolve("src/").toFilePath();
}
}
return null;
}

void main(List<String> arguments) async {
final parser = ArgParser()
..addOption(_buildDir,
Expand All @@ -79,13 +96,20 @@ void main(List<String> arguments) async {
return;
}

final scriptUri = Platform.script;
log("scriptUri: $scriptUri");
final srcPath = options.srcDir ?? scriptUri.resolve("../src").toFilePath();
final srcPath = options.srcDir ?? await findSources();

if (srcPath == null) {
stderr.writeln("No sources specified and current directory is not a "
"package root.");
exitCode = 1;
return;
}

final srcDir = Directory(srcPath);
if (!await srcDir.exists()) {
if (!await srcDir.exists() && !options.clean) {
throw 'Directory $srcPath does not exist';
}

log("srcPath: $srcPath");

final currentDirUri = Uri.file(".");
Expand All @@ -103,7 +127,9 @@ void main(List<String> arguments) async {
if (options.clean) {
await cleanup(options, srcDir.absolute.path, buildDir.absolute.path);
} else {
await build(options, srcDir.absolute.path, buildDir.absolute.path);
// pass srcDir absolute path because it will be passed to CMake as arg
// which will be running in different directory
await build(options, srcDir.absolute.path, buildDir.path);
}
}

Expand All @@ -121,8 +147,17 @@ Future<void> build(Options options, String srcPath, String buildPath) async {
}
}

Future<FileSystemEntity> cleanup(
Options options, String srcPath, String buildPath) async {
Future<void> cleanup(Options options, String srcPath, String buildPath) async {
if (srcPath == buildPath) {
stderr.writeln('Error: build path is same as source path.');
}

stderr.writeln("deleting $buildPath");
return Directory(buildPath).delete(recursive: true);

try {
await Directory(buildPath).delete(recursive: true);
} catch (e) {
stderr.writeln("Error: cannot be deleted");
stderr.writeln(e);
}
}
7 changes: 7 additions & 0 deletions jni/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
path:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion jni/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ dependencies:
plugin_platform_interface: ^2.0.2
ffi: ^2.0.0
path: ^1.8.0
args:
package_config: ^2.1.0
args: ^2.3.1

dev_dependencies:
## Temporarily linking to a personal fork of ffigen
Expand Down

0 comments on commit 36fee46

Please sign in to comment.