Skip to content

Commit

Permalink
1. Scan for filament version in composerjson file
Browse files Browse the repository at this point in the history
2. Run Filament prod-cache commands in Dockerfile if filament v3 and above detected
3. Revise GenerateCommandTest to ignore newline differences when checking if reference and generated files match
4. Create tests for laravel versions 10below and 11 for: filament_v3 support, AND filament_below_support_which_does_not_iclude_changes_for_v3_support
  • Loading branch information
Kathryn Anne S Tan committed Apr 3, 2024
1 parent 92207a3 commit fc40e21
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public function handle()
'dev' => $this->option('dev'),
'laravel_version' => $scan->laravelVersion( $this->options() ),
'fly' => $scan->isForFly(),
'octane' => $scan->octaneFlavor( $this->options() )
];
'octane' => $scan->octaneFlavor( $this->options() ),
'filament' => $scan->filamentVersion( $this->options() )
];

// Define the list of templates to render.
// The key is the template name, and the value is the output file name.
Expand Down
16 changes: 16 additions & 0 deletions app/Services/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ public function octaneFlavor( array $options )
return $options['octane'];
}

/**
* Detect filament declaration and version
*/
public function filamentVersion( array $options )
{
// Composer json contains filament requirement
$composerContent = (new \App\Services\File())->composerJsonContent( $options['path'] );

// Extract version
if( isset( $composerContent['require'] ) && isset($composerContent['require']['filament/filament']) ){
return trim($composerContent['require']['filament/filament'], '^');
}else{
return false;
}
}

/**
* Scan directory and check if applicable for Fly.io deployment
*/
Expand Down
5 changes: 5 additions & 0 deletions resources/views/dockerfile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
@include('octane-'.$octane)
@endif

@if( $filament && intval($filament) >= 3 )
# If we're using Filament v3 and above, run caching commands...
RUN php artisan icons:cache && php artisan filament:cache-components
@endif

@if($build_assets)
# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
Expand Down
4 changes: 0 additions & 4 deletions storage/logs/laravel.log

This file was deleted.

23 changes: 21 additions & 2 deletions tests/Feature/GenerateCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;

function ignoreFiles( )
{
return ['composer.json','frankenphp','rr','.rr.yaml'];
}

function assertEqualsIgnoreNewLine( $ins, $expected, $generated, $msg )
{
try{
// Ignore newlines when comparing strings; do this by removing new lines from strings to compare
$il_expected = str_replace( PHP_EOL, '', $expected );
$il_generated = str_replace( PHP_EOL, '', $generated );
$ins->assertEquals( $il_expected, $il_generated, $msg);
}catch (ExpectationFailedException $f) {

// However, if variants without newlines failed to match!
// That's a true failed match!
// To get a better diff error message, use original strings( with their newlines intact ) in comparison
$ins->assertEquals( $expected, $generated, $msg);
}
}

function getTestOptions( string $directory ): string
{
$composerContent = (new \App\Services\File())->composerJsonContent( $directory );
Expand Down Expand Up @@ -33,7 +50,9 @@ function getTestOptions( string $directory ): string
it('generates proper templates for each supported combination', function ( )
{
$directories = \File::directories( 'tests/Feature/Supported' );
foreach($directories as $dir) {
foreach($directories as $dir) {
#if( $dir != 'tests/Feature/Supported/10_filament_v3' ) continue; -- revise and uncomment this line if you want to test out a specific Support subfolder

// Detect options from composer.json
$options = getTestOptions( $dir );

Expand All @@ -57,7 +76,7 @@ function getTestOptions( string $directory ): string

// Third assert: contents are the same
// TODO: ignore different ARG VALUES
$this->assertEquals( $expected, $generated, $failedForMsg);
assertEqualsIgnoreNewLine( $this, $expected, $generated, $failedForMsg);

// Clean UP: Delete generated file, no longer needed
unlink( $reference->getFileName() );
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Supported/10_base/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"require": {
"laravel/framework": "^10.10"
"laravel/framework": "^10.10",
"filament/filament": "^2.0.0"
}
}
76 changes: 76 additions & 0 deletions tests/Feature/Supported/10_filament_v3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# syntax = docker/dockerfile:experimental

ARG PHP_VERSION=8.2
ARG NODE_VERSION=18
FROM fideloper/fly-laravel:${PHP_VERSION} as base

# PHP_VERSION needs to be repeated here
# See https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
ARG PHP_VERSION

LABEL fly_launch_runtime="laravel"

# copy application code, skipping files based on .dockerignore
COPY . /var/www/html

RUN composer install --optimize-autoloader --no-dev \
&& mkdir -p storage/logs \
&& php artisan optimize:clear \
&& chown -R www-data:www-data /var/www/html \
&& echo "MAILTO=\"\"\n* * * * * www-data /usr/bin/php /var/www/html/artisan schedule:run" > /etc/cron.d/laravel \
&& sed -i 's/protected \$proxies/protected \$proxies = "*"/g' app/Http/Middleware/TrustProxies.php;\
if [ -d .fly ]; then cp .fly/entrypoint.sh /entrypoint; chmod +x /entrypoint; fi;


# If we're using Filament v3 and above, run caching commands...
RUN php artisan icons:cache && php artisan filament:cache-components

# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr

RUN mkdir /app

RUN mkdir -p /app
WORKDIR /app
COPY . .
COPY --from=base /var/www/html/vendor /app/vendor

# Use yarn or npm depending on what type of
# lock file we might find. Defaults to
# NPM if no lock file is found.
# Note: We run "production" for Mix and "build" for Vite
RUN if [ -f "vite.config.js" ]; then \
ASSET_CMD="build"; \
else \
ASSET_CMD="production"; \
fi; \
if [ -f "yarn.lock" ]; then \
yarn install --frozen-lockfile; \
yarn $ASSET_CMD; \
elif [ -f "pnpm-lock.yaml" ]; then \
corepack enable && corepack prepare pnpm@latest-8 --activate; \
pnpm install --frozen-lockfile; \
pnpm run $ASSET_CMD; \
elif [ -f "package-lock.json" ]; then \
npm ci --no-audit; \
npm run $ASSET_CMD; \
else \
npm install; \
npm run $ASSET_CMD; \
fi;

# From our base container created above, we
# create our final image, adding in static
# assets that we generated above
FROM base

# Packages like Laravel Nova may have added assets to the public directory
# or maybe some custom assets were added manually! Either way, we merge
# in the assets we generated above rather than overwrite them
COPY --from=node_modules_go_brrr /app/public /var/www/html/public-npm
RUN rsync -ar /var/www/html/public-npm/ /var/www/html/public/ \
&& rm -rf /var/www/html/public-npm \
&& chown -R www-data:www-data /var/www/html/public

EXPOSE 8080
6 changes: 6 additions & 0 deletions tests/Feature/Supported/10_filament_v3/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"laravel/framework": "^10.0.0",
"filament/filament": "^3.0.0"
}
}
1 change: 1 addition & 0 deletions tests/Feature/Supported/11_base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ RUN composer install --optimize-autoloader --no-dev \
if [ -d .fly ]; then cp .fly/entrypoint.sh /entrypoint; chmod +x /entrypoint; fi;



# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Supported/11_base/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"require": {
"laravel/framework": "^11.0.0"
"laravel/framework": "^11.0.0",
"filament/filament": "^2.0.0"
}
}
78 changes: 78 additions & 0 deletions tests/Feature/Supported/11_filament_v3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# syntax = docker/dockerfile:experimental

