Skip to content

Commit

Permalink
Pint Restyle
Browse files Browse the repository at this point in the history
  • Loading branch information
kduma committed Dec 16, 2023
1 parent c28844d commit ac9791e
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 110 deletions.
79 changes: 32 additions & 47 deletions app/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Stringable;
use LaravelZero\Framework\Commands\Command;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -42,12 +41,11 @@ public function handle(ExtensionRunner $extensionRunner): void
{
$config_file = str($this->argument('config-file') ?: str(getcwd())->finish(DIRECTORY_SEPARATOR)->append('satis.json'));

if(!file_exists($config_file)) {
if (! file_exists($config_file)) {
$this->error("Config file {$config_file} does not exist");
exit(1);
}


$buildConfig = new BuildState(
temp_prefix: crc32($config_file),
config_file_path: $config_file,
Expand All @@ -57,93 +55,80 @@ public function handle(ExtensionRunner $extensionRunner): void

$extensionRunner->enableExtensionFromBuildState($this, $buildConfig);



if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_INITIAL_CLEAR_TEMP_DIRECTORY, $buildConfig)) {
$this->info("Clearing temp directory");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_INITIAL_CLEAR_TEMP_DIRECTORY, $buildConfig)) {
$this->info('Clearing temp directory');
$this->clearTempDirectory(
prefix: $buildConfig->getTempPrefix()
);
} else {
$this->info("Skipping clearing temp directory");
$this->info('Skipping clearing temp directory');
}
$extensionRunner->execute($this, BuildHooks::AFTER_INITIAL_CLEAR_TEMP_DIRECTORY, $buildConfig);



if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_CREATE_TEMP_DIRECTORY, $buildConfig)) {
$this->info("Creating temp directory");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_CREATE_TEMP_DIRECTORY, $buildConfig)) {
$this->info('Creating temp directory');
$this->createTempDirectory(
prefix: $buildConfig->getTempPrefix()
);
} else {
$this->info("Skipping creating temp directory");
$this->info('Skipping creating temp directory');
}
$extensionRunner->execute($this, BuildHooks::AFTER_CREATE_TEMP_DIRECTORY, $buildConfig);



if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_DOWNLOAD_FROM_S3, $buildConfig)) {
if (!$buildConfig->isForceFreshDownloads()) {
$this->info("Downloading from S3");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_DOWNLOAD_FROM_S3, $buildConfig)) {
if (! $buildConfig->isForceFreshDownloads()) {
$this->info('Downloading from S3');
$buildConfig->setPlaceholders(
placeholders: $this->downloadFromS3(prefix: $buildConfig->getTempPrefix())
);
} else {
$this->info("Skipping downloading from S3 because --fresh was passed");
$this->info('Skipping downloading from S3 because --fresh was passed');
}
} else {
$this->info("Skipping downloading from S3");
$this->info('Skipping downloading from S3');
}
$extensionRunner->execute($this, BuildHooks::AFTER_DOWNLOAD_FROM_S3, $buildConfig);



if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_BUILD_SATIS_REPOSITORY, $buildConfig)) {
$this->info("Building satis repository");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_BUILD_SATIS_REPOSITORY, $buildConfig)) {
$this->info('Building satis repository');
$this->buildSatisRepository(
config_file: $buildConfig->getConfigFilePath(),
prefix: $buildConfig->getTempPrefix(),
repository_url: $buildConfig->getRepositoryUrls()
);
} else {
$this->info("Skipping building satis repository");
$this->info('Skipping building satis repository');
}
$extensionRunner->execute($this, BuildHooks::AFTER_BUILD_SATIS_REPOSITORY, $buildConfig);


if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_UPLOAD_TO_S3, $buildConfig)) {
$this->info("Uploading to S3");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_UPLOAD_TO_S3, $buildConfig)) {
$this->info('Uploading to S3');
$this->uploadToS3(
prefix: $buildConfig->getTempPrefix(),
placeholders: $buildConfig->getPlaceholders()
);
} else {
$this->info("Skipping uploading to S3");
$this->info('Skipping uploading to S3');
}
$extensionRunner->execute($this, BuildHooks::AFTER_UPLOAD_TO_S3, $buildConfig);



