Skip to content

Create add:table command #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Add/Concerns/HasAdminOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ protected function configure()
}

/**
* Determine which resource path to
* Determine which resource path to use.
*
* @param string $path
* @return string
*/
public function determineResourcePath($path)
{
if ($this->option('admin')) {
return resource_path('admin/js/'.$path);
return 'admin/js/'.$path;
}
return resource_path('/app/js/'.$path);
return '/app/js/'.$path;
}
}
136 changes: 135 additions & 1 deletion src/Add/TableAddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,141 @@
namespace AwStudio\Snippets\Add;

use AwStudio\Snippets\BaseCommand;
use Illuminate\Contracts\Filesystem\FileExistsException;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class TableAddCommand extends BaseCommand
{
}
use Concerns\HasAdminOption;

/**
* The name of the console command.
*
* @var string
*/
protected $signature = 'add:table
{resource : The resource (path) for which the table should be created}
{name? : Specify the table name (resource name is used by default)}
';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add a new resource table';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Gets the stub path.
*
* @param string $name
* @return string
*/
protected function getStub($name)
{
return __DIR__."/../../stubs/{$name}";
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
[$path, $name] = $this->parseInput();

File::ensureDirectoryExists(resource_path($this->determineResourcePath($path)));

$this->createVueFile('Table', $path, $name);
$this->createVueFile('TableHead', $path, $name);
$this->createVueFile('TableBody', $path, $name);

$this->info("Created ${name}Table, ${name}TableHead and ${name}TableBody in " . $this->determineResourcePath($path));

return 0;
}

/**
* Parse the given input to determine name and path.
*
* @return array
*/
public function parseInput()
{
$input = $this->argument('resource');

if ($this->argument('name')) {
$name = Str::ucfirst($this->argument('name'));
} else {
$name = Str::of($input)->afterLast('/')->singular()->ucfirst();
}

$path = Str::of($input)->before('/')->ucfirst();

if (Str::contains($name, 'Table')) {
$name = Str::before($name, 'Table');
}

return [
$path,
$name,
];
}

/**
* Create a new vue file from a given Stub file name in a given path and name,.
*
* @param string $stub
* @param string $path
* @param string $name
* @return void
* @throws FileExistsException
*/
public function createVueFile($stub, $path, $name)
{
$file = $this->getStub($stub.'.stub.vue');

$content = $this->replaceFileContent(File::get($file), $name);

$outputName = $path.'/'.$name.''.$stub.'.vue';

$outputPath = resource_path($this->determineResourcePath($outputName));

if (File::exists($outputPath)) {
throw new FileExistsException("$outputName already exists");
}

File::put($outputPath, $content);
}

/**
* Replaces strings in the stub file with the corresponding term based on
* the given resource name.
*
* @param string $content
* @param string $resourceName
* @return string
*/
public function replaceFileContent($content, $resourceName)
{
return Str::of($content)
->replace('{{ Resource }}', Str::ucfirst($resourceName))
->replace('{{ resource }}', Str::lower($resourceName))
->replace(
'{{ routeResource }}',
Str::of($resourceName)->plural(100)->lower()
);
}
}
2 changes: 2 additions & 0 deletions src/SnippetsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AwStudio\Snippets;

use AwStudio\Snippets\Add\TableAddCommand;
use AwStudio\Snippets\Add\PageAddCommand;
use AwStudio\Snippets\Install\AdminCommand;
use AwStudio\Snippets\Install\AppCommand;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function registerCommands(): void
TypeScriptCommand::class,
VueCommand::class,
PageBuilderCommand::class,
TableAddCommand::class,
PageAddCommand::class,

// Make
Expand Down
26 changes: 26 additions & 0 deletions stubs/Table.stub.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<Index :table="table">
<Table :table="table">
<template v-slot:thead>
<{{ Resource }}TableHead :table="table" />
</template>
<template v-slot:tbody>
<{{ Resource }}TableBody :table="table" />
</template>
</Table>
</Index>
</template>

<script setup lang="ts">
import { useIndex } from '@macramejs/macrame-vue3';
import { Index, Table, Search } from '@macramejs/admin-vue3';
import { {{ Resource }} } from '@admin/modules/resources';
import {{ Resource }}TableBody from './{{ Resource }}TableBody.vue';
import {{ Resource }}TableHead from './{{ Resource }}TableHead.vue';

let table = useIndex<{{ Resource }}>({
route: '/admin/{{ routeResource }}/items',
syncUrl: true,
sortBy: [],
});
</script>
23 changes: 23 additions & 0 deletions stubs/TableBody.stub.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<Tr
v-for="(user, trKey) in table.items"
:key="`th-${trKey}`"
@click="Inertia.visit(`/admin/{{ routeResource }}/${{{ resource }}.id}`)"
>
<Td slim>
<strong>{{ {{ resource }}.id }}</strong>
</Td>
<Td slim> {{ {{ resource }}.name }} </Td>
</Tr>
</template>

<script setup lang="ts">
import { Inertia } from '@inertiajs/inertia';
import { PropType } from 'vue';
import { Index } from '@macramejs/macrame-vue3';
import { Tr, Td } from '@macramejs/admin-vue3';
import { {{ Resource }} } from '@admin/modules/resources';
defineProps({
table: Object as PropType<Index<{{ Resource }}>>,
});
</script>
15 changes: 15 additions & 0 deletions stubs/TableHead.stub.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<Tr>
<Th :table="table">#</Th>
<Th :table="table">Name</Th>
</Tr>
</template>

<script setup lang="ts">
import { PropType } from 'vue';
import { Index } from '@macramejs/macrame-vue3';
import { Tr, Th } from '@macramejs/admin-vue3';
const props = defineProps({
table: Object as PropType<Index>,
});
</script>