Skip to content

Commit

Permalink
chore: implement publishing config
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeerickson committed Sep 23, 2019
1 parent bb92b3d commit df17356
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 122 deletions.
44 changes: 19 additions & 25 deletions app/Commands/CraftDeveloper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
namespace App\Commands;

use App\CraftsmanFileSystem;
use Illuminate\Support\Arr;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use Codedungeon\PHPMessenger\Facades\Messenger;

class CraftDeveloper extends Command
{
Expand All @@ -18,33 +15,30 @@ class CraftDeveloper extends Command

public function handle()
{
echo "\n";

Messenger::debug("cwd: " . getcwd());

$fs = new CraftsmanFileSystem();
$value = $fs->getConfigValue("craftsman.paths.class");
Messenger::info($value);

$value = $fs->getConfigValue("full.fname");
Messenger::info($value);
debug("cwd: " . getcwd());
debug("local config: " . $fs->getLocalConfigFilename());

exit;
debug("config path: " . config_path());

// $path = Phar::running(false);
// if (strlen($path) > 0) {
// $path = dirname($path).DIRECTORY_SEPARATOR;
// }
$useCurrentDefault = $fs->getConfigValue("miscellaneous.useCurrentDefault");
debug("useCurrentDefault: {$useCurrentDefault}");

$templates = config('craftsman.templates');
foreach ($templates as $template) {
$template = getcwd() . DIRECTORY_SEPARATOR . $template;
echo ($template . ' templates exists: ' . file_exists($template) . PHP_EOL);
}
}
// echo "\nTemplates:\n";
// $templates = $fs->getConfigValue('templates');

public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
// foreach ($templates as $key => $template) {
// $template = getcwd() . DIRECTORY_SEPARATOR . $template;
// $exists = file_exists($template) ? ' exists' : ' does not exist';
// echo ($template . $exists . PHP_EOL);
// }

// echo "\nPaths:\n";
// $paths = $fs->getConfigValue('paths');
// foreach ($paths as $key => $path) {
// $path = getcwd() . DIRECTORY_SEPARATOR . $path;
// echo (str_pad($key, 12) . " => " . $path . PHP_EOL);
// }
}
}
2 changes: 1 addition & 1 deletion app/Commands/CraftMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function handle()
"fields" => $fields,
"foreign" => $foreign,
"down" => $this->option('down'),
"current" => $this->option('current'),
"current" => $this->option('current') || config('craftsman.miscellaneous.useCurrentDefault'),
"create" => $create,
"update" => $update
];
Expand Down
11 changes: 11 additions & 0 deletions app/Commands/CraftPublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,16 @@ public function handle()
(new Filesystem)->copyDirectory($src, $dest);
Messenger::success("Templates Published Successfully", "SUCCESS");
}

// copy config/craftsman
$src = $this->fs->getAppConfigFilename();
$dest = $this->fs->path_join(getcwd(), "config", "craftsman.php");
if (file_exists($dest) && !$overwrite) {
Messenger::error("{$dest} already exists", "ERROR");
} else {
if ($src !== $dest) {
copy($src, $dest);
}
}
}
}
51 changes: 43 additions & 8 deletions app/CraftsmanFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,53 @@ public function rmdir($dirname)

public function getLocalConfigFilename()
{
$localConfigFilename = getcwd() . DIRECTORY_SEPARATOR . "sandbox" . DIRECTORY_SEPARATOR . "config.php";
if ($this->isPhar()) {
$localConfigFilename = $this->path_join(getcwd(), "config", "craftsman.php");
} else {
$localConfigFilename = $this->path_join(getcwd(), "sandbox", "craftsman.php");
}

if (!file_exists($localConfigFilename)) {
return null;
}
return $localConfigFilename;
}

public function getAppPath()
{
return $this->pharPath();
}

public function getAppConfigFilename()
{
if (strlen($this->getPharPath()) > 0) {
return $this->path_join($this->getPharPath(), "config", "craftsman.php");
}
return $this->path_join(getcwd(), "config", "craftsman.php");
}

