Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from git #35

Merged
merged 3 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
an updater with git
  • Loading branch information
Zakarialabib committed Feb 5, 2023
commit cf6742bbf09205933ca1dc6c66824bfc862146d2
60 changes: 60 additions & 0 deletions app/Helpers/GitHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\Helpers;

class GitHandler
{
private $message;

public function fetchAndPull()
{
$branch = env('GIT_BRANCH', 'master');

exec("git fetch origin $branch", $output, $return_var);

if ($return_var === 0) {
$this->message = "Fetched updates from origin/$branch.";
exec("git reset --hard FETCH_HEAD", $output, $return_var);
if ($return_var === 0) {
$this->message = "Hard reset to latest updates from origin/$branch.";
exec("git clean -df", $output, $return_var);
if ($return_var === 0) {
$this->message = "Cleaned up untracked files and directories.";
} else {
$this->message = "Error cleaning untracked files and directories.";
}
} else {
$this->message = "Error resetting to latest updates from origin/$branch.";
}
} else {
$this->message = "Error fetching updates from origin/$branch.";
}

return $this->message;
Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved
}

public function pushChanges()
{
// Check if there are any changes to push
exec("git diff --exit-code", $diff, $return_var);

if ($return_var === 0) {
$this->message = "No changes to push.";
} else {
// Get the repository URL and branch from the .env file
$repoUrl = env('REPO_URL');
$repoBranch = env('REPO_BRANCH');

// Push the changes to the remote repository
exec("git push $repoUrl $repoBranch", $output, $return_var);

if ($return_var === 0) {
$this->message = "Changes pushed successfully.";
} else {
$this->message = "Error pushing changes.";
}
}
Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved
}
}
50 changes: 50 additions & 0 deletions app/Http/Livewire/Settings/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Livewire\Settings;

use App\Helpers\GitHandler;
use Livewire\Component;
use Illuminate\Support\Facades\Artisan;

class Update extends Component
{
protected $listeners = [
'updateProject'
];

public $message = "";

public function updateProject()
{

$gitHandler = new GitHandler();
$updated = $gitHandler->fetchAndPull();
// $updatedPush = $gitHandler->pushChanges();
Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved

if ($updated) {
return "Project featched";
} elseif ($updatedPush){
return "updating project";
} else {
return "Error updating project";
}

Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved
}

public function mount()
{
$this->message = 'Update your system with the latest version';
Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved
}

public function optimize()
{
// Artisan::call('optimize:clear');
// Artisan::call('optimize');
// exec("composer dump");
}
Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved

public function render()
{
return view('livewire.settings.update');
}
}
4 changes: 3 additions & 1 deletion resources/views/admin/settings/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
<span>{{ __('Settings') }}</span>
</a>
</div>
</div>
</div>
@livewire('settings.update')
</div>
</section>
@endsection
Expand Down Expand Up @@ -134,4 +135,5 @@ class="bi bi-check"></i> {{ __('Save Changes') }}</button>
</div>
</x-card>
</div>

</x-app-layout>
23 changes: 23 additions & 0 deletions resources/views/livewire/settings/update.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div x-data="{ update: false }">
<x-button wire:click="updateProject" primary type="button" @click="update = true">
{{ __('System Update') }}
</x-button>

<div x-show="update" class="h-screen w-screen fixed top-0 left-0" x-show="show"
x-transition:enter="ease-in-out duration-300" x-transition:enter-start="opacity-0"
x-transition:enter-end="opacity-100" x-transition:leave="ease-in-out duration-300"
x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0"
style="background-color: rgba(255, 255, 255, 0.5);">
<div class="flex flex-col justify-center items-center h-full w-full">
<div class="w-10 h-10 rounded-full border-4 border-dashed border-gray-400 animate-pulse"></div>
@if ($message)
<div class="text-gray-600">
{{ $message }}
</div>
@endif
<x-button type="" x-on:click="update = false" primary>
{{('Close')}}
</x-button>
</div>
</div>
</div>
Zakarialabib marked this conversation as resolved.
Show resolved Hide resolved