Added prepare method for exphp

This commit is contained in:
Netkas 2022-10-22 09:06:35 -04:00
parent 9e94c6ace3
commit c3f6c482f3
6 changed files with 199 additions and 37 deletions

View file

@ -12,5 +12,5 @@
/**
* The current version of the package structure file format
*/
const PackageStructureVersion = '1.0.0';
const PackageStructureVersion = '1.0';
}

139
src/ncc/CLI/BuildMenu.php Normal file
View file

@ -0,0 +1,139 @@
<?php
namespace ncc\CLI;
use ncc\Abstracts\CompilerExtensions;
use ncc\Abstracts\Options\BuildConfigurationValues;
use ncc\Classes\PhpExtension\Compiler;
use ncc\Exceptions\BuildConfigurationNotFoundException;
use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\MalformedJsonException;
use ncc\Exceptions\PackagePreparationFailedException;
use ncc\Interfaces\CompilerInterface;
use ncc\Objects\CliHelpSection;
use ncc\Objects\ProjectConfiguration;
use ncc\Utilities\Console;
class BuildMenu
{
/**
* Displays the main help menu
*
* @param $args
* @return void
*/
public static function start($args): void
{
if(isset($args['help']))
{
self::displayOptions();
exit(0);
}
self::build($args);
exit(0);
}
/**
* Builds the current project
*
* @param $args
* @return void
*/
private static function build($args): void
{
$project_path = getcwd() . DIRECTORY_SEPARATOR . 'project.json';
try
{
$ProjectConfiguration = ProjectConfiguration::fromFile($project_path);
}
catch (FileNotFoundException $e)
{
Console::outException('Cannot find the file \'project.json\'', $e, 1);
return;
}
catch (MalformedJsonException $e)
{
Console::outException('The file \'project.json\' contains malformed json data, please verify the syntax of the file', $e, 1);
return;
}
// Select the correct compiler for the specified extension
switch(strtolower($ProjectConfiguration->Project->Compiler->Extension))
{
case CompilerExtensions::PHP:
/** @var CompilerInterface $Compiler */
$Compiler = new Compiler($ProjectConfiguration);
break;
default:
Console::outError('The extension '. $ProjectConfiguration->Project->Compiler->Extension . ' is not supported', true, 1);
return;
}
$build_configuration = BuildConfigurationValues::DefaultConfiguration;
if(isset($args['config']))
{
$build_configuration = $args['config'];
}
try
{
$ProjectConfiguration->Build->getBuildConfiguration($build_configuration);
}
catch (BuildConfigurationNotFoundException $e)
{
Console::outException('The build configuration \'' . $build_configuration . '\' does not exist in the project configuration', $e, 1);
return;
}
Console::out(
' ===== BUILD INFO ===== ' . PHP_EOL .
' Package Name: ' . $ProjectConfiguration->Assembly->Package . PHP_EOL .
' Version: ' . $ProjectConfiguration->Assembly->Version . PHP_EOL .
' Compiler Extension: ' . $ProjectConfiguration->Project->Compiler->Extension . PHP_EOL .
' Compiler Version: ' . $ProjectConfiguration->Project->Compiler->MaximumVersion . ' - ' . $ProjectConfiguration->Project->Compiler->MinimumVersion . PHP_EOL .
' Build Configuration: ' . $build_configuration . PHP_EOL
);
Console::out('Preparing package');
try
{
$Compiler->prepare([], getcwd(), $build_configuration);
}
catch (PackagePreparationFailedException $e)
{
Console::outException('The package preparation process failed', $e, 1);
return;
}
exit(0);
}
/**
* Displays the main options section
*
* @return void
*/
private static function displayOptions(): void
{
$options = [
new CliHelpSection(['help'], 'Displays this help menu about the value command'),
new CliHelpSection(['build'], 'Builds the current project using the default build configuration'),
new CliHelpSection(['build', '--config'], 'Builds the current project with a specified build configuration')
];
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
Console::out('Usage: ncc build [options]');
Console::out('Options:' . PHP_EOL);
foreach($options as $option)
{
Console::out(' ' . $option->toString($options_padding));
}
}
}

View file

@ -74,7 +74,7 @@
}
catch(Exception $e)
{
Console::out('Error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')');
Console::outException('Error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')', $e, 1);
exit(1);
}

View file

