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

Set preferred-install for specific packages from command line #6301

Closed
jonathanjfshaw opened this issue Mar 26, 2017 · 10 comments
Closed

Set preferred-install for specific packages from command line #6301

jonathanjfshaw opened this issue Mar 26, 2017 · 10 comments

Comments

@jonathanjfshaw
Copy link

@jonathanjfshaw jonathanjfshaw commented Mar 26, 2017

It doesn't seem possible to set a global config preferred-install for specific packages from the command line.

I'm trying to setup containers for contributing to Drupal, using a third-party composer.json from drupal-composer/drupal-project. I'd like all drupal packages to be downloaded prefer-source, but all upstream dependencies to be prefer-dist.

However, from the cli this sort of thing does not seem possible:
composer config --global preferred-install '{"drupal/*":"source"}'

@alcohol

This comment has been minimized.

Copy link
Member

@alcohol alcohol commented Mar 27, 2017

The composer config command is somewhat of a glued together bundle of spaghetti. It could very well be the case that this particular config attribute has not been added yet to the list of cases that the command can handle.

From what I can tell by just quickly looking at the source code, it only accepts values on a global level, not on a vendor/package level: https://github.com/composer/composer/blob/master/src/Composer/Command/ConfigCommand.php#L300-L307

Pull requests are welcome.

@smoqadam

This comment has been minimized.

Copy link

@smoqadam smoqadam commented Jan 12, 2018

Hi

I'm a new contributer and want to help. I want to grab this one but have no idea what to do?

If this issue is not good for start please introduce me something else

@lf-jeremy

This comment has been minimized.

Copy link

@lf-jeremy lf-jeremy commented Oct 17, 2018

It is worth noting that in the meantime, this can be accomplished via config as an alternative option that might be acceptable in some use cases:

https://getcomposer.org/doc/06-config.md#preferred-install

That support was added by #3790 should anyone want to see related work for implementing this for cli.

It seems a cli option will involve the nuances of how explicit cli options may interact with config-based options that are inherited.

@alcohol

This comment has been minimized.

Copy link
Member

@alcohol alcohol commented Oct 18, 2018

@smoqadam it depends on your familiarity with php I suppose.

Basically, the config command can be used to modify the config section of the composer.json file from the command line interface.

This is the command in question: ConfigCommand

If you read through the execute() method you should be able to get a gist of how it handles the input it receives. The preferred-install config section is currently handled here:

'preferred-install' => array(
function ($val) {
return in_array($val, array('auto', 'source', 'dist'), true);
},
function ($val) {
return $val;
},
),

Perhaps it would suffice to simply treat it as a multi-value setting, similar to these:

$multiConfigValues = array(
'github-protocols' => array(
function ($vals) {
if (!is_array($vals)) {
return 'array expected';
}
foreach ($vals as $val) {
if (!in_array($val, array('git', 'https', 'ssh'))) {
return 'valid protocols include: git, https, ssh';
}
}
return true;
},
function ($vals) {
return $vals;
},
),
'github-domains' => array(
function ($vals) {
if (!is_array($vals)) {
return 'array expected';
}
return true;
},
function ($vals) {
return $vals;
},
),
'gitlab-domains' => array(
function ($vals) {
if (!is_array($vals)) {
return 'array expected';
}
return true;
},
function ($vals) {
return $vals;
},
),
);

However, none of these currently support the key: value format. So perhaps this is not the best way to approach it.

Maybe you can approach it like repositories:

// handle repositories
if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
if ($input->getOption('unset')) {
return $this->configSource->removeRepository($matches[1]);
}
if (2 === count($values)) {
return $this->configSource->addRepository($matches[1], array(
'type' => $values[0],
'url' => $values[1],
));
}
if (1 === count($values)) {
$value = strtolower($values[0]);
if (true === $booleanValidator($value)) {
if (false === $booleanNormalizer($value)) {
return $this->configSource->addRepository($matches[1], false);
}
} else {
$value = JsonFile::parseJson($values[0]);
return $this->configSource->addRepository($matches[1], $value);
}
}
throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs https://bar.com');
}

@adrianosferreira

This comment has been minimized.

Copy link
Contributor

@adrianosferreira adrianosferreira commented Jan 7, 2020

@alcohol I've jumped here, I hope it is OK. Can you check it once you get some time, please? #8517

I've chosen another approach, I followed the same approach as for extra and plataform, @jonathanjfshaw do you think it is enough to solve your issue (please check the PR)?

@Seldaek

This comment has been minimized.

Copy link
Member

@Seldaek Seldaek commented Jan 13, 2020

Fixed by #8517 - usable as composer config preferred-install.foo/bar dist for example.

@Seldaek Seldaek closed this Jan 13, 2020
@Seldaek Seldaek modified the milestones: Nice To Have, 1.10 Jan 13, 2020
@sergeynezbritskiy

This comment has been minimized.

Copy link

@sergeynezbritskiy sergeynezbritskiy commented Jan 14, 2020

@Seldaek Any idea when 1.10 will be released ? Looking forward to this feature.

@Seldaek

This comment has been minimized.

Copy link
Member

@Seldaek Seldaek commented Jan 15, 2020

You can use snapshots for now composer self-update --snapshot, but I'll try to release soon.

@sergeynezbritskiy

This comment has been minimized.

Copy link

@sergeynezbritskiy sergeynezbritskiy commented Jan 15, 2020

Hi @Seldaek thank you for your response so fast! I used snapshot and got this working,
but when I try to set it globally, like composer global config "preferred-install.sergeynezbritskiy/*" source, this config is pushed to $COMPOSER_HOME/composer.json, but expected to be pushed to $COMPOSER_HOME/config.json.
So if I want to set this configuration globally I still can't use composer global config command and need to edit global config.json manually
Any suggestions ?

@sergeynezbritskiy

This comment has been minimized.

Copy link

@sergeynezbritskiy sergeynezbritskiy commented Jan 15, 2020

Oh, sorry
composer config --global "preferred-install.sergeynezbritskiy/*" source
this command works fine, configuration is pushed to config.json in $COMPOSER_HOME
Waiting for official release then
@adrianosferreira @Seldaek thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.