Browse Source

Renamed the `Tools` class to `FinderTools`

tags/v1.0.0
Cédric Belin 3 weeks ago
parent
commit
025b29cf4a
10 changed files with 52 additions and 41 deletions
  1. +1
    -1
      bin/which.js
  2. +1
    -1
      lib/php/which.php
  3. +33
    -24
      lib/php/which/Finder.php
  4. +2
    -2
      lib/php/which/FinderTools.php
  5. +1
    -1
      lib/php/which/Program.php
  6. +1
    -1
      share/build.hxml
  7. +1
    -1
      src/which/FinderTools.hx
  8. +1
    -1
      src/which/Program.hx
  9. +2
    -2
      test/which/FinderTest.hx
  10. +9
    -7
      test/which/FinderToolsTest.hx

+ 1
- 1
bin/which.js
File diff suppressed because it is too large
View File


+ 1
- 1
lib/php/which.php View File

@@ -10,6 +10,6 @@ namespace which;
*/
function which(string $command, array $options = null) {
$executable = null;
Tools::which($command, $options)->then(function($result) use (&$executable) { $executable = $result; });
FinderTools::which($command, $options)->then(function($result) use (&$executable) { $executable = $result; });
return $executable;
}

+ 33
- 24
lib/php/which/Finder.php View File

@@ -5,6 +5,7 @@

namespace which;

