|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hidev |
|
6
|
|
|
* @package hidev |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hidev\components; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* `version` file component. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Version extends \hidev\base\ConfigFile |
|
17
|
|
|
{ |
|
18
|
|
|
protected $_file = 'version'; |
|
19
|
|
|
|
|
20
|
|
|
protected $fileRelease; |
|
21
|
|
|
protected $fileHash; |
|
22
|
|
|
protected $release; |
|
23
|
|
|
protected $date; |
|
24
|
|
|
protected $time; |
|
25
|
|
|
protected $zone; |
|
26
|
|
|
protected $hash; |
|
27
|
|
|
protected $commit; |
|
28
|
|
|
protected $branch; |
|
29
|
|
|
|
|
30
|
|
|
public function init() |
|
31
|
|
|
{ |
|
32
|
|
|
if ($this->exists()) { |
|
33
|
|
|
$this->parseFile(); |
|
34
|
|
|
$this->fileRelease = $this->release; |
|
35
|
|
|
$this->fileHash = $this->hash; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function parseFile() |
|
40
|
|
|
{ |
|
41
|
|
|
$line = trim($this->getFile()->read()); |
|
42
|
|
|
list($tmp, $this->release, $this->date, $this->time, $this->zone, $this->hash) = explode(' ', $line); |
|
|
|
|
|
|
43
|
|
|
$this->parseRelease(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function parseRelease() |
|
47
|
|
|
{ |
|
48
|
|
|
if (preg_match('/^(\w+)-([0-9\.]+)-(\S+)/', $this->release, $matches)) { |
|
49
|
|
|
$this->release = $matches[1]; |
|
50
|
|
|
$this->branch = $matches[2]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function renderFile() |
|
55
|
|
|
{ |
|
56
|
|
|
return implode(' ', [$this->getName(), $this->renderRelease(), $this->date, $this->time, $this->zone, $this->hash]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function renderRelease() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->branch) { |
|
62
|
|
|
return implode('-', [$this->release, $this->branch, substr($this->hash, 0, 7)]); |
|
63
|
|
|
} else { |
|
64
|
|
|
return $this->release; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function update($release) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->load(); |
|
71
|
|
|
$this->setRelease($release); |
|
72
|
|
|
$this->save(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function load() |
|
76
|
|
|
{ |
|
77
|
|
|
$line = trim($this->exec('git', ['log', '-n', '1', '--pretty=%ai %H %s'])[0]); |
|
|
|
|
|
|
78
|
|
|
list($this->date, $this->time, $this->zone, $this->hash, $this->commit) = explode(' ', $line, 5); |
|
79
|
|
|
if ($this->hash !== $this->fileHash) { |
|
80
|
|
|
$this->branch = $this->release; |
|
81
|
|
|
$this->release = 'dev'; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function save() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->getFile()->write($this->renderFile() . "\n"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setRelease($release = null, $branch = null) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->setBranch($branch); |
|
93
|
|
|
$this->release = $this->getRelease($release); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getRelease($release = null) |
|
97
|
|
|
{ |
|
98
|
|
|
return $release ?: $this->release ?: 'dev'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setBranch($branch) |
|
102
|
|
|
{ |
|
103
|
|
|
if (isset($branch)) { |
|
104
|
|
|
$this->branch = $branch; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getAbspath() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->getFile()->getAbspath(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getName() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->take('package')->name; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.