From 06e876bc03e8191c743cc6cd09d77feab28eab3a Mon Sep 17 00:00:00 2001 From: Netkas Date: Fri, 16 Dec 2022 12:20:45 -0500 Subject: [PATCH] Bug Fixes Part 1 --- src/default_repositories/github.json | 2 +- src/ncc/CLI/PackageManagerMenu.php | 19 ++- .../ComposerSourceBuiltin.php | 62 ++++++---- src/ncc/Classes/GitClient.php | 22 +--- .../Classes/GithubExtension/GithubService.php | 30 +++-- .../Classes/GitlabExtension/GitlabService.php | 16 ++- src/ncc/Classes/HttpClient.php | 24 ++-- .../Classes/NccExtension/PackageCompiler.php | 17 +-- src/ncc/Classes/PhpExtension/PhpCompiler.php | 116 ++++++++++-------- src/ncc/Objects/Vault/Entry.php | 1 - src/ncc/Utilities/Console.php | 2 +- src/ncc/Utilities/Functions.php | 94 ++++++++++---- src/ncc/Utilities/Resolver.php | 50 ++++---- 13 files changed, 260 insertions(+), 195 deletions(-) diff --git a/src/default_repositories/github.json b/src/default_repositories/github.json index f095339..f05cc8c 100644 --- a/src/default_repositories/github.json +++ b/src/default_repositories/github.json @@ -1,6 +1,6 @@ { "name": "github", "type": "github", - "host": "github.com", + "host": "api.github.com", "ssl": true } \ No newline at end of file diff --git a/src/ncc/CLI/PackageManagerMenu.php b/src/ncc/CLI/PackageManagerMenu.php index e873c57..5c7945d 100644 --- a/src/ncc/CLI/PackageManagerMenu.php +++ b/src/ncc/CLI/PackageManagerMenu.php @@ -32,7 +32,6 @@ { try { - Console::outException('Example Error', new Exception('test'), 1); self::installPackage($args); return; } @@ -195,15 +194,23 @@ // check if authentication is provided $entry_arg = ($args['auth'] ?? null); $credential_manager = new CredentialManager(); - $credential = $credential_manager->getVault()->getEntry($entry_arg); - if($credential == null) + if($entry_arg !== null) { - Console::outError(sprintf('Unknown credential entry \'%s\'', $entry_arg), true, 1); - return; + $credential = $credential_manager->getVault()->getEntry($entry_arg); + + if($credential == null) + { + Console::outError(sprintf('Unknown credential entry \'%s\'', $entry_arg), true, 1); + return; + } + } + else + { + $credential = null; } - if(!$credential->isCurrentlyDecrypted()) + if($credential !== null && !$credential->isCurrentlyDecrypted()) { // Try 3 times for($i = 0; $i < 3; $i++) diff --git a/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php b/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php index 85f94f5..e97d4b4 100644 --- a/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php +++ b/src/ncc/Classes/ComposerExtension/ComposerSourceBuiltin.php @@ -35,6 +35,7 @@ use ncc\ncc; use ncc\Objects\ComposerJson; use ncc\Objects\ComposerLock; + use ncc\Objects\Package; use ncc\Objects\ProjectConfiguration; use ncc\Objects\RemotePackageInput; use ncc\ThirdParty\Symfony\Filesystem\Filesystem; @@ -47,6 +48,7 @@ use ncc\Utilities\PathFinder; use ncc\Utilities\Resolver; use ncc\Utilities\RuntimeCache; + use ncc\Utilities\Validate; use SplFileInfo; class ComposerSourceBuiltin implements ServiceSourceInterface @@ -212,11 +214,10 @@ $built_package = $project_manager->build(); // Copy the project to the build directory - $out_path = $base_dir . DIRECTORY_SEPARATOR . 'build' . DIRECTORY_SEPARATOR . sprintf('%s=%s.ncc', $project_configuration->Assembly->Package, $project_configuration->Assembly->Version); + $out_path = $base_dir . DIRECTORY_SEPARATOR . 'build' . DIRECTORY_SEPARATOR . sprintf('%s.ncc', $project_configuration->Assembly->Package); $filesystem->copy($built_package, $out_path); $filesystem->remove($built_package); $built_packages[$project_configuration->Assembly->Package] = $out_path; - } return $built_packages; @@ -269,35 +270,50 @@ if($project_configuration->Assembly->Version == null || $project_configuration->Assembly->Version == '') $project_configuration->Assembly->Version = '1.0.0'; + if(!Validate::version($project_configuration->Assembly->Version)) + $project_configuration->Assembly->Version = Functions::convertToSemVer($project_configuration->Assembly->Version); + + if(!Validate::version($project_configuration->Assembly->Version)) + { + Console::outWarning(sprintf('Invalid version "%s" for package "%s", using "1.0.0" instead', $project_configuration->Assembly->Version, $project_configuration->Assembly->Name)); + $project_configuration->Assembly->Version = '1.0.0'; + } + + RuntimeCache::set(sprintf('composer_version_%s', $project_configuration->Assembly->Package), $project_configuration->Assembly->Version); + + $project_configuration->Assembly->UUID = Uuid::v1()->toRfc4122(); $project_configuration->Assembly->Package = self::toPackageName($composer_package->Name); // Add the update source $project_configuration->Project->UpdateSource = new ProjectConfiguration\UpdateSource(); - $project_configuration->Project->UpdateSource->Source = sprintf('%s=%s@composer', str_ireplace('\\', '/', $composer_package->Name), $composer_package->Version); + $project_configuration->Project->UpdateSource->Source = sprintf('%s@composer', str_ireplace('\\', '/', $composer_package->Name)); $project_configuration->Project->UpdateSource->Repository = null; // Process the dependencies - foreach ($composer_package->Require as $item) + if($composer_package->Require !== null && count($composer_package->Require) > 0) { - $package_name = self::toPackageName($item->PackageName); - $package_version = $composer_package->Version; - if ($package_version == null) + foreach ($composer_package->Require as $item) { - $package_version = '1.0.0'; + $package_name = self::toPackageName($item->PackageName); + $package_version = $composer_package->Version; + if ($package_version == null) + { + $package_version = '1.0.0'; + } + else + { + $package_version = Functions::parseVersion($package_version); + } + if ($package_name == null) + continue; + $dependency = new ProjectConfiguration\Dependency(); + $dependency->Name = $package_name; + $dependency->SourceType = DependencySourceType::Local; + $dependency->Version = $package_version; + $dependency->Source = $package_name . '.ncc'; + $project_configuration->Build->Dependencies[] = $dependency; } - else - { - $package_version = Functions::parseVersion($package_version); - } - if ($package_name == null) - continue; - $dependency = new ProjectConfiguration\Dependency(); - $dependency->Name = $package_name; - $dependency->SourceType = DependencySourceType::Local; - $dependency->Version = $package_version; - $dependency->Source = $package_name . '=' . $dependency->Version . '.ncc'; - $project_configuration->Build->Dependencies[] = $dependency; } // Create a build configuration @@ -563,7 +579,7 @@ private static function convertProject(string $package_path, ?ComposerJson $composer_package=null): ProjectConfiguration { if($composer_package == null) - ComposerJson::fromArray(Functions::loadJsonFile($package_path . DIRECTORY_SEPARATOR . 'composer.json')); + $composer_package = ComposerJson::fromArray(Functions::loadJsonFile($package_path . DIRECTORY_SEPARATOR . 'composer.json', Functions::FORCE_ARRAY)); $project_configuration = ComposerSourceBuiltin::generateProjectConfiguration($composer_package); $filesystem = new Filesystem(); @@ -658,10 +674,10 @@ } unset($file); } - - $project_configuration->toFile($package_path . DIRECTORY_SEPARATOR . 'project.json'); } + $project_configuration->toFile($package_path . DIRECTORY_SEPARATOR . 'project.json'); + // This part simply displays the package information to the command-line interface if(ncc::cliMode()) { diff --git a/src/ncc/Classes/GitClient.php b/src/ncc/Classes/GitClient.php index ec94a30..c7daca4 100644 --- a/src/ncc/Classes/GitClient.php +++ b/src/ncc/Classes/GitClient.php @@ -27,14 +27,7 @@ $process->setTimeout(3600); // 1 hour $process->run(function ($type, $buffer) { - if (Process::ERR === $type) - { - Console::outWarning($buffer); - } - else - { - Console::outVerbose($buffer); - } + Console::outVerbose($buffer); }); if (!$process->isSuccessful()) @@ -86,14 +79,7 @@ $process->setTimeout(3600); // 1 hour $process->run(function ($type, $buffer) { - if (Process::ERR === $type) - { - Console::outWarning($buffer); - } - else - { - Console::outVerbose($buffer); - } + Console::outVerbose($buffer); }); if (!$process->isSuccessful()) @@ -103,9 +89,7 @@ $process->run(function ($type, $buffer) { - if (Process::ERR === $type) - Console::outWarning($buffer); - + Console::outVerbose($buffer); }); if (!$process->isSuccessful()) diff --git a/src/ncc/Classes/GithubExtension/GithubService.php b/src/ncc/Classes/GithubExtension/GithubService.php index c5823a1..745eed2 100644 --- a/src/ncc/Classes/GithubExtension/GithubService.php +++ b/src/ncc/Classes/GithubExtension/GithubService.php @@ -153,10 +153,12 @@ $owner_f = str_ireplace(".", "%2F", $owner_f); $repository = urlencode($packageInput->Package); $httpRequest->Url = $protocol . '://' . $definedRemoteSource->Host . "/repos/$owner_f/$repository"; - $httpRequest->Type = HttpRequestType::POST; - $httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry); + $httpRequest->Type = HttpRequestType::GET; + $httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry, false); + $httpRequest->Headers[] = 'X-GitHub-Api-Version: 2022-11-28'; + $httpRequest->Headers[] = 'Accept: application/vnd.github+json'; - $response = HttpClient::request($httpRequest); + $response = HttpClient::request($httpRequest, true); if($response->StatusCode != 200) throw new GithubServiceException(sprintf('Failed to fetch releases for the given repository. Status code: %s', $response->StatusCode)); @@ -249,13 +251,21 @@ $latest_version = $version; } - return $releases[$latest_version]['package']; + $return = $releases[$latest_version]['package'] ?? null; + if($return === null) + throw new VersionNotFoundException('No releases found for the given repository.'); + + return $return; } if(!isset($releases[$packageInput->Version])) throw new VersionNotFoundException(sprintf('The given version "%s" does not exist.', $packageInput->Version)); - return $releases[$packageInput->Version]['package']; + $return = $releases[$packageInput->Version]['package'] ?? null; + if($return === null) + throw new VersionNotFoundException('No releases found for the given repository.'); + + return $return; } /** @@ -279,10 +289,12 @@ $owner_f = str_ireplace(".", "%2F", $owner_f); $repository = urlencode($packageInput->Package); $httpRequest->Url = $protocol . '://' . $definedRemoteSource->Host . "/repos/$owner_f/$repository/releases"; - $httpRequest->Type = HttpRequestType::POST; - $httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry); + $httpRequest->Type = HttpRequestType::GET; + $httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry, false); + $httpRequest->Headers[] = 'X-GitHub-Api-Version: 2022-11-28'; + $httpRequest->Headers[] = 'Accept: application/vnd.github+json'; - $response = HttpClient::request($httpRequest); + $response = HttpClient::request($httpRequest, true); if($response->StatusCode != 200) throw new GithubServiceException(sprintf('Failed to fetch releases for the given repository. Status code: %s', $response->StatusCode)); @@ -298,7 +310,7 @@ // Make the tag_name version friendly $release_version = str_replace('v', '', $release['tag_name']); $return[$release_version] = [ - 'url' => ($release['tarball_url'] ?? $release['zipball_url'] ?? null) + 'url' => ($release['zipball_url'] ?? $release['tarball_url'] ?? null) ]; if(isset($release['assets'])) diff --git a/src/ncc/Classes/GitlabExtension/GitlabService.php b/src/ncc/Classes/GitlabExtension/GitlabService.php index 74ee85f..b398d5d 100644 --- a/src/ncc/Classes/GitlabExtension/GitlabService.php +++ b/src/ncc/Classes/GitlabExtension/GitlabService.php @@ -118,11 +118,12 @@ $protocol = ($definedRemoteSource->SSL ? "https" : "http"); $owner_f = str_ireplace("/", "%2F", $packageInput->Vendor); $owner_f = str_ireplace(".", "%2F", $owner_f); - $repository = urlencode($packageInput->Package); - $httpRequest->Url = $protocol . '://' . $definedRemoteSource->Host . "/api/v4/projects/$owner_f%2F$repository"; + $project_f = str_ireplace("/", "%2F", $packageInput->Package); + $project_f = str_ireplace(".", "%2F", $project_f); + $httpRequest->Url = $protocol . '://' . $definedRemoteSource->Host . "/api/v4/projects/$owner_f%2F$project_f"; $httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry); - $response = HttpClient::request($httpRequest); + $response = HttpClient::request($httpRequest, true); if($response->StatusCode != 200) throw new GitlabServiceException(sprintf('Failed to fetch releases for the given repository. Status code: %s', $response->StatusCode)); @@ -210,10 +211,13 @@ $protocol = ($definedRemoteSource->SSL ? "https" : "http"); $owner_f = str_ireplace("/", "%2F", $owner); $owner_f = str_ireplace(".", "%2F", $owner_f); - $httpRequest->Url = $protocol . '://' . $definedRemoteSource->Host . "/api/v4/projects/$owner_f%2F$repository/releases"; + $repository_f = str_ireplace("/", "%2F", $repository); + $repository_f = str_ireplace(".", "%2F", $repository_f); + + $httpRequest->Url = $protocol . '://' . $definedRemoteSource->Host . "/api/v4/projects/$owner_f%2F$repository_f/releases"; $httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry); - $response = HttpClient::request($httpRequest); + $response = HttpClient::request($httpRequest, true); if($response->StatusCode != 200) throw new GitlabServiceException(sprintf('Failed to fetch releases for the given repository. Status code: %s', $response->StatusCode)); @@ -237,7 +241,7 @@ $return[$release_version] = $release['assets']['sources'][0]['url']; foreach($release['assets']['sources'] as $source) { - if($source['format'] == 'tar.gz') + if($source['format'] == 'zip') { $return[$release_version] = $source['url']; break; diff --git a/src/ncc/Classes/HttpClient.php b/src/ncc/Classes/HttpClient.php index 409ade0..73da79a 100644 --- a/src/ncc/Classes/HttpClient.php +++ b/src/ncc/Classes/HttpClient.php @@ -206,19 +206,19 @@ * @param string $headers * @return array */ - private static function parseHeaders(string $headers): array + private static function parseHeaders(string $input): array { - $headers = explode("\r", $headers); - $headers = array_filter($headers, function ($header) - { - return !empty($header); - }); - $headers = array_map(function ($header) { - return explode(':', $header, 2); - }, $headers); + $headers = array(); + $lines = explode("\n", $input); + $headers['HTTP'] = array_shift($lines); - return array_combine(array_map(function ($header) { return strtolower($header[0]); }, $headers), - array_map(function ($header) { return trim($header[1]); }, $headers) - ); + foreach ($lines as $line) { + $header = explode(':', $line, 2); + if (count($header) == 2) { + $headers[trim($header[0])] = trim($header[1]); + } + } + + return $headers; } } \ No newline at end of file diff --git a/src/ncc/Classes/NccExtension/PackageCompiler.php b/src/ncc/Classes/NccExtension/PackageCompiler.php index f27f2ac..08c96bb 100644 --- a/src/ncc/Classes/NccExtension/PackageCompiler.php +++ b/src/ncc/Classes/NccExtension/PackageCompiler.php @@ -116,7 +116,6 @@ throw new BuildException('Failed to build project', $e); } - throw new UnsupportedProjectTypeException('The project type \'' . $project_type->ProjectType . '\' is not supported'); } @@ -168,7 +167,6 @@ * @param string $build_configuration * @return string * @throws BuildConfigurationNotFoundException - * @throws BuildException * @throws IOException */ public static function writePackage(string $path, Package $package, ProjectConfiguration $configuration, string $build_configuration=BuildConfigurationValues::DefaultConfiguration): string @@ -176,21 +174,14 @@ // Write the package to disk $FileSystem = new Filesystem(); $BuildConfiguration = $configuration->Build->getBuildConfiguration($build_configuration); - if($FileSystem->exists($path . $BuildConfiguration->OutputPath)) - { - try - { - $FileSystem->remove($path . $BuildConfiguration->OutputPath); - } - catch(\ncc\ThirdParty\Symfony\Filesystem\Exception\IOException $e) - { - throw new BuildException('Cannot delete directory \'' . $path . $BuildConfiguration->OutputPath . '\', ' . $e->getMessage(), $e); - } - } + if(!$FileSystem->exists($path . $BuildConfiguration->OutputPath)) + $FileSystem->mkdir($path . $BuildConfiguration->OutputPath); // Finally write the package to the disk $FileSystem->mkdir($path . $BuildConfiguration->OutputPath); $output_file = $path . $BuildConfiguration->OutputPath . DIRECTORY_SEPARATOR . $package->Assembly->Package . '.ncc'; + if($FileSystem->exists($output_file)) + $FileSystem->remove($output_file); $FileSystem->touch($output_file); try diff --git a/src/ncc/Classes/PhpExtension/PhpCompiler.php b/src/ncc/Classes/PhpExtension/PhpCompiler.php index bb29ce9..f105d12 100644 --- a/src/ncc/Classes/PhpExtension/PhpCompiler.php +++ b/src/ncc/Classes/PhpExtension/PhpCompiler.php @@ -128,67 +128,75 @@ // TODO: Re-implement the scanning process outside the compiler, as this is will be redundant // Scan for components first. - Console::outVerbose('Scanning for components... '); - /** @var SplFileInfo $item */ - /** @noinspection PhpRedundantOptionalArgumentInspection */ - foreach($DirectoryScanner($source_path, True) as $item) + + if(file_exists($source_path)) { - // Ignore directories, they're not important. :-) - if(is_dir($item->getPathName())) - continue; + Console::outVerbose('Scanning for components... '); + /** @var SplFileInfo $item */ + /** @noinspection PhpRedundantOptionalArgumentInspection */ + foreach($DirectoryScanner($source_path, True) as $item) + { + // Ignore directories, they're not important. :-) + if(is_dir($item->getPathName())) + continue; - $Component = new Package\Component(); - $Component->Name = Functions::removeBasename($item->getPathname(), $this->path); - $this->package->Components[] = $Component; + $Component = new Package\Component(); + $Component->Name = Functions::removeBasename($item->getPathname(), $this->path); + $this->package->Components[] = $Component; - Console::outVerbose(sprintf('Found component %s', $Component->Name)); - } + Console::outVerbose(sprintf('Found component %s', $Component->Name)); + } - if(count($this->package->Components) > 0) - { - Console::outVerbose(count($this->package->Components) . ' component(s) found'); + if(count($this->package->Components) > 0) + { + Console::outVerbose(count($this->package->Components) . ' component(s) found'); + } + else + { + Console::outVerbose('No components found'); + } + + // Clear previous excludes and includes + $DirectoryScanner->setExcludes([]); + $DirectoryScanner->setIncludes([]); + + // Ignore component files + if($selected_build_configuration->ExcludeFiles !== null && count($selected_build_configuration->ExcludeFiles) > 0) + { + $DirectoryScanner->setExcludes(array_merge($selected_build_configuration->ExcludeFiles, ComponentFileExtensions::Php)); + } + else + { + $DirectoryScanner->setExcludes(ComponentFileExtensions::Php); + } + + Console::outVerbose('Scanning for resources... '); + /** @var SplFileInfo $item */ + foreach($DirectoryScanner($source_path) as $item) + { + // Ignore directories, they're not important. :-) + if(is_dir($item->getPathName())) + continue; + + $Resource = new Package\Resource(); + $Resource->Name = Functions::removeBasename($item->getPathname(), $this->path); + $this->package->Resources[] = $Resource; + + Console::outVerbose(sprintf('found resource %s', $Resource->Name)); + } + + if(count($this->package->Resources) > 0) + { + Console::outVerbose(count($this->package->Resources) . ' resources(s) found'); + } + else + { + Console::outVerbose('No resources found'); + } } else { - Console::outVerbose('No components found'); - } - - // Clear previous excludes and includes - $DirectoryScanner->setExcludes([]); - $DirectoryScanner->setIncludes([]); - - // Ignore component files - if($selected_build_configuration->ExcludeFiles !== null && count($selected_build_configuration->ExcludeFiles) > 0) - { - $DirectoryScanner->setExcludes(array_merge($selected_build_configuration->ExcludeFiles, ComponentFileExtensions::Php)); - } - else - { - $DirectoryScanner->setExcludes(ComponentFileExtensions::Php); - } - - Console::outVerbose('Scanning for resources... '); - /** @var SplFileInfo $item */ - foreach($DirectoryScanner($source_path) as $item) - { - // Ignore directories, they're not important. :-) - if(is_dir($item->getPathName())) - continue; - - $Resource = new Package\Resource(); - $Resource->Name = Functions::removeBasename($item->getPathname(), $this->path); - $this->package->Resources[] = $Resource; - - Console::outVerbose(sprintf('found resource %s', $Resource->Name)); - } - - if(count($this->package->Resources) > 0) - { - Console::outVerbose(count($this->package->Resources) . ' resources(s) found'); - } - else - { - Console::outVerbose('No resources found'); + Console::outWarning('Source path does not exist, skipping resource and component scanning'); } $selected_dependencies = []; diff --git a/src/ncc/Objects/Vault/Entry.php b/src/ncc/Objects/Vault/Entry.php index 6f52b6b..c1e45b0 100644 --- a/src/ncc/Objects/Vault/Entry.php +++ b/src/ncc/Objects/Vault/Entry.php @@ -149,7 +149,6 @@ return false; $this->Password = $this->encrypt(); - var_dump($this->Password); return true; } diff --git a/src/ncc/Utilities/Console.php b/src/ncc/Utilities/Console.php index 27710eb..642d3fb 100644 --- a/src/ncc/Utilities/Console.php +++ b/src/ncc/Utilities/Console.php @@ -126,7 +126,7 @@ /** @noinspection PhpRedundantOptionalArgumentInspection */ $tick_time = str_pad($tick_time, (strlen($tick_time) + (self::$largestTickLength - strlen($tick_time))), ' ', STR_PAD_RIGHT); - return '[' . $tick_time . ' - ' . date('TH:i:sP') . '] ' . $input; + return '[' . $tick_time . '] ' . $input; } /** diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index 928114c..1c7a41f 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -36,6 +36,8 @@ use ncc\ThirdParty\jelix\Version\Parser; use ncc\ThirdParty\Symfony\Filesystem\Filesystem; use PharData; + use RecursiveDirectoryIterator; + use RecursiveIteratorIterator; use Throwable; use ZipArchive; @@ -585,36 +587,82 @@ * @return string * @throws Exception */ - public static function extractArchive(string $path): string + public static function extractArchive(string $path): ?string { - $filesystem = new Filesystem(); - if(!$filesystem->exists(dirname($path))) - $filesystem->mkdir(dirname($path)); + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $mimeType = finfo_file($finfo, $path); + finfo_close($finfo); - switch(pathinfo($path, PATHINFO_EXTENSION)) + if ($mimeType == 'application/zip') { - case 'zip': - $zip = new ZipArchive(); - $zip->open($path); + $zip = new ZipArchive; + $res = $zip->open($path); + if ($res === TRUE) + { $zip->extractTo(dirname($path)); $zip->close(); - break; - - case 'tar': - $phar = new PharData($path); - $phar->extractTo(dirname($path)); - break; - - case 'gz': - $phar = new PharData($path); - $phar->decompress(); - break; - - default: - throw new Exception('Unsupported archive type'); + return dirname($path); + } + } + if ($mimeType == 'application/x-tar' || $mimeType == 'application/tar') + { + $phar = new PharData($path); + $phar->extractTo(dirname($path), null, true); + return dirname($path); + } + elseif ($mimeType == 'application/x-gzip' || $mimeType == 'application/gzip') + { + $phar = new PharData($path); + $phar->decompress(); + return dirname($path); } - return dirname($path); + return null; } + /** + * Scans the given directory for files and returns the found file + * + * @param string $path + * @param array $files + * @return string|null + */ + public static function searchDirectory(string $path, array $files): ?string + { + if (!is_dir($path)) + return null; + + // Search files in the given directory recursively + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); + foreach ($iterator as $file) + { + if (in_array($file->getFilename(), $files)) + return $file->getPathname(); + } + + return null; + } + + /** + * Attempts to convert a weird version number to a standard version number + * + * @param $version + * @return string + */ + public static function convertToSemVer($version) + { + $parts = explode('.', $version); + $major = $parts[0]; + $minor = $parts[1]; + $patch = $parts[2]; + $buildmetadata = $parts[3]; + + // Assemble the SemVer compatible string + $semver = "$major.$minor.$patch+$buildmetadata"; + + return $semver; + } + + + } \ No newline at end of file diff --git a/src/ncc/Utilities/Resolver.php b/src/ncc/Utilities/Resolver.php index bd14ef2..a9905e9 100644 --- a/src/ncc/Utilities/Resolver.php +++ b/src/ncc/Utilities/Resolver.php @@ -1,18 +1,16 @@ Type; + return RemoteSourceType::Defined; } /** @@ -277,38 +275,36 @@ * @param array $exlucde * @return ProjectDetectionResults */ - public static function detectProjectType(string $path, array $exlucde=[]): ProjectDetectionResults + public static function detectProjectType(string $path): ProjectDetectionResults { - $Scanner = new DirectoryScanner(); - $Scanner->setExcludes($exlucde); $project_files = [ - 'composer.json', - 'package.json', + 'project.json', + 'composer.json' ]; + $project_file = Functions::searchDirectory($path, $project_files); + $project_detection_results = new ProjectDetectionResults(); $project_detection_results->ProjectType = ProjectType::Unknown; - /** @var \SplFileInfo $item */ - foreach($Scanner($path, true) as $item) + if($project_file == null) { - if(in_array($item->getFilename(), $project_files)) - { - switch($item->getFilename()) - { - case 'composer.json': - $project_detection_results->ProjectType = ProjectType::Composer; - $project_detection_results->ProjectPath = dirname($item->getPathname()); - break; - - case 'project.json': - $project_detection_results->ProjectType = ProjectType::Ncc; - $project_detection_results->ProjectPath = dirname($item->getPathname()); - break; - } - } + return $project_detection_results; } + // Get filename of the project file + switch(basename($project_file)) + { + case 'project.json': + $project_detection_results->ProjectType = ProjectType::Ncc; + break; + + case 'composer.json': + $project_detection_results->ProjectType = ProjectType::Composer; + break; + } + + $project_detection_results->ProjectPath = dirname($project_file); return $project_detection_results; } } \ No newline at end of file