Skip to content

Commit 9730c1f

Browse files
committed
Update mechanism can use either tags or commits/branches
1 parent cae8c5b commit 9730c1f

15 files changed

+488
-307
lines changed

.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"

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"require": {
5151
"php": ">=7.2",
52+
"ext-json": "*",
5253
"ext-zip": "*",
5354
"laravel/framework": "^5.8|6.*",
5455
"guzzlehttp/guzzle": "6.*"

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',

src/AbstractRepositoryType.php

+24-62
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,65 +157,27 @@ 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-
}
176-
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-
}
167+
$folders = File::directories($releaseFolder);
184168

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;
199178
}
200179

201-
/**
202-
* Set access token.
203-
*
204-
* @param string $token
205-
*/
206-
public function setAccessToken(string $token): void
207-
{
208-
$this->accessToken = $token;
209-
}
210180

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);
219-
}
220181

221182
/**
222183
* Check if files in one array (i. e. directory) are also exist in a second one.
@@ -230,4 +191,5 @@ public function isDirectoryExcluded(array $directory, array $excludedDirs): bool
230191
{
231192
return count(array_intersect($directory, $excludedDirs)) ? true : false;
232193
}
194+
233195
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Contracts;
4+
5+
interface GithubRepositoryTypeContract extends SourceRepositoryTypeContract
6+
{
7+
const GITHUB_API_URL = 'https://api.github.com';
8+
const GITHUB_URL = 'https://github.com';
9+
}

src/Contracts/SourceRepositoryTypeContract.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Codedge\Updater\Contracts;
44

@@ -11,14 +11,16 @@ interface SourceRepositoryTypeContract
1111
*
1212
* @return mixed
1313
*/
14-
public function fetch($version = '');
14+
public function fetch(string $version = '');
1515

1616
/**
1717
* Perform the actual update process.
1818
*
19+
* @param string $version
20+
*
1921
* @return bool
2022
*/
21-
public function update(): bool;
23+
public function update(string $version = ''): bool;
2224

2325
/**
2426
* Check repository if a newer version than the installed one is available.
@@ -29,7 +31,7 @@ public function update(): bool;
2931
*
3032
* @return bool
3133
*/
32-
public function isNewVersionAvailable($currentVersion = ''): bool;
34+
public function isNewVersionAvailable(string $currentVersion = ''): bool;
3335

3436
/**
3537
* Get the version that is currenly installed.
@@ -40,7 +42,7 @@ public function isNewVersionAvailable($currentVersion = ''): bool;
4042
*
4143
* @return string
4244
*/
43-
public function getVersionInstalled($prepend = '', $append = ''): string;
45+
public function getVersionInstalled(string $prepend = '', string $append = ''): string;
4446

4547
/**
4648
* Get the latest version that has been published in a certain repository.
@@ -51,5 +53,5 @@ public function getVersionInstalled($prepend = '', $append = ''): string;
5153
*
5254
* @return string
5355
*/
54-
public function getVersionAvailable($prepend = '', $append = ''): string;
56+
public function getVersionAvailable(string $prepend = '', string $append = ''): string;
5557
}

src/Contracts/UpdaterContract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface UpdaterContract
1111
*
1212
* @return mixed
1313
*/
14-
public function source($name = '');
14+
public function source(string $name = '');
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Exceptions;
4+
5+
use Exception;
6+
use Illuminate\Support\Facades\Log;
7+
8+
final class InvalidRepositoryException extends Exception
9+
{
10+
public function report(): void
11+
{
12+
Log::error('The vendor and/or name of the repository is invalid.');
13+
}
14+
}

0 commit comments

Comments
 (0)