From 6c5bdde99a1dd8399ef542c9d28f987c5e70bc53 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Jan 2021 15:01:57 +0800 Subject: [PATCH 1/4] Add stubs publisher script and --- src/ServiceProvider.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 9b78cc8..c304afc 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -27,5 +27,9 @@ class ServiceProvider extends BaseServiceProvider $this->publishes([ __DIR__.'/config.php' => config_path('simple-crud.php'), ], 'config'); + + $this->publishes([ + __DIR__.'/stubs' => base_path('stubs/simple-crud'), + ], 'stubs'); } } From 6ae4d7a98d2e421be22f08484e9b8f48a4db25de Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Jan 2021 15:02:11 +0800 Subject: [PATCH 2/4] Use published stub file if it exists --- src/Generators/BaseGenerator.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php index e532ffd..b38591b 100644 --- a/src/Generators/BaseGenerator.php +++ b/src/Generators/BaseGenerator.php @@ -116,6 +116,12 @@ abstract class BaseGenerator implements GeneratorContract */ protected function getStubFileContent(string $stubName) { + $publishedStubPath = base_path('stubs/simple-crud/'.$stubName.'.stub'); + + if (is_file($publishedStubPath)) { + return $this->files->get($publishedStubPath); + } + return $this->files->get(__DIR__.'/../stubs/'.$stubName.'.stub'); } From b4e7d5faefc5126250d08dc89851f3538bc12f7f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 05:59:03 +0800 Subject: [PATCH 3/4] Add test for published stubs usage --- tests/Generators/ModelFactoryGeneratorTest.php | 42 +++++++++++++++++++++++ tests/stubs/database/factories/model-factory.stub | 25 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/stubs/database/factories/model-factory.stub diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index d93b37b..0633396 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -32,4 +32,46 @@ use Faker\Generator as Faker; "; $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); } + + /** @test */ + public function it_creates_model_factory_file_content_from_published_stub() + { + app('files')->makeDirectory(base_path('stubs/simple-crud/database/factories'), 0777, true, true); + app('files')->copy( + __DIR__.'/../stubs/database/factories/model-factory.stub', + base_path('stubs/simple-crud/database/factories/model-factory.stub') + ); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php'); + $this->assertFileExists($modelFactoryPath); + $modelFactoryContent = "full_model_name}; +use Illuminate\Database\Eloquent\Factories\Factory; + +class {$this->model_name}Factory extends Factory +{ + protected \$model = {$this->model_name}::class; + + public function definition() + { + return [ + 'title' => \$this->faker->word, + 'description' => \$this->faker->sentence, + 'creator_id' => function () { + return User::factory()->create()->id; + }, + ]; + } +} +"; + $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); + $this->removeFileOrDir(base_path('stubs')); + } } diff --git a/tests/stubs/database/factories/model-factory.stub b/tests/stubs/database/factories/model-factory.stub new file mode 100644 index 0000000..23c4374 --- /dev/null +++ b/tests/stubs/database/factories/model-factory.stub @@ -0,0 +1,25 @@ + $this->faker->word, + 'description' => $this->faker->sentence, + 'creator_id' => function () { + return User::factory()->create()->id; + }, + ]; + } +} From 0556252d6904cf6cde8ced83709af733f0298650 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 27 Jan 2021 10:56:15 +0800 Subject: [PATCH 4/4] Update readme.md file --- readme.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 2c2f449..45c5b0d 100644 --- a/readme.md +++ b/readme.md @@ -218,7 +218,7 @@ The generated functional tests will give you examples of how to adapt this code You can configure your own by publishing the config file: ```bash -$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" +$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=config ``` That will generate `config/simple-crud.php` file. @@ -242,6 +242,18 @@ return [
+## Publishing Stub Files + +Stub files is the templates which we use to generate the code for each model classes and files. We can customize the stub files as we needed by publishing them to our project directory. + +```bash +$ php artisan vendor:publish --provider="Luthfi\CrudGenerator\ServiceProvider" --tag=stubs +``` + +That will generate stub files on `stubs/simple-crud` directory. Now we can change some stub files based on our project needs. + +
+ ## Attention - The package will creates the **Model** class file, the command will stop if the **Model already exists**.