Skip to content

Commit 7900032

Browse files
save progress
1 parent 60dd22b commit 7900032

16 files changed

+130
-452
lines changed

ScriptHandler.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Composer\Script\Event;
66
use EM\CssCompiler\Processor\Processor;
7-
use Leafo\ScssPhp\Formatter\Compact;
87

98
class ScriptHandler
109
{
@@ -19,63 +18,29 @@ public static function compileCSS(Event $event)
1918
$configs = $extras['css-compiler'];
2019

2120
if (!is_array($configs)) {
22-
throw new \InvalidArgumentException('The extra.css-compiler setting must be an array or a configuration object.');
21+
throw new \InvalidArgumentException('The extra.css-compiler setting must be an array of a configuration objects.');
2322
}
2423

2524
if (array_keys($configs) !== range(0, count($configs) - 1)) {
2625
$configs = [$configs];
2726
}
2827

2928
$processor = new Processor($event->getIO());
30-
exec('rm -rf '. __DIR__ .'/var/*');
29+
30+
exec('rm -rf ' . __DIR__ . '/var/*');
31+
3132
foreach ($configs as $config) {
3233
if (!is_array($config)) {
33-
throw new \InvalidArgumentException('The extra.css-compiler setting must be an array of configuration objects.');
34+
throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
3435
}
3536

36-
$processor->resetFiles();
3737
foreach ($config['input'] as $item => $value) {
38-
/**
39-
"input": [
40-
"src/assets/scss"
41-
],
42-
*/
43-
$processor->attachFiles(__DIR__ . "/$value");
44-
}
45-
46-
// $formatter =
47-
switch ($config['format'] ?? 'compact') {
48-
case 'compressed':
49-
case 'crunched':
50-
case 'expanded':
51-
case 'nested':
52-
case 'compact':
53-
$formatter = 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($config['format'] ?? 'compact');
54-
break;
55-
default:
56-
if (!is_array($config)) {
57-
throw new \InvalidArgumentException('there\'re avaliable options: if by default: compact');
58-
}
59-
60-
break;
38+
$processor->attachFiles(__DIR__ . "/{$value}", __DIR__ . "/{$config['output']}");
6139
}
6240

63-
$processor->processFiles($formatter);
64-
65-
$output = $processor->concatOutput();
66-
$outputPath = __DIR__ . "/var/${config['output']}";
67-
$outputDir = dirname($outputPath);
68-
if (!is_dir($outputDir)) {
69-
// if (file_exists($outputPath)) {
70-
// $outputDir = dirname($outputPath);
71-
if (!is_dir($dir = dirname($outputDir))) {
72-
// if (!is_dir($outputDir)) {
73-
mkdir($outputDir, 0755, true);
74-
// mkdir($outputDir, 0777);
75-
}
76-
}
77-
file_put_contents(__DIR__ .'/var/output.css', $output);
78-
file_put_contents($outputPath, $output);
41+
$processor->processFiles($config['format'] ?? 'compact');
7942
}
43+
44+
$processor->saveOutput();
8045
}
8146
}

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
"php": ">= 7.0.4",
2525
"leafo/lessphp": "^0.5",
2626
"leafo/scssphp": "0.6.3 as dev-master",
27-
"phpunit/phpunit": "^5.4",
2827
"leafo/scssphp-compass": "dev-master"
2928
},
29+
"require-dev": {
30+
"composer/composer": "^1.1",
31+
"phpunit/phpunit": "^5.4"
32+
},
3033
"minimum-stability": "dev",
3134
"scripts": {
3235
"post-update-cmd": "@custom-events",
@@ -38,17 +41,15 @@
3841
"config": {
3942
"bin-dir": "bin/"
4043
},
41-
"require-dev": {
42-
"composer/composer": "^1.1"
43-
},
4444
"extra": {
4545
"css-compiler": [
4646
{
4747
"format": "compact",
4848
"input": [
49-
"src/assets/scss"
49+
"tests/shared-fixtures/scss",
50+
"tests/shared-fixtures/sass"
5051
],
51-
"output": "web/assets/app.css"
52+
"output": "var/cache/assets/app.css"
5253
}
5354
]
5455
}

composer.phar

1.56 MB
Binary file not shown.

src/Container/File.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace EM\CssCompiler\Container;
44

5-
//use Symfony\Component\Yaml\Inline;
6-
//use Symfony\Component\Yaml\Parser;
7-
//use Symfony\Component\Yaml\Yaml;
5+
use EM\CssCompiler\Exception\FileException;
86

97
class File
108
{
@@ -32,15 +30,10 @@ class File
3230
*/
3331
private $type;
3432

35-
// public function __construct(string $sourcePath, string $outputPath, string $sourceContent, string $parsedContent, string $type)
36-
public function __construct(string $sourcePath)
33+
public function __construct(string $sourcePath, string $outputPath)
3734
{
3835
$this->setSourcePath($sourcePath);
39-
// $this->sourcePath = $sourcePath;
40-
// $this->outputPath = $outputPath;
41-
// $this->sourceContent = $sourceContent;
42-
// $this->parsedContent = $parsedContent;
43-
// $this->type = $type ?: $this->type;
36+
$this->outputPath = $outputPath;
4437
}
4538

4639
public function getSourcePath() : string
@@ -80,6 +73,13 @@ public function setSourceContent(string $content) : self
8073
return $this;
8174
}
8275

76+
public function setSourceContentFromSourcePath() : self
77+
{
78+
$this->sourceContent = $this->readSourceContentByPath();
79+
80+
return $this;
81+
}
82+
8383
public function getParsedContent() : string
8484
{
8585
return $this->parsedContent;
@@ -120,4 +120,13 @@ private function detectSourceTypeFromPath(string $path)
120120
$this->type = 'unknown';
121121
}
122122
}
123+
124+
private function readSourceContentByPath()
125+
{
126+
if (!file_exists($this->getSourcePath())) {
127+
throw new FileException("file: {$this->sourcePath} doesn't exists");
128+
}
129+
130+
return file_get_contents($this->getSourcePath());
131+
}
123132
}

