Add option to force package build from source

This commit is contained in:
netkas 2024-09-20 17:04:30 -04:00
parent be70823a79
commit dda710119c
4 changed files with 54 additions and 26 deletions

View file

@ -168,6 +168,11 @@
$options[InstallPackageOptions::SKIP_DEPENDENCIES->value] = true; $options[InstallPackageOptions::SKIP_DEPENDENCIES->value] = true;
} }
if(isset($args['build-source']))
{
$options[InstallPackageOptions::BUILD_SOURCE->value] = true;
}
if($authentication !== null) if($authentication !== null)
{ {
$entry = (new CredentialManager())->getVault()?->getEntry($authentication); $entry = (new CredentialManager())->getVault()?->getEntry($authentication);
@ -560,6 +565,7 @@
new CliHelpSection(['install', '-p', '--skip-dependencies'], 'Installs a specified ncc package but skips the installation of dependencies'), new CliHelpSection(['install', '-p', '--skip-dependencies'], 'Installs a specified ncc package but skips the installation of dependencies'),
new CliHelpSection(['install', '-p', '--reinstall'], 'Installs a specified ncc package, reinstall if already installed'), new CliHelpSection(['install', '-p', '--reinstall'], 'Installs a specified ncc package, reinstall if already installed'),
new CliHelpSection(['install', '--prefer-static', '--static'], 'Installs a static version of the package from the remote repository if available'), new CliHelpSection(['install', '--prefer-static', '--static'], 'Installs a static version of the package from the remote repository if available'),
new CliHelpSection(['install', '--build-source'], 'Forces ncc to build the packages from source rather than trying to use a pre-built binary'),
new CliHelpSection(['uninstall', '--package', '-p'], 'Uninstalls a specified ncc package'), new CliHelpSection(['uninstall', '--package', '-p'], 'Uninstalls a specified ncc package'),
new CliHelpSection(['uninstall', '--package', '-p', '--version', '-v'], 'Uninstalls a specified ncc package version'), new CliHelpSection(['uninstall', '--package', '-p', '--version', '-v'], 'Uninstalls a specified ncc package version'),
new CliHelpSection(['uninstall-all'], 'Uninstalls all packages'), new CliHelpSection(['uninstall-all'], 'Uninstalls all packages'),

View file

@ -30,7 +30,7 @@
* @warning This will cause the package to fail to import of * @warning This will cause the package to fail to import of
* the dependencies are not met * the dependencies are not met
*/ */
case SKIP_DEPENDENCIES = 'skip_dependencies'; case SKIP_DEPENDENCIES = 'skip-dependencies';
/** /**
* Reinstall all packages if they are already installed, * Reinstall all packages if they are already installed,
@ -42,5 +42,11 @@
* Installs a static version of the package if it's available * Installs a static version of the package if it's available
* otherwise it will install non-static version * otherwise it will install non-static version
*/ */
case PREFER_STATIC = 'prefer_static'; case PREFER_STATIC = 'prefer-static';
/**
* Forces ncc to build packages from source rather than trying to obtain
* a pre-built version of the package
*/
case BUILD_SOURCE = 'build-source';
} }

View file

@ -521,7 +521,7 @@
)); ));
/** @noinspection SlowArrayOperationsInLoopInspection */ /** @noinspection SlowArrayOperationsInLoopInspection */
$installed_packages = array_merge($installed_packages, $this->install($dependency->getSource(), $authentication)); $installed_packages = array_merge($installed_packages, $this->install($dependency->getSource(), $authentication, $options));
} }
} }
@ -550,35 +550,45 @@
Console::out(sprintf('Fetching package %s/%s=%s from %s', $input->getVendor(), $input->getPackage(), $input->getVersion(), $input->getRepository())); Console::out(sprintf('Fetching package %s/%s=%s from %s', $input->getVendor(), $input->getPackage(), $input->getVersion(), $input->getRepository()));
try if(isset($options[InstallPackageOptions::BUILD_SOURCE->value]))
{ {
Console::outVerbose(sprintf( Console::outVerbose(sprintf(
'Attempting to fetch a pre-built ncc package for %s=%s from %s', 'Forcing ncc to build package %s/%s=%s from source',
$input->getPackage(), $input->getVersion(), $input->getRepository() $input->getVendor(), $input->getPackage(), $input->getVersion()
)); ));
// First try to fetch a pre-built package from the repository
$results = $this->repository_manager->getRepository($input->getRepository())->fetchPackage(
$input->getVendor(), $input->getPackage(), $input->getVersion(), $authentication, $options
);
$package_path = $this->downloadFile($results->getUrl(), PathFinder::getCachePath());
} }
catch(Exception $e) else
{ {
Console::outVerbose(sprintf( try
'Failed to fetch a pre-built ncc package for %s=%s from %s: %s',
$input->getPackage(), $input->getVersion(), $input->getRepository(), $e->getMessage()
));
// Clean up the package file if it exists
if(isset($package_path) && is_file($package_path))
{ {
ShutdownHandler::declareTemporaryPath($package_path); Console::outVerbose(sprintf(
} 'Attempting to fetch a pre-built ncc package for %s=%s from %s',
$input->getPackage(), $input->getVersion(), $input->getRepository()
));
// This is a warning because we can still attempt to build from source // First try to fetch a pre-built package from the repository
unset($results, $package_path); $results = $this->repository_manager->getRepository($input->getRepository())->fetchPackage(
$input->getVendor(), $input->getPackage(), $input->getVersion(), $authentication, $options
);
$package_path = $this->downloadFile($results->getUrl(), PathFinder::getCachePath());
}
catch(Exception $e)
{
Console::outVerbose(sprintf(
'Failed to fetch a pre-built ncc package for %s=%s from %s: %s',
$input->getPackage(), $input->getVersion(), $input->getRepository(), $e->getMessage()
));
// Clean up the package file if it exists
if(isset($package_path) && is_file($package_path))
{
ShutdownHandler::declareTemporaryPath($package_path);
}
// This is a warning because we can still attempt to build from source
unset($results, $package_path);
}
} }
if(!isset($package_path)) if(!isset($package_path))
@ -848,7 +858,7 @@
} }
default: default:
throw new NotSupportedException(sprintf('Cannot build from source %s, the project type %s is not supported', $archive, $project_detection->getProjectType())); throw new NotSupportedException(sprintf('Cannot build from source %s, the project type %s is not supported', $archive, $project_detection->getProjectType()->value));
} }
} }

View file

@ -96,6 +96,12 @@
$path = substr($path, 0, -1); $path = substr($path, 0, -1);
} }
if(is_file($path))
{
// We can assume the user is trying to load a project file
$path = dirname($path);
}
// Detect if the folder exists or not // Detect if the folder exists or not
if(!is_dir($path)) if(!is_dir($path))
{ {