-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from open-sausages/pulls/1.0/vendor-expose
API Add vendor-expose composer command
- Loading branch information
Showing
11 changed files
with
399 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace SilverStripe\VendorPlugin\Console; | ||
|
||
use Composer\Command\BaseCommand; | ||
use Composer\Plugin\Capability\CommandProvider; | ||
|
||
class VendorCommandProvider implements CommandProvider | ||
{ | ||
/** | ||
* Retreives an array of commands | ||
* | ||
* @return BaseCommand[] | ||
*/ | ||
public function getCommands() | ||
{ | ||
return [ | ||
new VendorExposeCommand() | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace SilverStripe\VendorPlugin\Console; | ||
|
||
use Composer\Command\BaseCommand; | ||
use Composer\Factory; | ||
use Composer\IO\ConsoleIO; | ||
use Composer\Util\Filesystem; | ||
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; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Provides `composer vendor-expose` behaviour | ||
*/ | ||
class VendorExposeCommand extends BaseCommand | ||
{ | ||
public function configure() | ||
{ | ||
$this->setName('vendor-expose'); | ||
$this->setDescription('Refresh all exposed vendor module folders'); | ||
$this->addArgument( | ||
'method', | ||
InputArgument::OPTIONAL, | ||
'Optional method to use. Defaults to last used value, or ' . VendorPlugin::METHOD_DEFAULT . ' otherwise' | ||
); | ||
$this->setHelp('This command will update all resources for all installed modules using the given method'); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new ConsoleIO($input, $output, $this->getHelperSet()); | ||
|
||
// Check modules to expose | ||
$modules = $this->getAllModules(); | ||
if (empty($modules)) { | ||
$io->write("No modules to expose"); | ||
return; | ||
} | ||
|
||
// Expose all modules | ||
$method = $input->getArgument('method'); | ||
$task = new VendorExposeTask($this->getProjectPath(), new Filesystem(), VendorModule::DEFAULT_TARGET); | ||
$task->process($io, $modules, $method); | ||
|
||
// Success | ||
$io->write("All modules updated!"); | ||
} | ||
|
||
/** | ||
* Find all modules | ||
* | ||
* @return VendorModule[] | ||
*/ | ||
protected function getAllModules() | ||
{ | ||
$modules = []; | ||
$basePath = $this->getProjectPath(); | ||
$search = Util::joinPaths($basePath, 'vendor', '*', '*'); | ||
foreach (glob($search, GLOB_ONLYDIR) 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; | ||
} | ||
} | ||
return $modules; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getProjectPath() | ||
{ | ||
return dirname(realpath(Factory::getComposerFile())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace SilverStripe\VendorPlugin\Methods; | ||
|
||
use Composer\Util\Platform; | ||
use RuntimeException; | ||
|
||
/** | ||
* Expose the vendor module resources via a symlink | ||
*/ | ||
class JunctionMethod extends SymlinkMethod | ||
{ | ||
const NAME = 'junction'; | ||
|
||
protected function createLink($source, $target) | ||
{ | ||
if (!Platform::isWindows()) { | ||
throw new RuntimeException("Cannot create junction on non-windows environment"); | ||
} | ||
$this->filesystem->junction($source, $target); | ||
|
||
// Check if this command succeeded | ||
$success = $this->filesystem->isJunction($target); | ||
if (!$success) { | ||
throw new RuntimeException("Could not create symlink at $target"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.