Skip to content

Commit 88e77bd

Browse files
committed
add an new class LineParser and action script
1 parent a61716a commit 88e77bd

File tree

7 files changed

+290
-3
lines changed

7 files changed

+290
-3
lines changed

.github/workflows/release.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Tag-release
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
release:
10+
name: Test on php ${{ matrix.php}}
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 10
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [7.3]
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
22+
- name: Set ENV for github-release
23+
# https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
24+
run: |
25+
echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
26+
echo "RELEASE_NAME=$GITHUB_WORKFLOW" >> $GITHUB_ENV
27+
28+
# usage refer https://github.com/shivammathur/setup-php
29+
- name: Setup PHP
30+
timeout-minutes: 5
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php}}
34+
tools: pecl, php-cs-fixer, phpunit
35+
extensions: mbstring, dom, fileinfo, mysql, openssl # , swoole-4.4.19 #optional, setup extensions
36+
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
37+
coverage: none #optional, setup coverage driver: xdebug, none
38+
39+
- name: Install dependencies # eg: v1.0.3
40+
run: |
41+
tag1=${GITHUB_REF#refs/*/}
42+
echo "release tag: ${tag1}"
43+
composer install --no-progress --no-suggest
44+
45+
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
46+
# Docs: https://getcomposer.org/doc/articles/scripts.md
47+
48+
# - name: Build phar and send to github assets
49+
# run: |
50+
# echo $RELEASE_TAG
51+
# echo $RELEASE_NAME
52+
# php -d phar.readonly=0 bin/kite phar:pack -o kite-${RELEASE_TAG}.phar --no-progress
53+
# php kite-${RELEASE_TAG}.phar -V
54+
55+
# https://github.com/actions/create-release
56+
- uses: meeDamian/[email protected]
57+
with:
58+
gzip: false
59+
token: ${{ secrets.GITHUB_TOKEN }}
60+
tag: ${{ env.RELEASE_TAG }}
61+
name: ${{ env.RELEASE_TAG }}
62+
# files: kite-${{ env.RELEASE_TAG }}.phar

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ composer.lock
77
*.log
88
*.pid
99
*.patch
10+
*.cache
1011
.DS_Store
1112
*.bz2

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<phpunit backupGlobals="false"
44
backupStaticAttributes="false"
5-
bootstrap="test/boot.php"
5+
bootstrap="test/bootstrap.php"
66
colors="false"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"

src/Traits/ReadMessageTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function read($message = null, bool $nl = false, array $opts = [])
3636
'stream' => self::$inputStream,
3737
], $opts);
3838

39-
return file_get_contents($opts['stream'], $opts['length']);
39+
return (string)file_get_contents($opts['stream'], $opts['length']);
4040
}
4141

