Skip to content

Commit

Permalink
Merge pull request #5 from moufmouf/commands
Browse files Browse the repository at this point in the history
Adding commands
  • Loading branch information
moufmouf authored Apr 11, 2017
2 parents 58a2e77 + 7e4927b commit d6a5731
Show file tree
Hide file tree
Showing 32 changed files with 1,170 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ humbuglog.txt
humbuglog.json
template/
.couscous/
/tests/fixtures/copy/
/npm-debug.log
/template
/build
3 changes: 3 additions & 0 deletions couscous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ menu:
order:
text: Managing assets order
relativeUrl: doc/order.html
cli:
text: Command line interface
relativeUrl: doc/cli.html
schema:
text: discovery.json format
relativeUrl: doc/discovery_schema.html
Expand Down
87 changes: 87 additions & 0 deletions doc/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Command line interface
subTitle:
currentMenu: cli
---

Discovery comes with a CLI (command line interface).

You can use the CLI commands to:

- debug what is happening
- modify your local `discovery.json`
- hook third party tools (like Gulp or Webpack)

Actually, *discovery* is adding a set of commands to composer.

## Listing asset types

```bash
$ composer discovery:list
```

will output the list of asset types and the list of assets in each asset-type:

```
my-asset-type-1:
assetA
assetB
assetC
my-asset-type-2:
assetD
```

You can also ask for the assets of a given asset-type:

```bash
$ composer discovery:list my-asset-type-2
```

will output:

```
my-asset-type-2:
assetD
```

Finally, if you need more data, you can use the JSON output (maybe you want to output the metadata associated to each asset or you want to integrate the output with a third party tool).

```bash
$ composer discovery:list --format=json
```

## Dumping the Discovery files

Discovery is "dumping" a set of files that enable extremely quick access to the assets. **If you change manually a `discovery.json` file in your project, you will need to regenerate those files.** You can do this with the `discovery:dump` command:

```bash
$ composer discovery:dump
```

## Adding an asset

You can add an asset to an asset type using the `discovery:add` command:

```bash
$ composer discovery:add "my-asset-type" "my-asset-value"
```

The asset is added to the `discovery.json` file at the root of your project.

You can also set the priority while adding an asset:

```bash
$ composer discovery:add "my-asset-type" "my-asset-value" --priority=42
```

Note: currently, discovery does not support adding metadata via the command line. You will need to edit the `discovery.json` file manually to add metadata to an asset.

## Removing an asset

You can remove an asset from a project using the `discovery:remove` command:

```bash
$ composer discovery:remove "my-asset-type" "my-asset-value"
```