use \php\_Boot\HxDynamicStr;
use \thenshim\PromiseTools;
use \php\Boot;
use \php\_Boot\HxString;
@@ -17,6 +18,11 @@ use \haxe\io\Path;
* Finds the instances of an executable in the system path.
*/
class Finder {
/**
* @var bool
* Value indicating whether the current platform is Windows.
*/
static public $isWindows;

/**
* @var \Array_hx
@@ -28,11 +34,6 @@ class Finder {
* The list of system paths.
*/
public $path;
/**
* @var string
* The character used to separate paths in the system path.
*/
public $pathSeparator;

/**
* Gets a value indicating whether the current platform is Windows.
@@ -40,15 +41,17 @@ class Finder {
* @return bool
*/
public static function get_isWindows () {
if (\Sys::systemName() === "Windows") {
return true;
}
$osType = \Sys::getEnv("OSTYPE");
if ($osType !== "cygwin") {
return $osType === "msys";
} else {
return true;
if (Finder::$isWindows === null) {
$tmp = null;
if (\Sys::systemName() !== "Windows") {
$osType = \Sys::getEnv("OSTYPE");
$tmp = ($osType === "cygwin") || ($osType === "msys");
} else {
$tmp = true;
}
Finder::$isWindows = $tmp;
}
return Finder::$isWindows;
}

/**
@@ -59,10 +62,11 @@ class Finder {
* @return void
*/
public function __construct ($options = null) {
$this->pathSeparator = (Finder::get_isWindows() ? ";" : ":");
$separator = (Finder::get_isWindows() ? ";" : ":");
$pathExt = \Sys::getEnv("PATHEXT");
$tmp = null;
if (Finder::get_isWindows()) {
$_this = HxString::split(\Sys::getEnv("PATHEXT"), $this->pathSeparator);
if ($pathExt !== null) {
$_this = HxString::split($pathExt, $separator);
$result = [];
$data = $_this->arr;
$_g_current = 0;
@@ -72,20 +76,25 @@ class Finder {
}
$tmp = \Array_hx::wrap($result);
} else {
$tmp = new \Array_hx();
$tmp = \Array_hx::wrap([
".exe",
".cmd",
".bat",
".com",
]);
}
$this->extensions = $tmp;
$this->path = HxString::split(\Sys::getEnv("PATH"), $this->pathSeparator);
$pathEnv = \Sys::getEnv("PATH");
$this->path = ($pathEnv !== null ? HxString::split($pathEnv, $separator) : new \Array_hx());
if ($options !== null) {
if (isset($options["extensions"])) {
$this->extensions = $options["extensions"];
$this->extensions = $options["extensions"]->map(function ($item) {
return HxDynamicStr::wrap($item)->toLowerCase();
});
}
if (isset($options["path"])) {
$this->path = $options["path"];
}
if (isset($options["pathSeparator"])) {
$this->pathSeparator = $options["pathSeparator"];
}
}
}

@@ -149,7 +158,7 @@ class Finder {
*/
public function find ($command) {
$_gthis = $this;
$_this = $this->path;
$_this = Boot::deref(((Finder::get_isWindows() ? \Array_hx::wrap([\Sys::getCwd()]) : new \Array_hx())))->concat($this->path);
$result = [];
$data = $_this->arr;
$_g_current = 0;
@@ -185,7 +194,7 @@ class Finder {
\Sys::getCwd(),
$directory,
])));
$_this = (\Array_hx::wrap([""]))->concat($this->extensions);
$_this = (\Array_hx::wrap([""]))->concat((Finder::get_isWindows() ? $this->extensions : new \Array_hx()));
$result = [];
$data = $_this->arr;
$_g_current = 0;


lib/php/which/Tools.php → lib/php/which/FinderTools.php View File

@@ -12,7 +12,7 @@ use \thenshim\Thenable;
/**
* Provides static extensions.
*/
class Tools {
class FinderTools {
/**
* Finds the first instance of the specified `command` in the system path.
*
@@ -41,4 +41,4 @@ class Tools {
}
}

Boot::registerClass(Tools::class, 'which.Tools');
Boot::registerClass(FinderTools::class, 'which.FinderTools');

+ 1
- 1
lib/php/which/Program.php View File

@@ -87,7 +87,7 @@ class Program {
echo("You must provide the name of a command to find." . PHP_EOL);
exit(64);
}
return new SyncFuture(new LazyConst(Outcome::Success(PromiseTools::catch_(Promise_Impl_::then(Tools::which(($rest->arr[0] ?? null), ["all" => $this->all]), function ($executables) use (&$_gthis) {
return new SyncFuture(new LazyConst(Outcome::Success(PromiseTools::catch_(Promise_Impl_::then(FinderTools::which(($rest->arr[0] ?? null), ["all" => $this->all]), function ($executables) use (&$_gthis) {
if (!$_gthis->silent) {
if (is_string($executables)) {
$executables = \Array_hx::wrap([$executables]);


+ 1
- 1
share/build.hxml View File

@@ -5,6 +5,6 @@

which.Finder
which.FinderException
which.FinderTools
which.Process
which.ProcessIdentity
which.Tools

src/which/Tools.hx → src/which/FinderTools.hx View File

@@ -8,7 +8,7 @@ import php.NativeStructArray;
#end

/** Provides static extensions. **/
@:expose class Tools {
@:expose class FinderTools {

/** Finds the first instance of the specified `command` in the system path. **/
public static function which(command: String, ?options: #if php NativeStructArray<WhichOptions> #else WhichOptions #end): Promise<Dynamic> {

+ 1
- 1
src/which/Program.hx View File

@@ -6,7 +6,7 @@ import tink.core.Promise as TinkPromise;
import which.FinderException;

using thenshim.PromiseTools;
using which.Tools;
using which.FinderTools;

#if nodejs
import js.Node;


+ 2
- 2
test/which/FinderTest.hx View File

@@ -54,14 +54,14 @@ class FinderTest extends Test {

// It should return `false` for a POSIX executable when test is run on Windows, otherwise `true`.
async.branch(branch -> finder.isExecutable("test/fixtures/executable.sh")
.then(isExec -> Assert.equals(isExec, !isWindows))
.then(isExec -> Assert.equals(!isWindows, isExec))
.catchError(e -> Assert.fail(Std.string(e)))
.finally(() -> branch.done())
);

// It should return `false` for a Windows executable when test is run on POSIX, otherwise `true`.
async.branch(branch -> finder.isExecutable("test/fixtures/executable.cmd")
.then(isExec -> Assert.equals(isExec, isWindows))
.then(isExec -> Assert.equals(isWindows, isExec))
.catchError(e -> Assert.fail(Std.string(e)))
.finally(() -> branch.done())
);


test/which/ToolsTest.hx → test/which/FinderToolsTest.hx View File

@@ -4,7 +4,7 @@ using StringTools;
using thenshim.PromiseTools;

/** Tests the features of the `Tools` class. **/
class ToolsTest extends Test {
class FinderToolsTest extends Test {

/** Value indicating whether the current platform is Windows. **/
static final isWindows = Sys.systemName() == "Windows";
@@ -12,14 +12,14 @@ class ToolsTest extends Test {
/** Tests the `which()` method. **/
function testWhich(async: Async) {
// It should return the path of the `executable.cmd` file on Windows.
async.branch(branch -> Tools.which("executable", {all: false, path: ["test/fixtures"]})
async.branch(branch -> FinderTools.which("executable", {all: false, path: ["test/fixtures"]})
.then(executable -> isWindows ? Assert.isTrue(executable.endsWith("\\test\\fixtures\\executable.cmd")) : Assert.fail("Exception not thrown"))
.catchError(e -> isWindows ? Assert.fail(Std.string(e)) : Assert.isTrue(Std.isOfType(e, FinderException)))
.finally(() -> branch.done())
);

// It should return all paths of the `executable.cmd` file on Windows.
async.branch(branch -> Tools.which("executable", {all: true, path: ["test/fixtures"]})
async.branch(branch -> FinderTools.which("executable", {all: true, path: ["test/fixtures"]})
.then((executables: Array<String>) -> if (!isWindows) Assert.fail("Exception not thrown") else {
Assert.equals(1, executables.length);
Assert.isTrue(executables[0].endsWith("\\test\\fixtures\\executable.cmd"));
@@ -29,14 +29,14 @@ class ToolsTest extends Test {
);

// It should return the path of the `executable.sh` file on POSIX.
async.branch(branch -> Tools.which("executable.sh", {all: false, path: ["test/fixtures"]})
async.branch(branch -> FinderTools.which("executable.sh", {all: false, path: ["test/fixtures"]})
.then(executable -> isWindows ? Assert.fail("Exception not thrown") : Assert.isTrue(executable.endsWith("/test/fixtures/executable.sh")))
.catchError(e -> isWindows ? Assert.isTrue(Std.isOfType(e, FinderException)) : Assert.fail(Std.string(e)))
.finally(() -> branch.done())
);

// It should return all paths of the `executable.sh` file on POSIX.
async.branch(branch -> Tools.which("executable.sh", {all: true, path: ["test/fixtures"]})
async.branch(branch -> FinderTools.which("executable.sh", {all: true, path: ["test/fixtures"]})
.then((executables: Array<String>) -> if (isWindows) Assert.fail("Exception not thrown") else {
Assert.equals(1, executables.length);
Assert.isTrue(executables[0].endsWith("/test/fixtures/executable.sh"));
@@ -45,15 +45,17 @@ class ToolsTest extends Test {
.finally(() -> branch.done())
);

/*
// It should return the value of the `onError` handler.
async.branch(branch -> Tools.which("foo", {all: false, onError: _ -> "bar/baz.qux"}).then(executable -> {
async.branch(branch -> FinderTools.which("foo", {all: false, onError: _ -> "bar/baz.qux"}).then(executable -> {
Assert.equals("bar/baz.qux", executable);
branch.done();
}));

async.branch(branch -> Tools.which("foo", {all: true, onError: _ -> ["bar", "baz", "qux"]}).then(executables -> {
async.branch(branch -> FinderTools.which("foo", {all: true, onError: _ -> ["bar", "baz", "qux"]}).then(executables -> {
Assert.same(["bar", "baz", "qux"], executables);
branch.done();
}));
*/
}
}

Loading…
Cancel
Save