Skip to content

Commit 56d8f7f

Browse files
authored
fix: Automatic Installation doesn't work well on windows OS (#18)
1 parent 374a3cd commit 56d8f7f

File tree

3 files changed

+141
-14
lines changed

3 files changed

+141
-14
lines changed

.github/workflows/run-tests.yml

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,39 @@ jobs:
127127
path: app/storage/logs
128128

129129
stub-tests:
130-
runs-on: ubuntu-latest
130+
runs-on: ${{ matrix.os }}
131131

132132
strategy:
133133
fail-fast: true
134134
matrix:
135+
os: [ubuntu-latest, windows-latest]
135136
php: [8.1, 8.0]
136137
laravel: [9.*]
137138
dependency-version: [prefer-lowest, prefer-stable]
138139

139-
name: Test Stubs P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
140+
name: Test Stubs ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
140141

141142
steps:
142143
- name: Setup PHP
143144
uses: shivammathur/setup-php@v2
144145
with:
145146
php-version: ${{ matrix.php }}
146-
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, mysql, mysqli, pdo_mysql
147+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, mysql, mysqli, pdo_mysql, fileinfo
147148
coverage: none
148149

149150
- name: Setup Laravel
150151
run: |
151-
composer create-project laravel/laravel:^9 .
152+
composer create-project laravel/laravel:^9.3 .
152153
composer require protonemedia/laravel-splade
153-
rm -rf vendor/protonemedia/laravel-splade
154+
155+
- name: Remove installed Splade (Unix)
156+
run: rm -rf vendor/protonemedia/laravel-splade
157+
if: matrix.os == 'ubuntu-latest'
158+
159+
- name: Remove installed Splade (Windows)
160+
run: rd "vendor/protonemedia/laravel-splade" /s /q
161+
shell: cmd
162+
if: matrix.os == 'windows-latest'
154163

155164
- name: Checkout code
156165
uses: actions/checkout@v2
@@ -163,18 +172,53 @@ jobs:
163172
php artisan splade:install
164173
165174
- name: Install NPM dependencies
175+
run: npm i
176+
177+
- name: Remove installed Splade and copy front-end build from Checkout (Unix)
166178
run: |
167-
npm i
168179
rm -rf node_modules/@protonemedia/laravel-splade/dist
169180
cp -R vendor/protonemedia/laravel-splade/dist node_modules/@protonemedia/laravel-splade/
181+
if: matrix.os == 'ubuntu-latest'
182+
183+
- name: Remove installed Splade and copy front-end build from Checkout (Windows)
184+
run: |
185+
rd "node_modules/@protonemedia/laravel-splade/dist" /s /q
186+
mkdir "node_modules/@protonemedia/laravel-splade/dist"
187+
xcopy "vendor/protonemedia/laravel-splade/dist" "node_modules/@protonemedia/laravel-splade/dist" /E /I
188+
shell: cmd
189+
if: matrix.os == 'windows-latest'
170190

171191
- name: Compile assets
172192
run: npm run build
173193

174-
- name: Start SSR server
194+
- name: Run Laravel Server (Unix)
195+
run: php artisan serve &
196+
if: matrix.os == 'ubuntu-latest'
197+
198+
- name: Run Test (Unix)
199+
run: php vendor/protonemedia/laravel-splade/TestStubs.php
200+
if: matrix.os == 'ubuntu-latest'
201+
202+
- name: Run Laravel Server (Windows) and Run Test
203+
run: |
204+
start /b cmd /v:on /c "(php artisan serve) &"
205+
php vendor/protonemedia/laravel-splade/TestStubs.php
206+
shell: cmd
207+
if: matrix.os == 'windows-latest'
208+
209+
- name: Start SSR server (Unix)
175210
run: |
176211
echo "SPLADE_SSR_ENABLED=true" >> .env
177212
node bootstrap/ssr/ssr.mjs &
213+
if: matrix.os == 'ubuntu-latest'
178214

179-
- name: Run Test command
215+
- name: Run Test command (Unix)
180216
run: php artisan splade:ssr-test
217+
if: matrix.os == 'ubuntu-latest'
218+
219+
- name: Start SSR server (Windows) and Run Test command
220+
run: |
221+
echo "SPLADE_SSR_ENABLED=true" >> .env
222+
node bootstrap/ssr/ssr.mjs &
223+
php artisan splade:ssr-test
224+
if: matrix.os == 'windows-latest'

