Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Feb 5, 2019
0 parents commit 06b1cd0
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "wpscholar/github-archive-installer",
"description": "A custom Composer installer that will install a dependency from a GitHub release archive .zip file when installing from distribution.",
"type": "composer-plugin",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Micah Wood",
"email": "micah@wpscholar.com"
}
],
"require": {
"composer-plugin-api": "^1.1"
},
"autoload": {
"psr-4": {
"wpscholar\\Composer\\": "src"
}
},
"extra": {
"class": "wpscholar\\Composer\\GithubArchiveInstaller"
}
}
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# GitHub Archive Installer

A custom Composer installer that will install a dependency from a GitHub release archive .zip file when installing from distribution.

## Why You Need It
It is very common that you might have a number of development only files in your code library source files. Unless explicitly installing from source, you generally don't need to have development only files in your final distribution. You may also want to perform some specific build steps such as building some production-ready JavaScript files. This installer allows you to keep generated code out of your repository while still being able to reliably deliver it in your final distribution.

Using a tool like Travis CI, you can automate the build of your final distribution and automatically attach the generated .zip file to the release on GitHub. Then, using this installer, you can easily configure your library to install the generated .zip file when installing as `dist` in Composer.

## How it Works

Any package with a direct dependency on `wpscholar/github-archive-installer` and a valid stable version number being installed from distribution will be installed from the GitHub archive `<repo-name>.zip` file associated with a specific release.

For example, if my Composer package is named `wpscholar/hello-world` then my generated .zip file should be named `hello-world.zip` in order to be properly installed using this installer.

This installer only changes the `distUrl` for the package in Composer. It doesn't override any existing installers that work based off of the `type` property in your `composer.json` file. For example, if you have a package of type `wordpress-plugin` your package would still install in the correct location in WordPress. However, when installing from `dist` it would simply pull from the .zip file attached to your GitHub release.
117 changes: 117 additions & 0 deletions src/GithubArchiveInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Installs GitHub archive files from a release when installing from distribution.
*
* @package wpscholar/Composer/GithubArchiveInstaller
*/

namespace wpscholar\Composer;

use Composer\Composer;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;

/**
* Class GithubArchiveInstaller
*
* @package wpscholar\Composer
*/
class GithubArchiveInstaller implements PluginInterface, EventSubscriberInterface {

const PACKAGE_TYPE = 'github-archive-installer';

/**
* Composer instance.
*
* @var \Composer\Composer
*/
protected $composer;

/**
* Input/Output interface.
*
* @var \Composer\IO\IOInterface
*/
protected $io;

/**
* Apply plugin modifications to Composer.
*
* @param \Composer\Composer $composer Composer instance
* @param \Composer\IO\IOInterface $io Input/Output interface
*/
public function activate( Composer $composer, IOInterface $io ) {
$this->composer = $composer;
$this->io = $io;
}

/**
* Returns an array of event names this subscriber wants to listen to.
*
* @return array
*/
public static function getSubscribedEvents() {
return array(
PackageEvents::PRE_PACKAGE_INSTALL => 'preInstall',
PackageEvents::PRE_PACKAGE_UPDATE => 'preInstall',
);
}

/**
* Set distribution URL before installing.
*
* @param \Composer\Installer\PackageEvent $event The package install or update event.
*/
public function preInstall( PackageEvent $event ) {

/**
* Get the package instance.
*
* @var \Composer\Package\Package $package
*/
$package = $this->getPackageFromOperation( $event->getOperation() );

if ( array_key_exists( 'wpscholar/github-archive-installer', $package->getRequires() ) ) {
if ( version_compare( $package->getFullPrettyVersion(), '0.0.0', '>=' ) ) {
$package->setDistUrl(
sprintf(
'https://github.com/%1$s/releases/download/%2$s/%3$s.zip',
$package->getName(),
$package->getFullPrettyVersion(),
explode( '/', $package->getName() )[1]
)
);
}
}
}

/**
* Convert operation to package instance.
*
* @param \Composer\DependencyResolver\Operation\OperationInterface $operation The operation
*
* @return \Composer\Package\PackageInterface The package of the operation
*/
public function getPackageFromOperation( OperationInterface $operation ) {
if ( 'update' === $operation->getJobType() ) {
/**
* Operation is an update operation.
*
* @var \Composer\DependencyResolver\Operation\UpdateOperation $operation
*/
return $operation->getTargetPackage();
}

/**
* Operation is an install operation.
*
* @var \Composer\DependencyResolver\Operation\InstallOperation $operation
*/
return $operation->getPackage();
}

}

0 comments on commit 06b1cd0

Please # to comment.