diff --git a/.idea/php.xml b/.idea/php.xml index a87bd36..dec4919 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,11 +1,26 @@ + + + + + + - + + + + + \ No newline at end of file diff --git a/src/ncc/Abstracts/BuiltinRemoteSourceType.php b/src/ncc/Abstracts/BuiltinRemoteSourceType.php new file mode 100644 index 0000000..4f3eeb3 --- /dev/null +++ b/src/ncc/Abstracts/BuiltinRemoteSourceType.php @@ -0,0 +1,17 @@ +getSources(); + + if(count($sources) == 0) + { + Console::out('No remote sources defined.', 1); + return; + } + + Console::out('Remote sources:', 1); + foreach($sources as $source) + { + Console::out(' - ' . $source->Name . ' (' . $source->Host . ')', 1); + } + + Console::out('Total: ' . count($sources), 1); + } + + /** + * @param $args + * @return void + */ + public static function addEntry($args): void + { + if(Resolver::resolveScope() !== Scopes::System) + { + Console::outError('Insufficient permissions to add entry.', true, 1); + return; + } + + $name = $args['name'] ?? null; + $type = $args['type'] ?? null; + $host = $args['host'] ?? null; + $ssl = $args['ssl'] ?? null; + + if($name == null) + { + Console::outError(sprintf('Missing required argument \'%s\'.', 'name'), true, 1); + return; + } + + if($type == null) + { + Console::outError(sprintf('Missing required argument \'%s\'.', 'type'), true, 1); + return; + } + + if($host == null) + { + Console::outError(sprintf('Missing required argument \'%s\'.', 'host'), true, 1); + return; + } + + if($ssl !== null) + { + $ssl = Functions::cbool($ssl); + } + + $source_manager = new RemoteSourcesManager(); + $source = new DefinedRemoteSource(); + $source->Name = $name; + $source->Type = $type; + $source->Host = $host; + $source->SSL = $ssl; + + if(!$source_manager->addRemoteSource($source)) + { + Console::outError(sprintf('Cannot add entry \'%s\', it already exists', $name), true, 1); + return; + } + + try + { + $source_manager->save(); + } + catch (IOException $e) + { + Console::outException('Cannot save remote sources file.', $e, 1); + return; + } + + Console::out(sprintf('Entry \'%s\' added successfully.', $name)); + } + + /** + * Removes an existing entry from the vault. + * + * @param $args + * @return void + */ + private static function removeEntry($args): void + { + $ResolvedScope = Resolver::resolveScope(); + + if($ResolvedScope !== Scopes::System) + Console::outError('Insufficient permissions to remove entries'); + + $name = $args['name'] ?? null; + + if($name == null) + { + Console::outError(sprintf('Missing required argument \'%s\'.', 'name'), true, 1); + return; + } + + $source_manager = new RemoteSourcesManager(); + + if(!$source_manager->deleteRemoteSource($name)) + { + Console::outError(sprintf('Cannot remove entry \'%s\', it does not exist', $name), true, 1); + return; + } + + try + { + $source_manager->save(); + } + catch (IOException $e) + { + Console::outException('Cannot save remote sources file.', $e, 1); + return; + + } + Console::out(sprintf('Entry \'%s\' removed successfully.', $name)); + } + + /** + * Displays the main options section + * + * @return void + */ + private static function displayOptions(): void + { + Console::out('Usage: ncc sources {command} [options]'); + Console::out('Options:'); + Console::outHelpSections([ + new CliHelpSection(['help'], 'Displays this help menu about the sources command'), + new CliHelpSection(['add'], 'Adds a new entry to the list of remote sources (See below)'), + new CliHelpSection(['remove', '--name'], 'Removes an entry from the list'), + new CliHelpSection(['list'], 'Lists all entries defined as remote sources'), + ]); + Console::out((string)null); + + } + } \ No newline at end of file diff --git a/src/ncc/Classes/ComposerExtension/ComposerSource.php b/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php similarity index 99% rename from src/ncc/Classes/ComposerExtension/ComposerSource.php rename to src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php index 31f3d9b..8fbd154 100644 --- a/src/ncc/Classes/ComposerExtension/ComposerSource.php +++ b/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php @@ -30,7 +30,7 @@ use ncc\Exceptions\UnsupportedCompilerExtensionException; use ncc\Exceptions\UnsupportedRunnerException; use ncc\Exceptions\UserAbortedOperationException; - use ncc\Interfaces\RemoteSourceInterface; + use ncc\Interfaces\ServiceSourceInterface; use ncc\Managers\ProjectManager; use ncc\Objects\ComposerLock; use ncc\Objects\ProjectConfiguration; @@ -47,7 +47,7 @@ use ncc\Utilities\RuntimeCache; use SplFileInfo; - class ComposerSource implements RemoteSourceInterface + class ComposerSourceBuiltin implements ServiceSourceInterface { /** * Attempts to acquire the package from the composer repository and @@ -134,7 +134,7 @@ { $package_path = $base_dir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->Name; // Generate the package configuration - $project_configuration = ComposerSource::generateProjectConfiguration($package->Name, $composer_lock); + $project_configuration = ComposerSourceBuiltin::generateProjectConfiguration($package->Name, $composer_lock); // Process the source files if ($package->Autoload !== null) diff --git a/src/ncc/Exceptions/GitCheckoutException.php b/src/ncc/Exceptions/GitCheckoutException.php new file mode 100644 index 0000000..cd84341 --- /dev/null +++ b/src/ncc/Exceptions/GitCheckoutException.php @@ -0,0 +1,19 @@ +message = $message; + } + } diff --git a/src/ncc/Exceptions/GitCloneException.php b/src/ncc/Exceptions/GitCloneException.php new file mode 100644 index 0000000..a9cc009 --- /dev/null +++ b/src/ncc/Exceptions/GitCloneException.php @@ -0,0 +1,20 @@ +message = $message; + } + } \ No newline at end of file diff --git a/src/ncc/Exceptions/GitlabServiceException.php b/src/ncc/Exceptions/GitlabServiceException.php new file mode 100644 index 0000000..202ee1e --- /dev/null +++ b/src/ncc/Exceptions/GitlabServiceException.php @@ -0,0 +1,20 @@ +message = $message; + } + } \ No newline at end of file diff --git a/src/ncc/Objects/DefinedRemoteSource.php b/src/ncc/Objects/DefinedRemoteSource.php new file mode 100644 index 0000000..7177e3c --- /dev/null +++ b/src/ncc/Objects/DefinedRemoteSource.php @@ -0,0 +1,75 @@ + $this->Name, + ($bytecode ? Functions::cbc('type') : 'type') => $this->Type, + ($bytecode ? Functions::cbc('host') : 'host') => $this->Host, + ($bytecode ? Functions::cbc('ssl') : 'ssl') => $this->SSL + ]; + } + + /** + * Constructs object from an array representation. + * + * @param array $data + * @return static + */ + public static function fromArray(array $data): self + { + $definedRemoteSource = new self(); + + $definedRemoteSource->Name = Functions::array_bc($data, 'name'); + $definedRemoteSource->Type = Functions::array_bc($data, 'type'); + $definedRemoteSource->Host = Functions::array_bc($data, 'host'); + $definedRemoteSource->SSL = Functions::array_bc($data, 'ssl'); + + return $definedRemoteSource; + } + } \ No newline at end of file diff --git a/src/ncc/Objects/HttpRequest.php b/src/ncc/Objects/HttpRequest.php index fc3760f..9b28347 100644 --- a/src/ncc/Objects/HttpRequest.php +++ b/src/ncc/Objects/HttpRequest.php @@ -43,6 +43,13 @@ */ public $Authentication; + /** + * An array of curl options to set + * + * @var array + */ + public $Options; + public function __construct() { $this->Type = HttpRequestType::GET; @@ -50,6 +57,7 @@ $this->Headers = [ 'User-Agent: ncc/1.0' ]; + $this->Options = []; } /** @@ -64,7 +72,8 @@ 'url' => $this->Url, 'headers' => $this->Headers, 'body' => $this->Body, - 'authentication' => $this->Authentication + 'authentication' => $this->Authentication, + 'options' => $this->Options ]; } @@ -82,6 +91,7 @@ $request->Headers = $data['headers']; $request->Body = $data['body']; $request->Authentication = $data['authentication']; + $request->Options = $data['options']; return $request; } } \ No newline at end of file diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index 13e6413..7087fd7 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -484,4 +484,26 @@ } return $randomString; } + + /** + * Returns a path to a temporary directory + * + * @param bool $create + * @param bool $set_as_tmp + * @return string + * @throws InvalidScopeException + */ + public static function getTmpDir(bool $create=true, bool $set_as_tmp=true): string + { + $path = PathFinder::getCachePath() . DIRECTORY_SEPARATOR . self::randomString(16); + if($create) + { + $filesystem = new Filesystem(); + /** @noinspection PhpRedundantOptionalArgumentInspection */ + $filesystem->mkdir($path, 0777); + } + if($set_as_tmp) + RuntimeCache::setFileAsTemporary($path); + return $path; + } } \ No newline at end of file diff --git a/src/ncc/Utilities/HttpClient.php b/src/ncc/Utilities/HttpClient.php index 5abb381..ebded8e 100644 --- a/src/ncc/Utilities/HttpClient.php +++ b/src/ncc/Utilities/HttpClient.php @@ -28,6 +28,9 @@ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + foreach($httpRequest->Options as $option => $value) + curl_setopt($curl, $option, $value); + switch($httpRequest->Type) { case HttpRequestType::GET: diff --git a/src/ncc/Utilities/PathFinder.php b/src/ncc/Utilities/PathFinder.php index 340eb6f..8872291 100644 --- a/src/ncc/Utilities/PathFinder.php +++ b/src/ncc/Utilities/PathFinder.php @@ -173,6 +173,17 @@ return self::getDataPath($scope, $win32) . DIRECTORY_SEPARATOR . 'package.lck'; } + /** + * @param string $scope + * @param bool $win32 + * @return string + * @throws InvalidScopeException + */ + public static function getRemouteSources(string $scope=Scopes::Auto, bool $win32=false): string + { + return self::getDataPath($scope, $win32) . DIRECTORY_SEPARATOR . 'sources'; + } + /** * Returns an array of all the package lock files the current user can access (For global-cross referencing) *