diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 382e763..7c03172 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,8 @@ on: - - pull_request + pull_request: + push: + branches: + - main jobs: test: diff --git a/README.md b/README.md index 53e63d8..c90ff43 100644 --- a/README.md +++ b/README.md @@ -16,23 +16,30 @@ cd ~/.hfctl git pull ``` -### Usage +## Usage +```shell +hfctl [arguments] +``` -| Command | Description | +### Commands +| Name | Description | | --- | --- | - -| `hfctl help` | Shows this help message | -| `hfctl version` | Displays hfctl and image version | -| `hfctl self-update` | Updates hfctl to the latest version | -| `hfctl create` | Creates a new Hyperf project | -| `hfctl composer` | Runs composer commands | -| `hfctl start` | Starts the Hyperf server | -| `hfctl stop` | Stops the Hyperf server | -| `hfctl restart` | Restart the Hyperf server | -| `hfctl watch` | Starts the Hyperf server with hyperf/watcher | -| `hfctl bin` (`cmd` or `command`) | Runs Hyperf commands (i.e.: `php bin/hyperf.php`) | -| `hfctl logs` | Shows the Hyperf container logs | +| `help` | Shows this help message | +| `version` | Displays `hfctl` and image version | +| `self-update` | Updates `hfctl` to the latest version | +| `create ` | Creates a new Hyperf project | +| `composer ` | Runs composer commands | +| `console ` | Runs Hyperf console commands (`php bin/hyperf.php `) | +| `start [port]` | Starts the Hyperf server (default port `9501`) | +| `stop` | Stops the Hyperf server | +| `restart` | Restart the Hyperf server | +| `watch [port]` | Starts the Hyperf watcher (default port `9501`) | +| `up` | Runs Docker Compose up | +| `down` | Runs Docker Compose down | +| `logs [-f\|--follow]` | Shows the Hyperf container logs (use `-f` or `--follow` to follow logs) | +| `test` | Runs PHPUnit tests | +| `lint` | Runs PHP Coding Standards Fixer | +| `analyse [-l]` | Runs PHPStan analyses (default level `5`) | ## Contributing - Please visit [CONTRIBUTING.md](CONTRIBUTING.md). diff --git a/hfctl b/hfctl index 615062a..5812d32 100755 --- a/hfctl +++ b/hfctl @@ -1,6 +1,6 @@ #!/usr/bin/env bash -HFCTL=v0.3.0 +HFCTL=v0.4.0 IMAGE=hyperf/hyperf:8.2-alpine-v3.18-swoole-v5.1 HFCID=runtime/hfctl.cid @@ -14,17 +14,22 @@ help() { echo -e " hfctl [arguments]" echo -e "" echo -e "\033[1;33mCommands:\033[0m" - echo -e " \033[1;32mhelp\033[0m Shows this help message" - echo -e " \033[1;32mversion\033[0m Displays hfctl and image version" - echo -e " \033[1;32mself-update\033[0m Updates hfctl to the latest version" - echo -e " \033[1;32mcreate [-it]\033[0m Creates a new Hyperf project \033[1;37m(use -it for interactive mode)\033[0m" - echo -e " \033[1;32mcomposer \033[0m Runs composer commands" - echo -e " \033[1;32mstart [port]\033[0m Starts the Hyperf server \033[1;37m(default port 9501)\033[0m" - echo -e " \033[1;32mstop\033[0m Stops the Hyperf server" - echo -e " \033[1;32mrestart\033[0m Restart the Hyperf server" - echo -e " \033[1;32mwatch [port]\033[0m Starts the Hyperf watcher \033[1;37m(default port 9501)\033[0m" - echo -e " \033[1;32mconsole \033[0m Runs Hyperf console commands \033[1;37m(php bin/hyperf.php )\033[0m" - echo -e " \033[1;32mlogs [-f|--follow]\033[0m Shows the Hyperf container logs \033[1;37m(use -f or --follow to follow logs)\033[0m" + echo -e " \033[1;32mhelp\033[0m Shows this help message" + echo -e " \033[1;32mversion\033[0m Displays hfctl and image version" + echo -e " \033[1;32mself-update\033[0m Updates hfctl to the latest version" + echo -e " \033[1;32mcreate Creates a new Hyperf project" + echo -e " \033[1;32mcomposer \033[0m Runs composer commands" + echo -e " \033[1;32mconsole \033[0m Runs Hyperf console commands \033[1;37m(php bin/hyperf.php )\033[0m" + echo -e " \033[1;32mstart [port]\033[0m Starts the Hyperf server \033[1;37m(default port 9501)\033[0m" + echo -e " \033[1;32mstop\033[0m Stops the Hyperf server" + echo -e " \033[1;32mrestart\033[0m Restart the Hyperf server" + echo -e " \033[1;32mwatch [port]\033[0m Starts the Hyperf watcher \033[1;37m(default port 9501)\033[0m" + echo -e " \033[1;32mup\033[0m Runs Docker Compose up" + echo -e " \033[1;32mdown\033[0m Runs Docker Compose down" + echo -e " \033[1;32mlogs [-f|--follow]\033[0m Shows the Hyperf container logs \033[1;37m(use -f or --follow to follow logs)\033[0m" + echo -e " \033[1;32mtest\033[0m Runs PHPUnit tests" + echo -e " \033[1;32mlint\033[0m Runs PHP Coding Standards Fixer" + echo -e " \033[1;32manalyse [-l]\033[0m Runs PHPStan analyses \033[1;37m(default level 5)\033[0m" echo -e "" } @@ -88,14 +93,43 @@ console() { } logs() { - pre-check cid - docker logs $@ $(cat $HFCID) + DC=$(docker compose ps --format 'json {{.Name}}') + if [[ -n $DC ]]; then + docker compose logs $@ + else + pre-check cid + docker logs $@ $(cat $HFCID) + fi +} + +up() { + docker compose up -d +} + +down() { + docker compose down --remove-orphans +} + +test() { + docker-run ${HFTTY:--it} php vendor/bin/co-phpunit --prepend test/bootstrap.php $@ +} + +lint() { + docker-run ${HFTTY:--it} php vendor/bin/php-cs-fixer fix $@ +} + +analyse() { + docker-run ${HFTTY:--it} php vendor/bin/phpstan analyse ${@:--l5} app } docker-run() { docker run --user $UID --rm -w /opt -v ./:/opt $1 --entrypoint $2 $IMAGE ${@:3} } +ps() { + docker ps +} + pre-check() { if ! [ -f bin/hyperf.php ]; then echo -e "\033[0;31mAre you in a Hyperf project?\033[0m" diff --git a/hfctl_test b/hfctl_test index 2edce57..6e42e85 100755 --- a/hfctl_test +++ b/hfctl_test @@ -71,6 +71,21 @@ function test_should_run_hyperf_console_commands() { assert_contains "Console Tool" "$($HFCTL console)" } +function test_should_run_test_command() { + cd $HFDIR + assert_contains "OK (1 test, 2 assertions)" "$($HFCTL test)" +} + +function test_should_run_lint_command() { + cd $HFDIR + assert_contains "Fixed 8 of 27 files" "$($HFCTL lint 2> /dev/null)" +} + +function test_should_run_analyse_command() { + cd $HFDIR + assert_contains "No errors" "$($HFCTL analyse 2> /dev/null)" +} + if [[ $0 == "./hfctl_test" ]]; then export HFTTY="-lno-tty" ./bashunit ./hfctl_test $@