Skip to content

Commit c60bf1c

Browse files
authoredJan 21, 2018
Merge pull request #30 from larapack/improve-setup-flow
Improve setup flow
2 parents 8160159 + 018bd65 commit c60bf1c

30 files changed

+1312
-59
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ before_script:
1919
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
2020

2121
script:
22-
- vendor/bin/phpunit --stop-on-failure
22+
- vendor/bin/phpunit

‎src/Commands/InstallCommand.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class InstallCommand extends Command
99
{
10-
protected $signature = 'hook:install {name} {version?} {--enable}';
10+
protected $signature = 'hook:install {name} {version?} {--enable} {--no-migrate} {--no-seed} {--no-publish}';
1111

1212
protected $description = 'Download and install a hook from remote https://larapack.io';
1313

@@ -29,7 +29,13 @@ public function handle()
2929
{
3030
$name = $this->argument('name');
3131

32-
$this->hooks->install($name, $this->argument('version'));
32+
$this->hooks->install(
33+
$name,
34+
$this->argument('version'),
35+
!$this->option('no-migrate'),
36+
!$this->option('no-seed'),
37+
!$this->option('no-publish')
38+
);
3339

3440
if ($this->option('enable')) {
3541
$this->hooks->enable($name);

‎src/Commands/UninstallCommand.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class UninstallCommand extends Command
99
{
10-
protected $signature = 'hook:uninstall {name} {--delete}';
10+
protected $signature = 'hook:uninstall {name} {--delete} {--no-unmigrate} {--no-unseed} {--no-unpublish}';
1111

1212
protected $description = 'Uninstall a hook';
1313

@@ -29,7 +29,13 @@ public function handle()
2929
{
3030
$name = $this->argument('name');
3131

32-
$this->hooks->uninstall($name, $this->option('delete'));
32+
$this->hooks->uninstall(
33+
$name,
34+
$this->option('delete'),
35+
!$this->option('no-unmigrate'),
36+
!$this->option('no-unseed'),
37+
!$this->option('no-unpublish')
38+
);
3339

3440
$this->info("Hook [{$name}] have been uninstalled.");
3541
}

‎src/Commands/UpdateCommand.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class UpdateCommand extends Command
99
{
10-
protected $signature = 'hook:update {name} {version?}';
10+
protected $signature = 'hook:update {name} {version?} {--no-migrate} {--no-seed} {--no-publish} {--force}';
1111

1212
protected $description = 'Update a hook';
1313

@@ -35,10 +35,17 @@ public function handle()
3535

3636
$hook = $hooks->where('name', $name)->first();
3737

38-
if ($this->hooks->update($name, $version)) {
39-
return $this->info("Hook [{$name}] have been updated!");
40-
}
41-
42-
return $this->info('Nothing to update.');
38+
$updated = $this->hooks->update(
39+
$name,
40+
$version,
41+
!$this->option('no-migrate'),
42+
!$this->option('no-seed'),
43+
!$this->option('no-publish'),
44+
$this->option('force')
45+
);
46+
47+
return $updated
48+
? $this->info("Hook [{$name}] have been updated!")
49+
: $this->info('Nothing to update.');
4350
}
4451
}

‎src/Hook.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,18 @@ public function loadComposerJson()
6767
$this->composerJson = json_decode($this->getComposerJsonFile(), true);
6868
}
6969

70-
public function getComposerJsonFile()
70+
public function getPath()
7171
{
72-
$path = 'vendor/'.$this->name;
73-
7472
if ($this->isLocal()) {
75-
$path = 'hooks/'.$this->name;
73+
return base_path('hooks/'.$this->name);
7674
}
7775

78-
return $this->filesystem->get(base_path($path.'/composer.json'));
76+
return base_path('vendor/'.$this->name);
77+
}
78+
79+
public function getComposerJsonFile()
80+
{
81+
return $this->filesystem->get($this->getPath().'/composer.json');
7982
}
8083

8184
public function setLatest($latest)
@@ -106,7 +109,7 @@ public function update(array $parameters)
106109
public function outdated()
107110
{
108111
if (is_null($this->latest)) {
109-
$this->latest = app('hooks')->outdated($hook);
112+
$this->latest = app('hooks')->outdated($this->name);
110113
}
111114

112115
return $this->latest != $this->version;

‎src/Hooks.php

+294-16
Large diffs are not rendered by default.

‎src/HooksServiceProvider.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Larapack\Hooks;
44

5+
use Illuminate\Filesystem\Filesystem;
56
use Illuminate\Foundation\AliasLoader;
67
use Illuminate\Support\ServiceProvider;
78

@@ -31,7 +32,12 @@ public function register()
3132
}
3233

3334
// Register Hooks system and aliases
34-
$this->app->singleton(Hooks::class, Hooks::class);
35+
$this->app->singleton(Hooks::class, function ($app) {
36+
$filesystem = $app[Filesystem::class];
37+
$migrator = $app['migrator'];
38+
39+
return new Hooks($filesystem, $migrator);
40+
});
3541
$this->app->alias(Hooks::class, 'hooks');
3642
}
3743

‎stub/composer.json

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
{
2-
"name": "kebab-case",
3-
"description": "This is my first hook.",
4-
"require": {
5-
"larapack/hooks": "~1.0"
6-
},
7-
"autoload": {
8-
"psr-4": {
9-
"StudlyCase\\": "src/"
2+
"name": "kebab-case",
3+
"description": "This is my first hook.",
4+
"require": {
5+
"larapack/hooks": "^1.0.5"
6+
},
7+
"autoload": {
8+
"psr-4": {
9+
"StudlyCase\\": "src/"
10+
}
11+
},
12+
"extra": {
13+
"hook": {
14+
"providers": [
15+
"StudlyCase\\StudlyCaseServiceProvider"
16+
],
17+
"aliases": {
18+
"StudlyCase": "StudlyCase\\StudlyCaseFacade"
19+
},
20+
"migrations": [
21+
"resources/database/migrations"
22+
],
23+
"seeders": [
24+
"resources/database/seeders"
25+
],
26+
"unseeders": [
27+
"resources/database/unseeders"
28+
],
29+
"assets": {
30+
"resources/assets": "public/vendor/kebab-case"
31+
}
32+
}
1033
}
11-
},
12-
"extra": {
13-
"hook": {
14-
"providers": [
15-
"StudlyCase\\StudlyCaseServiceProvider"
16-
]
17-
}
18-
}
1934
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alert('This is a sample file!');

‎stub/resources/database/migrations/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateStudlyCaseTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('snake_case', function (Blueprint $table) {
17+
$table->increments('id');
18+
19+
$table->string('name');
20+
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('snake_case');
33+
}
34+
}

‎stub/resources/database/seeders/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
use Illuminate\Support\Facades\DB;
5+
6+
class StudlyCaseTableSeeder extends Seeder
7+
{
8+
/**
9+
* Run the database seeds.
10+
*
11+
* @return void
12+
*/
13+
public function run()
14+
{
15+
// Don't add data if the data is already there
16+
if (DB::table('snake_case')->count() > 0) {
17+
return;
18+
}
19+
20+
DB::table('snake_case')->insert([
21+
['name' => 'foo'],
22+
['name' => 'bar'],
23+
['name' => 'baz'],
24+
]);
25+
}
26+
}

‎stub/resources/database/unseeders/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class StudlyCaseTableUnseeder extends Seeder
8+
{
9+
/**
10+
* Run the database seeds.
11+
*
12+
* @return void
13+
*/
14+
public function run()
15+
{
16+
// Skip if table does not exists.
17+
if (!Schema::hasTable('snake_case')) {
18+
return;
19+
}
20+
21+
DB::table('snake_case')
22+
->whereIn('name', ['foo', 'bar', 'baz'])
23+
->delete();
24+
}
25+
}

‎stub/src/StudlyCase.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace StudlyCase;
4+
5+
class StudlyCase
6+
{
7+
//
8+
}

‎stub/src/StudlyCaseFacade.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace StudlyCase;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class StudlyCaseFacade extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return StudlyCase::class;
17+
}
18+
}

‎tests/HooksTest.php

+649-9
Large diffs are not rendered by default.

‎tests/TestCase.php

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function setUp()
2525
// Cleanup old hooks before testing
2626
$filesystem->deleteDirectory(base_path('hooks'));
2727

28+
// Cleanup published files
29+
$filesystem->deleteDirectory(base_path('public/vendor'));
30+
2831
// Clear old hooks
2932
$hook = app(Hooks::class);
3033
$hook->readJsonFile();
@@ -57,6 +60,9 @@ public function setUp()
5760

5861
// Reload JSON files
5962
app(Hooks::class)->readJsonFile();
63+
64+
// Migrate
65+
$this->artisan('migrate');
6066
}
6167

