Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If specific packages are found in project installation, gets versions from yarn and nodejs from these packages config #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/NodeComposerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,26 @@ public function activate(Composer $composer, IOInterface $io)
$this->io = $io;

$extraConfig = $this->composer->getPackage()->getExtra();
$packageConfig = $extraConfig['mariusbuescher']['node-composer'] ?? [];

if (!isset($extraConfig['mariusbuescher']['node-composer'])) {
$this->updateConfigFromPackageProvidesConfig(
$packageConfig,
'node-version',
'imponeer/composer-nodejs-installer',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a hard coded package name? What will happen if you decide to delete this package?

I see a lot of problems coming from here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's hardcoded package name. If you remove it, node-composer will work as before this package was installed. It means that you need specify node version manually in your package config. So, probably if you use this package and specify also minimal version in config, you will be insurred what will happen if imponeer/composer-nodejs-installer for some unlikly reason will be deleted as composer package. In that case user will loose auto updates, but he still could use own package from last hardcoded nodejs version.

I don't see much problems here but yeah I agree hardcoding package names is not a very good idea overall.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added posibility to specify any other package from where to get version with package-for-node-version and package-for-yarn-version config values.

For example, such composer json:

{
  "name": "dummy/dummy",
  "description": "Dummy project to test if configuration works",
  "type": "project",
  "require": {
    "mariusbuescher/node-composer": "@dev",
    "something/yarn": "1.0.0",
  },
  "extra": {
        "mariusbuescher": {
            "node-composer": {
                "package-for-yarn-version": "something/yarn",
                "node-version": "6.11.0"            
            }
        }
    }
}

Will install yarn 1.0.0 and nodejs 6.11.0

Or if such composer.json:

{
  "name": "dummy/dummy",
  "description": "Dummy project to test if configuration works",
  "type": "project",
  "require": {
    "mariusbuescher/node-composer": "@dev",
  },
  "extra": {
        "mariusbuescher": {
            "node-composer": {
                "package-for-yarn-version": "something/yarn",
                "node-version": "6.11.0"            
            }
        }
    }
}

Will install only node 6.11.0 because something/yarn is not required in project, so no yarn version could be resolved.

Right now is not fully dynamically (tries to find if imponeer packages are installed if not other specified) but I think this will solve most issues with what if these imponeer packages will disapeer someday but if they are installed your package will have just some new extra functionality that will not break old packages but if user decides to use it will add some extra value and some possibilities.

'nodejs'
);
$this->updateConfigFromPackageProvidesConfig(
$packageConfig,
'yarn-version',
'imponeer/composer-yarn-installer',
'yarn'
);

if (empty($packageConfig)) {
throw new NodeComposerConfigException('You must configure the node composer plugin');
}

$this->config = Config::fromArray($extraConfig['mariusbuescher']['node-composer']);
$this->config = Config::fromArray($packageConfig);
}

public static function getSubscribedEvents()
Expand Down Expand Up @@ -143,4 +157,21 @@ public function deactivate(Composer $composer, IOInterface $io)
public function uninstall(Composer $composer, IOInterface $io)
{
}

/**
* Updates local config with data from some specific packages
*
* @param array $config Local config for the update
* @param string $configKey Local config key that will be updated if specific package will be found
* @param string $packageName Package name from where to fetch provides section data
* @param string $providesName Provides key name
*/
protected function updateConfigFromPackageProvidesConfig(array &$config, $configKey, $packageName, $providesName)
{
$foundPackages = $this->composer->getRepositoryManager()->getLocalRepository()->findPackages($packageName);
if (isset($foundPackages[0])) {
$provides = $foundPackages[0]->getProvides();
$config[$configKey] = $provides[$providesName]->getPrettyConstraint();
}
}
}