if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_REMOVE_MISSING_FILES_FROM_S3, $buildConfig)) {
$this->info("Removing missing files from S3");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_REMOVE_MISSING_FILES_FROM_S3, $buildConfig)) {
$this->info('Removing missing files from S3');
$this->removeMissingFilesFromS3(
prefix: $buildConfig->getTempPrefix()
);
} else {
$this->info("Skipping removing missing files from S3");
$this->info('Skipping removing missing files from S3');
}
$extensionRunner->execute($this, BuildHooks::AFTER_REMOVE_MISSING_FILES_FROM_S3, $buildConfig);



if($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_FINAL_CLEAR_TEMP_DIRECTORY, $buildConfig)) {
$this->info("Clearing temp directory");
if ($buildConfig->last_step_executed = $extensionRunner->execute($this, BuildHooks::BEFORE_FINAL_CLEAR_TEMP_DIRECTORY, $buildConfig)) {
$this->info('Clearing temp directory');
$this->clearTempDirectory(
prefix: $buildConfig->getTempPrefix()
);
} else {
$this->info("Skipping clearing temp directory");
$this->info('Skipping clearing temp directory');
}
$extensionRunner->execute($this, BuildHooks::AFTER_FINAL_CLEAR_TEMP_DIRECTORY, $buildConfig);
}
Expand Down Expand Up @@ -180,7 +165,7 @@ protected function downloadFromS3(string $prefix): Collection
$placeholders = collect();

