Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
Use experimental HSL IO abstraction instead of homegrown one
Browse files Browse the repository at this point in the history
While it is experimental, it is derived from the old hh-clilib one, and
has had more thought and review, so seems to be a good upgrade
regardless.
  • Loading branch information
fredemmott committed Nov 27, 2018
1 parent 144cbae commit 898acc4
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 299 deletions.
2 changes: 2 additions & 0 deletions .hhconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ safe_array = true
safe_vector_array = true
disallow_destruct = true
user_attributes=
# circular dependency with HHAST
ignored_paths=["vendor\/hhvm\/hhast\/.+"]
50 changes: 32 additions & 18 deletions src/CLIBase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Facebook\CLILib;

use namespace Facebook\TypeAssert;
use namespace HH\Lib\{C, Dict, Str, Vec};
use namespace HH\Lib\Experimental\IO;

/**
* This class contains all of the core functionality for using the Hack CLI
Expand Down Expand Up @@ -132,54 +133,67 @@ abstract class CLIBase implements ITerminal {
return $this->terminal->isInteractive();
}

final public function getStdin(): InputInterface {
final public function getStdin(): IO\ReadHandle {
return $this->terminal->getStdin();
}

final public function getStdout(): OutputInterface {
final public function getStdout(): IO\WriteHandle {
return $this->terminal->getStdout();
}

final public function getStderr(): OutputInterface {
final public function getStderr(): IO\WriteHandle {
return $this->terminal->getStderr();
}

<<__Deprecated('use runAsync instead')>>
final public static function main(): noreturn {
$result = \HH\Asio\join(static::runAsync());
exit($result);
}

/**
* This is usually the main entry point to create an instance of a Hack CLI.
* By default, the output is to stdout and errors are written to stderr. But
* those can be overriden by the actual concrete instance of this abstract
* class.
*
* This function does not return anything. Instead `exit()` is called with an
* exit code for success and failure.
* This function returns the exit code, and is expected to be used
* from an `<<__EntryPoint>>` function, e.g.:
*
* ```
* <<__EntryPoint>>
* async function myMainAsync(): Awaitable<noreturn> {
* $code = await MyCLI::runAsync();
* exit($code);
* }
* ```
*
* @see CLIBase::mainAsync
*/
final public static function main(): noreturn {
$in = new FileHandleInput(\STDIN);
$out = new FileHandleOutput(\STDOUT);
$err = new FileHandleOutput(\STDERR);
final public static async function runAsync(): Awaitable<int> {
$in = IO\stdin();
$out = IO\stdout();
$err = IO\stderr();
try {
$responder = new static(
vec(/* HH_IGNORE_ERROR[2050] */ $GLOBALS['argv']),
new Terminal($in, $out, $err),
);
$exit_code = \HH\Asio\join($responder->mainAsync());
exit($exit_code);
return await $responder->mainAsync();
} catch (ExitException $e) {
$code = $e->getCode();
$message = $e->getUserMessage();
if ($message !== null) {
if ($code === 0) {
\HH\Asio\join($out->writeAsync($message."\n"));
await $out->writeAsync($message."\n");
} else {
\HH\Asio\join($err->writeAsync($message."\n"));
await $err->writeAsync($message."\n");
}
}
exit($code);
return $code;
} catch (CLIException $e) {
\HH\Asio\join($err->writeAsync($e->getMessage()."\n"));
exit(1);
await $err->writeAsync($e->getMessage()."\n");
return 1;
}
}

Expand Down Expand Up @@ -328,7 +342,7 @@ abstract class CLIBase implements ITerminal {
* can be overridden - but generally not necessary.
*/
public async function displayHelpAsync(
OutputInterface $out,
IO\WriteHandle $out,
): Awaitable<void> {
$usage = 'Usage: '.C\firstx($this->argv);

Expand Down Expand Up @@ -389,7 +403,7 @@ abstract class CLIBase implements ITerminal {
/**
* DEPRECATED: use `displayHelpAsync` instead.
*/
final public function displayHelp(OutputInterface $out): void {
final public function displayHelp(IO\WriteHandle $out): void {
\HH\Asio\join($this->displayHelpAsync($out));
}
}
100 changes: 0 additions & 100 deletions src/FileHandleInput.hh

This file was deleted.

66 changes: 0 additions & 66 deletions src/FileHandleOutput.hh

This file was deleted.

7 changes: 4 additions & 3 deletions src/ITerminal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

namespace Facebook\CLILib;
use namespace HH\Lib\Experimental\IO;

/** Interface encapsulating the terminal.
*
Expand Down Expand Up @@ -38,20 +39,20 @@ interface ITerminal {
*
* By default, this is the process standard input, or file descriptor 0.
*/
public function getStdin(): InputInterface;
public function getStdin(): IO\ReadHandle;

/**
* Gets the standard process output for the current CLI.
*
* By default, this is the process standard output, or file descriptor 1.
*/
public function getStdout(): OutputInterface;
public function getStdout(): IO\WriteHandle;

/**
* Gets the standard error output for the current CLI.
*
* By default, this is the process standard error, or file descriptor 2.
*/
public function getStderr(): OutputInterface;
public function getStderr(): IO\WriteHandle;

}
40 changes: 0 additions & 40 deletions src/InputInterface.hh

This file was deleted.

Loading

0 comments on commit 898acc4

Please sign in to comment.