Skip to content

Commit

Permalink
add builder for build html, css
Browse files Browse the repository at this point in the history
add builder for convert php to html, scss to css with minified
  • Loading branch information
ve3 committed Jul 10, 2017
1 parent 53dd77b commit e7b16a2
Show file tree
Hide file tree
Showing 447 changed files with 37,459 additions and 90 deletions.
122 changes: 122 additions & 0 deletions _builder/app/MainApp.php
@@ -0,0 +1,122 @@
<?php
/**
* @license http://opensource.org/licenses/MIT MIT
*/


namespace App;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class MainApp extends Command
{


protected $confirmed = false;


protected $fullTargetPath;


protected function configure()
{
$this->setName('build')
->setDescription('Build the source into repository folder where it is ready to commit.')
->setHelp('Run this command to copy and build source files into exists repository folder where it is ready to commit. Example: php _builder/build.php build ../your-repo-folder');

$this->addArgument('target', InputArgument::REQUIRED, 'Set the target repository folder that files will be copied to. Use full path or relative path.');
}// configure


protected function interact(InputInterface $Input, OutputInterface $Output)
{
$this->fullTargetPath = realpath($Input->getArgument('target'));

$Io = new SymfonyStyle($Input, $Output);

if (empty($this->fullTargetPath)) {
$Io->error('Unable to locate target folder, please check your target argument and try again.');
unset($Io);
return;
}

$Io->block('Target: ' . $this->fullTargetPath);
$answer = $Io->confirm('Is this the correct target location?', false);

if ($answer !== true) {
return;
} else {
$this->confirmed = true;
}
}// interact


protected function execute(InputInterface $Input, OutputInterface $Output)
{
if ($this->confirmed !== true) {
return;
}

$Io = new SymfonyStyle($Input, $Output);

// clear target folder. --------------------------------------------
$Io->title('Clear target folder');
$ClearTarget = new SubApp\ClearTarget();
$result = $ClearTarget->run($this->fullTargetPath, $Input, $Output);
unset($ClearTarget);
$Output->writeln('');
$Output->writeln('');

if (isset($result) && $result === true) {
// copy source to target. -------------------------------------
$Io->title('Copy source folder to target');
$CopySource = new SubApp\CopySource();
$result2 = $CopySource->run($this->fullTargetPath, $Input, $Output);
unset($CopySource);
} else {
$Io->error('Failed to clear target folder.');
}
unset($result);
$Output->writeln('');
$Output->writeln('');

if (isset($result2) && $result2 === true) {
// convert php source file to html ---------------------------
$Io->title('Convert PHP source files to HTML');
$ConvertPhp = new SubApp\ConvertPhp();
$result3 = $ConvertPhp->run($this->fullTargetPath, $Input, $Output);
unset($ConvertPhp);
} elseif (isset($result2)) {
$Io->error('Failed to copy source folder.');
}
unset($result2);
$Output->writeln('');
$Output->writeln('');

if (isset($result3) && $result3 === true) {
// compile scss to css ----------------------------------------
$Io->title('Compile SCSS files to CSS');
$CompileScss = new SubApp\CompileScss();
$result4 = $CompileScss->run($this->fullTargetPath, $Input, $Output);
unset($CompileScss);
} elseif (isset($result3)) {
$Io->error('Failed to convert PHP to HTML files.');
}
unset($result3);
$Output->writeln('');
$Output->writeln('');

if (isset($result4) && $result4 === true) {
$Io->success('Completed, you are ready to commit the repository.');
} else {
$Io->error('Failed to compile SCSS.');
}
unset($result4);
}// execute


}
72 changes: 72 additions & 0 deletions _builder/app/SubApp/ClearTarget.php
@@ -0,0 +1,72 @@
<?php
/**
* @license http://opensource.org/licenses/MIT MIT
*/


