Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Oct 18, 2024
2 parents 74164c3 + 44cdbff commit 4461a8d
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Documentation for Laravel Auto Updater package
---

# Laravel Auto Updater

A robust Laravel package facilitating automatic updates from GitHub, GitLab, Bitbucket, or custom repositories for your Laravel applications. **Supports Laravel version 10 and above.**
Expand Down Expand Up @@ -73,11 +74,11 @@ Add these to your `.env` file:

```dotenv
RELEASE_URL=https://github.com/anisAronno/laravel-starter
PURCHASE_KEY=your_optional_purchase_key
LICENSE_KEY=your_optional_purchase_key
```

- `RELEASE_URL`: Your repository's release URL
- `PURCHASE_KEY`: (Optional) For authenticated APIs or private repos
- `LICENSE_KEY`: (Optional) For authenticated APIs or private repos

### Config File

Expand Down
149 changes: 84 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Laravel Self Updater

A robust Laravel package facilitating automatic updates from GitHub, GitLab, Bitbucket, or custom repositories for your Laravel applications.

**Supports Laravel version 10 and above.**

## Table of Contents
Expand All @@ -13,55 +14,53 @@ A robust Laravel package facilitating automatic updates from GitHub, GitLab, Bit
- [Environment Variables](#environment-variables)
- [Config File](#config-file)
- [Excluding Items from Updates](#excluding-items-from-updates)
- [Set Middleware in the Config File](#set-middleware-in-the-config-file)
- [Setting Middleware](#setting-middleware)
- [Application Version](#application-version)
- [Composer Dependencies](#composer-dependencies)
- [Custom VCS Providers](#custom-vcs-providers)
- [Usage](#usage)
- [Update Check Command](#update-check-command)
- [Update Initiate Command](#update-initiate-command)
- [Scheduling Updates](#scheduling-updates)
- [Modified Files Warning](#modified-files-warning)
- [Custom Update URL](#custom-update-url)
- [API Endpoints](#api-endpoints)
- [Checking for Updates](#checking-for-updates)
- [Initiating Updates](#initiating-updates)
- [Scheduling Automatic Updates](#scheduling-automatic-updates)
- [Handling Modified Files](#handling-modified-files)
- [Custom Update Sources](#custom-update-sources)
- [API Integration](#api-integration)
- [Blade Component](#blade-component)
- [Contribution Guidelines](#contribution-guidelines)
- [Contributing](#contributing)
- [License](#license)

## Features

- Multi-source support: Update from GitHub, GitLab, Bitbucket, or custom repositories
- Simple configuration via environment variables and config file
- Built-in commands for update checks and initiation
- Exclusion of sensitive files/folders from updates
- Comprehensive error handling and logging
- Version tracking through `composer.json`
- Global Blade component for easy integration
- API endpoints for programmatic update management
- Configurable middleware for API security
- Optional Composer dependencies management during updates
- **Multi-source Support**: Update from GitHub, GitLab, Bitbucket, or custom repositories
- **Simple Configuration**: Easy setup via environment variables and config file
- **Built-in Commands**: Convenient commands for update checks and initiation
- **File Exclusion**: Protect sensitive files/folders during updates
- **Error Handling**: Comprehensive logging and error management
- **Version Tracking**: Utilizes `composer.json` for version management
- **UI Integration**: Global Blade component for easy frontend implementation
- **API Endpoints**: Programmatic update management
- **Security**: Configurable middleware for API protection
- **Composer Integration**: Optional management of Composer dependencies during updates
- **Extensibility**: Support for custom VCS providers

## Installation

Install the package via Composer:

```bash
composer require anisaronno/laravel-self-updater
```
1. Install the package via Composer:
```bash
composer require anisaronno/laravel-self-updater
```

Publish the configuration file:
2. Publish the configuration file:
```bash
php artisan vendor:publish --tag=self-updater-config
```
This creates `self-updater.php` in your `config` directory.

```bash
php artisan vendor:publish --tag=self-updater-config
```

This creates `self-updater.php` in your `config` directory.

Optionally, publish assets and views:

```bash
php artisan vendor:publish --tag=self-updater-assets
php artisan vendor:publish --tag=self-updater-views
```
3. (Optional) Publish assets and views:
```bash
php artisan vendor:publish --tag=self-updater-assets
php artisan vendor:publish --tag=self-updater-views
```

## Configuration

Expand All @@ -71,20 +70,20 @@ Add these to your `.env` file:

```dotenv
RELEASE_URL=https://github.com/anisAronno/laravel-starter
PURCHASE_KEY=your_optional_purchase_key
LICENSE_KEY=your_optional_purchase_key
```

- `RELEASE_URL`: Your repository's release URL
- `PURCHASE_KEY`: (Optional) For authenticated APIs or private repos
- `LICENSE_KEY`: (Optional) For authenticated APIs or private repos

### Config File

The `config/self-updater.php` file contains important settings:

- **Repository Configuration**: The file uses `VCSProviderFactory` to create an appropriate adapter based on your `RELEASE_URL`.
- **Excluded Items**: Define files and folders to exclude from updates.
- **Middleware**: Specify which middleware to apply to the self-updater's API endpoints.
- **Composer Dependencies**: Configure whether to run Composer install or update during the update process.
1. **Repository Configuration**: Uses `VCSProviderFactory` to create an appropriate adapter based on your `RELEASE_URL`.
2. **Excluded Items**: Define files and folders to exclude from updates.
3. **Middleware**: Specify which middleware to apply to the self-updater's API endpoints.
4. **Composer Dependencies**: Configure whether to run Composer install or update during the update process.

### Excluding Items from Updates

Expand All @@ -97,20 +96,18 @@ Edit the `exclude_items` array in `config/self-updater.php`:
'storage',
'node_modules',
'vendor',
// Add custom exclusions here
// Add your custom exclusions here
],
```

### Set Middleware in the Config File
### Setting Middleware

To configure the middleware, edit the `middleware` array in the `config/self-updater.php` file:
Configure the middleware in `config/self-updater.php`:

```php
"middleware" => ['web'],
```

By default, the middleware is set to `web`.

### Application Version

Specify your app's version in `composer.json`:
Expand All @@ -123,47 +120,69 @@ Specify your app's version in `composer.json`:

### Composer Dependencies

Configure whether to run Composer install or update during the update process:
Configure Composer behavior during updates in `config/self-updater.php`:

```php
'require_composer_install' => false,
'require_composer_update' => false,
```

Set these to `true` if you want to run Composer install or update respectively during the update process.
Set these to `true` to run Composer install or update respectively during the update process.

### Custom VCS Providers

Extend functionality with custom VCS providers:

```php
use AnisAronno\LaravelSelfUpdater\Services\VCSProvider\VCSProviderFactory;

// Register a new provider
VCSProviderFactory::registerProvider('custom-vcs.com', YourCustomVCSProvider::class);

// Remove a provider
VCSProviderFactory::removeProvider('custom-vcs.com');

// Check if a provider is registered
$isRegistered = VCSProviderFactory::hasProvider('custom-vcs.com');

// Get all registered providers
$providers = VCSProviderFactory::getProviders();
```

Ensure your custom provider implements `VCSProviderInterface`.

After making configuration changes, refresh the config cache:
After configuration changes, refresh the config cache:

```bash
php artisan config:cache
```

## Usage

### Update Check Command
### Checking for Updates

Check for available updates:
Run the following command to check for available updates:

```bash
php artisan update:check
```

### Update Initiate Command
### Initiating Updates

Start the update process:
To start the update process, use:

```bash
php artisan update:initiate
```

### Scheduling Updates
### Scheduling Automatic Updates

Add to `app/Console/Kernel.php` (Laravel 10):
For Laravel 10, add to `app/Console/Kernel.php`:

```php
protected function schedule(Schedule $schedule)
{
$schedule->command('update:initiate')->daily();
$schedule->command('update:initiate')->dailyAt('01:00');
}
```

Expand All @@ -172,14 +191,14 @@ For Laravel 11+, add to `routes/console.php`:
```php
use Illuminate\Support\Facades\Schedule;

Schedule::command('update:initiate')->daily();
Schedule::command('update:initiate')->dailyAt('01:00');
```

### Modified Files Warning
### Handling Modified Files

The updater warns about modified project files, excluding `.env` and `storage/`.
The updater will warn about modified project files, excluding `.env` and `storage/`.

## Custom Update URL
## Custom Update Sources

For custom update sources, ensure your API returns:

Expand All @@ -192,7 +211,7 @@ For custom update sources, ensure your API returns:
}
```

## API Endpoints
## API Integration

Access these endpoints for programmatic updates:

Expand All @@ -203,16 +222,16 @@ These endpoints are protected by the middleware specified in the config file.

## Blade Component

Use the global component anywhere in your views:
Use the global component in your views:

```blade
<x-self-updater />
```

## Contribution Guidelines
## Contributing

We welcome contributions! Please see our [Contribution Guide](https://github.com/anisAronno/laravel-self-updater/blob/develop/CONTRIBUTING.md) for details.

## License

This package is open-source software licensed under the [MIT License](https://opensource.org/licenses/MIT).
This package is open-source software licensed under the [MIT License](https://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion config/self-updater-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
return [
'release_url' => env('RELEASE_URL', 'https://github.com/anisAronno/laravel-starter'),
'purchase_key' => env('PURCHASE_KEY', null),
'license_key' => env('LICENSE_KEY') ?? env('PURCHASE_KEY', null),
'request_timeout' => 120,
'exclude_items' => [
'.env',
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/CheckUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function handle()

return Command::SUCCESS;
} catch (\Throwable $th) {
$this->error('An error occurred while checking for updates: '.$th->getMessage());
$this->error('Error: '.$th->getMessage());

return Command::FAILURE;
}
Expand Down
28 changes: 20 additions & 8 deletions src/Services/VCSProvider/AbstractVCSProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AnisAronno\LaravelSelfUpdater\Contracts\VCSProviderInterface;
use AnisAronno\LaravelSelfUpdater\Services\ApiRequestService;
use Illuminate\Http\Client\RequestException;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -49,14 +50,20 @@ abstract protected function parseReleaseData(array $data): array;
*/
protected function fetchReleaseData(?string $version = null): array
{
$url = $this->buildApiUrl($version);
$response = $this->makeApiRequest($url);

if (empty($response)) {
throw new InvalidArgumentException('Failed to fetch release data.');
try {
$url = $this->buildApiUrl($version);
$response = $this->makeApiRequest($url);

if (empty($response)) {
throw new InvalidArgumentException('No release data found.');
}

return $this->parseReleaseData($response);
} catch (RequestException $e) {
throw new InvalidArgumentException('Request failed: '.$e->getMessage(), 0, $e);
} catch (\Throwable $e) {
throw new InvalidArgumentException('An unexpected error occurred: '.$e->getMessage(), 0, $e);
}

return $this->parseReleaseData($response);
}

/**
Expand All @@ -66,13 +73,18 @@ abstract protected function buildApiUrl(?string $version): string;

/**
* Make an API request.
*
* @param string $url The API URL.
* @return array The API response data.
*
* @throws RequestException If an error occurs.
*/
protected function makeApiRequest(string $url): array
{
$response = ApiRequestService::get($url);

if ($response->failed()) {
return [];
$response->throw();
}

return $response->json();
Expand Down
Loading

0 comments on commit 4461a8d

Please sign in to comment.