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

[1.x] Adds blade support #256

Draft
wants to merge 66 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
89c97cb
Adds blade support
nunomaduro Mar 15, 2024
a542c13
Improves error handling
nunomaduro Mar 15, 2024
fd39d90
Rebuilds
nunomaduro Mar 15, 2024
3b45cb5
Deletes version file
nunomaduro Mar 15, 2024
4b2c343
Ignores version file
nunomaduro Mar 15, 2024
741f377
Update app/Fixers/LaravelBladeFixer.php
nunomaduro Mar 15, 2024
4ee92d9
Update resources/presets/laravel.php
nunomaduro Mar 15, 2024
9458bc7
Rebuilds
nunomaduro Mar 15, 2024
25160ce
Validates with node and npm are installed
nunomaduro Mar 15, 2024
c22d555
Rebuilds
nunomaduro Mar 15, 2024
e9fce59
Changes underlying prettier plugins
nunomaduro Mar 16, 2024
ece1d68
Changes version
nunomaduro Mar 16, 2024
8b547fc
Rebuilds
nunomaduro Mar 16, 2024
dce1b6b
Merge pull request #258 from laravel/feat/prettier-plugin
nunomaduro Mar 16, 2024
fb4061e
Improves error handling
nunomaduro Mar 16, 2024
ac1dbb7
Rebuilds
nunomaduro Mar 16, 2024
dbde266
Merge pull request #259 from laravel/feat/prettier-plugin
nunomaduro Mar 16, 2024
8a6aba7
chore: bumps dependencies
nunomaduro Mar 16, 2024
4e19273
Updates prettier plugin blade version
nunomaduro Mar 17, 2024
d191f53
Rebuilds
nunomaduro Mar 17, 2024
b94e796
Fix tests for the laravel skeleton
Jubeki Mar 17, 2024
e68acf4
Fix static analysis
Jubeki Mar 17, 2024
eaff017
Fix static analysis
Jubeki Mar 17, 2024
6c66735
Fix failing tests
Jubeki Mar 17, 2024
7ec420d
Merge pull request #260 from Jubeki/fix-tests
nunomaduro Mar 17, 2024
8615608
Uses Pint to format code within blade tags
nunomaduro Mar 18, 2024
647ed31
wip
nunomaduro Mar 18, 2024
d3bd5e4
wip
nunomaduro Mar 18, 2024
32f42e5
Uses prettier as worker mode
nunomaduro Mar 18, 2024
59ace97
Uses `"printWidth": 120`
nunomaduro Mar 18, 2024
b74f3e8
Resolves `LaravelBladeFixer` from IOC
nunomaduro Mar 18, 2024
ea25d2e
Rebuilds
nunomaduro Mar 18, 2024
85ad368
Properly ensures the process is terminated
nunomaduro Mar 18, 2024
1ad5058
Rebuilds
nunomaduro Mar 18, 2024
d23f749
Bumps `prettier-plugin-blade`
nunomaduro Mar 18, 2024
6173045
Fixes worker getting confused
nunomaduro Mar 18, 2024
04a2add
Rebuilds
nunomaduro Mar 18, 2024
7558542
Refactors ignorables
nunomaduro Mar 19, 2024
3b64ce8
Rebuilds
nunomaduro Mar 19, 2024
2618dac
Fixes single line attributes
nunomaduro Mar 19, 2024
54bc0ff
Rebuilds
nunomaduro Mar 19, 2024
1511a37
Adds `--blade` option
nunomaduro Mar 20, 2024
42ee061
Rebuilds
nunomaduro Mar 20, 2024
8c4ce67
Keeps improving linting of blade files
nunomaduro Mar 23, 2024
83e50c5
Rebuilds
nunomaduro Mar 23, 2024
be09e72
Fixes configuration
nunomaduro Mar 23, 2024
cc01378
Rebuilds
nunomaduro Mar 23, 2024
61cb222
Adjust wrapping
nunomaduro Mar 23, 2024
2f093ca
Rebuilds
nunomaduro Mar 23, 2024
ba86429
Adjusts style
nunomaduro Mar 24, 2024
5a889e3
Rebuilds
nunomaduro Mar 24, 2024
7e97525
Ignores cache for now
nunomaduro Mar 26, 2024
b325827
150 printWidth
nunomaduro Mar 26, 2024
33bbf45
Rebuilds
nunomaduro Mar 26, 2024
4d3e126
Puts SVGs in the same line
nunomaduro Mar 26, 2024
3148bcc
Coding style
nunomaduro Mar 26, 2024
f3371d2
Ignores minified code
nunomaduro Mar 26, 2024
58e661c
Refactors code
nunomaduro Mar 27, 2024
4a086d3
Rebuilds
nunomaduro Mar 27, 2024
d06e89d
Style
nunomaduro Apr 15, 2024
d11cbbd
inline paragrahps
nunomaduro Apr 15, 2024
e775568
Merge branch 'main' into feat/blade
nunomaduro Apr 15, 2024
f80b15e
Rebuils
nunomaduro Apr 15, 2024
50229c4
Rebuilds
nunomaduro Apr 15, 2024
587cb48
bumps dependencies
nunomaduro Apr 22, 2024
3da5103
Rebuilds
nunomaduro Apr 22, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/vendor
composer.lock
.phpunit.result.cache
/node_sandbox/package-lock.json
/node_sandbox/node_modules
/node_sandbox/version
10 changes: 10 additions & 0 deletions app/Exceptions/PrettierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class PrettierException extends Exception
{
//
}
7 changes: 5 additions & 2 deletions app/Factories/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Factories;