@ -5,11 +5,10 @@
namespace ncc\Classes\PhpExtension;
use FilesystemIterator;
use ncc\Abstracts\CompilerOptions;
use ncc\Abstracts\ComponentFileExtensions;
use ncc\Abstracts\Options\BuildConfigurationValues;
use ncc\Abstracts\Versions;
use ncc\Exceptions\BuildConfigurationNotFoundException;
use ncc\Exceptions\BuildException;
use ncc\Exceptions\PackagePreparationFailedException;
use ncc\Interfaces\CompilerInterface;
use ncc\ncc;
@ -18,6 +17,7 @@
use ncc\ThirdParty\theseer\DirectoryScanner\DirectoryScanner;
use ncc\ThirdParty\theseer\DirectoryScanner\Exception;
use ncc\Utilities\Console;
use ncc\Utilities\Functions;
use SplFileInfo;
class Compiler implements CompilerInterface
@ -45,12 +45,12 @@
* This function must be called before calling the build function, otherwise the operation will fail
*
* @param array $options
* @param string $src
* @param string $path
* @param string $build_configuration
* @return void
* @throws PackagePreparationFailedException
*/
public function prepare(array $options, string $src, string $build_configuration=BuildConfigurationValues::DefaultConfiguration)
public function prepare(array $options, string $path, string $build_configuration=BuildConfigurationValues::DefaultConfiguration): void
{
// Auto-select the default build configuration
if($build_configuration == BuildConfigurationValues::DefaultConfiguration)
@ -79,9 +79,8 @@
if(ncc::cliMode())
{
Console::out('Building autoloader');
Console::out('Scanning project files');
Console::out('theseer\DirectoryScanner - Copyright (c) 2009-2014 Arne Blankerts <arne@blankerts.de> All rights reserved.');
Console::out('theseer\Autoload - Copyright (c) 2010-2016 Arne Blankerts <arne@blankerts.de> and Contributors All rights reserved.');
}
// First scan the project files and create a file struct.
@ -89,8 +88,7 @@
try
{
$DirectoryScanner->unsetFlag(FilesystemIterator::
FOLLOW_SYMLINKS);
$DirectoryScanner->unsetFlag(FilesystemIterator::FOLLOW_SYMLINKS);
}
catch (Exception $e)
{
@ -101,61 +99,84 @@
$DirectoryScanner->setIncludes(ComponentFileExtensions::Php);
$DirectoryScanner->setExcludes($selected_build_configuration->ExcludeFiles);
// Append trailing slash to the end of the path if it's not already there
if(substr($path, -1) !== DIRECTORY_SEPARATOR)
{
$path .= DIRECTORY_SEPARATOR;
}
$source_path = $path . $this->project->Build->SourcePath;
// Scan for components first.
Console::out('Scanning for components...', false);
Console::out('Scanning for components... ', false);
/** @var SplFileInfo $item */
foreach($DirectoryScanner($src, true) as $item)
foreach($DirectoryScanner($source_path, true) as $item)
{
// Ignore directories, they're not important. :-)
if(is_dir($item->getPath()))
if(is_dir($item->getPathName()))
continue;
$Component = new Package\Component();
$Component->Name = $item->getPath();
$Component->Name = Functions::removeBasename($item->getPathname(), $path);
$this->package->Components[] = $Component;
}
if(count($this->package->Components) > 0)
if(ncc::cliMode())
{
Console::out(count($this->package->Components) . ' component(s) found');
}
else
{
Console::out('No components found');
if(count($this->package->Components) > 0)
{
Console::out(count($this->package->Components) . ' component(s) found');
}
else
{
Console::out('No components found');
}
}
// Now scan for resources
Console::out('Scanning for resources...', false);
// Clear previous excludes and includes
$DirectoryScanner->setExcludes([]);
$DirectoryScanner->setIncludes([]);
// Ignore component files
$DirectoryScanner->setExcludes(array_merge(
$selected_build_configuration->ExcludeFiles, ComponentFileExtensions::Php
));
// Scan for components first.
Console::out('Scanning for resources... ', false);
/** @var SplFileInfo $item */
foreach($DirectoryScanner($src, true) as $item)
foreach($DirectoryScanner($source_path, true) as $item)
{
// Ignore directories, they're not important. :-)
if(is_dir($item->getPath()))
if(is_dir($item->getPathName()))
continue;
$Resource = new Package\Resource();
$Resource->Name = $item->getPath();
$Resource->Name = Functions::removeBasename($item->getPathname(), $path);
$this->package->Resources[] = $Resource;
}
if(count($this->package->Resources) > 0)
if(ncc::cliMode())
{
Console::out(count($this->package->Resources) . ' resources(s) found');
}
else
{
Console::out('No resources found');
if(count($this->package->Resources) > 0)
{
Console::out(count($this->package->Resources) . ' resources(s) found');
}
else
{
Console::out('No resources found');
}
}
}
public function build(array $options, string $src)
public function build(array $options, string $path): string
{
// TODO: Implement build() method.
if($this->package == null)
{
throw new BuildException('The prepare() method must be called before building the package');
}
// TODO: Implement build() method
}
}

View file

@ -80,6 +80,8 @@
public function __construct()
{
$this->MagicBytes = new MagicBytes();
$this->Header = new Header();
$this->Assembly = new Assembly();
$this->Components = [];
$this->Dependencies = [];
$this->Resources = [];

View file

@ -108,9 +108,9 @@
{
$ProjectConfigurationObject = new ProjectConfiguration();
$ProjectConfigurationObject->Project = Functions::array_bc($data, 'project');
$ProjectConfigurationObject->Assembly = Functions::array_bc($data, 'assembly');
$ProjectConfigurationObject->Build = Functions::array_bc($data, 'build');
$ProjectConfigurationObject->Project = Project::fromArray(Functions::array_bc($data, 'project'));
$ProjectConfigurationObject->Assembly = Assembly::fromArray(Functions::array_bc($data, 'assembly'));
$ProjectConfigurationObject->Build = Build::fromArray(Functions::array_bc($data, 'build'));
return $ProjectConfigurationObject;
}