Skip to content

Commit

Permalink
chore: add custom template support
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeerickson committed Apr 18, 2019
1 parent dbb883f commit d046ef1
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ app/Http/
resources/
app/Post.php
app/Test.php
templates/custom.mustache
3 changes: 3 additions & 0 deletions app/Commands/CraftClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CraftClass extends Command
protected $signature = 'craft:class
{name : Class name}
{--c|constructor : Include constructor method}
{--t|template= : Template path (override configuration file)}
{--w|overwrite : Overwrite existing class}
';
/**
Expand All @@ -29,6 +30,7 @@ class CraftClass extends Command
protected $description = 'Craft Class
<name> Class Name
--constructor, -c Include constructor method
--template, -t Path to custom template (override config file)
--overwrite, -w Overwrite existing class
';

Expand All @@ -54,6 +56,7 @@ public function handle()
$data = [
"name" => $className,
"constructor" => $this->option("constructor"),
"template" => $this->option("template"),
"overwrite" => $this->option("overwrite"),
];

Expand Down
3 changes: 3 additions & 0 deletions app/Commands/CraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CraftController extends Command
protected $signature = 'craft:controller {name : Controller Name}
{--m|model= : Associated model}
{--w|overwrite : Overwrite existing controller}
{--t|template= : Template path (override configuration file)}
{--l|validation : Scaffold validation}
{--a|api : create API controller (skips create and update methods)}
';
Expand All @@ -39,6 +40,7 @@ class CraftController extends Command
--validation, -l Create validation blocks
--api, -a Create API controller (skips create and update methods)
--empty, -e Create empty controller
--template, -t Template path (override configuration file)
--overwrite, -w Overwrite existing controller
';

Expand All @@ -65,6 +67,7 @@ public function handle()
$data = [
"model" => $model,
"name" => $controllerName,
"template" => $this->option('template'),
"overwrite" => $this->option('overwrite'),
];

Expand Down
3 changes: 3 additions & 0 deletions app/Commands/CraftFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CraftFactory extends Command
protected $signature = 'craft:factory
{name : Factory Name}
{--m|model= : Associated model}
{--t|template= : Template path (override configuration file)}
{--w|overwrite : Overwrite existing factory}
';
/**
Expand All @@ -29,6 +30,7 @@ class CraftFactory extends Command
protected $description = 'Craft Factory
<name> Factory Name
--model, -m Use <model> when creating controller
--template, -t Template path (override configuration file)
--overwrite, -w Overwrite existing factory
';

Expand Down Expand Up @@ -58,6 +60,7 @@ public function handle()
$data = [
"model" => $model,
"name" => $factoryName,
"template" => $this->option('template'),
"overwrite" => $this->option('overwrite'),
];

Expand Down
2 changes: 2 additions & 0 deletions app/Commands/CraftModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CraftModel extends Command
protected $signature = 'craft:model
{name : Model name}
{--t|tablename= : Tablename if different than model name}
{--m|template= : Template path (override configuration file)}
{--w|overwrite : Overwrite existing model}
';

Expand All @@ -36,6 +37,7 @@ class CraftModel extends Command
protected $description = 'Craft Model
<name> Model Name (eg App\Models\Post)
--tablename, -t Desired tablename
--template, -m Template path (override configuration file)
--overwrite, -w Overwrite existing model
';

Expand Down
3 changes: 3 additions & 0 deletions app/Commands/CraftSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CraftSeed extends Command
{name : Seed name}
{--m|model= : Associated model}
{--r|rows= : Alternate number of rows to use in factory call}
{--t|template= : Template path (override configuration file)}
{--w|overwrite : Overwrite existing seed}
';

Expand All @@ -37,6 +38,7 @@ class CraftSeed extends Command
<name> Seed Name
--model, -m Path to model
--rows, -r Number of rows to use in factory call (Optional)
--template, -t Template path (override configuration file)
--overwrite, -w Overwrite existing seed
';

Expand Down Expand Up @@ -71,6 +73,7 @@ public function handle()
"name" => $seedName,
"num_rows" => $num_rows,
"overwrite" => $overwrite,
"template" => $this->option('template'),
];

$this->fs->createFile('seed', $seedName, $data);
Expand Down
27 changes: 20 additions & 7 deletions app/CraftsmanFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use Codedungeon\PHPMessenger\Facades\Messenger;
use CraftsmanFileSystemException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -109,6 +110,12 @@ private function createMergeFile($src, $dest, $data)
*/
public function getTemplateFilename($type)
{
if (strpos($type, "<project>") !== false) {
$filename = str_replace("<project>", "", $type);
$filename = getcwd().DIRECTORY_SEPARATOR.$filename;
$filename = str_replace("//", "/", $filename);
return $filename;
}
return config("craftsman.templates.{$type}");
}

