Skip to content

Commit d6a5731

Browse files
authored
Merge pull request #5 from moufmouf/commands
Adding commands
2 parents 58a2e77 + 7e4927b commit d6a5731

32 files changed

+1170
-82
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ humbuglog.txt
88
humbuglog.json
99
template/
1010
.couscous/
11+
/tests/fixtures/copy/
12+
/npm-debug.log
13+
/template
14+
/build

couscous.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ menu:
4343
order:
4444
text: Managing assets order
4545
relativeUrl: doc/order.html
46+
cli:
47+
text: Command line interface
48+
relativeUrl: doc/cli.html
4649
schema:
4750
text: discovery.json format
4851
relativeUrl: doc/discovery_schema.html

doc/cli.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Command line interface
3+
subTitle:
4+
currentMenu: cli
5+
---
6+
7+
Discovery comes with a CLI (command line interface).
8+
9+
You can use the CLI commands to:
10+
11+
- debug what is happening
12+
- modify your local `discovery.json`
13+
- hook third party tools (like Gulp or Webpack)
14+
15+
Actually, *discovery* is adding a set of commands to composer.
16+
17+
## Listing asset types
18+
19+
```bash
20+
$ composer discovery:list
21+
```
22+
23+
will output the list of asset types and the list of assets in each asset-type:
24+
25+
```
26+
my-asset-type-1:
27+
assetA
28+
assetB
29+
assetC
30+
my-asset-type-2:
31+
assetD
32+
```
33+
34+
You can also ask for the assets of a given asset-type:
35+
36+
```bash
37+
$ composer discovery:list my-asset-type-2
38+
```
39+
40+
will output:
41+
42+
```
43+
my-asset-type-2:
44+
assetD
45+
```
46+
47+
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).
48+
49+
```bash
50+
$ composer discovery:list --format=json
51+
```
52+
53+
## Dumping the Discovery files
54+
55+
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:
56+
57+
```bash
58+
$ composer discovery:dump
59+
```
60+
61+
## Adding an asset
62+
63+
You can add an asset to an asset type using the `discovery:add` command:
64+
65+
```bash
66+
$ composer discovery:add "my-asset-type" "my-asset-value"
67+
```
68+
69+
The asset is added to the `discovery.json` file at the root of your project.
70+
71+
You can also set the priority while adding an asset:
72+
73+
```bash
74+
$ composer discovery:add "my-asset-type" "my-asset-value" --priority=42
75+
```
76+
77+
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.
78+
79+
## Removing an asset
80+
81+
You can remove an asset from a project using the `discovery:remove` command:
82+
83+
```bash
84+
$ composer discovery:remove "my-asset-type" "my-asset-value"
85+
```
86+
87+
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).

