Skip to content

Commit c55e3ad

Browse files
authored
Merge pull request #76 from codedge/#71-branch-version
Update based on branch
2 parents 8ee7976 + 954dfa2 commit c55e3ad

27 files changed

+1607
-417
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
clover.xml
12
build
23
vendor

.styleci.yml

-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ preset: laravel
22

33
risky: false
44

5-
enabled:
6-
- no_useless_else
7-
- phpdoc_align
8-
- phpdoc_no_empty_return
9-
- phpdoc_order
10-
- phpdoc_separation
11-
125
finder:
136
exclude:
147
- "resources"

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function update(UpdaterManager $updater)
168168
// .. and shorthand for this:
169169
$updater->source()->update;
170170

171-
$updater->fetch() // Same as above...
171+
$updater->fetch(); // Same as above...
172172
}
173173
```
174174

@@ -183,6 +183,28 @@ of your software.
183183
Just make sure you set the proper repository in your `config/self-updater.php`
184184
file.
185185

186+
#### Tag-based updates
187+
188+
This is the default. Updates will be fetched by using a tagged commit, aka release.
189+
190+
#### Branch-based updates
191+
192+
Select the branch that should be used via the `use_branch` setting [inside the configuration](https://github.com/codedge/laravel-selfupdater/blob/master/config/self-update.php).
193+
194+
```php
195+
// ...
196+
'repository_types' => [
197+
'github' => [
198+
'type' => 'github',
199+
'repository_vendor' => env('SELF_UPDATER_REPO_VENDOR', ''),
200+
'repository_name' => env('SELF_UPDATER_REPO_NAME', ''),
201+
// ...
202+
'use_branch' => 'v2',
203+
],
204+
// ...
205+
];
206+
```
207+
186208
### Using Http archives
187209
The package comes with an _Http_ source repository type to fetch
188210
releases from an HTTP directory listing containing zip archives.

composer.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@
4949
},
5050
"require": {
5151
"php": ">=7.2",
52+
"ext-json": "*",
5253
"ext-zip": "*",
5354
"laravel/framework": "^5.8|6.*",
5455
"guzzlehttp/guzzle": "6.*"
5556
},
5657
"require-dev": {
57-
"phpunit/phpunit": "^8.0",
58+
"dg/bypass-finals": "^1.1",
5859
"mockery/mockery": "^1.0",
59-
"orchestra/testbench": "^4.0"
60+
"orchestra/testbench": "^4.0",
61+
"phpunit/phpunit": "^8.0"
62+
},
63+
"scripts": {
64+
"test": "./vendor/bin/phpunit"
6065
}
6166
}

composer.lock

+50-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/self-update.php

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'repository_url' => '',
4545
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
4646
'private_access_token' => env('SELF_UPDATER_GITHUB_PRIVATE_ACCESS_TOKEN', ''),
47+
'use_branch' => env('SELF_UPDATER_USE_BRANCH', ''),
4748
],
4849
'http' => [
4950
'type' => 'http',

phpunit.xml.dist

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
processIsolation="false"
1111
stopOnFailure="true">
1212

13+
<extensions>
14+
<extension class="Codedge\Updater\Tests\Hooks\BypassFinalHook"/>
15+
</extensions>
16+
1317
<testsuites>
1418
<testsuite name="Laravel Application Self-Updater Test Suite">
1519
<directory>tests</directory>

src/AbstractRepositoryType.php

+23-64
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Codedge\Updater;
66

77
use Codedge\Updater\Events\HasWrongPermissions;
8+
use Exception;
89
use GuzzleHttp\Client;
910
use Illuminate\Support\Facades\File;
1011
use Symfony\Component\Finder\Finder;
@@ -17,22 +18,20 @@
1718
*/
1819
abstract class AbstractRepositoryType
1920
{
20-
const ACCESS_TOKEN_PREFIX = 'Bearer ';
21-
2221
/**
2322
* @var array
2423
*/
2524
protected $config;
2625

2726
/**
28-
* Access token for private repository access.
27+
* @var Finder|SplFileInfo[]
2928
*/
30-
private $accessToken = '';
29+
protected $pathToUpdate;
3130

3231
/**
33-
* @var Finder|SplFileInfo[]
32+
* @var string
3433
*/
35-
protected $pathToUpdate;
34+
public $storagePath;
3635

3736
/**
3837
* Unzip an archive.
@@ -41,7 +40,7 @@ abstract class AbstractRepositoryType
4140
* @param string $targetDir
4241
* @param bool $deleteZipArchive
4342
*
44-
* @throws \Exception
43+
* @throws Exception
4544
*
4645
* @return bool
4746
*/
@@ -55,7 +54,7 @@ protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive =
5554
$res = $zip->open($file);
5655

5756
if (! $res) {
58-
throw new \Exception("Cannot open zip archive [{$file}].");
57+
throw new Exception("Cannot open zip archive [{$file}].");
5958
}
6059

6160
if (empty($targetDir)) {
@@ -76,14 +75,14 @@ protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive =
7675
/**
7776
* Check a given directory recursively if all files are writeable.
7877
*
79-
* @throws \Exception
78+
* @throws Exception
8079
*
8180
* @return bool
8281
*/
8382
protected function hasCorrectPermissionForUpdate(): bool
8483
{
8584
if (! $this->pathToUpdate) {
86-
throw new \Exception('No directory set for update. Please set the update with: setPathToUpdate(path).');
85+
throw new Exception('No directory set for update. Please set the update with: setPathToUpdate(path).');
8786
}
8887

8988
$collection = collect($this->pathToUpdate->files())->each(function ($file) { /* @var \SplFileInfo $file */
@@ -101,12 +100,12 @@ protected function hasCorrectPermissionForUpdate(): bool
101100
* Download a file to a given location.
102101
*
103102
* @param Client $client
104-
* @param string $source
103+
* @param string $source Url for the source (.zip)
105104
* @param string $storagePath
106105
*
107106
* @return mixed|\Psr\Http\Message\ResponseInterface
108107
*/
109-
protected function downloadRelease(Client $client, $source, $storagePath)
108+
protected function downloadRelease(Client $client, string $source, $storagePath)
110109
{
111110
$headers = [];
112111

@@ -158,64 +157,24 @@ protected function setPathToUpdate(string $path, array $exclude)
158157
}
159158

160159
/**
161-
* Create a releas sub-folder inside the storage dir.
160+
* Create a release sub-folder inside the storage dir.
162161
*
163-
* @param string $storagePath
162+
* @param string $releaseFolder
164163
* @param string $releaseName
165164
*/
166-
public function createReleaseFolder($storagePath, $releaseName)
165+
public function createReleaseFolder(string $releaseFolder, $releaseName)
167166
{
168-
$subDirName = File::directories($storagePath);
169-
$directories = File::directories($subDirName[0]);
170-
171-
File::makeDirectory($storagePath.'/'.$releaseName);
172-
173-
foreach ($directories as $directory) { /* @var string $directory */
174-
File::moveDirectory($directory, $storagePath.'/'.$releaseName.'/'.File::name($directory));
175-
}
167+
$folders = File::directories($releaseFolder);
176168

177-
$files = File::allFiles($subDirName[0], true);
178-
foreach ($files as $file) { /* @var \SplFileInfo $file */
179-
File::move($file->getRealPath(), $storagePath.'/'.$releaseName.'/'.$file->getFilename());
180-
}
181-
182-
File::deleteDirectory($subDirName[0]);
183-
}
184-
185-
/**
186-
* Get the access token.
187-
*
188-
* @param bool $withPrefix
189-
*
190-
* @return string
191-
*/
192-
public function getAccessToken($withPrefix = true): string
193-
{
194-
if ($withPrefix) {
195-
return self::ACCESS_TOKEN_PREFIX.$this->accessToken;
169+
if (count($folders) === 1) {
170+
// Only one sub-folder inside extracted directory
171+
File::moveDirectory($folders[0], $this->storagePath.$releaseName);
172+
File::deleteDirectory($folders[0]);
173+
File::deleteDirectory($releaseFolder);
174+
} else {
175+
// Release (with all files and folders) is already inside, so we need to only rename the folder
176+
File::moveDirectory($releaseFolder, $this->storagePath.$releaseName);
196177
}
197-
198-
return $this->accessToken;
199-
}
200-
201-
/**
202-
* Set access token.
203-
*
204-
* @param string $token
205-
*/
206-
public function setAccessToken(string $token): void
207-
{
208-
$this->accessToken = $token;
209-
}
210-
211-
/**
212-
* Check if an access token has been set.
213-
*
214-
* @return bool
215-
*/
216-
public function hasAccessToken(): bool
217-
{
218-
return ! empty($this->accessToken);
219178
}
220179

221180
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codedge\Updater\Contracts;
6+
7+
interface GithubRepositoryTypeContract extends SourceRepositoryTypeContract
8+
{
9+
const GITHUB_API_URL = 'https://api.github.com';
10+
const GITHUB_URL = 'https://github.com';
11+
}

src/Contracts/SourceRepositoryTypeContract.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codedge\Updater\Contracts;
46

57
interface SourceRepositoryTypeContract
@@ -11,14 +13,16 @@ interface SourceRepositoryTypeContract
1113
*
1214
* @return mixed
1315
*/
14-
public function fetch($version = '');
16+
public function fetch(string $version = '');
1517

1618
/**
1719
* Perform the actual update process.
1820
*
21+
* @param string $version
22+
*
1923
* @return bool
2024
*/
21-
public function update(): bool;
25+
public function update(string $version = ''): bool;
2226

2327
/**
2428
* Check repository if a newer version than the installed one is available.
@@ -29,7 +33,7 @@ public function update(): bool;
2933
*
3034
* @return bool
3135
*/
32-
public function isNewVersionAvailable($currentVersion = ''): bool;
36+
public function isNewVersionAvailable(string $currentVersion = ''): bool;
3337

3438
/**
3539
* Get the version that is currenly installed.
@@ -40,7 +44,7 @@ public function isNewVersionAvailable($currentVersion = ''): bool;
4044
*
4145
* @return string
4246
*/
43-
public function getVersionInstalled($prepend = '', $append = ''): string;
47+
public function getVersionInstalled(string $prepend = '', string $append = ''): string;
4448

4549
/**
4650
* Get the latest version that has been published in a certain repository.
@@ -51,5 +55,5 @@ public function getVersionInstalled($prepend = '', $append = ''): string;
5155
*
5256
* @return string
5357
*/
54-
public function getVersionAvailable($prepend = '', $append = ''): string;
58+
public function getVersionAvailable(string $prepend = '', string $append = ''): string;
5559
}

0 commit comments

Comments
 (0)