use App\Fixers\LaravelBladeFixer;
use App\Repositories\ConfigurationJsonRepository;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
Expand All @@ -18,7 +19,6 @@ class ConfigurationFactory
'_ide_helper_models.php',
'_ide_helper.php',
'.phpstorm.meta.php',
'*.blade.php',
];

/**
Expand All @@ -45,7 +45,10 @@ public static function preset($rules)
->setFinder(self::finder())
->setRules(array_merge($rules, resolve(ConfigurationJsonRepository::class)->rules()))
->setRiskyAllowed(true)
->setUsingCache(true);
->setUsingCache(true)
->registerCustomFixers([
new LaravelBladeFixer(),
]);
}

/**
Expand Down
112 changes: 112 additions & 0 deletions app/Fixers/LaravelBladeFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace App\Fixers;

use App\Exceptions\PrettierException;
use App\Prettier;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

class LaravelBladeFixer extends AbstractFixer implements ConfigurableFixerInterface
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'Pint/laravel_blade';
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition('Fixes Laravel Blade files.', []);
}

/**
* {@inheritdoc}
*/
public function getPriority(): int
{
return -2;
}

/**
* {@inheritdoc}
*/
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
{
$path = $file->getRealPath();

if (! str_ends_with($path, '.blade.php')) {
return;
}

$content = $tokens->generateCode();

if (str_contains($content, '<x-mail::') || str_contains($content, '@component(\'mail::')) {
return;
}

/** @var \Illuminate\Process\ProcessResult $result */
$result = app(Prettier::class)->run([$path, ...$this->mapConfigurationToCliOptions()]);

if ($result->failed()) {
throw new PrettierException($result->errorOutput());
}

$tokens->setCode($result->output());
}

/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('sortTailwindcssClasses', 'Sort tailwindcss classes.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('sortHtmlAttributes', 'Sort html attributes.'))
->setAllowedTypes(['string'])
->setDefault('none')
->getOption(),
]);
}

/**
* Maps the configuration to CLI options.
*
* @return array<string, string>
*/
protected function mapConfigurationToCliOptions(): array
{
$configuration = $this->configuration;

return array_values(array_filter([
$configuration['sortTailwindcssClasses']
? '--sort-tailwindcss-classes=true'
: '--sort-tailwindcss-classes=false',
'--sort-html-attributes',
$configuration['sortHtmlAttributes'],
]));
}
}
110 changes: 110 additions & 0 deletions app/NodeSandbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace App;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;

class NodeSandbox
{
/**
* The version of the sandbox.
*
* @var string
*/
const VERSION = '1';

/**
* The path to the sandbox.
*
* @var string
*/
protected $path;

/**
* Indicates if the sandbox is initialized.
*
* @var bool
*/
protected $initialized = false;

/**
* Creates a new node sandbox instance.
*
* @param string
* @return void
*/
public function __construct($path)
{
$this->path = $path;
}

/**
* Ensures the sandbox is initialized.
*
* @return void
*/
public function ensureInitialized()
{
if ($this->initialized) {
return;
}

if (! File::exists($this->path.'/version') || File::get($this->path.'/version') !== static::VERSION) {
File::deleteDirectory($this->path.'/node_modules');
File::delete($this->path.'/package-lock.json');

$this->installNodeDependencies();

File::put($this->path.'/version', static::VERSION);
}
}

/**
* The sandbox path.
*
* @return string
*/
public function path()
{
return $this->path;
}

/**
* Run the given commands.
*
* @param array<int, string>
* @return \Illuminate\Process\ProcessResult
*/
public function run($commands)
{
$this->ensureInitialized();

$process = Process::command(implode(' ', $commands))
->path($this->path);

return $process->run();
}

/**
* Install the node dependencies.
*
* @return void
*/
private function installNodeDependencies()
{
$commands = [
'npm install',
];

$result = Process::command(implode(' && ', $commands))
->path($this->path)
->run();

if ($result->failed()) {
$reason = $result->output();

abort(1, sprintf('Pint was unable to install its node dependencies. Reason: %s', $reason));
}
}
}
40 changes: 40 additions & 0 deletions app/Prettier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App;

class Prettier
{
/**
* The node sandbox instance.
*
* @var \App\NodeSandbox
*/
protected $sandbox;

/**
* The node sandbox instance.
*
* @param \App\NodeSandbox
* @return void
*/
public function __construct($sandbox)
{
$this->sandbox = $sandbox;
}

/**
* Run the prettier command.
*
* @param array<int, string>
* @return \Illuminate\Process\ProcessResult
*/
public function run($params = [])
{
return $this->sandbox->run([
'./node_modules/.bin/prettier',
'--config',
$this->sandbox->path().'/'.'.prettierrc',
...$params,
]);
}
}
15 changes: 15 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Providers;

use App\NodeSandbox;
use App\Prettier;
use Illuminate\Support\ServiceProvider;
use Phar;
use PhpCsFixer\Error\ErrorsManager;
use Symfony\Component\EventDispatcher\EventDispatcher;

Expand Down Expand Up @@ -32,5 +35,17 @@ public function register()
$this->app->singleton(EventDispatcher::class, function () {
return new EventDispatcher();
});

$this->app->singleton(NodeSandbox::class, function () {
return new NodeSandbox(
Phar::running()
? (dirname(Phar::running(false), 2).'/node_sandbox')
: base_path('node_sandbox'),
);
});

$this->app->singleton(Prettier::class, function ($app) {
return new Prettier($app->make(NodeSandbox::class));
});
}
}
Binary file modified builds/pint
Binary file not shown.
Loading
Loading