doc/discovery_schema.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ currentMenu: schema
1111
"some_asset_type": [
1212
{
1313
"value": "some_value",
14-
"meta": {
14+
"metadata": {
1515
"some": "metadata",
1616
"more": "metadata"
1717
},
@@ -33,7 +33,7 @@ Each asset object can be represented this way:
3333
```json
3434
{
3535
"value": "some_value",
36-
"meta": {
36+
"metadata": {
3737
"some": "metadata",
3838
"more": "metadata"
3939
},

doc/metadata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Assets are represented by **strings**. Yet, if you need to pass more complex obj
1212
"some_asset_type": [
1313
{
1414
"value": "some_value",
15-
"meta": {
15+
"metadata": {
1616
"some": "metadata",
1717
"more": "metadata"
1818
}

index.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ currentMenu: discovery-introduction
66
Discovery
77
=========
88

9-
Publish and discover assets in your PHP projects
9+
Publish and discover assets in your PHP projects.
1010

1111
[![Latest Stable Version](https://poser.pugx.org/thecodingmachine/discovery/v/stable)](https://packagist.org/packages/thecodingmachine/discovery)
1212
[![Total Downloads](https://poser.pugx.org/thecodingmachine/discovery/downloads)](https://packagist.org/packages/thecodingmachine/discovery)
@@ -115,13 +115,18 @@ More documentation
115115
<a href="doc/order.html" class="btn btn-primary btn-large btn-block">Managing assets order</a>
116116
</div>
117117
<div class="col-xs-12 col-sm-6">
118-
<a href="doc/conventions.html" class="btn btn-primary btn-large btn-block">Naming conventions</a>
118+
<a href="doc/cli.html" class="btn btn-primary btn-large btn-block">Command line interface</a>
119119
</div>
120120
</div>
121121
<div class="row">
122+
<div class="col-xs-12 col-sm-6">
123+
<a href="doc/conventions.html" class="btn btn-primary btn-large btn-block">Naming conventions</a>
124+
</div>
122125
<div class="col-xs-12 col-sm-6">
123126
<a href="doc/internals.html" class="btn btn-primary btn-large btn-block">Internals and performance</a>
124127
</div>
128+
</div>
129+
<div class="row">
125130
<div class="col-xs-12 col-sm-6">
126131
<a href="doc/discovery_schema.html" class="btn btn-primary btn-large btn-block">discovery.json schema</a>
127132
</div>

src/Asset.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,30 @@ public static function fromArray(array $array) : Asset
108108
{
109109
return new self($array['value'], $array['package'], $array['packageDir'], $array['priority'], $array['metadata']);
110110
}
111+
112+
/**
113+
* Returns a simplified array representing the object.
114+
*
115+
* @return array|string
116+
*/
117+
public function toSimpleArray()
118+
{
119+
$item = [
120+
'value' => $this->getValue(),
121+
];
122+
123+
if ($this->getPriority() !== 0.0) {
124+
$item['priority'] = $this->getPriority();
125+
}
126+
127+
if (!empty($this->getMetadata())) {
128+
$item['metadata'] = $this->getMetadata();
129+
}
130+
131+
if (count($item) === 1) {
132+
return $item['value'];
133+
} else {
134+
return $item;
135+
}
136+
}
111137
}

src/AssetOperation.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,21 @@ public static function buildFromArray(array $array, string $package, string $pac
101101

102102
return new self(self::ADD, new Asset($value, $package, $packageDir, $priority, $metadata));
103103
}
104+
105+
/**
106+
* Returns a simplified array/string representing this asset operation.
107+
*
108+
* @return array|string
109+
*/
110+
public function toSimpleArray()
111+
{
112+
$simple = $this->asset->toSimpleArray();
113+
if ($this->operation === self::REMOVE) {
114+
if (is_string($simple)) {
115+
$simple = [ 'value' => $simple ];
116+
}
117+
$simple['action'] = self::REMOVE;
118+
}
119+
return $simple;
120+
}
104121
}

src/AssetsBuilder.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Composer\Installer\InstallationManager;
77
use Composer\IO\IOInterface;
88
use Composer\Package\PackageInterface;
9+
use Composer\Package\RootPackageInterface;
10+
use Composer\Repository\RepositoryInterface;
911
use Symfony\Component\Filesystem\Filesystem;
1012
use TheCodingMachine\Discovery\Utils\JsonException;
1113

@@ -36,6 +38,27 @@ public function __construct(InstallationManager $installationManager, IOInterfac
3638
$this->rootDir = $rootDir;
3739
}
3840

41+
/**
42+
* Find discovery.json files in the passed repository and builds an asset type.
43+
*
44+
* @param RepositoryInterface $repository
45+
* @param RootPackageInterface $rootPackage
46+
* @return array|AssetType[]
47+
*/
48+
public function findAssetTypes(RepositoryInterface $repository) : array
49+
{
50+
$unorderedPackagesList = $repository->getPackages();
51+
52+
$orderedPackageList = PackagesOrderer::reorderPackages($unorderedPackagesList);
53+
54+
$packages = array_filter($orderedPackageList, function (PackageInterface $package) {
55+
$packageInstallPath = $this->getInstallPath($package);
56+
57+
return file_exists($packageInstallPath.'/discovery.json');
58+
});
59+
60+
return $this->buildAssetTypes($packages);
61+
}
3962

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

85108
$fileSystem = new Filesystem();
86109

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

93116
return $discoveryFileLoader->loadDiscoveryFile(new \SplFileInfo($path), $package->getName(), $packageDir);
94117
}
118+
119+
private function getInstallPath(PackageInterface $package) : string
120+
{
121+
if ($package instanceof RootPackageInterface) {
122+
return getcwd();
123+
} else {
124+
return $this->installationManager->getInstallPath($package);
125+
}
126+
}
95127
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\Discovery\Commands;
5+
6+
7+
use Composer\Command\BaseCommand;
8+
use Composer\Factory;
9+
use Composer\Repository\CompositeRepository;
10+
use Composer\Repository\InstalledArrayRepository;
11+
use TheCodingMachine\Discovery\AssetsBuilder;
12+
13+
abstract class AbstractDiscoveryCommand extends BaseCommand
14+
{
15+
/**
16+
* @return AssetType[]
17+
*/
18+
protected function getAssetTypes() : array
19+
{
20+
$installationManager = $this->getComposer()->getInstallationManager();
21+
$rootDir = dirname(Factory::getComposerFile());
22+
$assetBuilder = new AssetsBuilder($installationManager, $this->getIO(), $rootDir);
23+
24+
$localRepos = $this->getComposer()->getRepositoryManager()->getLocalRepository();
25+
26+
$repos = array(
27+
$localRepos,
28+
new InstalledArrayRepository([$this->getComposer()->getPackage()]),
29+
);
30+
$compositeRepos = new CompositeRepository($repos);
31+
return $assetBuilder->findAssetTypes($compositeRepos);
32+
}
33+
}

0 commit comments

Comments
 (0)