Add --dry-run support to Artisan generator commands #57334
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add
--dry-run
Flag to Artisan Generator CommandsDescription
This PR adds a
--dry-run
flag to all Laravel Artisan generator commands, allowing developers to preview the operations that would be performed without actually creating any files or making any changes to the filesystem.This feature follows the industry-standard preview approach (not make-and-revert) used by popular tools like NestJS CLI, npm.. etc.
Motivation
Currently, when developers run Artisan generator commands (e.g.,
php artisan make:model Post -mfsc
), files are created immediately without any opportunity to preview what will be generated. This can lead to:The
--dry-run
flag solves these problems by providing a safe preview mode.Changes
Core Implementation
New
DryRunnable
Trait (src/Illuminate/Console/Concerns/DryRunnable.php
)configureDryRun()
- Adds the--dry-run
option to a commandisDryRun()
- Checks if command is running in dry-run moderecordDryRunOperation()
- Records operations that would be performeddisplayDryRunOperations()
- Displays formatted preview outputclearDryRunOperations()
- Clears recorded operationsgetDryRunOperations()
- Returns all recorded operationsGeneratorCommand
Integration (src/Illuminate/Console/GeneratorCommand.php
)DryRunnable
traithandle()
method to check for dry-run modehandleDryRun()
method for preview logichandleTestCreationDryRun()
method for test file previews-v
) shows actual file content previewModelMakeCommand
Integration (src/Illuminate/Foundation/Console/ModelMakeCommand.php
)--dry-run
flag to all sub-commands:make:factory
make:migration
make:seeder
make:controller
make:request
(Store and Update)make:policy
ControllerMakeCommand
Integration (src/Illuminate/Routing/Console/ControllerMakeCommand.php
)--dry-run
flag when generating related models and requestsMigrateMakeCommand
Integration (src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php
)DryRunnable
traitMigrateCommand
Integration (src/Illuminate/Database/Console/Migrations/MigrateCommand.php
)DryRunnable
trait--dry-run
works as an alias for existing--pretend
flagCommands Supporting Dry-Run
All generator commands that extend
GeneratorCommand
now support--dry-run
:make:model
,make:controller
,make:migration
,make:seeder
make:factory
,make:policy
,make:request
,make:resource
make:rule
,make:event
,make:listener
,make:job
make:mail
,make:notification
,make:middleware
,make:provider
make:test
,make:command
,make:cast
,make:channel
make:component
,make:exception
,make:observer
,make:scope
Plus:
migrate
(shows SQL queries via existing--pretend
functionality)Usage Examples
Basic Usage
# Preview creating a model php artisan make:model Post --dry-run
Output:
With Multiple Flags
# Preview model with migration, factory, and seeder php artisan make:model Post -mfs --dry-run
Each sub-command (model, migration, factory, seeder) displays its own dry-run output.
With Verbose Output
# Show file content preview php artisan make:model Post --dry-run -v
Shows the actual stub content that would be generated.
Existing Files
# When file already exists php artisan make:model User --dry-run
Shows a "SKIP" operation indicating the file already exists.
Technical Details
Design Decisions
Preview-Only Approach: No files are created or modified in dry-run mode. This is safer and more performant than make-and-revert approaches.
Flag Propagation: When
make:model Post -m --dry-run
is called, the--dry-run
flag is automatically passed to themake:migration
sub-command.Detailed Output: Shows all relevant information (paths, namespaces, classes, file sizes) to help developers understand what will be created.
Consistent Formatting: Uses Laravel's existing component styling for consistent output across all commands.
Non-Invasive: Existing commands continue to work exactly as before. The trait is only activated when the
--dry-run
flag is used.Code Quality
string
,array
,void
,bool
)@return array<int, array{type: string, description: string, details: array}>
)Testing
Test Coverage
DryRunnableTest.php
- Unit tests for theDryRunnable
traitDryRunCommandTest.php
- Integration tests for generator commands-m
flag)Running Tests
Benefits
For Developers
For Teams
For Laravel
Comparison with Other Frameworks
NestJS
Laravel (This PR)
Breaking Changes
None. This is a purely additive feature with no impact on existing functionality.
Future Enhancements
Potential future additions (not in this PR):
config:cache
,route:cache
, etc.)Checklist
Screenshots
Additional Notes
This implementation is production-ready and has been tested with various command combinations. The
DryRunnable
trait is designed to be easily added to any custom Artisan command, making it extensible for package developers and application-specific commands.Note to Maintainers: This PR implements a frequently requested feature that brings Laravel in line with other modern CLI tools. The implementation is non-invasive, well-tested, and follows all Laravel coding standards.