Skip to content

Commit 300d2a4

Browse files
committed
Refactored downloader to use artifact class as a container.
1 parent 91dcaaa commit 300d2a4

8 files changed

Lines changed: 895 additions & 756 deletions

File tree

.vortex/installer/src/Command/InstallCommand.php

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DrevOps\VortexInstaller\Command;
66

7+
use DrevOps\VortexInstaller\Downloader\Artifact;
78
use DrevOps\VortexInstaller\Downloader\Downloader;
89
use DrevOps\VortexInstaller\Downloader\RepositoryDownloader;
910
use DrevOps\VortexInstaller\Prompts\Handlers\Starter;
@@ -85,6 +86,11 @@ class InstallCommand extends Command implements CommandRunnerAwareInterface, Exe
8586
*/
8687
protected ?Downloader $fileDownloader = NULL;
8788

89+
/**
90+
* The artifact representing the repository and reference to install.
91+
*/
92+
protected Artifact $artifact;
93+
8894
/**
8995
* {@inheritdoc}
9096
*/
@@ -145,16 +151,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
145151
$this->header();
146152

147153
// Only validate if using custom repository or custom reference.
148-
if ($this->shouldValidateRepositoryAndRef()) {
154+
if (!$this->artifact->isDefault()) {
149155
Task::action(
150156
label: 'Validating repository and reference',
151157
action: function (): string {
152-
$repo = (string) $this->config->get(Config::REPO);
153-
$ref = (string) $this->config->get(Config::REF);
154-
$this->getRepositoryDownloader()->validate($repo, $ref);
158+
$this->getRepositoryDownloader()->validate($this->artifact);
155159
return 'Repository and reference validated successfully';
156160
},
157-
hint: fn(): string => sprintf('Checking repository "%s" and reference "%s"', $this->config->get(Config::REPO), $this->config->get(Config::REF)),
161+
hint: fn(): string => sprintf('Checking repository "%s" and reference "%s"', $this->artifact->getRepo(), $this->artifact->getRef()),
158162
success: fn(string $return): string => $return
159163
);
160164
Tui::line('');
@@ -178,11 +182,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
178182
Task::action(
179183
label: 'Downloading Vortex',
180184
action: function (): string {
181-
$version = $this->getRepositoryDownloader()->download($this->config->get(Config::REPO), $this->config->get(Config::REF), $this->config->get(Config::TMP));
185+
$version = $this->getRepositoryDownloader()->download($this->artifact, $this->config->get(Config::TMP));
182186
$this->config->set(Config::VERSION, $version);
183187
return $version;
184188
},
185-
hint: fn(): string => sprintf('Downloading from "%s" repository at ref "%s"', $this->config->get(Config::REPO), $this->config->get(Config::REF)),
189+
hint: fn(): string => sprintf('Downloading from "%s" repository at ref "%s"', $this->artifact->getRepo(), $this->artifact->getRef()),
186190
success: fn(string $return): string => sprintf('Vortex downloaded (%s)', $return)
187191
);
188192

@@ -326,9 +330,25 @@ protected function resolveOptions(array $arguments, array $options): void {
326330
Env::putFromDotenv($dest_env_file);
327331
}
328332

329-
[$repo, $ref] = RepositoryDownloader::parseUri($options[static::OPTION_URI] ?: RepositoryDownloader::DEFAULT_REPO . '#stable');
330-
$this->config->set(Config::REPO, $repo);
331-
$this->config->set(Config::REF, $ref);
333+
// Build URI for artifact.
334+
$uri_from_option = !empty($options[static::OPTION_URI]) && is_scalar($options[static::OPTION_URI]) ? strval($options[static::OPTION_URI]) : NULL;
335+
$repo = Env::get(Config::REPO) ?: ($this->config->get(Config::REPO) ?: NULL);
336+
$ref = Env::get(Config::REF) ?: ($this->config->get(Config::REF) ?: NULL);
337+
338+
// Priority: option URI > env/config repo+ref > default.
339+
$uri = $uri_from_option;
340+
if (!$uri && $repo) {
341+
$uri = $ref ? $repo . '#' . $ref : $repo;
342+
}
343+
344+
try {
345+
$this->artifact = Artifact::fromUri($uri);
346+
$this->config->set(Config::REPO, $this->artifact->getRepo());
347+
$this->config->set(Config::REF, $this->artifact->getRef());
348+
}
349+
catch (\RuntimeException $e) {
350+
throw new \RuntimeException(sprintf('Invalid repository URI: %s', $e->getMessage()), $e->getCode(), $e);
351+
}
332352

333353
// Check if the project is a Vortex project.
334354
$this->config->set(Config::IS_VORTEX_PROJECT, File::contains($this->config->getDst() . DIRECTORY_SEPARATOR . 'README.md', '/badge\/Vortex-/'));
@@ -533,15 +553,14 @@ protected function header(): void {
533553
$title = 'Welcome to the Vortex interactive installer';
534554
$content = '';
535555

536-
$ref = $this->config->get(Config::REF);
537-
if ($ref == RepositoryDownloader::REF_STABLE) {
556+
if ($this->artifact->isStable()) {
538557
$content .= 'This tool will guide you through installing the latest ' . Tui::underscore('stable') . ' version of Vortex into your project.' . PHP_EOL;
539558
}
540-
elseif ($ref == RepositoryDownloader::REF_HEAD) {
559+
elseif ($this->artifact->isDevelopment()) {
541560
$content .= 'This tool will guide you through installing the latest ' . Tui::underscore('development') . ' version of Vortex into your project.' . PHP_EOL;
542561
}
543562
else {
544-
$content .= sprintf('This tool will guide you through installing a ' . Tui::underscore('custom') . ' version of Vortex into your project at commit "%s".', $ref) . PHP_EOL;
563+
$content .= sprintf('This tool will guide you through installing a ' . Tui::underscore('custom') . ' version of Vortex into your project at commit "%s".', $this->artifact->getRef()) . PHP_EOL;
545564
}
546565

547566
$content .= PHP_EOL;
@@ -737,28 +756,6 @@ public function cleanup(): void {
737756
}
738757
}
739758

740-
/**
741-
* Check if repository and reference validation should be performed.
742-
*
743-
* Validation is skipped for default Vortex repo with stable/HEAD refs.
744-
* Validation is required for custom repositories or custom references.
745-
*
746-
* @return bool
747-
* TRUE if validation should be performed, FALSE otherwise.
748-
*/
749-
protected function shouldValidateRepositoryAndRef(): bool {
750-
$repo = $this->config->get(Config::REPO);
751-
$ref = $this->config->get(Config::REF);
752-
753-
// Check if using default repository and default ref.
754-
$default_repo_without_git = RepositoryDownloader::normalizeRepoUrl(RepositoryDownloader::DEFAULT_REPO);
755-
$is_default_repo = ($repo === RepositoryDownloader::DEFAULT_REPO || $repo === $default_repo_without_git);
756-
$is_default_ref = ($ref === RepositoryDownloader::REF_STABLE || $ref === RepositoryDownloader::REF_HEAD);
757-
758-
// Skip validation only if both are default.
759-
return !($is_default_repo && $is_default_ref);
760-
}
761-
762759
/**
763760
* Get the repository downloader.
764761
*

0 commit comments

Comments
 (0)