phpvms/app/Console/Commands/Version.php
Nabeel S 0e13905098
Upstream null version; build version tags not being saved properly #575 (#578)
* Check for null version from upstream #575

* Fix for pre-release version numbering

* Move popup to right

* Split get/generate build ID #575
2020-02-23 12:23:19 -05:00

65 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Contracts\Command;
use App\Services\VersionService;
use Symfony\Component\Yaml\Yaml;
class Version extends Command
{
protected $signature = 'phpvms:version {--write} {--base-only} {--write-full-version} {version?}';
private $versionSvc;
public function __construct(VersionService $versionSvc)
{
parent::__construct();
$this->versionSvc = $versionSvc;
}
/**
* Run dev related commands
*
* @throws \Symfony\Component\Yaml\Exception\ParseException
*/
public function handle()
{
if ($this->option('write')) {
// Write the updated build number out to the file
$version_file = config_path('version.yml');
$cfg = Yaml::parse(file_get_contents($version_file));
// If a version is being passed in, the update the build, etc data against this
if ($this->argument('version')) {
$version = \SemVer\SemVer\Version::fromString($this->argument('version'));
if ($this->option('write-full-version')) {
$cfg['current']['major'] = $version->getMajor();
$cfg['current']['minor'] = $version->getMinor();
$cfg['current']['patch'] = $version->getPatch();
}
$prerelease = $version->getPreRelease();
if (strpos($prerelease, '.') !== false) {
$prerelease = explode('.', $prerelease);
$cfg['current']['prerelease'] = $prerelease[0];
$cfg['current']['buildmetadata'] = $prerelease[1];
} else {
$cfg['current']['prerelease'] = $prerelease;
}
}
// Always write out the build ID/build number which is the commit hash
$build_number = $this->versionSvc->generateBuildId($cfg);
$cfg['current']['commit'] = $build_number;
$cfg['build']['number'] = $build_number;
file_put_contents($version_file, Yaml::dump($cfg, 4, 2));
}
$version = $this->versionSvc->getCurrentVersion(!$this->option('base-only'));
echo $version."\n";
}
}