Skip to content

Commit

Permalink
add link command
Browse files Browse the repository at this point in the history
  • Loading branch information
bangnokia committed Nov 6, 2020
1 parent 3828758 commit 8d0348c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
9 changes: 6 additions & 3 deletions app/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle(Parser $parser)

$pages = collect(
$this->storage->allFiles('resources/views/pages')
)->map(fn ($filePath) => $this->buildPage($filePath));
)->map(fn ($filePath) => $this->buildPage($filePath, $posts));

$this->buildIndexPage($posts, $pages);

Expand Down Expand Up @@ -86,10 +86,13 @@ protected function buildPost(string $filePath): \Post
return $post;
}

protected function buildPage(string $filePath): string
protected function buildPage(string $filePath, Collection $posts): string
{
$pageSlug = str_replace('.blade.php', '', basename($filePath));
$this->storage->put("public/{$pageSlug}.html", view('pages.'.$pageSlug)->render());
$this->storage->put(
"public/{$pageSlug}.html",
view('pages.'.$pageSlug, ['posts' => $posts])->render()
);

return $pageSlug;
}
Expand Down
15 changes: 6 additions & 9 deletions app/Commands/CreateNewSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ public function handle(Filesystem $filesystem)
return 1;
}

$filesystem->copyDirectory(base_path('stubs'), getcwd().'/'.$directory);
$filesystem->copyDirectory(base_path('stubs'), $sitePath = getcwd().'/'.$directory);

$this->createSymbolicLinks($directory, $filesystem);
dump($sitePath);

return 0;
}
$this->call('link', [
'root' => $sitePath,
]);

protected function createSymbolicLinks($directory, Filesystem $filesystem)
{
$filesystem->link(getcwd()."/{$directory}/images", getcwd()."/{$directory}/public/images");
$filesystem->link(getcwd()."/{$directory}/resources/css", getcwd()."/{$directory}/public/css");
$filesystem->link(getcwd()."/{$directory}/resources/js", getcwd()."/{$directory}/public/js");
return 0;
}
}

40 changes: 40 additions & 0 deletions app/Commands/SymlinkCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Commands;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;

class SymlinkCommand extends Command
{
protected $signature = 'link {root?}';

protected $hidden = true;

protected $description = 'Command description';

public function handle(Filesystem $filesystem)
{
$sitePath = $this->argument('root') ?: getcwd();

foreach ($this->linkableDirectories() as $source => $target) {
if ($filesystem->isDirectory($targetLink = "{$sitePath}/{$target}")) {
$filesystem->deleteDirectory($targetLink);
}

$filesystem->delete($targetLink);

$filesystem->link("{$sitePath}/{$source}", $targetLink);
}
}

protected function linkableDirectories(): array
{
return [
'images' => 'public/images',
'resources/css' => 'public/css',
'resources/js' => 'public/js'
];
}
}
3 changes: 2 additions & 1 deletion tests/Feature/CreateNewSiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
});

afterEach(function () {
exec('rm -rf tests/tmp');
$filesystem = new \Illuminate\Filesystem\Filesystem();
$filesystem->deleteDirectory(base_path('tests/tmp'));
});
23 changes: 23 additions & 0 deletions tests/Feature/SymlinkCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$filesystem = new \Illuminate\Filesystem\Filesystem();

it('can delete directories before create symbolic links', function () use ($filesystem) {
$filesystem->makeDirectory('tests/tmp');

chdir('tests/tmp');

$filesystem->makeDirectory('public/images', 0755, true);
$filesystem->makeDirectory('public/css', 0755, true);
$filesystem->makeDirectory('public/js', 0755, true);

$this->artisan('link');

expect(is_link('public/images'))->toBeTrue();
expect(is_link('public/css'))->toBeTrue();
expect(is_link('public/js'))->toBeTrue();
});

afterEach(function () use($filesystem) {
$filesystem->deleteDirectory(base_path('tests/tmp'));
});

0 comments on commit 8d0348c

Please sign in to comment.