Browse Source

The `Finder` does not accept anymore strings for `extensions` and `path` arguments

tags/v8.0.0
Cédric Belin 2 months ago
parent
commit
d9587945b2
4 changed files with 27 additions and 32 deletions
  1. +1
    -1
      bin/where.dart
  2. +17
    -22
      lib/src/io/finder.dart
  3. +3
    -3
      lib/src/io/where.dart
  4. +6
    -6
      test/where_test.dart

+ 1
- 1
bin/where.dart View File

@@ -15,7 +15,7 @@ Future<void> main(List<String> args) async {
options = parseOptions(args);
if (options.help) return print(usage);
if (options.version) return print(packageVersion);
if (options.rest.isEmpty) throw const FormatException("A command must be provided.");
if (options.rest.isEmpty) throw const FormatException("You must provide the name of a command to find.");
}

on FormatException {


+ 17
- 22
lib/src/io/finder.dart View File

@@ -4,42 +4,38 @@ part of "../io.dart";
class Finder {

/// Creates a new finder from the following parameters:
/// - [extensions]: A string, or a list of strings, specifying the executable file extensions. Defaults to the `PATHEXT` environment variable.
/// - [path]: A string, or a list of strings, specifying the system path. Defaults to the `PATH` environment variable.
/// - [path]: A list of strings specifying the system path. Defaults to the `PATH` environment variable.
/// - [extensions]: A list of strings specifying the executable file extensions. Defaults to the `PATHEXT` environment variable.
Finder({List<String> extensions, List<String> path}) {
final pathSeparator = isWindows ? ";" : ":";

if (path is! List) path = path.toString().split(pathSeparator)..retainWhere((item) => item.isNotEmpty);
if (path.isEmpty) {
final pathEnv = io.Platform.environment["PATH"] ?? "";
if (pathEnv.isNotEmpty) path = pathEnv.split(pathSeparator);
}

if (extensions is! List) extensions = extensions.toString().split(pathSeparator)..retainWhere((item) => item.isNotEmpty);
if (extensions.isEmpty && isWindows) {
if (extensions == null) {
final pathExt = io.Platform.environment["PATHEXT"] ?? "";
extensions = pathExt.isNotEmpty ? pathExt.split(pathSeparator) : [".exe", ".cmd", ".bat", ".com"];
}

this.extensions.addAll(List<String>.from(extensions.map((extension) => extension.toLowerCase())));
this.path.addAll(List<String>.from(path.map((directory) => directory.replaceAll(RegExp(r'^"+|"+$'), ""))));
if (path == null) {
final pathEnv = io.Platform.environment["PATH"] ?? "";
if (pathEnv.isNotEmpty) path = pathEnv.split(pathSeparator);
}

this.extensions.addAll(extensions.map((extension) => extension.toLowerCase()));
this.path.addAll(path);
}

/// The list of executable file extensions.
final List<String> extensions = <String>[];
final List<String> extensions = [];

/// Value indicating whether the current platform is Windows.
static bool get isWindows {
if (io.Platform.isWindows) return true;
return io.Platform.environment["OSTYPE"] == "cygwin" || io.Platform.environment["OSTYPE"] == "msys";
}
static bool get isWindows =>
io.Platform.isWindows || io.Platform.environment["OSTYPE"] == "cygwin" || io.Platform.environment["OSTYPE"] == "msys";

/// The list of system paths.
final List<String> path = <String>[];
final List<String> path = [];

/// Finds the instances of the specified [command] in the system path.
Stream<io.File> find(String command) async* {
for (final directory in path) yield* _findExecutables(directory, command);
for (final directory in [if (isWindows) io.Directory.current.path, ...path]) yield* _findExecutables(directory, command);
}

/// Gets a value indicating whether the specified [file] is executable.
@@ -50,8 +46,7 @@ class Finder {
}

/// Checks that the specified [file] is executable according to the executable file extensions.
bool _checkFileExtension(String file) =>
extensions.contains(p.extension(file).toLowerCase()) || extensions.contains(file.toLowerCase());
bool _checkFileExtension(String file) => extensions.contains(p.extension(file).toLowerCase());

/// Checks that the file referenced by the specified [fileStats] is executable according to its permissions.
Future<bool> _checkFilePermissions(FileStat fileStats) async {
@@ -74,7 +69,7 @@ class Finder {

/// Finds the instances of a [command] in the specified [directory].
Stream<io.File> _findExecutables(String directory, String command) async* {
for (final extension in ["", ...extensions]) {
for (final extension in ["", if (isWindows) ...extensions]) {
final resolvedPath = p.canonicalize("${p.join(directory, command)}${extension.toLowerCase()}");
if (await isExecutable(resolvedPath)) yield io.File(resolvedPath);
}


+ 3
- 3
lib/src/io/where.dart View File

@@ -7,9 +7,9 @@ part of "../io.dart";
/// If a [onError] handler is provided, it is called with the [command] as argument, and its return value is used instead.
///
/// Optional parameters allow to customize the function behavior:
/// - [path]: A string, or a list of strings, specifying the system path. Defaults to the `PATH` environment variable.
/// - [extensions]: A string, or a list of strings, specifying the executable file extensions. Defaults to the `PATHEXT` environment variable.
Future where(String command, {bool all = false, extensions = "", Function(String command) onError, path = ""}) async {
/// - [path]: A list of strings specifying the system path. Defaults to the `PATH` environment variable.
/// - [extensions]: A list of strings specifying the executable file extensions. Defaults to the `PATHEXT` environment variable.
Future where(String command, {bool all = false, List<String> extensions, Function(String command) onError, List<String> path}) async {
final finder = Finder(extensions: extensions, path: path);
final list = <String>[];



+ 6
- 6
test/where_test.dart View File

@@ -6,7 +6,7 @@ import "package:where/where.dart";
void main() => group("where()", () {
test("should return the path of the `executable.cmd` file on Windows", () async {
try {
final executable = await where("executable", all: false, path: "test/fixtures");
final executable = await where("executable", all: false, path: ["test/fixtures"]);
if (!Platform.isWindows) fail("Exception not thrown");
else expect(executable, allOf(const TypeMatcher<String>(), endsWith(r"\test\fixtures\executable.cmd")));
}
@@ -19,7 +19,7 @@ void main() => group("where()", () {

test("should return all the paths of the `executable.cmd` file on Windows", () async {
try {
final executables = await where("executable", all: true, path: "test/fixtures");
final executables = await where("executable", all: true, path: ["test/fixtures"]);
if (!Platform.isWindows) fail("Exception not thrown");
else {
expect(executables, allOf(isList, hasLength(1)));
@@ -35,7 +35,7 @@ void main() => group("where()", () {

test("should return the path of the `executable.sh` file on POSIX", () async {
try {
final executable = await where("executable.sh", all: false, path: "test/fixtures");
final executable = await where("executable.sh", all: false, path: ["test/fixtures"]);
if (Platform.isWindows) fail("Exception not thrown");
else expect(executable, allOf(const TypeMatcher<String>(), endsWith("/test/fixtures/executable.sh")));
}
@@ -48,7 +48,7 @@ void main() => group("where()", () {

test("should return all the paths of the `executable.sh` file on POSIX", () async {
try {
final executables = await where("executable.sh", all: true, path: "test/fixtures");
final executables = await where("executable.sh", all: true, path: ["test/fixtures"]);
if (Platform.isWindows) fail("Exception not thrown");
else {
expect(executables, allOf(isList, hasLength(1)));
@@ -63,10 +63,10 @@ void main() => group("where()", () {
});

test("should return the value of the `onError` handler", () async {
final executable = await where("executable", all: false, onError: (_) => "foo", path: "test/fixtures");
final executable = await where("executable", all: false, onError: (_) => "foo", path: ["test/fixtures"]);
if (!Platform.isWindows) expect(executable, "foo");

final executables = await where("executable.sh", all: true, onError: (_) => ["foo"], path: "test/fixtures");
final executables = await where("executable.sh", all: true, onError: (_) => ["foo"], path: ["test/fixtures"]);
if (Platform.isWindows) {
expect(executables, allOf(isList, hasLength(1)));
expect(executables.first, "foo");


Loading…
Cancel
Save