Skip to content

Commit

Permalink
Moved installer utilities to traits and classes. Part 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 19, 2024
1 parent bcefe54 commit 3234427
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 374 deletions.
420 changes: 125 additions & 295 deletions .vortex/installer/src/Command/InstallCommand.php

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions .vortex/installer/src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ public function set(string $name, mixed $value): void {
}
}

public function getDstDir(): ?string {
return $this->get('VORTEX_INSTALL_DST_DIR');
}

/**
* Shorthand to get the value of whether install should be quiet.
*/
public function isQuiet(): bool {
return (bool) $this->get('quiet', FALSE);
}

/**
* Shorthand to get the value of VORTEX_INSTALL_DEBUG.
*/
public function isInstallDebug(): bool {
return (bool) $this->get('VORTEX_INSTALL_DEBUG', FALSE);
}

}
80 changes: 80 additions & 0 deletions .vortex/installer/src/Traits/DownloadTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace DrevOps\Installer\Traits;

/**
* Environment trait.
*/
trait DownloadTrait {

protected function downloadScaffoldLocal(): void {
$dst = $this->config->get('VORTEX_INSTALL_TMP_DIR');
$repo = $this->config->get('VORTEX_INSTALL_LOCAL_REPO');
$ref = $this->config->get('VORTEX_INSTALL_COMMIT');

$this->status(sprintf('Downloading Vortex from the local repository "%s" at ref "%s".', $repo, $ref), self::INSTALLER_STATUS_MESSAGE, FALSE);

$command = sprintf('git --git-dir="%s/.git" --work-tree="%s" archive --format=tar "%s" | tar xf - -C "%s"', $repo, $repo, $ref, $dst);
$this->doExec($command, $output, $code);

$this->status(implode(PHP_EOL, $output), self::INSTALLER_STATUS_DEBUG);

if ($code != 0) {
throw new \RuntimeException(implode(PHP_EOL, $output));
}

$this->status(sprintf('Downloaded to "%s".', $dst), self::INSTALLER_STATUS_DEBUG);

print ' ';
$this->status('Done', self::INSTALLER_STATUS_SUCCESS);
}

protected function downloadScaffoldRemote(): void {
$dst = $this->config->get('VORTEX_INSTALL_TMP_DIR');
$org = 'drevops';
$project = 'vortex';
$ref = $this->config->get('VORTEX_INSTALL_COMMIT');
$release_prefix = $this->config->get('VORTEX_VERSION');

if ($ref == 'HEAD') {
$release_prefix = $release_prefix == 'develop' ? NULL : $release_prefix;
$ref = $this->findLatestVortexRelease($org, $project, $release_prefix);
$this->config->set('VORTEX_VERSION', $ref);
}

$url = sprintf('https://github.com/%s/%s/archive/%s.tar.gz', $org, $project, $ref);
$this->status(sprintf('Downloading Vortex from the remote repository "%s" at ref "%s".', $url, $ref), self::INSTALLER_STATUS_MESSAGE, FALSE);
$this->doExec(sprintf('curl -sS -L "%s" | tar xzf - -C "%s" --strip 1', $url, $dst), $output, $code);

if ($code != 0) {
throw new \RuntimeException(implode(PHP_EOL, $output));
}

$this->status(sprintf('Downloaded to "%s".', $dst), self::INSTALLER_STATUS_DEBUG);

$this->status('Done', self::INSTALLER_STATUS_SUCCESS);
}

protected function findLatestVortexRelease(string $org, string $project, string $release_prefix): ?string {
$release_url = sprintf('https://api.github.com/repos/%s/%s/releases', $org, $project);
$release_contents = file_get_contents($release_url, FALSE, stream_context_create([
'http' => ['method' => 'GET', 'header' => ['User-Agent: PHP']],
]));

if (!$release_contents) {
throw new \RuntimeException(sprintf('Unable to download release information from "%s".', $release_url));
}

$records = json_decode($release_contents, TRUE);
foreach ($records as $record) {
if (isset($record['tag_name']) && ($release_prefix && str_contains((string) $record['tag_name'], $release_prefix) || !$release_prefix)) {
return $record['tag_name'];
}
}

return NULL;
}

}
2 changes: 1 addition & 1 deletion .vortex/installer/src/Traits/EnvTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function getValueFromDstDotenv(string $name, mixed $default = NULL): m
return $env_value;
}

$file = $this->getDstDir() . '/.env';
$file = $this->config->getDstDir() . '/.env';
if (!is_readable($file)) {
return $default;
}
Expand Down
4 changes: 2 additions & 2 deletions .vortex/installer/src/Traits/PrinterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function printBox(string $content, string $title = '', string $fill =
}

protected function printTick(?string $text = NULL): void {
if (!empty($text) && $this->isInstallDebug()) {
if (!empty($text) && $this->config->isInstallDebug()) {
print PHP_EOL;
$this->status($text, self::INSTALLER_STATUS_DEBUG, FALSE);
}
Expand Down Expand Up @@ -141,7 +141,7 @@ protected function status(string $message, int $level = self::INSTALLER_STATUS_M
break;
}

if ($level != self::INSTALLER_STATUS_DEBUG || $this->isInstallDebug()) {
if ($level != self::INSTALLER_STATUS_DEBUG || $this->config->isInstallDebug()) {
$this->out(($use_prefix ? $prefix . ' ' : '') . $message, $color, $use_eol);
}
}
Expand Down
Loading

1 comment on commit 3234427

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.