Skip to content

Commit

Permalink
Merge pull request #3 from phar-io/addWizzard
Browse files Browse the repository at this point in the history
Use a wizard after "create-project" to gather the basic informations
  • Loading branch information
sebastianfeldmann authored Mar 27, 2021
2 parents e37703b + 1e5d25b commit 1981105
Show file tree
Hide file tree
Showing 14 changed files with 575 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/vendor/
composer.lock
.phpunit.cache
tools
58 changes: 7 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,32 @@
# Mediator

This is a demo plugin that can be used to create your own Composer Plugin
that installs a PHAR file instead of a sh*tload of sourcecode
This is a template project to create a Composer plugin that installs a PHAR file instead of a sh*tload of sourcecode

## Usage

Create your own copy of this project by running
Create your own copy of this project by running and following the instructions.

```bash
$ composer create-project phar-io/mediator /path/to/your/source/path
```

After that you should replace or adapt the following informations in these
files:
### Adding your public signing keys

### `src/Plugin.php`

* Line 11: replace `PharIo\Mediator` with a namespace for your Plugin
Note: We need this same namespace later in line 16 of the `compposer.json`
again
* Line 25: replace `JUnitDiff` with the name of your Package.
* Line 35: Replace `junitdiff` with the name of how you want to reference the
phar-file you are downloading here.
* Line 39: replace the URL to the PHAR file that you want to download. For a
Guide to replacement-variables see the section "Replacements" later in this
document
* Line 44: replace the URl to the signature file that you want to use to verify
the integrity of the downloaded PHAR-file. Again: See the section
"Replacements" later in this doc for replacement variables.
* If you want to download more than one PHAR-file, duplicate the lines 33 to 46
and add more `File`-objects to the `FileList`.
* (optional) Line 27: If you want to modify the keys directory, you can specify
the path to your own key directory here.

### `keys/junitdiff.key`

Replace this key with the public key corresponding to the private key that you
are using to sign the PHAR file with. You can get that by using something like
this command
If you are signing your releases you can add the public key to your plugin.
Export your public key like this.

```bash
$ gpg --export -a junitdiff > keys/mykey.key
$ gpg --export -a mykey > keys/mykey.key
```

This will export the key with the ID `junitdiff` into a file `mykey.key` inside
This will export the key with the ID `mykey` into a file `mykey.key` inside
the `keys` directory.

You can add more than one key to that keys-folder and each of the keys will be
used to check for a verification for the signature. So for projects that have
more than one person signing builds you can add all their public keys to this
folder.

### `composer.json`

* Line 2: replace the `name` with your ownn one
* Line 3: replace the `description` with your own one
* Line 5: replace the `licence` with the one you support
* Line 6 through 11: Replace the `authors` with the ones relevant for your
project or the plugin, depending on your favours.
* Line 16: replace the Namespace of the `Plugin` class with the namespace of
your `Plugin`-class (as defined above)
* Line 20: replace the namespace `\\Org_Heigl\\SinglePharPlugin` here as well
with the namespace of your `Plugin`-class.

### LICENSE

Replace the license-file with the appropriate one

### README.md

Replace the README-File with one appropriate for your project

## Replacements

You can use different variables within the URLs that will be replaced with the
Expand Down
30 changes: 29 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,47 @@
"name": "Andreas Heigl",
"email": "andreas@heigl.org",
"role": "Developer"
},
{
"name": "Arne Blankerts",
"email": "arne@blankerts.de",
"role": "Developer"
},
{
"name": "Sebastian Feldmann",
"email": "sf@sebastian-feldmann.info",
"role": "Developer"
}
],
"require": {
"php": "^7.2 || ~8.0.0",
"ext-dom": "*",
"composer-plugin-api": "^1.1||^2.0",
"phar-io/composer-distributor": "^0.2"
"phar-io/composer-distributor": "^1.0"
},
"extra": {
"class": "PharIo\\Mediator\\Plugin"
},
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"PharIo\\Mediator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PharIo\\MediatorTest\\": "tests/"
}
},
"require-dev": {
"composer/composer": "^2.0"
},
"scripts":{
"post-create-project-cmd": [
"PharIo\\Mediator\\Installer::install",
"composer update"
]
}
}
4 changes: 4 additions & 0 deletions phive.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="^9.5.0" installed="9.5.0" location="./tools/phpunit" copy="false"/>
</phive>
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
71 changes: 71 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace PharIo\Mediator;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Script\Event;
use PharIo\Mediator\Service\ConfigurationReader;
use PharIo\Mediator\Service\CreateComposerJson;
use PharIo\Mediator\Service\CreateConfigurationFile;
use PharIo\Mediator\Service\CreatePluginClass;
use PharIo\Mediator\Service\DeleteFiles;
use PharIo\Mediator\Service\NamespaceReader;
use SplFileInfo;
use function realpath;