namespace App\SubApp;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ClearTarget
{


/**
* Clear the target folder except some folder or file.
*
* @param string $targetDir Target folder.
* @param InputInterface $Input
* @param OutputInterface $Output
* @return boolean Return true on success.
*/
public function run($targetDir, InputInterface $Input, OutputInterface $Output)
{
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($targetDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

$success = true;

$Progress = new ProgressBar($Output, iterator_count($files));
$Progress->start();

if (is_array($files) || is_object($files)) {
foreach ($files as $fileinfo) {
if (stristr($fileinfo->getRealPath(), '.git/') !== false || stristr($fileinfo->getRealPath(), '.git\\') !== false) {
continue;
} elseif ($fileinfo->getFilename() == '.git') {
continue;
}

if ($fileinfo->isWritable()) {
if ($fileinfo->isDir()) {
$result = rmdir($fileinfo->getRealPath());
if ($result !== true) {
$success = false;
}
unset($result);
} else {
$result = unlink($fileinfo->getRealPath());
if ($result !== true) {
$success = false;
}
unset($result);
}
$Progress->advance();
}
}// endforeach;
}

unset($files);
$Progress->finish();
unset($Progress);

return $success;
}// run


}
169 changes: 169 additions & 0 deletions _builder/app/SubApp/CompileScss.php
@@ -0,0 +1,169 @@
<?php
/**
* @license http://opensource.org/licenses/MIT MIT
*/


namespace App\SubApp;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CompileScss
{


/**
* Count files
*
* @param string $scssFolder
*/
private function countFiles($scssFolder)
{
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($scssFolder, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
$count = 0;

if (is_array($files) || is_object($files)) {
foreach ($files as $fileinfo) {
if (stristr($fileinfo->getFileName(), '_') === false && $fileinfo->isFile()) {
$count++;
}
}// endforeach;
}

unset($files);
return $count;
}// countFiles


/**
* Count sass cache.
*
* @param string $sassCache
*/
private function countSassCache($sassCache)
{
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($sassCache, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

return iterator_count($files);
}// countSassCache


/**
* recursively delete file and folder including itself.
*
* @param string $targetDir
* @param ProgressBar $Progress
*/
private function recursiveDelete($targetDir, ProgressBar $Progress)
{
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($targetDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);


if (is_array($files) || is_object($files)) {
foreach ($files as $fileinfo) {
if ($fileinfo->isWritable()) {
if ($fileinfo->isDir()) {
$result = rmdir($fileinfo->getRealPath());
if ($result !== true) {
$success = false;
}
unset($result);
} else {
$result = unlink($fileinfo->getRealPath());
if ($result !== true) {
$success = false;
}
unset($result);
}
$Progress->advance();
}
}// endforeach;
}

rmdir($targetDir);
$Progress->advance();
}// recursiveDelete


/**
* Compile scss to css files.
*
* @param string $targetDir
* @param InputInterface $Input
* @param OutputInterface $Output
* @return boolean Return true on success.
*/
public function run($targetDir, InputInterface $Input, OutputInterface $Output)
{
$scssFolder = $targetDir . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'scss';
$cssFolder = $targetDir . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'css';

$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($scssFolder, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

$Progress = new ProgressBar($Output, ($this->countFiles($scssFolder) + $this->countSassCache($targetDir . DIRECTORY_SEPARATOR . '.sass-cache')));
$Progress->start();

if (is_array($files) || is_object($files)) {
foreach ($files as $fileinfo) {
if (stristr($fileinfo->getFileName(), '_') === false && $fileinfo->isFile()) {
$fileExt = $fileinfo->getExtension();
$fileName = $fileinfo->getBasename('.' . $fileinfo->getExtension());

$relativeParentFolder = str_replace($scssFolder, '', $fileinfo->getPath());// always prepend with slash or back slash if there is value but not append with slash or back slash.

if ($relativeParentFolder != null && !is_dir($cssFolder . $relativeParentFolder)) {
// if no target folder in css, create them.
$old = umask(0);
@mkdir($cssFolder . $relativeParentFolder, 0777, true);
umask($old);
unset($old);
}

// compile .scss to .css
exec(
'sass'
. ' ' . $fileinfo->getRealPath() . ' ' . $cssFolder . $relativeParentFolder . DIRECTORY_SEPARATOR . $fileName . '.css'
. ' --style="expanded"'
. ' --force'
);

// also compile minified .css
exec(
'sass'
. ' ' . $fileinfo->getRealPath() . ' ' . $cssFolder . $relativeParentFolder . DIRECTORY_SEPARATOR . $fileName . '.min.css'
. ' --style="compressed"'
. ' --force'
);

$Progress->advance();

unset($fileExt, $fileName);
}
}// endforeach;
}

// delete the ".sass-cache" folder.
$this->recursiveDelete($targetDir . DIRECTORY_SEPARATOR . '.sass-cache', $Progress);

$Progress->finish();
unset($Progress);

return true;
}// run


}

0 comments on commit e7b16a2

Please sign in to comment.