Skip to content
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

Wip/support uploading packages update manager changes #197

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"twig/twig": "^3.14",
"wikimedia/less.php": "~3.0",
"wikimedia/minify": "~2.2",
"winter/laravel-config-writer": "^1.0.1"
"winter/laravel-config-writer": "^1.0.1",
"winter/packager": "~0.2"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When merging we should probably release v1 of Packager.

},
"require-dev": {
"phpunit/phpunit": "^9.5.8",
Expand Down
117 changes: 116 additions & 1 deletion src/Console/Command.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<?php namespace Winter\Storm\Console;
<?php

namespace Winter\Storm\Console;

use Illuminate\Console\Command as BaseCommand;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Termwind\Termwind;
use Winter\Storm\Extension\ExtendableTrait;
use Winter\Storm\Support\Traits\Emitter;
use function Termwind\renderUsing;

/**
* Command base class
* Contains utilities to make developing CLI commands nicer
*
* @author Luke Towers
*
* @method static mixed extend(callable $callback, bool $scoped = false, ?object $outerScope = null)
*/
abstract class Command extends BaseCommand implements SignalableCommandInterface
{
use Traits\HandlesCleanup;
use Traits\ProvidesAutocompletion;
use ExtendableTrait;
use Emitter;

/**
* @var \Winter\Storm\Foundation\Application
Expand All @@ -34,6 +49,41 @@ public function __construct()
if (!empty($this->replaces)) {
$this->setAliases($this->replaces);
}

$this->extendableConstruct();
}

/**
* Override the laravel run function to allow us to run callbacks on the command prior to excution.
* Run the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
public function run(InputInterface $input, OutputInterface $output): int
{
$this->output = $this->laravel->make(
OutputStyle::class, ['input' => $input, 'output' => $output]
);

$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);

$this->fireEvent('beforeRun', [$this]);

$renderer = Termwind::getRenderer();
renderUsing($this->output->getOutput());

try {
// Wizardy
return SymfonyCommand::run(
$this->input = $input, $this->output
);
} finally {
$this->untrap();
// Restore the original termwind renderer
renderUsing($renderer);
}
}

/**
Expand All @@ -59,4 +109,69 @@ public function error($string, $verbosity = null)
{
$this->components->error($string, $this->parseVerbosity($verbosity));
}

/**
* Magic allowing for extendable properties
*
* @param $name
* @return mixed|null
*/
public function __get($name)
{
return $this->extendableGet($name);
}

/**
* Magic allowing for extendable properties
*
* @param $name
* @param $value
* @return void
*/
public function __set($name, $value)
{
$this->extendableSet($name, $value);
}

/**
* Magic allowing for dynamic extension
*
* @param $name
* @param $params
* @return mixed
*/
public function __call($name, $params)
{
if ($name === 'extend') {
if (empty($params[0]) || !is_callable($params[0])) {
throw new \InvalidArgumentException('The extend() method requires a callback parameter or closure.');
}
if ($params[0] instanceof \Closure) {
return $params[0]->call($this, $params[1] ?? $this);
}
return \Closure::fromCallable($params[0])->call($this, $params[1] ?? $this);
}

return $this->extendableCall($name, $params);
}

/**
* Magic allowing for dynamic static extension
*
* @param $name
* @param $params
* @return mixed|void
*/
public static function __callStatic($name, $params)
{
if ($name === 'extend') {
if (empty($params[0])) {
throw new \InvalidArgumentException('The extend() method requires a callback parameter or closure.');
}
self::extendableExtendCallback($params[0], $params[1] ?? false, $params[2] ?? null);
return;
}

return parent::__callStatic($name, $params);
}
}
12 changes: 12 additions & 0 deletions src/Foundation/Extension/WinterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Winter\Storm\Foundation\Extension;

interface WinterExtension
{
public function getPath(): string;

public function getVersion(): string;

public function getIdentifier(): string;
}
74 changes: 74 additions & 0 deletions src/Packager/Commands/InfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Winter\Storm\Packager\Commands;

use Illuminate\Support\Facades\Cache;
use System\Classes\Packager\Composer;
use Winter\Packager\Commands\BaseCommand;
use Winter\Packager\Exceptions\CommandException;
use Winter\Packager\Exceptions\WorkDirException;

class InfoCommand extends BaseCommand
{
protected ?string $package = null;

/**
* Command handler.
*
* @param string|null $package
* @param boolean $dryRun
* @param boolean $dev
* @return void
* @throws CommandException
*/
public function handle(?string $package = null): void

Check failure on line 24 in src/Packager/Commands/InfoCommand.php

View workflow job for this annotation

GitHub Actions / Code Analysis

PHPDoc tag @param references unknown parameter: $dev

Check failure on line 24 in src/Packager/Commands/InfoCommand.php

View workflow job for this annotation

GitHub Actions / Code Analysis

PHPDoc tag @param references unknown parameter: $dryRun
{
$this->package = $package;
}

/**
* @inheritDoc
*/
public function arguments(): array
{
$arguments = [
'--format' => 'json',
];

if (!$this->package) {
return $arguments;
}

$arguments['package'] = $this->package;

return $arguments;
}

/**
* @throws CommandException
* @throws WorkDirException
*/
public function execute(): array
{
$output = $this->runComposerCommand();
$message = implode(PHP_EOL, $output['output']);

if ($output['code'] !== 0) {
throw new CommandException($message);
}

$result = json_decode($message, JSON_OBJECT_AS_ARRAY);

Check failure on line 60 in src/Packager/Commands/InfoCommand.php

View workflow job for this annotation

GitHub Actions / Code Analysis

Parameter #2 $associative of function json_decode expects bool|null, int given.

return $this->package
? $result ?? []
: $result['installed'] ?? [];
}

/**
* @inheritDoc
*/
public function getCommandName(): string
{
return 'info';
}
}
70 changes: 70 additions & 0 deletions src/Packager/Commands/RemoveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Winter\Storm\Packager\Commands;

use Illuminate\Support\Facades\Cache;
use Winter\Packager\Commands\BaseCommand;
use Winter\Packager\Exceptions\CommandException;
use Winter\Storm\Packager\Composer;

class RemoveCommand extends BaseCommand
{
protected ?string $package = null;
protected bool $dryRun = false;

/**
* Command handler.
*
* @param string|null $package
* @param boolean $dryRun
* @return void
* @throws CommandException
*/
public function handle(?string $package = null, bool $dryRun = false): void
{
if (!$package) {
throw new CommandException('Must provide a package');
}

$this->package = $package;
$this->dryRun = $dryRun;
}

/**
* @inheritDoc
*/
public function arguments(): array
{
$arguments = [];

if ($this->dryRun) {
$arguments['--dry-run'] = true;
}

$arguments['packages'] = [$this->package];

return $arguments;
}

public function execute()
{
$output = $this->runComposerCommand();
$message = implode(PHP_EOL, $output['output']);

if ($output['code'] !== 0) {
throw new CommandException($message);
}

Cache::forget(Composer::COMPOSER_CACHE_KEY);

return $message;
}

/**
* @inheritDoc
*/
public function getCommandName(): string
{
return 'remove';
}
}
82 changes: 82 additions & 0 deletions src/Packager/Commands/RequireCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Winter\Storm\Packager\Commands;

use Illuminate\Support\Facades\Cache;
use Winter\Storm\Packager\Composer;
use Winter\Packager\Commands\BaseCommand;
use Winter\Packager\Exceptions\CommandException;
use Winter\Packager\Exceptions\WorkDirException;

class RequireCommand extends BaseCommand
{
protected ?string $package = null;
protected bool $dryRun = false;
protected bool $dev = false;

/**
* Command handler.
*
* @param string|null $package
* @param boolean $dryRun
* @param boolean $dev
* @return void
* @throws CommandException
*/
public function handle(?string $package = null, bool $dryRun = false, bool $dev = false): void
{
if (!$package) {
throw new CommandException('Must provide a package');
}

$this->package = $package;
$this->dryRun = $dryRun;
$this->dev = $dev;
}

/**
* @inheritDoc
*/
public function arguments(): array
{
$arguments = [];

if ($this->dryRun) {
$arguments['--dry-run'] = true;
}

if ($this->dev) {
$arguments['--dev'] = true;
}

$arguments['packages'] = [$this->package];

return $arguments;
}

/**
* @throws CommandException
* @throws WorkDirException
*/
public function execute(): string
{
$output = $this->runComposerCommand();
$message = implode(PHP_EOL, $output['output']);

if ($output['code'] !== 0) {
throw new CommandException($message);
}

Cache::forget(Composer::COMPOSER_CACHE_KEY);

return $message;
}

/**
* @inheritDoc
*/
public function getCommandName(): string
{
return 'require';
}
}
Loading
Loading