1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Pickle |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* @license |
8
|
|
|
* |
9
|
|
|
* New BSD License |
10
|
|
|
* |
11
|
|
|
* Copyright © 2015-2015, Pickle community. All rights reserved. |
12
|
|
|
* |
13
|
|
|
* Redistribution and use in source and binary forms, with or without |
14
|
|
|
* modification, are permitted provided that the following conditions are met: |
15
|
|
|
* * Redistributions of source code must retain the above copyright |
16
|
|
|
* notice, this list of conditions and the following disclaimer. |
17
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
18
|
|
|
* notice, this list of conditions and the following disclaimer in the |
19
|
|
|
* documentation and/or other materials provided with the distribution. |
20
|
|
|
* * Neither the name of the Hoa nor the names of its contributors may be |
21
|
|
|
* used to endorse or promote products derived from this software without |
22
|
|
|
* specific prior written permission. |
23
|
|
|
* |
24
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
25
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
28
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
29
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
30
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
31
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
32
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
33
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
namespace Pickle\Package\Util\Header; |
38
|
|
|
|
39
|
|
|
use Composer\Package\Version\VersionParser; |
40
|
|
|
use Pickle\Base\Interfaces; |
41
|
|
|
use Pickle\Package\Util; |
42
|
|
|
|
43
|
|
|
class Version |
44
|
|
|
{ |
45
|
|
|
protected $package; |
46
|
|
|
protected $header; |
47
|
|
|
protected $version; |
48
|
|
|
protected $macroNameRegex; |
49
|
|
|
|
50
|
|
|
public function __construct(Interfaces\Package $package) |
51
|
|
|
{ |
52
|
|
|
$this->package = $package; |
53
|
|
|
$this->macroNameRegex = '(?:PHP_)?' . strtoupper($this->package->getSimpleName()).'_(?:EXT_)?VERSION'; |
|
|
|
|
54
|
|
|
|
55
|
|
|
$this->version = $this->getVersionFromHeader(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function fileHasVersionMacro($fname) |
59
|
|
|
{ |
60
|
|
|
return $this->getVersionFromHeaderFile($fname) !== null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function rglob($pattern, $flags = 0) { |
64
|
|
|
$files = glob($pattern, $flags); |
65
|
|
|
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { |
66
|
|
|
$files = array_merge($files, self::rglob($dir.'/'.basename($pattern), $flags)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
return $files; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getVersionFromHeader() |
72
|
|
|
{ |
73
|
|
|
$headers = self::rglob($this->package->getSourceDir().DIRECTORY_SEPARATOR.'*.h'); |
|
|
|
|
74
|
|
|
foreach ($headers as $header) { |
75
|
|
|
$macroValue = $this->getVersionFromHeaderFile($header); |
76
|
|
|
if ($macroValue !== null) { |
77
|
|
|
return $macroValue; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
throw new \Exception("Couldn't parse or find the version defined in the {$this->macroNameRegex} macro"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Extract the macro value from a header file. |
85
|
|
|
* |
86
|
|
|
* @throws \Exception if we couldn't read the file |
87
|
|
|
* |
88
|
|
|
* @return string|null return NULL if the macro couldn't be found, the macro value otherwise |
89
|
|
|
*/ |
90
|
|
|
protected function getVersionFromHeaderFile(string $headerFilePath): ?string |
91
|
|
|
{ |
92
|
|
|
$headerFileContents = file_get_contents($headerFilePath); |
93
|
|
|
if ($headerFileContents === false) { |
94
|
|
|
throw new \Exception("Could not read header file {$headerFilePath}"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->getVersionFromHeaderFileContents($headerFileContents); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Extract the macro value from the contents of a header file. |
102
|
|
|
* |
103
|
|
|
* @return string|null return NULL if the macro couldn't be found, the macro value otherwise |
104
|
|
|
*/ |
105
|
|
|
protected function getVersionFromHeaderFileContents(string $headerFileContents): ?string |
106
|
|
|
{ |
107
|
|
|
// Match versions surrounded by quotes and versions without quotes |
108
|
|
|
$versionMatcher = '(".*"|.*\b)'; |
109
|
|
|
$pat = ',define\s+' . $this->macroNameRegex . '\s+' . $versionMatcher . ',i'; |
110
|
|
|
|
111
|
|
|
return preg_match($pat, $headerFileContents, $result) ? trim($result[1], '"') : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function updateJSON() |
115
|
|
|
{ |
116
|
|
|
if ($this->package->getPrettyVersion() == $this->version) { |
117
|
|
|
/* Don't touch, it's the same. */ |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$dumper = new Util\Dumper(); |
122
|
|
|
$composer_json = $this->package->getRootDir().DIRECTORY_SEPARATOR.'composer.json'; |
123
|
|
|
|
124
|
|
|
$this->package->replaceVersion((new VersionParser())->normalize($this->version), $this->version); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$len = file_put_contents($composer_json, json_encode($dumper->dump($this->package), JSON_PRETTY_PRINT)); |
127
|
|
|
|
128
|
|
|
if (!$len) { |
129
|
|
|
throw new \Exception("Failed to update '$package_json'"); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function __tostring() |
134
|
|
|
{ |
135
|
|
|
return $this->version; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
140
|
|
|
|