Skip to content

Commit ec23939

Browse files
update
1 parent c9fb538 commit ec23939

13 files changed

Lines changed: 647 additions & 215 deletions

File tree

bin/brushcss

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -6,108 +6,6 @@ require __DIR__ . '/../vendor/autoload.php';
66
use BrushCSS\CLI\Application;
77

88
$app = new Application();
9-
$app->run($argv);
109

11-
12-
/*
13-
declare(strict_types=1);
14-
15-
use BrushCSS\JIT\JITEngine;
16-
use BrushCSS\JIT\ClassExtractor;
17-
use BrushCSS\JIT\Resolver;
18-
19-
require __DIR__ . '/../vendor/autoload.php';
20-
21-
$command = $argv[1] ?? 'help';
22-
23-
switch ($command) {
24-
25-
case 'build':
26-
echo "Building BrushCSS...\n";
27-
28-
$configFile = getcwd() . '/brushcss.php';
29-
30-
if (!file_exists($configFile)) {
31-
echo "Config file not found: brushcss.php\n";
32-
exit(1);
33-
}
34-
35-
$config = require $configFile;
36-
37-
$extractor = new ClassExtractor();
38-
$resolver = new Resolver();
39-
$jit = new JITEngine($resolver);
40-
41-
$viewDir = getcwd() . '/resources/views';
42-
43-
if (!is_dir($viewDir)) {
44-
echo "views directory not found\n";
45-
exit(1);
46-
}
47-
48-
$files = [];
49-
50-
foreach (new RecursiveIteratorIterator(
51-
new RecursiveDirectoryIterator($viewDir)
52-
) as $file) {
53-
if ($file->isFile() && preg_match('/\.(php|html)$/', $file)) {
54-
$files[] = $file->getPathname();
55-
}
56-
}
57-
58-
$classes = [];
59-
60-
foreach ($files as $file) {
61-
$classes = [
62-
...$classes,
63-
...$extractor->extractFromFile($file)
64-
];
65-
}
66-
67-
$css = $jit->buildFromClasses(array_unique($classes), $config);
68-
69-
$output = getcwd() . '/public/css/style.css';
70-
71-
if (!is_dir(dirname($output))) {
72-
mkdir(dirname($output), 0777, true);
73-
}
74-
75-
file_put_contents($output, $css);
76-
77-
echo "CSS generated at public/css/style.css ✅\n";
78-
break;
79-
80-
case 'init':
81-
$configPath = getcwd() . '/brushcss.php';
82-
83-
if (file_exists($configPath)) {
84-
echo "brushcss.php already exists\n";
85-
exit;
86-
}
87-
88-
file_put_contents($configPath, <<<PHP
89-
<?php
90-
91-
return [
92-
'colors' => [
93-
'blue-500' => '#3b82f6',
94-
'red-100' => '#fee2e2',
95-
],
96-
'spacing' => [
97-
'4' => '16px',
98-
'5' => '20px',
99-
],
100-
];
101-
PHP);
102-
103-
echo "brushcss.php created ✅\n";
104-
break;
105-
106-
default:
107-
echo "BrushCSS CLI\n";
108-
echo "Usage:\n";
109-
echo " brushcss build Build CSS\n";
110-
echo " brushcss init Create config\n";
111-
break;
112-
}
113-
*/
10+
$app->boot();
11+
$app->run();

brush.config.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
return [
4+
'input' => 'resources/css/main.css',
5+
'output' => 'public/style.css',
6+
7+
'scan' => [
8+
'resources/views'
9+
],
10+
11+
'theme' => [
12+
'spacing' => [
13+
'1' => '0.25rem',
14+
'2' => '0.5rem',
15+
'3' => '1rem',
16+
'4' => '1.5rem',
17+
'5' => '2rem',
18+
],
19+
'colors' => [
20+
'red' => [
21+
'500' => '#ef4444'
22+
],
23+
'blue' => [
24+
'500' => '#3b82f6'
25+
]
26+
],
27+
'breakpoints' => [
28+
'md' => '768px',
29+
'lg' => '1024px'
30+
]
31+
],
32+
33+
'minify' => true
34+
];

src/AST/Node.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BrushCSS\AST;
6+
7+
interface Node
8+
{
9+
public function toCss(): string;
10+
}

src/AST/RuleNode.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BrushCSS\AST;
4+
5+
class RuleNode implements Node
6+
{
7+
public function __construct(
8+
public string $selector,
9+
public array $rules
10+
) {}
11+
12+
public function toCss(): string
13+
{
14+
$body = '';
15+
foreach ($this->rules as $k => $v) {
16+
$body .= "$k:$v;";
17+
}
18+
return "{$this->selector}{{$body}}";
19+
}
20+
}

src/AST/VariantNode.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace BrushCSS\AST;
3+
4+
class VariantNode implements Node
5+
{
6+
public function __construct(
7+
public string $variant,
8+
public Node $child,
9+
public array $config
10+
) {}
11+
12+
public function toCss(): string
13+
{
14+
return match ($this->variant) {
15+
'hover' => $this->pseudo(':hover'),
16+
'focus' => $this->pseudo(':focus'),
17+
'md' => "@media (min-width: {$this->config['theme']['breakpoints']['md']}){" . $this->child->toCss() . "}",
18+
default => $this->child->toCss()
19+
};
20+
}
21+
22+
private function pseudo(string $pseudo): string
23+
{
24+
if ($this->child instanceof RuleNode) {
25+
return (new RuleNode(
26+
$this->child->selector . $pseudo,
27+
$this->child->rules
28+
))->toCss();
29+
}
30+
return $this->child->toCss();
31+
}
32+
}