public function getConfigValue($key)
{
if ($this->getLocalConfigFilename()) {
$data = require($this->getLocalConfigFilename());
if (Arr::has($data, $key)) {
return Arr::get($data, $key);
}
return config($key);
$localConfigFilename = $this->getLocalConfigFilename();
$appConfigFilename = $this->getAppConfigFilename();

if (file_exists($localConfigFilename)) {
$configData = array_replace_recursive(
require($appConfigFilename),
require($localConfigFilename)
);
return Arr::get($configData, $key);
}
return config($key);
}

public function mergeConfigFrom($path, $key)
{
$config = $this->app['config']->get($key, []);

$this->app['config']->set($key, array_replace_recursive(require $path, $config));
}

/**
* @param $asset
* @param $data
Expand Down Expand Up @@ -312,6 +341,12 @@ public function getTemplatesDirectory()
return $this->getPharPath() . "templates";
}

public function isPhar()
{
return strlen(Phar::running(false)) > 0;
}


/**
* @return string
*/
Expand Down Expand Up @@ -581,7 +616,7 @@ public function createFile($type = null, $filename = null, $data = [])
$vars["teardown"] = (isset($data["teardown"])) ? $data["teardown"] : false;
$vars["constructor"] = (isset($data["constructor"])) ? $data["constructor"] : false;
$vars["foreign"] = (isset($data["foreign"])) ? $data["foreign"] : false;
$vars["current"] = (isset($data["current"])) ? $data["current"] : false;
$vars["current"] = (isset($data["current"])) ? $data["current"] : config("craftsman.miscellaneous.useCurrentDefault");
$vars["create"] = (isset($data["create"])) ? $data["create"] : true;
$vars["update"] = (isset($data["update"])) ? $data["update"] : false;

Expand Down
23 changes: 23 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Codedungeon\PHPMessenger\Facades\Messenger;

if (!function_exists('path_join')) {
/**
* @return string|string[]|null
Expand Down Expand Up @@ -182,3 +184,24 @@ function dlog($msg = "")
}
}
}

if (!function_exists("msg_info")) {
function msg_info($msg = "")
{
Messenger::info($msg);
}
}

if (!function_exists("msg_debug")) {
function msg_debug($msg = "")
{
Messenger::debug($msg);
}
}

if (!function_exists("debug")) {
function debug($msg = "")
{
Messenger::debug($msg);
}
}
34 changes: 0 additions & 34 deletions builds/config.php

This file was deleted.

60 changes: 60 additions & 0 deletions builds/config/craftsman.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

return [

/*
|-----------------------------------------------------------------------------
| Craftsman Paths
|------------------------------------------------------------------------------
|
| This value determines the "paths" which will be used when crafting assets
| In most cases, these are the sensible defaults used by Laravel.
| You might want to adjust the `models` path to something like `app/Models`.
*/

'paths' => [
'class' => 'app',
'controllers' => 'app/Http/Controllers',
'resources' => 'app/Http/Resources',
'factories' => 'database/factories',
'migrations' => 'database/migrations',
'models' => 'app',
'requests' => 'app/Http/Requests',
'seeds' => 'database/seeds',
'templates' => 'templates',
'tests' => 'tests',
'views' => 'resources/views',
],

/*
|-----------------------------------------------------------------------------
| Templates
|------------------------------------------------------------------------------
|
| This value determines the default template paths for each asset
| If you wish to override any of these templates, you should create them
| in using the following naming convention `templates/class.mustache.user.php`
| accordingly. You should not modify the default templates directly as they
| will be overwritten during any updates..
*/

'templates' => [
'base' => 'templates',
'class' => 'templates/class.mustache',
'api-controller' => 'templates/api-controller.mustache',
'binding-controller' => 'templates/binding-controller.mustache',
'empty-controller' => 'templates/empty-controller.mustache',
'resource-controller' => 'templates/resource-controller.mustache',
'controller' => 'templates/controller.mustache',
'factory' => 'templates/factory.mustache',
'migration' => 'templates/migration.mustache',
'model' => 'templates/model.mustache',
'request' => 'templates/form-request.mustache',
'seed' => 'templates/seed.mustache',
'test' => 'templates/test.mustache',
'view-create' => 'templates/view-create.mustache',
'view-edit' => 'templates/view-edit.mustache',
'view-index' => 'templates/view-index.mustache',
'view-show' => 'templates/view-show.mustache',
],
];
Binary file modified builds/laravel-craftsman
100755 → 100644
Binary file not shown.
34 changes: 0 additions & 34 deletions config.php

This file was deleted.

12 changes: 12 additions & 0 deletions config/craftsman.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@
'view-show' => 'templates/view-show.mustache',
],

/*
|-----------------------------------------------------------------------------
| Miscellaneous
|------------------------------------------------------------------------------
|
| Various crafting options
| useCurrentDefault - determines how migrations define timestamps
*/

"miscellaneous" => [
"useCurrentDefault" => true,
]
];
5 changes: 0 additions & 5 deletions config/test.php

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "laravel-craftsman",
"version": "1.7.2",
"build": "230",
"build": "276",
"description": "Laravel Craftsman",
"main": "index.js",
"directories": {
Expand Down
4 changes: 3 additions & 1 deletion tasks/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ info "Build number bumped to $BUILD ..." " INFO "

php laravel-craftsman app:build
rm -rf builds/templates
rm -rf builds/config
cp -r templates builds/templates
cp config.php builds
mkdir builds/config
cp config/craftsman.php builds/config

printf "\n"
success "Build Completed Successfully" " SUCCESS "
Expand Down
4 changes: 2 additions & 2 deletions templates/migration.mustache
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Create{{model}}Table extends Migration
{
Expand Down
Loading

0 comments on commit df17356

Please sign in to comment.