Skip to content

Commit

Permalink
Merge pull request #1 from ARCANEDEV/update-workspaces
Browse files Browse the repository at this point in the history
Adding the ability to set multiple workspaces
  • Loading branch information
arcanedev-maroc committed Nov 27, 2018
2 parents 9384cfc + 0af71df commit fa6152f
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Expand Up @@ -23,7 +23,7 @@ checks:
tools:
external_code_coverage:
timeout: 600
runs: 4
runs: 3
php_code_sniffer:
enabled: true
config:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -14,6 +14,8 @@

This Assets package allows you to structure your assets into multiple workspaces (like frontoffice + backoffice + components + packages…).

**IMPORTANT:** This package is a helper like `php artisan preset` built on top of the [Yarn's workspaces feature](https://yarnpkg.com/lang/en/docs/workspaces).

## Features

* A very flexible assets management.
Expand Down Expand Up @@ -87,8 +89,6 @@ For example:
//...
```

Thank to [Yarn's workspaces feature](https://yarnpkg.com/lang/en/docs/workspaces).

## Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests, please check the [contribution guidelines](CONTRIBUTING.md).
Expand Down
2 changes: 1 addition & 1 deletion _docs/1-Installation-and-Setup.md
Expand Up @@ -30,7 +30,7 @@ You need also to install [Yarn](https://yarnpkg.com) `version >= 1.0`.

## Composer

You can install this package via [Composer](http://getcomposer.org/) by running this command: `composer require arcanedev/laravel-assets`.
You can install this package via [Composer](http://getcomposer.org/) by running this command: `composer require arcanedev/laravel-assets --dev`.

### Artisan commands

Expand Down
18 changes: 13 additions & 5 deletions _docs/2-Configuration.md
Expand Up @@ -15,15 +15,23 @@ use Arcanedev\Assets\Pipes\Presets;
return [

/* -----------------------------------------------------------------
| Root
| Workspaces
| -----------------------------------------------------------------
*/

// The root directory for your assets
'root-directory' => 'assets',
'default-workspace' => 'default',

// Recommended to be a single word like a prefixed npm vendor, eg: @assets/{module}
'root-package' => 'assets',
'workspaces' => [

'default' => [
// The root directory for your assets
'root-directory' => 'assets',

// Recommended to be a single word like a prefixed npm vendor, eg: @assets/{module}
'root-package' => 'assets',
],

],

// The public directory
'public-directory' => 'public',
Expand Down
2 changes: 1 addition & 1 deletion _docs/3-Usage.md
Expand Up @@ -30,5 +30,5 @@ php artisan assets:make
You can also specify the name and the preset if you want to avoid the console questions:

```bash
php artisan assets:make admin --preset=bootstrap
php artisan assets:make admin --preset bootstrap
```
18 changes: 13 additions & 5 deletions config/assets.php
Expand Up @@ -5,15 +5,23 @@
return [

/* -----------------------------------------------------------------
| Root
| Workspaces
| -----------------------------------------------------------------
*/

// The root directory for your assets
'root-directory' => 'assets',
'default-workspace' => 'default',

// Recommended to be a single word like a prefixed npm vendor, eg: @assets/{module}
'root-package' => 'assets',
'workspaces' => [

'default' => [
// The root directory for your assets
'root-directory' => 'assets',

// Recommended to be a single word like a prefixed npm vendor, eg: @assets/{module}
'root-package' => 'assets',
],

],

// The public directory
'public-directory' => 'public',
Expand Down
33 changes: 25 additions & 8 deletions src/Console/Command.php
@@ -1,9 +1,9 @@
<?php namespace Arcanedev\Assets\Console;

use Arcanedev\Assets\Exceptions\NotInitiatedException;
use Illuminate\Pipeline\Pipeline;
use Arcanedev\Assets\Helpers\Workspaces;
use Illuminate\Console\Command as BaseCommand;
use Illuminate\Support\Facades\File;
use Illuminate\Pipeline\Pipeline;

/**
* Class Command
Expand Down Expand Up @@ -49,19 +49,36 @@ protected function pipeline()
*/
protected function getPassable()
{
return [
'root-directory' => config('assets.root-directory', 'assets'),
'root-package' => config('assets.root-package', 'assets'),
'public-directory' => config('assets.public-directory', 'public'),
];
$default = $this->getSelectedWorkspace();
$workspace = Workspaces::get($default);

return array_merge(
$workspace,
[
'workspace' => $default,
'public-directory' => config('assets.public-directory', 'public'),
]
);
}

/**
* Check if the assets was initiated.
*/
protected function ensureIsInitiated()
{
if ( ! File::isDirectory(config('assets.root-directory')))
$default = $this->getSelectedWorkspace();

if ( ! Workspaces::rootDirectoryExists($default))
NotInitiatedException::throw();
}

/**
* Get the default workspace.
*
* @return \Illuminate\Config\Repository|mixed
*/
protected function getSelectedWorkspace()
{
return $this->option('workspace') ?: Workspaces::getDefaultName();
}
}
4 changes: 3 additions & 1 deletion src/Console/InitCommand.php
Expand Up @@ -20,7 +20,9 @@ class InitCommand extends Command
*
* @var string
*/
protected $signature = 'assets:init {name=laravel : The assets name folder}';
protected $signature = 'assets:init
{name=laravel : The assets name folder}
{--W|workspace= : Select a predefined workspace}';

/**
* The console command description.
Expand Down
5 changes: 4 additions & 1 deletion src/Console/MakeCommand.php
Expand Up @@ -21,7 +21,10 @@ class MakeCommand extends Command
*
* @var string
*/
protected $signature = 'assets:make {name} {--P|preset= : Preset for the new asset}';
protected $signature = 'assets:make
{name : Name of the asset}
{--W|workspace= : Select a predefined workspace}
{--P|preset= : Preset for the new asset}';

/**
* The console command description.
Expand Down
72 changes: 59 additions & 13 deletions src/Helpers/Stub.php
@@ -1,5 +1,7 @@
<?php namespace Arcanedev\Assets\Helpers;

use Illuminate\Support\Facades\File;

/**
* Class Stub
*
Expand All @@ -14,7 +16,7 @@ class Stub
*/

/** @var string */
protected $raw;
protected $content;

/* -----------------------------------------------------------------
| Main Methods
Expand All @@ -28,7 +30,7 @@ class Stub
*/
public static function make($file = null)
{
return (new static)->setRaw(
return (new static)->setContent(
static::load($file ?: static::getFilePath())
);
}
Expand All @@ -46,15 +48,25 @@ protected static function getFilePath()
}

/**
* Set the raw file content.
* Get the stub content.
*
* @return string
*/
public function content()
{
return $this->content;
}

/**
* Set the stub content.
*
* @param string $raw
* @param string $content
*
* @return static
*/
protected function setRaw($raw)
protected function setContent($content)
{
$this->raw = $raw;
$this->content = $content;

return $this;
}
Expand All @@ -69,26 +81,60 @@ protected function setRaw($raw)
*/
public function replace($search, $replace)
{
return $this->setRaw(
str_replace($search, $replace, $this->raw)
return $this->setContent(
str_replace($search, $replace, $this->content)
);
}

/**
* Get the content.
*
* @param string $file
*
* @return string
*/
public static function load($file)
{
return file_get_contents(static::path($file));
return File::get(static::path($file));
}

/**
* Save the content.
*
* @param string $path
*
* @return int
*/
public function save($path)
{
file_put_contents($path, $this->raw);
return File::put($path, $this->content);
}

/**
* Prepare the stub path.
*
* @param string|null $file
*
* @return string
*/
public static function path($file = null)
{
$path = dirname(__DIR__, 2).'/stubs';
$path = static::getBasePath();

if ( ! is_null($file))
$path = "{$path}/".trim($file, '/');

return is_null($file) ? $path : $path.'/'.trim($file, '/');
return $path;
}

/**
* Get the base stubs' path.
*
* @return string
*/
public static function getBasePath()
{
return realpath(dirname(__DIR__, 2).'/stubs');
}

/**
Expand All @@ -98,6 +144,6 @@ public static function path($file = null)
*/
public function toArray()
{
return json_decode($this->raw, true);
return json_decode($this->content, true);
}
}
86 changes: 86 additions & 0 deletions src/Helpers/Workspaces.php
@@ -0,0 +1,86 @@
<?php namespace Arcanedev\Assets\Helpers;

use Illuminate\Support\Arr;

/**
* Class Workspaces
*
* @package Arcanedev\Assets\Helpers
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class Workspaces
{
/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Get the default workspace name.
*
* @param string|null $default
*
* @return \Illuminate\Config\Repository|mixed
*/
public static function getDefaultName($default = null)
{
return config('assets.default-workspace', $default);
}

/**
* Get the workspace configs.
*
* @param string $name
* @param array|null $default
*
* @return array|null
*/
public static function get($name, $default = null)
{
return Arr::get(static::all(), $name, $default);
}

/**
* Get all the workspaces.
*
* @return array
*/
public static function all()
{
return config('assets.workspaces');
}

/**
* Get all workspaces' names.
*
* @return array
*/
public static function keys()
{
return array_keys(static::all());
}

/**
* Get all the root directories.
*
* @return array|mixed
*/
public static function getAllRootDirectories()
{
return array_column(static::all(), 'root-directory');
}

/**
* Check if the root directory exists by the given workspace.
*
* @param string $name
*
* @return bool
*/
public static function rootDirectoryExists($name)
{
$directory = config("assets.workspaces.{$name}.root-directory");

return ! is_null($directory) && is_dir($directory);
}
}

0 comments on commit fa6152f

Please sign in to comment.