Skip to content

Commit

Permalink
add front end make commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nivv committed Sep 24, 2021
1 parent 4e7b20f commit 19d39b4
Show file tree
Hide file tree
Showing 11 changed files with 1,108 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.lock
.env.backup
.phpunit.result.cache
tests/_output
node_modules
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"kalnoy/nestedset": "^6.0",
"spatie/laravel-tags": "^3.1",
"spatie/laravel-sluggable": "^2.6",
"infab/translatable-revisions": "dev-main",
"infab/translatable-revisions": "^0.0.1",
"laravel/fortify": "^1.8"
},
"require-dev": {
Expand Down
147 changes: 147 additions & 0 deletions src/Console/VueApiModelMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Ikoncept\Fabriq\Console;
use Illuminate\Support\Str;
use Illuminate\Console\GeneratorCommand;
use Infab\Core\Console\ReplacesModelName;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;

class VueApiModelMakeCommand extends GeneratorCommand
{
use ReplacesModelName;

/**
* The console command name.
*
* @var string
*/
protected $name = 'make:vue-api-model-template';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates a Fabriq opionated javascript api model from stubs';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'VueApiModel';

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$stub = parent::buildClass($name);

$model = $this->argument('name');

return $model ? $this->replaceModel($stub, $model) : $stub;
}

/**
* Replace the model for the given stub.
*
* @param string $stub
* @param string $model
* @return string
*/
protected function replaceModel($stub, $model)
{
$modelClass = $this->parseModel($model);

$replace = [
'DummyFullModelClass' => $modelClass,
'{{ namespacedModel }}' => $modelClass,
'{{namespacedModel}}' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'{{ model }}' => class_basename($modelClass),
'{{model}}' => class_basename($modelClass),
'{{ pluralModel }}' => Str::pluralStudly(class_basename($modelClass)),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
'{{ pluralModelVariable }}' => Str::plural(lcfirst(class_basename($modelClass))),
'{{ swedishPluralName }}' => Str::lower($this->option('swedish-name-plural')),
'{{ swedishName }}' => Str::lower($this->option('swedish-name')),
'{{ SwedishName }}' => Str::studly($this->option('swedish-name')),
'{{ SwedishPluralName }}' => Str::studly($this->option('swedish-name-plural')),
];


return str_replace(
array_keys($replace), array_values($replace), $stub
);
}

/**
* Get the fully-qualified model class name.
*
* @param string $model
* @return string
*
* @throws \InvalidArgumentException
*/
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}

return $this->qualifyModel($model);
}

protected function alreadyExists($rawName)
{
$name = class_basename(str_replace('\\', '/', $rawName));

$path = "{$this->laravel['path']}/../resources/js/models/{$name}.js";

return file_exists($path);
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getOptions()
{
return [
['swedish-name', 's', InputOption::VALUE_OPTIONAL, 'Swedish name'],
['swedish-name-plural', 'sp', InputOption::VALUE_OPTIONAL, 'Swedish plural name'],
];
}


/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/vue.api-model.stub';
}

/**
* Get the destination class path.
*
* @param string $name
* @return string
*/
protected function getPath($name)
{
$name = class_basename(str_replace('\\', '/', $name));

return "{$this->laravel['path']}/../resources/js/models/{$name}.js";
}
}
147 changes: 147 additions & 0 deletions src/Console/VueEditTemplateMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Ikoncept\Fabriq\Console;
use Illuminate\Support\Str;
use Illuminate\Console\GeneratorCommand;
use Infab\Core\Console\ReplacesModelName;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;

class VueEditTemplateMakeCommand extends GeneratorCommand
{
use ReplacesModelName;

/**
* The console command name.
*
* @var string
*/
protected $name = 'make:vue-edit-template';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates a Fabriq opionated Vue templates for editing from stubs';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'VueEditTemplate';

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$stub = parent::buildClass($name);

$model = $this->argument('name');

return $model ? $this->replaceModel($stub, $model) : $stub;
}

/**
* Replace the model for the given stub.
*
* @param string $stub
* @param string $model
* @return string
*/
protected function replaceModel($stub, $model)
{
$modelClass = $this->parseModel($model);

$replace = [
'DummyFullModelClass' => $modelClass,
'{{ namespacedModel }}' => $modelClass,
'{{namespacedModel}}' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'{{ model }}' => class_basename($modelClass),
'{{model}}' => class_basename($modelClass),
'{{ pluralModel }}' => Str::pluralStudly(class_basename($modelClass)),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
'{{ pluralModelVariable }}' => Str::plural(lcfirst(class_basename($modelClass))),
'{{ swedishPluralName }}' => Str::lower($this->option('swedish-name-plural')),
'{{ swedishName }}' => Str::lower($this->option('swedish-name')),
'{{ SwedishName }}' => Str::studly($this->option('swedish-name')),
'{{ SwedishPluralName }}' => Str::studly($this->option('swedish-name-plural')),
];


return str_replace(
array_keys($replace), array_values($replace), $stub
);
}

/**
* Get the fully-qualified model class name.
*
* @param string $model
* @return string
*
* @throws \InvalidArgumentException
*/
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}

return $this->qualifyModel($model);
}

protected function alreadyExists($rawName)
{
$name = Str::plural(Str::camel(class_basename(str_replace('\\', '/', $rawName))));

$path = "{$this->laravel['path']}/../resources/js/{$name}/Edit.vue";

return file_exists($path);
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getOptions()
{
return [
['swedish-name', 's', InputOption::VALUE_OPTIONAL, 'Swedish name'],
['swedish-name-plural', 'sp', InputOption::VALUE_OPTIONAL, 'Swedish plural name'],
];
}


/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/vue.edit.component.vue';
}

/**
* Get the destination class path.
*
* @param string $name
* @return string
*/
protected function getPath($name)
{
$name = Str::plural(Str::camel(class_basename(str_replace('\\', '/', $name))));

return "{$this->laravel['path']}/../resources/js/{$name}/Edit.vue";
}
}
Loading

0 comments on commit 19d39b4

Please sign in to comment.