diff --git a/src/Console/DacapoStubPublishCommand.php b/src/Console/DacapoStubPublishCommand.php new file mode 100644 index 00000000..4e56397c --- /dev/null +++ b/src/Console/DacapoStubPublishCommand.php @@ -0,0 +1,51 @@ +laravel->basePath('stubs'))) { + (new Filesystem)->makeDirectory($stubsPath); + } + + $files = [ + realpath(__DIR__ . '/../Dacapo/Infra/Adapter/Stub/dacapo.migration.create.stub') => $stubsPath . '/dacapo.migration.create.stub', + realpath(__DIR__ . '/../Dacapo/Infra/Adapter/Stub/dacapo.migration.update.stub') => $stubsPath . '/dacapo.migration.update.stub', + ]; + + foreach ($files as $from => $to) { + if (!file_exists($to) || $this->option('force')) { + file_put_contents($to, file_get_contents($from)); + } + } + + $this->info('Stubs published successfully.'); + } +} diff --git a/src/Dacapo/Infra/Adapter/LaravelMigrationCreateStub.php b/src/Dacapo/Infra/Adapter/LaravelMigrationCreateStub.php new file mode 100644 index 00000000..b7da2066 --- /dev/null +++ b/src/Dacapo/Infra/Adapter/LaravelMigrationCreateStub.php @@ -0,0 +1,22 @@ +migrationUpdateStub = $migrationUpdateStub; + } /** * @param SchemaList $schemaList @@ -81,7 +90,7 @@ protected function makeMigrationConnection(Schema $schema): string */ protected function makeMigrationContents(Schema $schema): string { - $stub = file_get_contents(__DIR__ . '/../../App/Storage/stubs/migration.update.stub'); + $stub = $this->migrationUpdateStub->getStub(); $stub = str_replace('{{ class }}', $this->makeMigrationClassName($schema), $stub); $stub = str_replace('{{ connection }}', $this->makeMigrationConnection($schema), $stub); $stub = str_replace('{{ table }}', $schema->getTableName(), $stub); diff --git a/src/Dacapo/UseCase/Converter/SchemaToCreateIndexMigrationConverter.php b/src/Dacapo/UseCase/Converter/SchemaToCreateIndexMigrationConverter.php index d088d770..2944f17a 100644 --- a/src/Dacapo/UseCase/Converter/SchemaToCreateIndexMigrationConverter.php +++ b/src/Dacapo/UseCase/Converter/SchemaToCreateIndexMigrationConverter.php @@ -7,11 +7,20 @@ use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList; use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile; use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList; +use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationUpdateStub; class SchemaToCreateIndexMigrationConverter { const MIGRATION_COLUMN_INDENT = ' '; - protected Schema $schema; + protected MigrationUpdateStub $migrationUpdateStub; + + /** + * @param MigrationUpdateStub $migrationUpdateStub + */ + public function __construct(MigrationUpdateStub $migrationUpdateStub) + { + $this->migrationUpdateStub = $migrationUpdateStub; + } /** * @param SchemaList $schemaList @@ -63,7 +72,7 @@ protected function makeMigrationName(Schema $schema): string */ protected function makeMigrationContents(Schema $schema): string { - $stub = file_get_contents(__DIR__ . '/../../App/Storage/stubs/migration.update.stub'); + $stub = $this->migrationUpdateStub->getStub(); $stub = str_replace('{{ class }}', $this->makeMigrationClassName($schema), $stub); $stub = str_replace('{{ connection }}', $this->makeMigrationConnection($schema), $stub); $stub = str_replace('{{ table }}', $schema->getTableName(), $stub); diff --git a/src/Dacapo/UseCase/Converter/SchemaToCreateTableMigrationConverter.php b/src/Dacapo/UseCase/Converter/SchemaToCreateTableMigrationConverter.php index 60fea0a6..d5955ce2 100644 --- a/src/Dacapo/UseCase/Converter/SchemaToCreateTableMigrationConverter.php +++ b/src/Dacapo/UseCase/Converter/SchemaToCreateTableMigrationConverter.php @@ -8,16 +8,21 @@ use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile; use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList; use UcanLab\LaravelDacapo\Dacapo\UseCase\Builder\DatabaseBuilder; +use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationCreateStub; class SchemaToCreateTableMigrationConverter { const MIGRATION_COLUMN_INDENT = ' '; protected DatabaseBuilder $databaseBuilder; + protected MigrationCreateStub $migrationCreateStub; - public function __construct(DatabaseBuilder $databaseBuilder) - { + public function __construct( + DatabaseBuilder $databaseBuilder, + MigrationCreateStub $migrationCreateStub + ) { $this->databaseBuilder = $databaseBuilder; + $this->migrationCreateStub = $migrationCreateStub; } /** @@ -70,7 +75,7 @@ protected function makeMigrationName(Schema $schema): string */ protected function makeMigrationContents(Schema $schema): string { - $stub = file_get_contents(__DIR__ . '/../../App/Storage/stubs/migration.create.stub'); + $stub = $this->migrationCreateStub->getStub(); $stub = str_replace('{{ namespace }}', $this->makeMigrationNamespace($schema), $stub); $stub = str_replace('{{ class }}', $this->makeMigrationClassName($schema), $stub); $stub = str_replace('{{ connection }}', $this->makeMigrationConnection($schema), $stub); diff --git a/src/Dacapo/UseCase/Shared/Stub/MigrationCreateStub.php b/src/Dacapo/UseCase/Shared/Stub/MigrationCreateStub.php new file mode 100644 index 00000000..b10316df --- /dev/null +++ b/src/Dacapo/UseCase/Shared/Stub/MigrationCreateStub.php @@ -0,0 +1,8 @@ + LocalSchemaListRepository::class, MigrationListRepository::class => LocalMigrationListRepository::class, + MigrationCreateStub::class => LaravelMigrationCreateStub::class, + MigrationUpdateStub::class => LaravelMigrationUpdateStub::class, ]; protected array $commands = [ DacapoInitCommand::class, DacapoCommand::class, DacapoClearCommand::class, + DacapoStubPublishCommand::class, DacapoUninstallCommand::class, ];