Skip to content

Commit

Permalink
Merge pull request #12 from open-sausages/pulls/1/throwing-a-public
Browse files Browse the repository at this point in the history
API Support root-project expose and respect public folder
  • Loading branch information
Aaron Carlino authored Jan 12, 2018
2 parents ad91e76 + 48a6204 commit 38af368
Show file tree
Hide file tree
Showing 7 changed files with 490 additions and 171 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"extra": {
"class": "SilverStripe\\VendorPlugin\\VendorPlugin",
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.3.x-dev"
}
},
"scripts": {
Expand All @@ -27,6 +27,7 @@
},
"minimum-stability": "dev",
"require": {
"composer/installers": "^1.4",
"composer-plugin-api": "^1.1"
},
"require-dev": {
Expand Down
103 changes: 86 additions & 17 deletions src/Console/VendorExposeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use Composer\Factory;
use Composer\IO\ConsoleIO;
use Composer\Util\Filesystem;
use Generator;
use SilverStripe\VendorPlugin\Library;
use SilverStripe\VendorPlugin\Util;
use SilverStripe\VendorPlugin\VendorExposeTask;
use SilverStripe\VendorPlugin\VendorModule;
use SilverStripe\VendorPlugin\VendorPlugin;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,7 +23,7 @@ class VendorExposeCommand extends BaseCommand
public function configure()
{
$this->setName('vendor-expose');
$this->setDescription('Refresh all exposed vendor module folders');
$this->setDescription('Refresh all exposed module/theme/project folders');
$this->addArgument(
'method',
InputArgument::OPTIONAL,
Expand All @@ -35,50 +36,118 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$io = new ConsoleIO($input, $output, $this->getHelperSet());

// Check modules to expose
$modules = $this->getAllModules();
// Check libraries to expose
$modules = $this->getAllLibraries();
if (empty($modules)) {
$io->write("No modules to expose");
return;
}

// Query first library for base destination
$basePublicPath = $modules[0]->getBasePublicPath();

// Expose all modules
$method = $input->getArgument('method');
$task = new VendorExposeTask($this->getProjectPath(), new Filesystem(), VendorModule::DEFAULT_TARGET);
$task = new VendorExposeTask($this->getProjectPath(), new Filesystem(), $basePublicPath);
$task->process($io, $modules, $method);

// Success
$io->write("All modules updated!");
}

/**
* Find all modules
* Get all libraries
*
* @return VendorModule[]
* @return Library[]
*/
protected function getAllModules()
protected function getAllLibraries()
{
$modules = [];
$basePath = $this->getProjectPath();
$search = Util::joinPaths($basePath, 'vendor', '*', '*');
foreach (glob($search, GLOB_ONLYDIR) as $modulePath) {

// Get all modules
foreach ($this->getModulePaths() as $modulePath) {
// Filter by non-composer folders
$composerPath = Util::joinPaths($modulePath, 'composer.json');
if (!file_exists($composerPath)) {
continue;
}
// Build module
$name = basename($modulePath);
$vendor = basename(dirname($modulePath));
$module = new VendorModule($basePath, "{$vendor}/{$name}");
// Check if this module has folders to expose
if ($module->getExposedFolders()) {
$modules[] = $module;

// Ensure this library should be exposed, and has at least one folder
$module = new Library($basePath, $modulePath);
if (!$module->requiresExpose() || !$module->getExposedFolders()) {
continue;
}

// Save this module
$modules[] = $module;
}
return $modules;
}

/**
* Find all modules
*
* @deprecated 1.3..2.0
* @return Library[]
*/
protected function getAllModules()
{
return $this->getAllLibraries();
}

/**
* Search all paths that could contain a module / theme
*
* @return Generator
*/
protected function getModulePaths()
{
// Project root is always returned
$basePath = $this->getProjectPath();
yield $basePath;

// Get vendor modules
$search = Util::joinPaths($basePath, 'vendor', '*', '*');
foreach (glob($search, GLOB_ONLYDIR) as $modulePath) {
if ($this->isPathModule($modulePath)) {
yield $modulePath;
}
}

// Check if public/ folder exists
$publicExists = is_dir(Util::joinPaths($basePath, Library::PUBLIC_PATH));
if (!$publicExists) {
return;
}

// Search all base folders / modules
$search = Util::joinPaths($basePath, '*');
foreach (glob($search, GLOB_ONLYDIR) as $modulePath) {
if ($this->isPathModule($modulePath)) {
yield $modulePath;
}
}

// Check all themes
$search = Util::joinPaths($basePath, 'themes', '*');
foreach (glob($search, GLOB_ONLYDIR) as $themePath) {
yield $themePath;
}
}

/**
* Check if the given path is a silverstripe module
*
* @param string $path
* @return bool
*/
protected function isPathModule($path)
{
return file_exists(Util::joinPaths($path, '_config'))
|| file_exists(Util::joinPaths($path, '_config.php'));
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit 38af368

Please sign in to comment.