final class Installer
{
private $io;

private $composer;

private function __construct(IOInterface $io, Composer $composer)
{
$this->io = $io;
$this->composer = $composer;
}

public static function install(Event $event): void
{
$installer = new self($event->getIO(), $event->getComposer());

$installer->runInstallation();
}

public function runInstallation(): void
{
$configReader = new ConfigurationReader($this->io);
$namespaceReader = new NamespaceReader($this->io);
$config = $configReader->getConfiguration();
$namespace = $namespaceReader->getNamespace();

$rootDir = realpath(__DIR__ . '/..');

$composerJson = new CreateComposerJson(new SplFileInfo($rootDir));
$composerJson($config, $namespace);

$mediatorXml = new CreateConfigurationFile(new SplFileInfo($rootDir));
$mediatorXml($config);

$pluginPhp = new CreatePluginClass(new SplFileInfo($rootDir));

$deletor = new DeleteFiles(
new SplFileInfo($rootDir . '/keys/junitdiff.key'),
new SplFileInfo($rootDir . '/src'),
new SplFileInfo($rootDir . '/tests'),
new SplFileInfo($rootDir . '/.git'),
new SplFileInfo($rootDir . '/.gitignore'),
new SplFileInfo($rootDir . '/composer.lock'),
new SplFileInfo($rootDir . '/LICENSE'),
new SplFileInfo($rootDir . '/phive.xml'),
new SplFileInfo($rootDir . '/phpunit.xml'),
new SplFileInfo($rootDir . '/README.md')
);
$deletor();

$pluginPhp($namespace);
}
}
45 changes: 6 additions & 39 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,15 @@
<?php
/**
* Copyright The ComposerDistributor-Team
*
* Licenses under the MIT-license. For details see the included file LICENSE.md
*/

declare(strict_types=1);

namespace PharIo\Mediator;

use Composer\Installer\PackageEvent;
use PharIo\ComposerDistributor\File;
use PharIo\ComposerDistributor\FileList;
use PharIo\ComposerDistributor\PluginBase;
use PharIo\ComposerDistributor\Url;
use PharIo\ComposerDistributor\ConfiguredMediator;

class Plugin extends PluginBase
final class Plugin extends ConfiguredMediator
{
public function installOrUpdateFunction(PackageEvent $event): void
{
$installer = $this->createInstaller(
// Replace this with the name of your plugin
'org_heigl/single-phar-plugin',
// replace this with the path to your key directory
__DIR__ . '/../keys/',
// needs to be passed on!
$event
);

$installer->install(new FileList(
new File(
// Replace this with the name of the binary that you want to use within the folder vendor/bin/
'junitdiff',
Url::fromString(
// replace this with the path to the phar file. Replacements are described in the
// [README.md](https://)
'https://github.com/heiglandreas/JUnitDiff/releases/download/%version%/junitdiff.phar'
),
Url::fromString(
// replace this with the path to the signature-file for the phar. Replacements are described in the
// [README.md](https://)
'https://github.com/heiglandreas/JUnitDiff/releases/download/%version%/junitdiff.phar.asc'
)
)
));
}
public function getDistributorConfig(): string
{
return __DIR__ . '/../mediator.xml';
}
}
Loading

0 comments on commit 1981105

Please sign in to comment.