Skip to content

Commit

Permalink
Merge pull request #42 from acrollet/1.0
Browse files Browse the repository at this point in the history
Add support for composer 2
  • Loading branch information
moufmouf authored Nov 4, 2020
2 parents 499dc32 + 6491267 commit 8b26f68
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=5.3.0",
"composer-plugin-api": "^1.0.0",
"composer-plugin-api": "^1.0 || ^2.0",
"ext-openssl": "*"
},
"require-dev": {
Expand Down
11 changes: 3 additions & 8 deletions src/NodeJsInstaller.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mouf\NodeJsInstaller;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Util\RemoteFilesystem;

Expand All @@ -14,10 +15,10 @@ class NodeJsInstaller

protected $rfs;

public function __construct(IOInterface $io)
public function __construct(IOInterface $io, Composer $composer)
{
$this->io = $io;
$this->rfs = new RemoteFilesystem($io);
$this->rfs = new RemoteFilesystem($io, $composer->getConfig());
}

/**
Expand Down Expand Up @@ -199,7 +200,6 @@ public function install($version, $targetDirectory)
$this->io->write(" Downloading from $url");

$cwd = getcwd();
chdir(__DIR__.'/../../../../');

$fileName = 'vendor/'.pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME);

Expand Down Expand Up @@ -291,9 +291,6 @@ private function extractTo($tarGzFile, $targetDir)

public function createBinScripts($binDir, $targetDir, $isLocal)
{
$cwd = getcwd();
chdir(__DIR__.'/../../../../');

if (!file_exists($binDir)) {
$result = mkdir($binDir, 0775, true);
if ($result === false) {
Expand All @@ -311,8 +308,6 @@ public function createBinScripts($binDir, $targetDir, $isLocal)
$this->createBinScript($binDir, $fullTargetDir, 'node.bat', 'node', $isLocal);
$this->createBinScript($binDir, $fullTargetDir, 'npm.bat', 'npm', $isLocal);
}

chdir($cwd);
}

/**
Expand Down
39 changes: 33 additions & 6 deletions src/NodeJsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
*/
class NodeJsPlugin implements PluginInterface, EventSubscriberInterface
{
const NODEJS_TARGET_DIR = 'vendor/nodejs/nodejs';
const DOWNLOAD_NODEJS_EVENT = 'download-nodejs';

/**
* @var Composer
*/
protected $composer;

const DOWNLOAD_NODEJS_EVENT = 'download-nodejs';

/**
* @var IOInterface
*/
Expand All @@ -35,6 +38,19 @@ public function activate(Composer $composer, IOInterface $io)
$this->io = $io;
}

public function deactivate(Composer $composer, IOInterface $io)
{
$binDir = $composer->getConfig()->get('bin-dir');
$this->onDeactivate($binDir);
}

public function uninstall(Composer $composer, IOInterface $io)
{
$binDir = $composer->getConfig()->get('bin-dir');
$targetDir = self::NODEJS_TARGET_DIR;
$this->onUninstall($binDir, $targetDir);
}

/**
* Let's register the harmony dependencies update events.
*
Expand All @@ -61,7 +77,7 @@ public static function getSubscribedEvents()
public function onPostUpdateInstall(Event $event)
{
$settings = array(
'targetDir' => 'vendor/nodejs/nodejs',
'targetDir' => self::NODEJS_TARGET_DIR,
'forceLocal' => false,
'includeBinInPath' => false,
);
Expand Down Expand Up @@ -90,7 +106,7 @@ public function onPostUpdateInstall(Event $event)
$this->verboseLog("<info>NodeJS installer:</info>");
$this->verboseLog(" - Requested version: ".$versionConstraint);

$nodeJsInstaller = new NodeJsInstaller($this->io);
$nodeJsInstaller = new NodeJsInstaller($this->io, $this->composer);

$isLocal = false;

Expand Down Expand Up @@ -181,7 +197,7 @@ private function installLocalVersion($binDir, NodeJsInstaller $nodeJsInstaller,
*/
private function installBestPossibleLocalVersion(NodeJsInstaller $nodeJsInstaller, $versionConstraint, $targetDir)
{
$nodeJsVersionsLister = new NodeJsVersionsLister($this->io);
$nodeJsVersionsLister = new NodeJsVersionsLister($this->io, $this->composer);
$allNodeJsVersions = $nodeJsVersionsLister->getList();

$nodeJsVersionMatcher = new NodeJsVersionMatcher();
Expand Down Expand Up @@ -245,7 +261,17 @@ private function onUninstall($binDir, $targetDir)
}
}

// Now, let's remove the links
$this->onDeactivate($binDir);
}

/**
* Deactivates NodeJS links.
*/
private function onDeactivate($binDir)
{
$fileSystem = new Filesystem();

// Remove the links.
$this->verboseLog("Removing NodeJS and NPM links from Composer bin directory");
foreach (array("node", "npm", "node.bat", "npm.bat") as $file) {
$realFile = $binDir.DIRECTORY_SEPARATOR.$file;
Expand All @@ -254,4 +280,5 @@ private function onUninstall($binDir, $targetDir)
}
}
}

}
5 changes: 3 additions & 2 deletions src/NodeJsVersionsLister.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mouf\NodeJsInstaller;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Util\RemoteFilesystem;

Expand All @@ -18,10 +19,10 @@ class NodeJsVersionsLister

const NODEJS_DIST_URL = "https://nodejs.org/dist/";

public function __construct(IOInterface $io)
public function __construct(IOInterface $io, Composer $composer)
{
$this->io = $io;
$this->rfs = new RemoteFilesystem($io);
$this->rfs = new RemoteFilesystem($io, $composer->getConfig());
}

public function getList()
Expand Down

0 comments on commit 8b26f68

Please sign in to comment.