From 948798487a8a08a8acdaa8c2ce692232870ca9c4 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Sun, 14 Aug 2022 18:22:21 +0100 Subject: [PATCH] Added tailwind:plugin command --- Plugin.php | 3 + console/TailwindPlugin.php | 133 +++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 console/TailwindPlugin.php diff --git a/Plugin.php b/Plugin.php index 838a317..32ae238 100644 --- a/Plugin.php +++ b/Plugin.php @@ -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 @@ -49,6 +50,8 @@ public function boot() $this->extendBrandSettingsForm(); $this->extendBackendAuthController(); } + + $this->registerConsoleCommand('tailwindui.plugin', TailwindPlugin::class); } /** diff --git a/console/TailwindPlugin.php b/console/TailwindPlugin.php new file mode 100644 index 0000000..bb686ca --- /dev/null +++ b/console/TailwindPlugin.php @@ -0,0 +1,133 @@ +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')); + + 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); + } +}