collect(Storage::disk('s3')->allFiles())
->map(fn($file) => str($file))->each(function (Stringable $s3_path) use ($prefix, $placeholders) {
->map(fn ($file) => str($file))->each(function (Stringable $s3_path) use ($prefix, $placeholders) {
$temp_path = $s3_path->start('/')->start($prefix);

if ($s3_path->afterLast('.') == 'tar' || $s3_path->afterLast('.') == 'zip') {
Expand All @@ -200,7 +185,7 @@ protected function downloadFromS3(string $prefix): Collection
/**
* Generate satis repository
*/
protected function buildSatisRepository(string $config_file, string $prefix, array $repository_url = null): void
protected function buildSatisRepository(string $config_file, string $prefix, ?array $repository_url = null): void
{
$application = new Application();
$application->setAutoExit(false); // prevent `$application->run` method from exitting the script
Expand All @@ -210,7 +195,7 @@ protected function buildSatisRepository(string $config_file, string $prefix, arr
[
'command' => 'build',
'file' => $config_file,
'output-dir' => (string)config('filesystems.disks.temp.root')->append($prefix)
'output-dir' => (string) config('filesystems.disks.temp.root')->append($prefix),
] + ($repository_url ? ['--repository-url' => $repository_url] : [])
)
);
Expand All @@ -222,11 +207,11 @@ protected function buildSatisRepository(string $config_file, string $prefix, arr
protected function removeMissingFilesFromS3(int $prefix): void
{
$all_local_files = collect(Storage::disk('temp')->allFiles($prefix))
->map(fn($file) => str($file))
->map(fn(Stringable $temp_path) => $temp_path->after($prefix)->ltrim('/'));
->map(fn ($file) => str($file))
->map(fn (Stringable $temp_path) => $temp_path->after($prefix)->ltrim('/'));

collect(Storage::disk('s3')->allFiles())
->map(fn($file) => str($file))
->map(fn ($file) => str($file))
->diff($all_local_files)
->each(function (Stringable $s3_path) {
$this->line("Deleting {$s3_path} because it is missing", verbosity: OutputInterface::VERBOSITY_VERBOSE);
Expand All @@ -240,9 +225,9 @@ protected function removeMissingFilesFromS3(int $prefix): void
protected function uploadToS3(int $prefix, Collection $placeholders): void
{
collect(Storage::disk('temp')->allFiles($prefix))
->map(fn($file) => str($file))
->map(fn ($file) => str($file))
->tap(function (Collection $files) use ($prefix, $placeholders) {
[$placeholder_files, $normal_files] = $files->partition(fn(Stringable $file) => $placeholders->contains($file->toString()));
[$placeholder_files, $normal_files] = $files->partition(fn (Stringable $file) => $placeholders->contains($file->toString()));

$placeholder_files->each(function (Stringable $temp_path) use ($prefix) {
$s3_path = $temp_path->after($prefix)->ltrim('/');
Expand Down
12 changes: 6 additions & 6 deletions app/Extensions/FileRestrictionsMapFileGeneratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ public function hook(BuildStateInterface $buildState): void

$packages = json_decode(Storage::disk('temp')->get(str('packages.json')->start('/')->start($buildState->getTempPrefix())), true);
$packages = collect($packages['available-packages'])
->map(fn($package) => [
->map(fn ($package) => [
str($packages['metadata-url'])->replace('%package%', $package),
str($packages['metadata-url'])->replace('%package%', $package . '~dev'),
str($packages['metadata-url'])->replace('%package%', $package.'~dev'),
])
->flatten()
->map(fn($path) => json_decode(Storage::disk('temp')->get($path->start('/')->start($buildState->getTempPrefix())), true))
->map(fn ($path) => json_decode(Storage::disk('temp')->get($path->start('/')->start($buildState->getTempPrefix())), true))
->pluck('packages')
->map(function ($packages) {
return collect($packages)
->map(function ($versions, $package) {
return collect($versions)
->filter(fn($version) => isset($version['dist']['url']))
->map(fn($version) => [
->filter(fn ($version) => isset($version['dist']['url']))
->map(fn ($version) => [
'package' => $package,
'url' => $version['dist']['url'],
'version' => $version['version_normalized'],
Expand All @@ -50,7 +50,7 @@ public function hook(BuildStateInterface $buildState): void

return $package;
})
->map(function ($package) use ($url_host) {
->map(function ($package) {
$package['tags'][] = "{$package['package']}:{$package['version']}";

if (preg_match('/^(\\d)\\.(\\d)\\.(\\d)\\.(\\d)$/u', $package['version'], $matches)) {
Expand Down
5 changes: 3 additions & 2 deletions app/Extensions/Internals/BuildExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
class BuildExtension
{
public function __construct(
public string $name,
public string $name,
public ?string $key = null,
) { }
) {
}
}
3 changes: 2 additions & 1 deletion app/Extensions/Internals/BuildHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class BuildHook
{
public function __construct(
public BuildHooks $hook,
) { }
) {
}
}
8 changes: 7 additions & 1 deletion app/Extensions/Internals/BuildState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
class BuildState implements BuildStateInterface
{
protected Collection $placeholders;

public bool $last_step_executed = false;

protected Collection $config;

public function __construct(
Expand Down Expand Up @@ -53,12 +55,14 @@ public function getRepositoryUrls(): array
public function setRepositoryUrls(array $repository_urls): BuildState
{
$this->repository_urls = $repository_urls;

return $this;
}

public function addRepositoryUrl(string $repository_url): BuildState
{
$this->repository_urls[] = $repository_url;

return $this;
}

Expand All @@ -70,6 +74,7 @@ public function getPlaceholders(): Collection
public function setPlaceholders(Collection $placeholders): BuildState
{
$this->placeholders = $placeholders;

return $this;
}

Expand All @@ -81,12 +86,13 @@ public function isForceFreshDownloads(): bool
public function setForceFreshDownloads(bool $force_fresh_downloads): BuildState
{
$this->force_fresh_downloads = $force_fresh_downloads;

return $this;
}

public function isLastStepSkipped(): bool
{
return !$this->last_step_executed;
return ! $this->last_step_executed;
}

public function getConfig(): Collection
Expand Down
9 changes: 0 additions & 9 deletions app/Extensions/Internals/BuildStateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ public function getTempPrefix(): string;

public function setTempPrefix(string $prefix): self;



public function getConfigFilePath(): string;

public function setConfigFilePath(string $path): self;



public function setRepositoryUrls(array $repository_urls): BuildState;

public function getRepositoryUrls(): array;

public function addRepositoryUrl(string $repository_url): BuildState;



public function setPlaceholders(Collection $placeholders): BuildState;

public function getPlaceholders(): Collection;
Expand All @@ -34,9 +28,6 @@ public function isForceFreshDownloads(): bool;

public function setForceFreshDownloads(bool $force_fresh_downloads): BuildState;




public function isLastStepSkipped(): bool;

public function getConfig(): Collection;
Expand Down
3 changes: 2 additions & 1 deletion app/Extensions/Internals/ExtensionDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public function __construct(
public readonly string $key,
public readonly string $class_name,
public readonly Collection $hooks,
) { }
) {
}
}
Loading

0 comments on commit ac9791e

Please sign in to comment.