src/Exception/CompilerException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EM\CssCompiler\Exception;
4+
5+
class CompilerException extends \Exception
6+
{
7+
}

src/Exception/FileException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EM\CssCompiler\Exception;
4+
5+
class FileException extends \Exception
6+
{
7+
}

src/Processor/Processor.php

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use Composer\IO\IOInterface;
66
use EM\CssCompiler\Container\File;
7-
use Leafo\ScssPhp\Compiler;
7+
use EM\CssCompiler\Exception\CompilerException;
8+
use Leafo\ScssPhp\Compiler as SASSCompiler;
9+
use lessc as LESSCompiler;
10+
use scss_compass as CompassCompiler;
811

912
class Processor
1013
{
1114
const TYPE_SCSS = 'scss';
15+
const TYPE_COMPASS = 'scss';
1216
const TYPE_SASS = 'sass';
1317
const TYPE_LESS = 'less';
1418
/**
@@ -19,69 +23,120 @@ class Processor
1923
* @var File[]
2024
*/
2125
private $files = [];
22-
// public function __construct(IOInterface $io)
23-
// {
24-
// $this->io = $io;
25-
// }
26+
27+
public function __construct(IOInterface $io)
28+
{
29+
$this->io = $io;
30+
}
2631

2732
public function resetFiles()
2833
{
2934
$this->files = [];
3035
}
3136

32-
public function attachFiles(string $path)
37+
public function attachFiles(string $inputPath, string $outputPath)
3338
{
34-
if (is_dir($path)) {
35-
$files = scandir($path);
39+
if (is_dir($inputPath)) {
40+
$files = scandir($inputPath);
3641
unset($files[0], $files[1]);
3742

3843
foreach ($files as $file) {
39-
$absolutePath = "$path/$file";
44+
$absolutePath = "$inputPath/$file";
4045
if (is_file($absolutePath)) {
41-
$this->files[] = new File($absolutePath);
46+
$this->files[] = new File($absolutePath, $outputPath);
4247
} else {
43-
$this->attachFiles($absolutePath);
48+
$this->attachFiles($absolutePath, $outputPath);
4449
}
4550
}
51+
} else if (is_file($inputPath)) {
52+
$this->files[] = new File($inputPath, $outputPath);
4653
} else {
47-
$this->files[] = new File($path);
54+
throw new \Exception('file doesn\'t exists');
4855
}
4956
}
5057

51-
public function concatOutput() : string
58+
public function concatOutput() : array
5259
{
53-
$output = '';
60+
$outputMap = [];
5461
foreach ($this->files as $file) {
55-
$output .= $file->getParsedContent();
62+
if (!isset($outputMap[$file->getOutputPath()])) {
63+
$outputMap[$file->getOutputPath()] = $file->getParsedContent();
64+
} else {
65+
$outputMap[$file->getOutputPath()] .= $file->getParsedContent();
66+
}
5667
}
5768

58-
return $output;
69+
return $outputMap;
70+
}
71+
72+
public function saveOutput()
73+
{
74+
foreach ($this->concatOutput() as $path => $content) {
75+
76+
$directory = dirname($path);
77+
if (!is_dir($dir = $directory)) {
78+
$this->io->write("creating directory: {$directory}");
79+
mkdir($directory, 0755, true);
80+
}
81+
82+
$this->io->write("creating output: {$path}");
83+
file_put_contents($path, $content);
84+
}
5985
}
6086

6187
public function processFiles(string $formatter)
6288
{
63-
$lessCompiler = new \lessc();
64-
// $sassCompiler = new Compiler();
65-
// $compass = new \scss_compass($sassCompiler);
66-
$sassCompiler = new Compiler();
89+
switch ($formatter) {
90+
case 'compressed':
91+
case 'crunched':
92+
case 'expanded':
93+
case 'nested':
94+
case 'compact':
95+
$formatter = 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter);
96+
break;
97+
default:
98+
throw new \InvalidArgumentException('available options are: xxx');
99+
}
100+
// -f=format Set the output format, includes "default", "compressed"
101+
102+
// switch ($formatter) {
103+
// case 'compressed':
104+
// case 'crunched':
105+
// case 'expanded':
106+
// case 'nested':
107+
// case 'compact':
108+
// $formatter = 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter);
109+
// break;
110+
// default:
111+
// throw new \InvalidArgumentException('available options are: xxx');
112+
// }
113+
114+
$lessCompiler = new LESSCompiler();
115+
// $lessCompiler->setFormatter()
116+
$sassCompiler = new SASSCompiler();
67117
$sassCompiler->setFormatter($formatter);
68-
new \scss_compass($sassCompiler);
118+
119+
/** attaches compass functionality to the SASS compiler */
120+
new CompassCompiler($sassCompiler);
69121

70122
foreach ($this->files as $file) {
71-
$content = file_get_contents($file->getSourcePath());
72-
$file->setSourceContent($content);
123+
$this->io->write("processing file: {$file->getSourcePath()}");
124+
$file->setSourceContentFromSourcePath();
73125

74126
switch ($file->getType()) {
75127
case static::TYPE_SCSS:
76-
$content = $sassCompiler->compile($content);
128+
$content = $sassCompiler->compile($file->getSourceContent());
77129
break;
78130
case static::TYPE_SASS:
79-
$content = $sassCompiler->compile($content);
131+
$content = $sassCompiler->compile($file->getSourceContent());
80132
break;
81133
case static::TYPE_LESS:
82-
$content = $lessCompiler->compile($content);
134+
$content = $lessCompiler->compile($file->getSourceContent());
83135
break;
136+
default:
137+
throw new CompilerException('unknown compiler');
84138
}
139+
85140
$file->setParsedContent($content);
86141
}
87142
}

src/assets/scss/partials/game-results.scss

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)