Skip to content

Start REPL if xp cmd is invoked without a class #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
if: "!contains(github.event.head_commit.message, 'skip ci')"
name: PHP ${{ matrix.php-versions }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.php-versions == '8.1' }}
continue-on-error: ${{ matrix.php-versions >= '8.4' }}
strategy:
fail-fast: false
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
os: [ubuntu-latest, windows-latest]

steps:
Expand All @@ -24,17 +24,14 @@ jobs:
run: git config --system core.autocrlf false; git config --system core.eol lf

- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Set up PHP ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: date.timezone=Europe/Berlin

- name: Setup Problem Matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Validate composer.json and composer.lock
run: composer validate

Expand All @@ -43,15 +40,15 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2.1.3
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: >
curl -sSL https://baltocdn.com/xp-framework/xp-runners/distribution/downloads/e/entrypoint/xp-run-8.5.3.sh > xp-run &&
curl -sSL https://baltocdn.com/xp-framework/xp-runners/distribution/downloads/e/entrypoint/xp-run-8.8.0.sh > xp-run &&
composer install --prefer-dist &&
echo "vendor/autoload.php" > composer.pth

Expand Down
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Commands ChangeLog

## ?.?.? / ????-??-??

* Added PHP 8.2, 8.3 and 8.4 to the test matrix - @thekid

## 11.0.0 / 2021-10-21

* Implemented xp-framework/rfc#341, dropping compatibility with XP 9
Expand Down
14 changes: 13 additions & 1 deletion src/main/php/util/cmd/ParamString.class.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace util\cmd;

use lang\CommandLine;

/**
* This class provides helpful functions for commandline applications
Expand All @@ -17,12 +19,22 @@ class ParamString {
/**
* Constructor
*
* @param array list default NULL the argv array. If omitted, $_SERVER['argv'] is used
* @param ?string[] $list If omitted, $_SERVER['argv'] is used
*/
public function __construct($list= null) {
$this->setParams(null === $list ? $_SERVER['argv'] : $list);
}

/**
* Parses a command line using UN*X-style quoting
*
* @param ?string $line
* @return self
*/
public static function parse($line) {
return new self('' === ($line ?? '') ? [] : CommandLine::$UNIX->parse($line));
}

/**
* Set the parameter string
*
Expand Down
78 changes: 64 additions & 14 deletions src/main/php/xp/command/CmdRunner.class.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace xp\command;

use lang\reflect\{Modifiers, Package, TargetInvocationException};
use lang\{ClassLoader, ClassNotFoundException, System, Throwable, XPClass};
use lang\{ClassLoader, ClassNotFoundException, Throwable, XPClass};
use rdbms\ConnectionManager;
use util\cmd\{Commands, Config, ParamString};
use util\cmd\{Command, Commands, Config, ParamString};
use util\{Properties, PropertyAccess, PropertyManager};
use xp\runtime\Help;

Expand Down Expand Up @@ -53,8 +53,9 @@ protected function commandUsage(XPClass $class) {
$markdown= '# '.$class->getSimpleName()."\n\n";
$text= '';
} else {
@list($headline, $text)= explode("\n", $comment, 2);
$markdown= '# '.ltrim($headline, ' #')."\n\n";
$p= strcspn($comment, ".\n");
$markdown= '# '.ltrim(substr($comment, 0, $p), ' #')."\n\n";
$text= substr($comment, $p);
}

$markdown.= "- Usage\n ```sh\n$ xp cmd ".Commands::nameOf($class);
Expand Down Expand Up @@ -122,7 +123,7 @@ protected function listCommands() {
$commandsIn= function($package) {
$markdown= '';
foreach ($package->getClasses() as $class) {
if ($class->isSubclassOf('util.cmd.Command') && !Modifiers::isAbstract($class->getModifiers())) {
if ($class->isSubclassOf(Command::class) && !Modifiers::isAbstract($class->getModifiers())) {
$markdown.= ' $ xp cmd '.$class->getSimpleName()."\n";
}
}
Expand All @@ -143,6 +144,61 @@ protected function listCommands() {
Help::render(self::$err, $markdown, []);
}

/**
* Starts repl
*
* @param util.cmd.Config $config
* @return void
*/
protected function startRepl($config) {
foreach (Commands::allPackages() as $package) {
self::$out->writeLine("\e[33m@", $package, "\e[0m");
}
self::$out->writeLine('XP Command REPL. Use "ls" to list commands, "exit" to exit');
self::$out->writeLine("\e[36m", str_repeat('═', 72), "\e[0m");
foreach ($config->sources() as $source) {
self::$out->writeLine('Config: ', $source);
}

$prompt= (getenv('USERNAME') ?: getenv('USER') ?: posix_getpwuid(posix_geteuid())['name']).'@'.gethostname();
$exit= 0;
do {
self::$out->write("\n\e[", 0 === $exit ? '44' : '41', ";1;37m", $prompt, " cmd \$\e[0m ");

$command= $args= null;
sscanf(self::$in->readLine(), "%[^ ] %[^\r]", $command, $args);
switch ($command) {
case 'exit': return;
case null: $exit= 0; break;
case 'ls':
foreach (array_merge(Commands::allPackages(), [Package::forName(null)]) as $package) {
foreach ($package->getClasses() as $class) {
if ($class->isSubclassOf(Command::class) && !Modifiers::isAbstract($class->getModifiers())) {
self::$out->write("* \e[37m", $class->getSimpleName(), "\e[0m");
if ($comment= $class->getComment()) {
self::$out->writeLine(" - \e[3m", substr($comment, 0, strcspn($comment, ".\n")), "\e[0m");
} else {
self::$out->writeLine();
}
}
}
}
$exit= 0;
break;

// Treat any other input as a command name
default:
try {
$exit= $this->runCommand($command, ParamString::parse($args), $config);
} catch (\Throwable $e) {
self::$err->writeLine(Throwable::wrap($e));
$exit= 255;
}
break;
}
} while (true);
}

/**
* Main method
*
Expand All @@ -152,12 +208,6 @@ protected function listCommands() {
*/
public function run(ParamString $params, Config $config= null) {

// No arguments given - show our own usage
if ($params->count < 1) {
$this->selfUsage();
return 1;
}

// Configure properties
$config || $config= new Config();

Expand All @@ -181,10 +231,10 @@ public function run(ParamString $params, Config $config= null) {
break 2;
}

// Sanity check
// Without class: Start REPL
if (!$params->exists($offset)) {
self::$err->writeLine('*** Missing classname');
return 1;
$this->startRepl($config);
return 0;
}

// Use default path for config if no sources set
Expand Down
13 changes: 10 additions & 3 deletions src/test/php/util/cmd/unittest/CmdRunnerTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class CmdRunnerTest extends AbstractRunnerTest {
/** @return xp.command.AbstractRunner */
protected function runner() { return new CmdRunner(); }

#[Test, Values([[[]], [['-?']]])]
public function selfUsage($args) {
$return= $this->runWith($args);
#[Test]
public function selfUsage() {
$return= $this->runWith(['-?']);
$this->assertEquals(1, $return);
$this->assertOnStream($this->err, 'xp cmd [class]');
$this->assertEquals('', $this->out->bytes());
Expand All @@ -35,4 +35,11 @@ public function longClassUsage() {
$this->assertEquals('', $this->out->bytes());
$this->assertFalse($command->wasRun());
}

#[Test]
public function startRepl() {
$return= $this->runWith([], 'exit');
$this->assertEquals(0, $return);
$this->assertOnStream($this->out, 'XP Command REPL');
}
}