-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathupdate-foundation
143 lines (111 loc) · 3.47 KB
/
update-foundation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/php
<?php
set_time_limit(0);
define('GITHUB_API_LARAVEL_TAGS', 'https://api.github.com/repos/laravel/framework/tags');
define('GITHUB_LARAVEL_ARCHIVE', 'https://github.com/laravel/framework/archive/');
$timer = microtime(true);
$tempPath = __DIR__.'/.temp';
$targetPath = __DIR__.'/../src/Illuminate/Foundation/';
$version = getVersion();
echo "Latest Laravel Version: {$version}".PHP_EOL.PHP_EOL;
createDirectory($tempPath);
handleArchive($version, $tempPath);
moveDirectory($tempPath, $targetPath, $version);
deleteDirectory($tempPath);
$timer = round(microtime(true) - $timer, 2);
echo PHP_EOL."Finished in {$timer} seconds.".PHP_EOL;
/**
* Get the latest Laravel version.
*/
function getVersion(): string
{
$request = curl_init(GITHUB_API_LARAVEL_TAGS);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_USERAGENT, 'PHP Script');
$response = curl_exec($request);
curl_close($request);
if (! $response) {
echo 'Error: Could not get the latest Laravel version.';
exit(1);
}
$tags = json_decode($response);
$version = $tags[0]->name;
$current = getCurrentVersion();
if ($version === $current && ! in_array('--force', $_SERVER['argv'])) {
echo "Laravel is already on the latest version ({$current}).".PHP_EOL;
echo 'Use --force to force update.'.PHP_EOL;
exit(1);
}
return $version;
}
/**
* Get the current Laravel version.
*/
function getCurrentVersion(): ?string
{
if (! file_exists($file = __DIR__.'/../src/Illuminate/Foundation/Application.php')) {
return null;
}
$current = file_get_contents($file);
preg_match('/VERSION = \'(.*)\'/', $current, $matches);
return "v{$matches[1]}";
}
/**
* Create a directory if it doesn't exist.
*/
function createDirectory(string $path): void
{
if (file_exists($path)) {
deleteDirectory($path);
}
mkdir($path, 0777, true);
}
/**
* Download and extract the archive.
*/
function handleArchive(string $version, string $path): void
{
$file = "{$version}.tar.gz";
$archive = "{$path}/{$file}";
file_put_contents($archive, fopen(GITHUB_LARAVEL_ARCHIVE.$file, 'r'));
echo "Downloaded the archive to: {$archive}".PHP_EOL;
(new PharData($archive))->extractTo($path);
echo "Extracted archive to: {$path}".PHP_EOL;
}
/**
* Move the Foundation directory into the project.
*/
function moveDirectory(string $tempPath, string $targetPath, string $version): void
{
$version = substr($version, 1);
$extracted = "{$tempPath}/framework-{$version}/src/Illuminate/Foundation";
if (! file_exists($extracted)) {
echo 'Error: Extracted Foundation directory not found.';
exit(1);
}
if (file_exists($targetPath)) {
deleteDirectory($targetPath);
}
mkdir($targetPath, 0777, true);
rename($extracted, $targetPath);
echo "Moved Foundation to: {$targetPath}".PHP_EOL;
}
/**
* Delete a directory recursively.
*/
function deleteDirectory(string $directory): void
{
if (! file_exists($directory)) {
return;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileInfo) {
$remove = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$remove($fileInfo->getRealPath());
}
rmdir($directory);
echo "Deleted directory: {$directory}".PHP_EOL;
}