6268
public function tearDown()

‎tests/fixtures/composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "local-test-hook",
3+
"description": "This is my first hook.",
4+
"require": {
5+
"larapack/hooks": "^1.0.5"
6+
},
7+
"autoload": {
8+
"psr-4": {
9+
"LocalTestHook\\": "src/"
10+
}
11+
},
12+
"extra": {
13+
"hook": {
14+
"providers": [
15+
"LocalTestHook\\LocalTestHookServiceProvider"
16+
],
17+
"aliases": {
18+
"LocalTestHook": "LocalTestHook\\LocalTestHookFacade"
19+
},
20+
"migrations": [
21+
"resources/database/migrations"
22+
],
23+
"seeders": [
24+
"resources/database/seeders"
25+
],
26+
"unseeders": [
27+
"resources/database/unseeders"
28+
],
29+
"assets": {
30+
"resources/assets": "public/vendor/local-test-hook"
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alert('This is a sample file!');

‎tests/fixtures/resources/database/migrations/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLocalTestHookTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('local_test_hook', function (Blueprint $table) {
17+
$table->increments('id');
18+
19+
$table->string('name');
20+
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('local_test_hook');
33+
}
34+
}

‎tests/fixtures/resources/database/seeders/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
use Illuminate\Support\Facades\DB;
5+
6+
class LocalTestHookTableSeeder extends Seeder
7+
{
8+
/**
9+
* Run the database seeds.
10+
*
11+
* @return void
12+
*/
13+
public function run()
14+
{
15+
// Don't add data if the data is already there
16+
if (DB::table('local_test_hook')->count() > 0) {
17+
return;
18+
}
19+
20+
DB::table('local_test_hook')->insert([
21+
['name' => 'foo'],
22+
['name' => 'bar'],
23+
['name' => 'baz'],
24+
]);
25+
}
26+
}

‎tests/fixtures/resources/database/unseeders/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class LocalTestHookTableUnseeder extends Seeder
8+
{
9+
/**
10+
* Run the database seeds.
11+
*
12+
* @return void
13+
*/
14+
public function run()
15+
{
16+
// Skip if table does not exists.
17+
if (!Schema::hasTable('local_test_hook')) {
18+
return;
19+
}
20+
21+
DB::table('local_test_hook')
22+
->whereIn('name', ['foo', 'bar', 'baz'])
23+
->delete();
24+
}
25+
}

‎tests/fixtures/src/LocalTestHook.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace LocalTestHook;
4+
5+
class LocalTestHook
6+
{
7+
//
8+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LocalTestHook;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class LocalTestHookFacade extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return LocalTestHook::class;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace LocalTestHook;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LocalTestHookServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap any application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Register any application services.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
//
27+
}
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.