TestStubs.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
function value($value, ...$args)
4+
{
5+
return $value instanceof \Closure ? $value(...$args) : $value;
6+
}
7+
8+
function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
9+
{
10+
$attempts = 0;
11+
12+
$backoff = [];
13+
14+
if (is_array($times)) {
15+
$backoff = $times;
16+
17+
$times = count($times) + 1;
18+
}
19+
20+
beginning:
21+
$attempts++;
22+
$times--;
23+
24+
try {
25+
return $callback($attempts);
26+
} catch (Exception $e) {
27+
if ($times < 1 || ($when && ! $when($e))) {
28+
throw $e;
29+
}
30+
31+
$sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
32+
33+
if ($sleepMilliseconds) {
34+
usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
35+
}
36+
37+
goto beginning;
38+
}
39+
}
40+
41+
function get()
42+
{
43+
$contents = file_get_contents('http://127.0.0.1:8000/');
44+
45+
if (!$contents) {
46+
throw new Exception("No contents");
47+
}
48+
49+
return $contents;
50+
}
51+
52+
$home = retry(10, fn () => get(), 1000);
53+
54+
$needles = [
55+
'<div id="app" data-components="',
56+
'Welcome to your Splade application!',
57+
];
58+
59+
$missing = [];
60+
61+
foreach ($needles as $needle) {
62+
if (!str_contains($home, $needle)) {
63+
$missing[] = $needle;
64+
echo "Not found: " . $needle . PHP_EOL;
65+
}
66+
}
67+
68+
if (empty($missing)) {
69+
echo "OK";
70+
exit(0);
71+
}
72+
73+
exit(1);

src/Commands/SpladeInstallCommand.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,21 @@ public function handle(): int
7272
return self::SUCCESS;
7373
}
7474

75+
/**
76+
* End of line symbol.
77+
*
78+
* @return string
79+
*/
80+
private static function eol(): string
81+
{
82+
return windows_os() ? "\n" : PHP_EOL;
83+
}
84+
7585
protected function installExceptionHandler()
7686
{
7787
$exceptionHandler = file_get_contents(app_path('Exceptions/Handler.php'));
7888

79-
$search = 'public function register()' . PHP_EOL . ' {';
89+
$search = 'public function register()' . static::eol() . ' {';
8090

8191
$registerMethodAfter = Str::after($exceptionHandler, $search);
8292

@@ -90,7 +100,7 @@ protected function installExceptionHandler()
90100
app_path('Exceptions/Handler.php'),
91101
str_replace(
92102
$registerMethodAfter,
93-
PHP_EOL . ' ' . $renderable . PHP_EOL . $registerMethodAfter,
103+
static::eol() . ' ' . $renderable . static::eol() . $registerMethodAfter,
94104
$exceptionHandler
95105
)
96106
);
@@ -105,11 +115,11 @@ protected function installRouteMiddleware()
105115
{
106116
$httpKernel = file_get_contents(app_path('Http/Kernel.php'));
107117

108-
$search = 'protected $routeMiddleware = [' . PHP_EOL;
118+
$search = 'protected $routeMiddleware = [' . static::eol();
109119

110120
$routeMiddlewareAfter = Str::after($httpKernel, $search);
111121

112-
$routeMiddleware = "'splade' => \ProtoneMedia\Splade\Http\SpladeMiddleware::class";
122+
$routeMiddleware = "'splade' => \ProtoneMedia\Splade\Http\SpladeMiddleware::class";
113123

114124
if (Str::contains($httpKernel, $routeMiddleware)) {
115125
return;
@@ -119,7 +129,7 @@ protected function installRouteMiddleware()
119129
app_path('Http/Kernel.php'),
120130
str_replace(
121131
$routeMiddlewareAfter,
122-
' ' . $routeMiddleware . ',' . PHP_EOL . $routeMiddlewareAfter,
132+
' ' . $routeMiddleware . ',' . static::eol() . $routeMiddlewareAfter,
123133
$httpKernel
124134
)
125135
);
@@ -165,7 +175,7 @@ protected static function updateNodePackages(callable $callback, $dev = true)
165175

166176
file_put_contents(
167177
base_path('package.json'),
168-
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
178+
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . static::eol()
169179
);
170180
}
171181
}

0 commit comments

Comments
 (0)