Skip to content

Commit

Permalink
Merge pull request #81 from codedge/#79-not-existing-versions
Browse files Browse the repository at this point in the history
Check correct commit version
  • Loading branch information
codedge authored Feb 10, 2020
2 parents c55e3ad + 781d94e commit 6f8c2be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
45 changes: 21 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,31 @@ a `private_access_token` field, where you can set the token.
## Usage
To start an update process, i. e. in a controller, just use:
```php
public function update()
{
// This downloads and install the latest version of your repo
Updater::update();

// Just download the source and do the actual update elsewhere
Updater::fetch();

// Check if a new version is available and pass current version
Updater::isNewVersionAvailable('1.2');
}
```
Route::get('/', function (\Codedge\Updater\UpdaterManager $updater) {

Of course you can inject the _updater_ via method injection:
```php
public function update(UpdaterManager $updater)
{
// Check if new version is available
if($updater->source()->isNewVersionAvailable()) {

$updater->update(); // Same as above

// .. and shorthand for this:
$updater->source()->update;

$updater->fetch(); // Same as above...
}
// Get the current installed version
$updater->source()->getVersionInstalled();

// Get the new version available
$updater->source()->getVersionAvailable();

// Run the update process
$updater->source()->update();

} else {
echo "No new version available.";
}

});
```

**Note:** Currently the fetching of the source is a _synchronous_ process.
**IMPORTANT**:
You're responsible to set the current version installed, either in the config file or better via the env variable `SELF_UPDATER_VERSION_INSTALLED`.

Currently the fetching of the source is a _synchronous_ process.
It is not run in background.

### Using Github
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Codedge\Updater\Events\UpdateAvailable;
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
use GuzzleHttp\Client;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -47,20 +49,31 @@ public function fetch(string $version = '')
File::makeDirectory($this->storagePath, 493, true, true);
}

$release = $releaseCollection->first();
$release = $this->selectRelease($releaseCollection, $version);

if (! empty($version)) {
$release = $releaseCollection->where('sha', $version)->first();
}

$storageFolder = $this->storagePath.$release->sha.'-'.now()->timestamp;
$storageFolder = $this->storagePath.$release->commit->author->date.'-'.now()->timestamp;
$storageFilename = $storageFolder.'.zip';

if (! $this->isSourceAlreadyFetched($release->sha)) {
if (! $this->isSourceAlreadyFetched($release->commit->author->date)) {
$this->downloadRelease($this->client, $this->generateZipUrl($release->sha), $storageFilename);
$this->unzipArchive($storageFilename, $storageFolder);
$this->createReleaseFolder($storageFolder, $release->sha);
$this->createReleaseFolder($storageFolder, $release->commit->author->date);
}
}

public function selectRelease(Collection $collection, string $version)
{
$release = $collection->first();

if (! empty($version)) {
if ($collection->contains('commit.author.date', $version)) {
$release = $collection->where('commit.author.date', $version)->first();
} else {
Log::info('No release for version "'.$version.'" found. Selecting latest.');
}
}

return $release;
}

/**
Expand All @@ -85,8 +98,6 @@ public function isNewVersionAvailable($currentVersion = ''): bool

$versionAvailable = $this->getVersionAvailable();

//dd($version, $versionAvailable, version_compare($version, $versionAvailable, '<'));

if (version_compare($version, $versionAvailable, '<')) {
if (! $this->versionFileExists()) {
$this->setVersionFile($versionAvailable);
Expand Down

0 comments on commit 6f8c2be

Please sign in to comment.