Skip to content

Commit

Permalink
Merge pull request #60 from mehrancodes/enahnce-forge-environemnt-var…
Browse files Browse the repository at this point in the history
…iables-modifier

WIP: Enahnce forge environemnt variables modifier
  • Loading branch information
mehrancodes authored Feb 19, 2024
2 parents f8ca17c + 2bcd578 commit 3d5422e
Show file tree
Hide file tree
Showing 8 changed files with 1,040 additions and 742 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run Pest Tests
on: [ pull_request ]
jobs:
run-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3

- name: Install dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist

- name: Run Pest tests
run: ./vendor/bin/pest

- name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
28 changes: 0 additions & 28 deletions app/Actions/ArrayToText.php

This file was deleted.

68 changes: 68 additions & 0 deletions app/Actions/MergeEnvironmentVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/**
* This file is part of Veyoze CLI.
*
* (c) Mehran Rasulian <mehran.rasulian@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Actions;

use App\Traits\Outputifier;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;

class MergeEnvironmentVariables
{
use AsAction;
use Outputifier;

public function handle(string $source, array $newVariables): string
{
$output = '';

if (! empty($source)) {
$output = $this->searchReplaceExistingVariables($source, $newVariables);
}

foreach ($newVariables as $newKey => $newValue) {
$output .= "$newKey=$newValue\n";
}

return $output;
}

protected function searchReplaceExistingVariables(string $source, array &$newVariables): string
{
$separator = Str::contains($source, ';') ? ';' : "\n";
$output = '';

foreach (explode($separator, $source) as $variable) {
if (empty($variable)) {
$output .= "\n";

continue;
}

[$key, $value] = explode('=', $variable, 2);

if (empty($key)) {
$this->warning("No key found for the assigned value \"$value\" inside your environment variables! Make sure to remove it.");

continue;
}

$value = array_key_exists($key, $newVariables) ? Arr::pull($newVariables, $key) : $value;

$output .= "$key=$value\n";
}

return $output;
}
}
10 changes: 3 additions & 7 deletions app/Services/Forge/Pipeline/UpdateEnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace App\Services\Forge\Pipeline;

use App\Actions\ArrayToText;
use App\Actions\MergeEnvironmentVariables;
use App\Actions\TextToArray;
use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
Expand Down Expand Up @@ -42,12 +42,8 @@ public function __invoke(ForgeService $service, Closure $next)

protected function getBothEnvsMerged(ForgeService $service, array $newKeys): string
{
$sourceKeys = TextToArray::run(
$service->forge->siteEnvironmentFile($service->server->id, $service->site->id)
);
$source = $service->forge->siteEnvironmentFile($service->server->id, $service->site->id);

return ArrayToText::run(
array_merge($sourceKeys, $newKeys)
);
return MergeEnvironmentVariables::run($source, $newKeys);
}
}
20 changes: 17 additions & 3 deletions app/Traits/Outputifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protected function information(string $message): int
{
render(sprintf(<<<'html'
<div class="font-bold">
<span class="bg-blue px-2 text-white mr-1">
<span class="bg-blue-400 px-2 text-white mr-1">
INFO
</span>
%s
Expand All @@ -24,7 +24,7 @@ protected function fail(string $message): int
{
render(sprintf(<<<'html'
<div class="font-bold">
<span class="bg-red px-2 text-white mr-1">
<span class="bg-red-400 px-2 text-white mr-1">
FAIL
</span>
%s
Expand All @@ -34,11 +34,25 @@ protected function fail(string $message): int
return 0;
}

protected function warning(string $message): int
{
render(sprintf(<<<'html'
<div class="font-bold">
<span class="bg-orange-400 px-2 text-white mr-1">
WARNING
</span>
%s
</div>
html, trim($message)));

return 0;
}

protected function success(string $message): int
{
render(sprintf(<<<'html'
<div class="font-bold">
<span class="bg-green px-2 text-white mr-1">
<span class="bg-green-400 px-2 text-white mr-1">
SUCCESS
</span>
%s
Expand Down
Loading

0 comments on commit 3d5422e

Please sign in to comment.