Skip to content

Commit

Permalink
Allow addons to publish by default. Closes #1072
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Jul 24, 2020
1 parent 8ca0cee commit 9e9159f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Console/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\File;
use Statamic\Statamic;

class Install extends Command
{
Expand Down Expand Up @@ -34,6 +35,7 @@ public function handle()
$this->addons()
->createFiles()
->publish()
->runCallbacks()
->clearViews()
->clearCache();
}
Expand Down Expand Up @@ -98,4 +100,11 @@ protected function clearCache()

return $this;
}

protected function runCallbacks()
{
Statamic::runAfterInstalledCallbacks($this);

return $this;
}
}
26 changes: 22 additions & 4 deletions src/Providers/AddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class AddonServiceProvider extends ServiceProvider
protected $routes = [];
protected $middlewareGroups = [];
protected $viewNamespace;
protected $publishAfterInstall = true;

public function boot()
{
Expand All @@ -52,7 +53,8 @@ public function boot()
->bootPublishables()
->bootRoutes()
->bootMiddleware()
->bootViews();
->bootViews()
->bootPublishAfterInstall();
});
}

Expand Down Expand Up @@ -167,7 +169,7 @@ protected function bootPublishables()
return [$origin => public_path("vendor/{$package}/{$destination}")];
});

$this->publishes($publishables->all());
$this->publishes($publishables->all(), $this->getAddon()->slug());

return $this;
}
Expand Down Expand Up @@ -291,7 +293,7 @@ public function registerScript(string $path)

$this->publishes([
$path => public_path("vendor/{$name}/js/{$filename}.js"),
]);
], $this->getAddon()->slug());

Statamic::script($name, $filename);
}
Expand All @@ -308,7 +310,7 @@ public function registerStylesheet(string $path)

$this->publishes([
$path => public_path("vendor/{$name}/css/{$filename}.css"),
]);
], $this->getAddon()->slug());

Statamic::style($name, $filename);
}
Expand All @@ -333,4 +335,20 @@ private function getAddon()
return Str::startsWith($class, $addon->namespace());
});
}

protected function bootPublishAfterInstall()
{
if (! $this->publishAfterInstall) {
return $this;
}

Statamic::afterInstalled(function ($command) {
$command->call('vendor:publish', [
'--tag' => $this->getAddon()->slug(),
'--force' => true
]);
});

return $this;
}
}
13 changes: 13 additions & 0 deletions src/Statamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Statamic
protected static $actionRoutes = [];
protected static $jsonVariables = [];
protected static $bootedCallbacks = [];
protected static $afterInstalledCallbacks = [];

public static function version()
{
Expand Down Expand Up @@ -245,4 +246,16 @@ public static function runBootedCallbacks()
$callback();
}
}

public static function afterInstalled(Closure $callback)
{
static::$afterInstalledCallbacks[] = $callback;
}

public static function runAfterInstalledCallbacks($command)
{
foreach (static::$afterInstalledCallbacks as $callback) {
$callback($command);
}
}
}

1 comment on commit 9e9159f

@duncanmcclean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Jason!!

Please sign in to comment.