Skip to content

Commit

Permalink
fixing version management
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed May 2, 2017
1 parent 8a453ce commit 958fffa
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 72 deletions.
10 changes: 10 additions & 0 deletions src/base/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ public function setPath($path)
$this->type = $this->findType();
}

public function getAbspath()
{
$path = $this->getPath();
if (strncmp($path, '/', 1)) {
$path = getcwd() . '/' . $path;
}

return $path;
}

public function getPath()
{
return $this->_path;
Expand Down
79 changes: 79 additions & 0 deletions src/components/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Automation tool mixed with code generator for easier continuous development
*
* @link https://github.com/hiqdev/hidev
* @package hidev
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
*/

namespace hidev\components;

/**
* `version` file component.
*/
class Version extends \hidev\base\ConfigFile
{
protected $_file = 'version';

protected $fileRelease;
protected $fileHash;
protected $release;
protected $date;
protected $time;
protected $zone;
protected $hash;
protected $commit;

public function init()
{
if ($this->exists()) {
$line = trim($this->getFile()->read());
list($this->release, $this->date, $this->time, $this->zone, $this->hash) = explode(' ', $line);
$this->fileRelease = $this->release;
$this->fileHash = $this->hash;
}
}

public function update($release)
{
$this->load();
$this->setRelease($release);
$this->save();
}

public function load()
{
$line = trim($this->exec('git', ['log', '-n', '1', '--pretty=%ai %H %s'])[0]);
list($this->date, $this->time, $this->zone, $this->hash, $this->commit) = explode(' ', $line, 5);
if ($this->hash !== $this->fileHash) {
$this->release = implode('-', ['dev', $this->release, substr($this->hash, 0, 7)]);
}
}

public function save()
{
$this->getFile()->write(implode(' ', [$this->release, $this->date, $this->time, $this->zone, $this->hash]) . "\n");
}

public function setRelease($release = null)
{
$this->release = $this->getRelease($release);
}

public function getRelease($release = null)
{
return $release ?: $this->release ?: 'dev';
}

public function pretty()
{
return implode(' ', [$this->release, $this->date, $this->time, $this->hash]);
}

public function getAbspath()
{
return $this->getFile()->getAbspath();
}
}
10 changes: 9 additions & 1 deletion src/config/basis.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,21 @@
'vendor' => [
'class' => \hidev\components\Vendor::class,
],
'version' => [
'class' => \hidev\components\Version::class,
],
'own.version' => [
'class' => \hidev\components\Version::class,
'file' => dirname(dirname(__DIR__)) . '/version',
],
'codeception' => [
'class' => \hidev\components\WTF::class,
],
],
'controllerMap' => [
'--version' => [
'class' => \hidev\controllers\OwnVersionController::class,
'class' => \hidev\controllers\VersionController::class,
'own' => true,
],
'.gitignore' => [
'class' => \hidev\controllers\GitignoreController::class,
Expand Down
32 changes: 0 additions & 32 deletions src/controllers/OwnVersionController.php

This file was deleted.

64 changes: 25 additions & 39 deletions src/controllers/VersionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,41 @@
namespace hidev\controllers;

/**
* Goal for version file management.
* Version management.
*/
class VersionController extends FileController
class VersionController extends \hidev\base\Controller
{
protected $_file = 'version';
public $own;

public $fileVersion;
public $defaultAction = 'show';

public $version;
public $date;
public $time;
public $zone;
public $hash;
public $commit;

public function init()
{
if ($this->exists()) {
$line = trim($this->getFile()->read());
list($this->version, $this->date, $this->time, $this->zone, $this->hash) = explode(' ', $line);
$this->fileVersion = $this->version;
}
}

public function actionMake($version = null)
{
$this->setVersion($version);
$this->actionLoad();
$this->actionSave();
}

public function actionLoad()
{
$line = trim($this->exec('git', ['log', '-n', '1', '--pretty=%ai %H %s'])[0]);
list($this->date, $this->time, $this->zone, $this->hash, $this->commit) = explode(' ', $line, 5);
}

public function actionSave()
/**
* Show current version.
* @param string $release
*/
public function actionShow($release = null)
{
$this->getFile()->write(implode(' ', [$this->version, $this->date, $this->time, $this->zone, $this->hash]) . "\n");
$version = $this->getComponent();
$version->load();
$version->setRelease($release);
$dir = dirname($version->getAbspath());
$pretty = $version->pretty();
$name = $this->take('package')->name;
echo "$name $pretty\n";
echo "(run from $dir)\n";
}

public function setVersion($version = null)
/**
* Update version.
* @param string $release
*/
public function actionUpdate($release = null)
{
$this->version = $this->getVersion($version);
$this->getComponent()->update($release);
}

public function getVersion($version = null)
public function getComponent()
{
return $version ?: $this->version ?: 'dev';
return $this->take(($this->own ? 'own.' : '') . 'version');
}
}

0 comments on commit 958fffa

Please sign in to comment.