src/CLI/Application.php

Lines changed: 28 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,46 @@
22

33
namespace BrushCSS\CLI;
44

5-
use BrushCSS\CLI\Commands\AddCommand;
5+
use Symfony\Component\Console\Application as SymfonyApplication;
6+
use BrushCSS\CLI\BuildCommand;
7+
use BrushCSS\Config\ConfigLoader;
68

7-
final class Application
9+
final class Application extends SymfonyApplication
810
{
9-
public function run(array $argv): void
10-
{
11-
$cmd = $argv[1] ?? null;
12-
13-
match ($cmd) {
14-
15-
'add' => (new AddCommand())
16-
->handle($argv[2] ?? ''),
17-
18-
'build' => $this->build(),
19-
20-
default => $this->help()
21-
};
22-
}
11+
private array $config = [];
2312

24-
private function builds(): void
13+
public function __construct()
2514
{
26-
echo "🔍 Scanning files...\n";
27-
28-
$paths = ['examples']; // change to 'views' in real app
29-
30-
$extractor = new \BrushCSS\JIT\ClassExtractor();
31-
$resolver = new \BrushCSS\JIT\Resolver();
32-
$engine = new \BrushCSS\JIT\JITEngine($resolver);
33-
34-
$classes = [];
35-
36-
foreach ($paths as $path) {
37-
foreach (glob($path . '/*.php') as $file) {
38-
$content = file_get_contents($file);
39-
40-
$found = $extractor->extract($content);
41-
42-
echo "📄 {$file}" . implode(', ', $found) . "\n";
43-
44-
$classes = array_merge($classes, $found);
45-
}
46-
}
47-
48-
$classes = array_unique($classes);
49-
50-
echo "⚙️ Generating CSS...\n";
51-
52-
$css = $engine->build($classes);
53-
54-
if (!is_dir('public')) {
55-
mkdir('public', 777, true);
15+
parent::__construct('BrushCSS', '1.0.0');
5616
}
5717

58-
file_put_contents('public/style.css', $css);
59-
60-
echo "✅ CSS written to public/style.css\n";
61-
echo "Building CSS...\n";
62-
}
63-
64-
private function help(): void
18+
// -----------------------------
19+
// BOOTSTRAP SYSTEM
20+
// -----------------------------
21+
public function boot(): void
6522
{
66-
echo "brushcss add <plugin>\nbrushcss build\n";
23+
$this->loadConfig();
24+
$this->registerCommands();
6725
}
6826

69-
private function build(): void
70-
{
71-
echo "🚀 Build started\n";
72-
73-
$paths = ['examples'];
74-
75-
$extractor = new \BrushCSS\JIT\ClassExtractor();
76-
$resolver = new \BrushCSS\JIT\Resolver();
77-
$engine = new \BrushCSS\JIT\JITEngine($resolver);
78-
79-
$classes = [];
80-
81-
foreach ($paths as $path) {
82-
83-
echo "📂 Scanning: {$path}\n";
84-
85-
$files = glob($path . '/*.php');
86-
87-
if (!$files) {
88-
echo "❌ No files found in {$path}\n";
89-
}
90-
91-
foreach ($files as $file) {
92-
93-
echo "📄 Reading: {$file}\n";
94-
95-
$content = file_get_contents($file);
96-
97-
if (!$content) {
98-
echo "❌ Empty file: {$file}\n";
99-
}
100-
101-
$found = $extractor->extract($content);
102-
103-
echo "🎯 Classes: " . json_encode($found) . "\n";
104-
105-
$classes = array_merge($classes, $found);
106-
}
27+
// -----------------------------
28+
// CONFIG LOADING
29+
// -----------------------------
30+
private function loadConfig(): void
31+
{
32+
$this->config = ConfigLoader::load();
10733
}
10834

109-
$classes = array_unique($classes);
110-
111-
echo "📦 Final classes: " . json_encode($classes) . "\n";
112-
113-
$css = $engine->build($classes);
114-
115-
echo "🧾 Generated CSS:\n$css\n";
116-
117-
if (!is_dir('public')) {
118-
echo "📁 Creating public directory...\n";
119-
mkdir('public', 0777, true);
35+
public function config(): array
36+
{
37+
return $this->config;
12038
}
12139

122-
$result = file_put_contents('public/style.css', $css);
123-
124-
if ($result === false) {
125-
echo "❌ Failed to write CSS file\n";
126-
} else {
127-
echo "✅ CSS written to public/style.css\n";
40+
// -----------------------------
41+
// COMMAND REGISTRATION
42+
// -----------------------------
43+
private function registerCommands(): void
44+
{
45+
$this->add(new BuildCommand());
12846
}
12947
}
130-
}

0 commit comments

Comments
 (0)