Skip to content

Commit 6f8c2be

Browse files
authored
Merge pull request #81 from codedge/#79-not-existing-versions
Check correct commit version
2 parents c55e3ad + 781d94e commit 6f8c2be

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

README.md

+21-24
Original file line numberDiff line numberDiff line change
@@ -145,34 +145,31 @@ a `private_access_token` field, where you can set the token.
145145
## Usage
146146
To start an update process, i. e. in a controller, just use:
147147
```php
148-
public function update()
149-
{
150-
// This downloads and install the latest version of your repo
151-
Updater::update();
152-
153-
// Just download the source and do the actual update elsewhere
154-
Updater::fetch();
155-
156-
// Check if a new version is available and pass current version
157-
Updater::isNewVersionAvailable('1.2');
158-
}
159-
```
148+
Route::get('/', function (\Codedge\Updater\UpdaterManager $updater) {
160149

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

166-
$updater->update(); // Same as above
167-
168-
// .. and shorthand for this:
169-
$updater->source()->update;
170-
171-
$updater->fetch(); // Same as above...
172-
}
153+
// Get the current installed version
154+
$updater->source()->getVersionInstalled();
155+
156+
// Get the new version available
157+
$updater->source()->getVersionAvailable();
158+
159+
// Run the update process
160+
$updater->source()->update();
161+
162+
} else {
163+
echo "No new version available.";
164+
}
165+
166+
});
173167
```
174168

175-
**Note:** Currently the fetching of the source is a _synchronous_ process.
169+
**IMPORTANT**:
170+
You're responsible to set the current version installed, either in the config file or better via the env variable `SELF_UPDATER_VERSION_INSTALLED`.
171+
172+
Currently the fetching of the source is a _synchronous_ process.
176173
It is not run in background.
177174

178175
### Using Github

src/SourceRepositoryTypes/GithubRepositoryTypes/GithubBranchType.php

+21-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Codedge\Updater\Events\UpdateAvailable;
99
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
1010
use GuzzleHttp\Client;
11+
use Illuminate\Support\Collection;
1112
use Illuminate\Support\Facades\File;
13+
use Illuminate\Support\Facades\Log;
1214
use Illuminate\Support\Str;
1315
use InvalidArgumentException;
1416
use Psr\Http\Message\ResponseInterface;
@@ -47,20 +49,31 @@ public function fetch(string $version = '')
4749
File::makeDirectory($this->storagePath, 493, true, true);
4850
}
4951

50-
$release = $releaseCollection->first();
52+
$release = $this->selectRelease($releaseCollection, $version);
5153

52-
if (! empty($version)) {
53-
$release = $releaseCollection->where('sha', $version)->first();
54-
}
55-
56-
$storageFolder = $this->storagePath.$release->sha.'-'.now()->timestamp;
54+
$storageFolder = $this->storagePath.$release->commit->author->date.'-'.now()->timestamp;
5755
$storageFilename = $storageFolder.'.zip';
5856

59-
if (! $this->isSourceAlreadyFetched($release->sha)) {
57+
if (! $this->isSourceAlreadyFetched($release->commit->author->date)) {
6058
$this->downloadRelease($this->client, $this->generateZipUrl($release->sha), $storageFilename);
6159
$this->unzipArchive($storageFilename, $storageFolder);
62-
$this->createReleaseFolder($storageFolder, $release->sha);
60+
$this->createReleaseFolder($storageFolder, $release->commit->author->date);
61+
}
62+
}
63+
64+
public function selectRelease(Collection $collection, string $version)
65+
{
66+
$release = $collection->first();
67+
68+
if (! empty($version)) {
69+
if ($collection->contains('commit.author.date', $version)) {
70+
$release = $collection->where('commit.author.date', $version)->first();
71+
} else {
72+
Log::info('No release for version "'.$version.'" found. Selecting latest.');
73+
}
6374
}
75+
76+
return $release;
6477
}
6578

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

8699
$versionAvailable = $this->getVersionAvailable();
87100

88-
//dd($version, $versionAvailable, version_compare($version, $versionAvailable, '<'));
89-
90101
if (version_compare($version, $versionAvailable, '<')) {
91102
if (! $this->versionFileExists()) {
92103
$this->setVersionFile($versionAvailable);

0 commit comments

Comments
 (0)