4242
/**
@@ -62,7 +62,7 @@ public static function readln($message = null, bool $nl = false, array $opts = [
6262
'stream' => self::$inputStream,
6363
], $opts);
6464

65-
return trim(fgets($opts['stream'], $opts['length']));
65+
return trim((string)fgets($opts['stream'], $opts['length']));
6666
}
6767

6868
/**

src/Util/LineParser.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Cli\Util;
11+
12+
use function count;
13+
use function explode;
14+
use function ltrim;
15+
use function strlen;
16+
use function substr;
17+
18+
/**
19+
* Class LineParser
20+
*
21+
* @package Inhere\Console\Util
22+
*/
23+
class LineParser
24+
{
25+
/**
26+
* full command line string.
27+
* eg: 'kite git acp -m "feat: support start an interactive shell for run application"'
28+
*
29+
* @var string
30+
*/
31+
private $line;
32+
33+
/**
34+
* the exploded nodes by space.
35+
*
36+
* @var array
37+
*/
38+
private $nodes = [];
39+
40+
/**
41+
* the parsed args
42+
*
43+
* @var array
44+
*/
45+
private $args = [];
46+
47+
/**
48+
* @param string $line full command line string.
49+
*
50+
* @return array
51+
*/
52+
public static function parseIt(string $line): array
53+
{
54+
return (new self($line))->parse();
55+
}
56+
57+
/**
58+
* Class constructor.
59+
*
60+
* @param string $line full command line string.
61+
*/
62+
public function __construct(string $line)
63+
{
64+
$this->setLine($line);
65+
}
66+
67+
/**
68+
* @return array
69+
*/
70+
public function parse(): array
71+
{
72+
if ('' === $this->line) {
73+
return [];
74+
}
75+
76+
$this->nodes = explode(' ', $this->line);
77+
if (count($this->nodes) === 1) {
78+
$this->args = $this->nodes;
79+
return $this->args;
80+
}
81+
82+
$quoteChar = '';
83+
$fullItem = '';
84+
foreach ($this->nodes as $item) {
85+
if ('' === $item) {
86+
continue;
87+
}
88+
89+
$goon = true;
90+
$start = $item[0];
91+
92+
$len = strlen($item);
93+
$end = $item[$len - 1];
94+
95+
// $start is start char
96+
if ($start === "'" || $start === '"') {
97+
$item = substr($item, 1);
98+
if ($quoteChar === $start) {
99+
$this->args[] = $fullItem . ' ' . $item;
100+
// must clear
101+
$quoteChar = $fullItem = '';
102+
} else { // start
103+
if ($fullItem) {
104+
$this->args[] = $fullItem;
105+
}
106+
107+
$quoteChar = $start;
108+
$fullItem = $item;
109+
}
110+
111+
$goon = false;
112+
}
113+
114+
// $end is end char
115+
if ($end === "'" || $end === '"') {
116+
$item = substr($item, 0, -1);
117+
if ($quoteChar === $end) {
118+
$this->args[] = $fullItem . ' ' . $item;
119+
// must clear
120+
$quoteChar = $fullItem = '';
121+
} else {
122+
if ($fullItem) {
123+
$this->args[] = $fullItem;
124+
}
125+
126+
$fullItem = $item;
127+
}
128+
129+
$goon = false;
130+
}
131+
132+
if ($goon === true) {
133+
if ($quoteChar) {
134+
$fullItem .= ' ' . $item;
135+
} else {
136+
$this->args[] = $item;
137+
}
138+
}
139+
}
140+
141+
if ($fullItem) {
142+
$this->args[] = $fullItem;
143+
}
144+
145+
return $this->args;
146+
}
147+
148+
/**
149+
* @return string
150+
*/
151+
public function getLine(): string
152+
{
153+
return $this->line;
154+
}
155+
156+
/**
157+
* @param string $line
158+
*/
159+
public function setLine(string $line): void
160+
{
161+
$this->line = ltrim($line);
162+
}
163+
164+
/**
165+
* @return array
166+
*/
167+
public function getNodes(): array
168+
{
169+
return $this->nodes;
170+
}
171+
}

test/Util/LineParserTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\CliTest\Util;
11+
12+
use Toolkit\Cli\Util\LineParser;
13+
use PHPUnit\Framework\TestCase;
14+
use function count;
15+
16+
class LineParserTest extends TestCase
17+
{
18+
public function testParse(): void
19+
{
20+
$line = 'kite git status';
21+
$args = LineParser::parseIt($line);
22+
self::assertCount(3, $args);
23+
24+
$line = 'kite git commit -m "the commit message"';
25+
$args = LineParser::parseIt($line);
26+
self::assertCount(5, $args);
27+
$len = count($args);
28+
self::assertSame('the commit message', $args[$len-1]);
29+
30+
$line = 'kite git commit -m "the commit message';
31+
$args = LineParser::parseIt($line);
32+
self::assertCount(5, $args);
33+
$len = count($args);
34+
self::assertSame('the commit message', $args[$len-1]);
35+
36+
$line = 'kite top sub -a "the a message" --foo val1 --bar "val 2"';
37+
$args = LineParser::parseIt($line);
38+
self::assertCount(9, $args);
39+
$len = count($args);
40+
self::assertSame('val 2', $args[$len-1]);
41+
42+
$line = 'kite top sub -a "the a message " --foo val1 --bar "val 2"';
43+
$args = LineParser::parseIt($line);
44+
self::assertCount(9, $args);
45+
$len = count($args);
46+
self::assertSame('val 2', $args[$len-1]);
47+
48+
49+
$line = 'kite top sub -a "the a message "foo val1 --bar "val 2"';
50+
$args = LineParser::parseIt($line);
51+
self::assertCount(9, $args);
52+
}
53+
}
File renamed without changes.

0 commit comments

Comments
 (0)