Expand Down Expand Up @@ -306,17 +313,23 @@ public function createFile($type = null, $filename = null, $data = [])
$path = $this->getOutputPath($type);

$namespace = "";
$src = $this->getUserTemplate("./config.php", $type);

if (!file_exists($src)) {
$src = config("craftsman.templates.{$type}");
}
if (isset($data["template"])) {
$src = $this->getTemplateFilename($data["template"]);
} else {
$src = $this->getUserTemplate("./config.php", $type);
if (!file_exists($src)) {
$src = config("craftsman.templates.{$type}");
}

$src = $this->getPharPath().$src;
$src = $this->getPharPath().$src;
$src = str_replace("//", "/", $src);
}

if (!file_exists($src)) {
printf("\n\n");
Log::error("Unable to locate template './{$src}' Has it been deleted?");
// throw new CraftsmanFileSystemException($errMsg);
exit(1);
}

Expand Down Expand Up @@ -415,8 +428,6 @@ public function createFile($type = null, $filename = null, $data = [])
$vars["constructor"] = false;
}

// $vars["model_path"] = str_replace("/", "\\", $vars["model_path"]);

$template = $this->fs->get($src);

$mustache = new Mustache_Engine();
Expand All @@ -430,6 +441,7 @@ public function createFile($type = null, $filename = null, $data = [])
$this->fs->put($dest, $template_data);
$result = [
"filename" => $dest,
"fullPath" => getcwd().DIRECTORY_SEPARATOR.$dest,
"status" => "success",
"message" => "{$dest} created successfully",
];
Expand All @@ -445,6 +457,7 @@ public function createFile($type = null, $filename = null, $data = [])
Messenger::success("✔︎ {$dest} created successfully\n");
}


return $result;
}

Expand Down
31 changes: 31 additions & 0 deletions app/Exceptions/CraftsmanFileSystemException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Exceptions;

use Exception;

/**
* Define a custom exception class
*/
class CraftsmanFileSystemException extends Exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0, Exception $previous = null)
{
// some code

// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}

// custom string representation of object
public function __toString()
{
return __CLASS__.": [{$this->code}]: {$this->message}\n";
}

public function customFunction()
{
echo "A custom function for this type of exception\n";
}
}
Binary file modified builds/laravel-craftsman
Binary file not shown.
12 changes: 12 additions & 0 deletions builds/templates/custom.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace {{namespace}};

