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] Added tailwind:plugin command #10

Draft
wants to merge 1 commit into
base: main
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: 3 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use System\Controllers\Settings as SettingsController;
use Backend\Controllers\Preferences as PreferencesController;
use Backend\Models\Preference as PreferenceModel;
use Winter\TailwindUI\Console\TailwindPlugin;

/**
* TailwindUI Plugin Information File
Expand Down Expand Up @@ -49,6 +50,8 @@ public function boot()
$this->extendBrandSettingsForm();
$this->extendBackendAuthController();
}

$this->registerConsoleCommand('tailwindui.plugin', TailwindPlugin::class);
}

/**
Expand Down
133 changes: 133 additions & 0 deletions console/TailwindPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Winter\TailwindUI\Console;

use File;
use Illuminate\Console\Command;
use System\Classes\PluginManager;
use Illuminate\Support\Facades\Artisan;

class TailwindPlugin extends Command
{
/**
* @var string|null The default command name for lazy loading.
*/
protected static $defaultName = 'tailwind:plugin';

/**
* @var string The name and signature of this command.
*/
protected $signature = 'tailwind:plugin
{plugin : Defines plugin to compile tailwind assets}
{webpackArgs?* : Arguments to pass through to the Webpack CLI}';

/**
* @var string The console command description.
*/
protected $description = 'Compile plugin tailwind css';

protected ?string $config = null;
protected ?string $original = null;
protected ?string $backendCss = null;

public function __construct()
{
parent::__construct();

$dir = plugins_path('winter/tailwindui');

$this->config = $dir . '/tailwind.config.js';
$this->original = $dir . '/tailwind.original.config.js';
$this->backendCss = $dir . '/assets/css/dist/backend.css';
}

public function __destruct()
{
if (File::exists($this->config) && File::exists($this->original)) {
File::delete($this->config);
File::move($this->original, $this->config);
}

if (File::exists($this->backendCss) && File::exists($this->backendCss . '.original')) {
File::delete($this->backendCss);
File::move($this->backendCss . '.original', $this->backendCss);
}
}

/**
* Execute the console command.
* @return int
*/
public function handle(): int
{
$plugin = PluginManager::instance()->findByIdentifier($this->argument('plugin'));
Copy link
Member

Choose a reason for hiding this comment

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

You can use the \System\Console\Traits\HasPluginArgument to add automatic support for autocompletion, validation, and resolution of the plugin argument.


if (!$plugin) {
throw new \InvalidArgumentException(sprintf('Plugin `%s` not found', $this->argument('plugin')));
}

$pluginConfig = $plugin->getPluginPath() . '/tailwind.config.js';

if (!File::exists($pluginConfig)) {
throw new \RuntimeException('Unable to locate plugin tailwind config');
}

File::move($this->config, $this->original);

$this->copyFileWithRelativePathResolution($pluginConfig, $this->config);

File::copy($this->backendCss, $this->backendCss . '.original');

$this->mix();

// find all css rules in the original
$data = $this->diff(File::get($this->backendCss . '.original'), File::get($this->backendCss));

$out = $plugin->getPluginPath() . '/assets/dist/css';

if (!File::exists($out)) {
File::makeDirectory($out);
}

File::put($out . '/backend.css', $data);

return 0;
}

public function copyFileWithRelativePathResolution(string $file, string $path): bool
{
$data = File::get($file);

// replace ./ paths
$data = preg_replace('/(\s|\(|\[)?("|\')?\.\//', '$1$2' . dirname($file) . '/', $data);
// replace root config import
$data = preg_replace(
'/(\'|")(.*?)\/winter\/tailwindui\/tailwind\.config\.js(\'|")/',
'"./tailwind.original.config.js"',
$data
);

return File::put($path, $data);
}

public function diff(string $a, string $b): string
{
preg_match_all('/((.*?){(.*?)})/', $a, $matches);
$matches = $matches[1];

foreach ($matches as $match) {
$b = str_replace($match, '', $b);
}

return $b;
}

public function mix(): int
{
$webpackArgs = $this->argument('webpackArgs')
? ' -- ' . implode(' ', $this->argument('webpackArgs'))
: '';

return Artisan::call('mix:compile --package winter.tailwindui --production' . $webpackArgs);
}
}