diff --git a/src/base/File.php b/src/base/File.php index e44388e..2eb18d4 100644 --- a/src/base/File.php +++ b/src/base/File.php @@ -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; diff --git a/src/components/Version.php b/src/components/Version.php new file mode 100644 index 0000000..8cf39de --- /dev/null +++ b/src/components/Version.php @@ -0,0 +1,79 @@ +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(); + } +} diff --git a/src/config/basis.php b/src/config/basis.php index 89a9f9f..457d7d8 100644 --- a/src/config/basis.php +++ b/src/config/basis.php @@ -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, diff --git a/src/controllers/OwnVersionController.php b/src/controllers/OwnVersionController.php deleted file mode 100644 index 1651640..0000000 --- a/src/controllers/OwnVersionController.php +++ /dev/null @@ -1,32 +0,0 @@ -version $this->date $this->time $this->hash\n"; - echo "run from $path\n"; - } -} diff --git a/src/controllers/VersionController.php b/src/controllers/VersionController.php index 6f08199..f631af8 100644 --- a/src/controllers/VersionController.php +++ b/src/controllers/VersionController.php @@ -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'); } }