ARG PHP_VERSION=8.2
ARG NODE_VERSION=18
FROM fideloper/fly-laravel:${PHP_VERSION} as base

# PHP_VERSION needs to be repeated here
# See https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
ARG PHP_VERSION

LABEL fly_launch_runtime="laravel"

# copy application code, skipping files based on .dockerignore
COPY . /var/www/html

RUN composer install --optimize-autoloader --no-dev \
&& mkdir -p storage/logs \
&& php artisan optimize:clear \
&& chown -R www-data:www-data /var/www/html \
&& echo "MAILTO=\"\"\n* * * * * www-data /usr/bin/php /var/www/html/artisan schedule:run" > /etc/cron.d/laravel \
&& sed -i='' '/->withMiddleware(function (Middleware \$middleware) {/a\
\$middleware->trustProxies(at: "*");\
' bootstrap/app.php; \
if [ -d .fly ]; then cp .fly/entrypoint.sh /entrypoint; chmod +x /entrypoint; fi;


# If we're using Filament v3 and above, run caching commands...
RUN php artisan icons:cache && php artisan filament:cache-components

# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr

RUN mkdir /app

RUN mkdir -p /app
WORKDIR /app
COPY . .
COPY --from=base /var/www/html/vendor /app/vendor

# Use yarn or npm depending on what type of
# lock file we might find. Defaults to
# NPM if no lock file is found.
# Note: We run "production" for Mix and "build" for Vite
RUN if [ -f "vite.config.js" ]; then \
ASSET_CMD="build"; \
else \
ASSET_CMD="production"; \
fi; \
if [ -f "yarn.lock" ]; then \
yarn install --frozen-lockfile; \
yarn $ASSET_CMD; \
elif [ -f "pnpm-lock.yaml" ]; then \
corepack enable && corepack prepare pnpm@latest-8 --activate; \
pnpm install --frozen-lockfile; \
pnpm run $ASSET_CMD; \
elif [ -f "package-lock.json" ]; then \
npm ci --no-audit; \
npm run $ASSET_CMD; \
else \
npm install; \
npm run $ASSET_CMD; \
fi;

# From our base container created above, we
# create our final image, adding in static
# assets that we generated above
FROM base

# Packages like Laravel Nova may have added assets to the public directory
# or maybe some custom assets were added manually! Either way, we merge
# in the assets we generated above rather than overwrite them
COPY --from=node_modules_go_brrr /app/public /var/www/html/public-npm
RUN rsync -ar /var/www/html/public-npm/ /var/www/html/public/ \
&& rm -rf /var/www/html/public-npm \
&& chown -R www-data:www-data /var/www/html/public

EXPOSE 8080
6 changes: 6 additions & 0 deletions tests/Feature/Supported/11_filament_v3/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"laravel/framework": "^11.0.0",
"filament/filament": "^3.0.0"
}
}

0 comments on commit fc40e21

Please sign in to comment.