Branch version strings like dev-master are not supported
Hello @theseer
Following our recent discussion, I would like here to remember the use case I found about version string not yet supported.
Context When we want to create a manifest for a package under development. From master branch or another one, and with latest commit not the same as the one corresponding to the latest git tag. Picking info from VCS like PHPUnit did with https://github.com/sebastianbergmann/phpunit/blob/master/build/scripts/phar-manifest.php#L3-L15
Current behaviour
Prints workaround version 0.0.0-dev
Expected behaviour
Prints version dev-master@045047b37d1dd99bf7e97487e2dc10820439f29b
Script to reproduce issue
<?php
use PharIo\Version\InvalidVersionException;
use PharIo\Version\Version;
// branch@commit_hash
$rootPackageVersion = 'dev-master@045047b37d1dd99bf7e97487e2dc10820439f29b'
$default = '0.0.0-dev';
$version = $rootPackageVersion ?? $default;
try {
new Version($version);
} catch (InvalidVersionException $e) {
$version = $default;
}
var_dump($version);
I agree that to be a potential problem, at least it certainly is a short coming.
I'm not sure yet though how to properly address it: As such a hash only version identifier is not really comparable, we can only tell two hash-versions are different but there is no means of telling which might be "higher".
That being said, non-release versions need to be somehow supported. But that potentially opens up a can of worms ;)
I've tested some of common versions we can find from Composer installation (use cases are identified in following script by $versionProvider)
use PharIo\Version\VersionConstraintParser;
use Composer\Semver\VersionParser;
// @see https://github.com/composer/semver/
// @see https://github.com/phar-io/version
// composer
$parser1 = new VersionParser();
// phar-io
$parser2 = new VersionConstraintParser();
$versionProvider = [
'1.2.3a40',
'1.2.3p456',
'dev-master@4ce7aef0217756c440502a84aa650c3a7f16387b',
'3.x-dev',
'dev-master',
'dev-master as 1.0.0',
'4.0.x-dev@ac18809'
];
foreach ($versionProvider as $input) {
$input = $parser1->normalizeDefaultBranch($input);
$parts = explode('@', $input);
$normalized = count($parts) > 1 ? $parser1->normalize($parts[0]) : $parser1->normalize($input);
$branch = $parser1->normalizeDefaultBranch($normalized);
try {
$versionConstraint = $parser2->parse($branch);
$message = 'version constraint: ' . $versionConstraint->asString();
} catch (\Throwable $e) {
$message = 'issue : ' . $e->getMessage();
}
$results = [
'input' => $input,
'composer' => $branch,
'phar.io' => $message,
];
var_dump($results);
echo '------------------------------' . PHP_EOL;
}
And with a little workaround with composer/semver component, we can accept all versions.
See results given
array(3) {
["input"]=>
string(8) "1.2.3a40"
["composer"]=>
string(15) "1.2.3.0-alpha40"
["phar.io"]=>
string(35) "version constraint: 1.2.3.0-alpha40"
}
------------------------------
array(3) {
["input"]=>
string(9) "1.2.3p456"
["composer"]=>
string(16) "1.2.3.0-patch456"
["phar.io"]=>
string(36) "version constraint: 1.2.3.0-patch456"
}
------------------------------
array(3) {
["input"]=>
string(51) "dev-master@4ce7aef0217756c440502a84aa650c3a7f16387b"
["composer"]=>
string(11) "9999999-dev"
["phar.io"]=>
string(31) "version constraint: 9999999-dev"
}
------------------------------
array(3) {
["input"]=>
string(7) "3.x-dev"
["composer"]=>
string(29) "3.9999999.9999999.9999999-dev"
["phar.io"]=>
string(49) "version constraint: 3.9999999.9999999.9999999-dev"
}
------------------------------
array(3) {
["input"]=>
string(11) "9999999-dev"
["composer"]=>
string(11) "9999999-dev"
["phar.io"]=>
string(31) "version constraint: 9999999-dev"
}
------------------------------
array(3) {
["input"]=>
string(19) "dev-master as 1.0.0"
["composer"]=>
string(11) "9999999-dev"
["phar.io"]=>
string(31) "version constraint: 9999999-dev"
}
------------------------------
array(3) {
["input"]=>
string(17) "4.0.x-dev@ac18809"
["composer"]=>
string(23) "4.0.9999999.9999999-dev"
["phar.io"]=>
string(43) "version constraint: 4.0.9999999.9999999-dev"
}
------------------------------
At least, I can fix issue I got when trying to build an XML manifest with humbug/box into my specialized builder (see https://github.com/box-project/box/pull/606/files#diff-632c2ecab2ad4eacf30de3ede218310156dcc4ef7a14b44d06fef7add418dc54)