class {{model}}
{
{{#constructor}}
function __construct() {}
{{/constructor}}

public function testMethod($param = "") {}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codedungeon/laravel-craftsman",
"version": "1.0.12",
"version": "1.0.13",
"description": "Laravel Craftsman",
"keywords": [
"framework",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "laravel-craftsman",
"version": "1.0.12",
"build": "99",
"version": "1.0.13",
"build": "126",
"description": "Laravel Craftsman",
"main": "index.js",
"directories": {
Expand All @@ -12,6 +12,7 @@
"build": "./tasks/build.sh",
"build:deploy": "./tasks/build.sh --deploy",
"deploy": "./tasks/deploy.sh",
"publish": "./tasks/publish.sh",
"test": "vendor/bin/phpunit",
"test:ci": "vendor/bin/phpunit -c phpunit.ci.xml",
"test:coverage": "vendor/bin/phpunit --testsuite Unit --coverage-html coverage",
Expand Down
12 changes: 12 additions & 0 deletions templates/custom.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace {{namespace}};

class {{model}}
{
{{#constructor}}
function __construct() {}
{{/constructor}}

public function testMethod($param = "") {}
}
19 changes: 19 additions & 0 deletions tests/Feature/CraftClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,23 @@ public function should_create_class_with_namespace()

$this->fs->rmdir("app/Test");
}

/** @test */
public function should_create_class_using_user_template()
{
$class = "App/Test/SampleClass";

$this->artisan("craft:class {$class} --template <project>/templates/custom.mustache")
->assertExitCode(0);

$filename = $this->pathJoin("app/Test", "SampleClass.php");
$this->assertFileExists($filename);

$this->assertFileContainsString($filename, "class SampleClass");
$this->assertFileContainsString($filename, "testMethod");

$this->fs->rmdir("app/Test");

}

}
24 changes: 24 additions & 0 deletions tests/Feature/CraftControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,28 @@ public function should_create_api_controller()

$this->fs->rmdir("app/Http");
}

/** @test */
public function should_create_controller_using_custom_template()
{
$model = "App/Models/Contact";
$model_path = "App\\Models\\Contact";

$this->artisan("craft:controller CustomController --model {$model} --template <project>/templates/custom.mustache --overwrite")
->assertExitCode(0);

$controllerPath = $this->fs->controller_path();
$filename = $this->fs->path_join($controllerPath, "CustomController.php");
$this->assertFileExists($filename);

$parts = explode("/", $model);
$model_name = array_pop($parts);

// spot check merged data
$data = file_get_contents($filename);

$this->assertStringContainsString("testMethod", $data);

$this->fs->rmdir("app/Http");
}
}
18 changes: 18 additions & 0 deletions tests/Feature/CraftFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ public function should_execute_craft_factory_command()

$this->fs->rmdir("database/factories");
}

/** @test */
public function should_craft_factory_using_custom_template()
{
$model = "App/Models/Test";
$model_path = "App\\Models\\Test";

$this->artisan("craft:factory TestFactory --model {$model} --template <project>/templates/custom.mustache --overwrite")
->assertExitCode(0);

$factoryPath = $this->fs->factory_path();
$filename = $this->fs->path_join($factoryPath, "TestFactory.php");
$this->assertFileExists($filename);

$this->assertFileContainsString($filename, "testMethod");

// $this->fs->rmdir("database/factories");
}
}
16 changes: 16 additions & 0 deletions tests/Feature/CraftModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public function should_execute_custom_craft_model_command()

$this->fs->rmdir("app/Models");
}

/** @test */
public function should_craft_model_using_custom_template()
{
$model = "Post";

$this->artisan('craft:model App/Models/Post --template <project>/templates/custom.mustache --overwrite')
->assertExitCode(0);

$modelPath = $this->fs->model_path();
$filename = $this->pathJoin($modelPath, "Models", "{$model}.php");

$this->assertFileContainsString($filename, "class {$model} extends Model");

// $this->fs->rmdir("app/Models");
}
}
16 changes: 16 additions & 0 deletions tests/Feature/CraftSeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public function should_execute_craft_seed_command()

$this->fs->rmdir("database/seeds");
}

public function should_craft_seed_using_custom_template()
{
$class = "TestsTableSeeder";

$this->artisan("craft:seed TestsTableSeeder --template <project>/templates/custom.mustache --overwrite")
->assertExitCode(0);

$seedPath = $this->fs->seed_path();
$filename = $this->pathJoin($seedPath, "{$class}.php");
var_dump($filename);

// $this->assertFileContainsString($filename, "testMethod");

// $this->fs->rmdir("database/seeds");
}
}
Loading

0 comments on commit d046ef1

Please sign in to comment.