The asset is removed from the `discovery.json` file at the root of your project or a "remove" action is added in the `discovery.json` file at the root of your project (if the asset is defined in a dependency).
4 changes: 2 additions & 2 deletions doc/discovery_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ currentMenu: schema
"some_asset_type": [
{
"value": "some_value",
"meta": {
"metadata": {
"some": "metadata",
"more": "metadata"
},
Expand All @@ -33,7 +33,7 @@ Each asset object can be represented this way:
```json
{
"value": "some_value",
"meta": {
"metadata": {
"some": "metadata",
"more": "metadata"
},
Expand Down
2 changes: 1 addition & 1 deletion doc/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Assets are represented by **strings**. Yet, if you need to pass more complex obj
"some_asset_type": [
{
"value": "some_value",
"meta": {
"metadata": {
"some": "metadata",
"more": "metadata"
}
Expand Down
9 changes: 7 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ currentMenu: discovery-introduction
Discovery
=========

Publish and discover assets in your PHP projects
Publish and discover assets in your PHP projects.

[![Latest Stable Version](https://poser.pugx.org/thecodingmachine/discovery/v/stable)](https://packagist.org/packages/thecodingmachine/discovery)
[![Total Downloads](https://poser.pugx.org/thecodingmachine/discovery/downloads)](https://packagist.org/packages/thecodingmachine/discovery)
Expand Down Expand Up @@ -115,13 +115,18 @@ More documentation
<a href="doc/order.html" class="btn btn-primary btn-large btn-block">Managing assets order</a>
</div>
<div class="col-xs-12 col-sm-6">
<a href="doc/conventions.html" class="btn btn-primary btn-large btn-block">Naming conventions</a>
<a href="doc/cli.html" class="btn btn-primary btn-large btn-block">Command line interface</a>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6">
<a href="doc/conventions.html" class="btn btn-primary btn-large btn-block">Naming conventions</a>
</div>
<div class="col-xs-12 col-sm-6">
<a href="doc/internals.html" class="btn btn-primary btn-large btn-block">Internals and performance</a>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6">
<a href="doc/discovery_schema.html" class="btn btn-primary btn-large btn-block">discovery.json schema</a>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,30 @@ public static function fromArray(array $array) : Asset
{
return new self($array['value'], $array['package'], $array['packageDir'], $array['priority'], $array['metadata']);
}

/**
* Returns a simplified array representing the object.
*
* @return array|string
*/
public function toSimpleArray()
{
$item = [
'value' => $this->getValue(),
];

if ($this->getPriority() !== 0.0) {
$item['priority'] = $this->getPriority();
}

if (!empty($this->getMetadata())) {
$item['metadata'] = $this->getMetadata();
}

if (count($item) === 1) {
return $item['value'];
} else {
return $item;
}
}
}
17 changes: 17 additions & 0 deletions src/AssetOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,21 @@ public static function buildFromArray(array $array, string $package, string $pac

return new self(self::ADD, new Asset($value, $package, $packageDir, $priority, $metadata));
}

/**
* Returns a simplified array/string representing this asset operation.
*
* @return array|string
*/
public function toSimpleArray()
{
$simple = $this->asset->toSimpleArray();
if ($this->operation === self::REMOVE) {
if (is_string($simple)) {
$simple = [ 'value' => $simple ];
}
$simple['action'] = self::REMOVE;
}
return $simple;
}
}
34 changes: 33 additions & 1 deletion src/AssetsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Composer\Installer\InstallationManager;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
use Composer\Repository\RepositoryInterface;
use Symfony\Component\Filesystem\Filesystem;
use TheCodingMachine\Discovery\Utils\JsonException;

Expand Down Expand Up @@ -36,6 +38,27 @@ public function __construct(InstallationManager $installationManager, IOInterfac
$this->rootDir = $rootDir;
}

/**
* Find discovery.json files in the passed repository and builds an asset type.
*
* @param RepositoryInterface $repository
* @param RootPackageInterface $rootPackage
* @return array|AssetType[]
*/
public function findAssetTypes(RepositoryInterface $repository) : array
{
$unorderedPackagesList = $repository->getPackages();

$orderedPackageList = PackagesOrderer::reorderPackages($unorderedPackagesList);

$packages = array_filter($orderedPackageList, function (PackageInterface $package) {
$packageInstallPath = $this->getInstallPath($package);

return file_exists($packageInstallPath.'/discovery.json');
});

return $this->buildAssetTypes($packages);
}

/**
* Builds the AssetTypes that will be exported in the generated TheCodingMachine\Discovery class.
Expand Down Expand Up @@ -80,7 +103,7 @@ public function buildAssetTypes(array $discoveryPackages) : array
*/
private function getDiscoveryJson(PackageInterface $package) : array
{
$packageInstallPath = $this->installationManager->getInstallPath($package);
$packageInstallPath = $this->getInstallPath($package);

$fileSystem = new Filesystem();

Expand All @@ -92,4 +115,13 @@ private function getDiscoveryJson(PackageInterface $package) : array

return $discoveryFileLoader->loadDiscoveryFile(new \SplFileInfo($path), $package->getName(), $packageDir);
}

private function getInstallPath(PackageInterface $package) : string
{
if ($package instanceof RootPackageInterface) {
return getcwd();
} else {
return $this->installationManager->getInstallPath($package);
}
}
}
33 changes: 33 additions & 0 deletions src/Commands/AbstractDiscoveryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace TheCodingMachine\Discovery\Commands;


use Composer\Command\BaseCommand;
use Composer\Factory;
use Composer\Repository\CompositeRepository;
use Composer\Repository\InstalledArrayRepository;
use TheCodingMachine\Discovery\AssetsBuilder;

abstract class AbstractDiscoveryCommand extends BaseCommand
{
/**
* @return AssetType[]
*/
protected function getAssetTypes() : array
{
$installationManager = $this->getComposer()->getInstallationManager();
$rootDir = dirname(Factory::getComposerFile());
$assetBuilder = new AssetsBuilder($installationManager, $this->getIO(), $rootDir);

$localRepos = $this->getComposer()->getRepositoryManager()->getLocalRepository();

$repos = array(
$localRepos,
new InstalledArrayRepository([$this->getComposer()->getPackage()]),
);
$compositeRepos = new CompositeRepository($repos);
return $assetBuilder->findAssetTypes($compositeRepos);
}
}
60 changes: 60 additions & 0 deletions src/Commands/AddAssetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php


namespace TheCodingMachine\Discovery\Commands;


use Composer\Command\BaseCommand;
use Composer\Factory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use TheCodingMachine\Discovery\Asset;
use TheCodingMachine\Discovery\AssetOperation;
use TheCodingMachine\Discovery\AssetsBuilder;
use TheCodingMachine\Discovery\AssetType;
use TheCodingMachine\Discovery\DiscoveryFileLoader;
use TheCodingMachine\Discovery\Dumper;

class AddAssetCommand extends BaseCommand
{
protected function configure()
{
$this->setName('discovery:add')
->setDescription('Add an asset')
->addArgument('asset-type', InputArgument::REQUIRED, 'The asset type')
->addArgument('value', InputArgument::REQUIRED, 'The asset to add')
->addOption('priority', 'p', InputOption::VALUE_REQUIRED, 'The priority', 0.0);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$assetTypeName = $input->getArgument('asset-type');
$assetValue = $input->getArgument('value');

$priority = $input->getOption('priority');

if (!is_numeric($priority)) {
$output->writeln('<error>The priority must be a numeric value.</error>');
return;
}

$loader = new DiscoveryFileLoader();

if (file_exists('discovery.json')) {
$assetOperationTypes = $loader->loadDiscoveryFile(new \SplFileInfo('discovery.json'), '', '');
} else {
$assetOperationTypes = [];
}

$assetOperationTypes[$assetTypeName][] = new AssetOperation(AssetOperation::ADD, new Asset($assetValue, '', '', $priority, null));

$loader->saveDiscoveryFile($assetOperationTypes, new \SplFileObject('discovery.json', 'w+'));

$dumper = new Dumper($this->getComposer(), $this->getIO());
$dumper->dumpDiscoveryFiles();

$output->writeln('discovery.json file successfully modified.');
}
}
28 changes: 28 additions & 0 deletions src/Commands/CommandProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace TheCodingMachine\Discovery\Commands;

use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;

/**
* List of all commands provided by this package.
*/
class CommandProvider implements CommandProviderCapability
{

/**
* Retrieves an array of commands
*
* @return \Composer\Command\BaseCommand[]
*/
public function getCommands()
{
return [
new ListAssetTypesCommand(),
new DumpCommand(),
new AddAssetCommand(),
new RemoveAssetCommand(),
];
}
}
Loading

0 comments on commit d6a5731

Please sign in to comment.