Skip to content

Commit 9da1ef0

Browse files
Sean MolenaarSean Molenaar
Sean Molenaar
authored and
Sean Molenaar
committed
Prepare for signed releases
1 parent 1525c48 commit 9da1ef0

File tree

5 files changed

+171
-5
lines changed

5 files changed

+171
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
# JIRA plugin
1111
atlassian-ide-plugin.xml
1212

13-
config.json
13+
config.json
14+
*.pem

src/PHPDraft/In/Tests/ApibFileParserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,29 @@
1010

1111

1212
use PHPDraft\Core\TestBase;
13+
use PHPDraft\In\ApibFileParser;
14+
use ReflectionClass;
1315

1416
class ApibFileParserTest extends TestBase
1517
{
18+
public function setUp()
19+
{
20+
$this->class = new ApibFileParser(__DIR__.'/ApibFileParserTest.php');
21+
$this->reflection = new ReflectionClass('PHPDraft\In\ApibFileParser');
22+
}
23+
24+
public function tearDown()
25+
{
26+
unset($this->class);
27+
unset($this->reflection);
28+
}
29+
30+
public function testSetup()
31+
{
32+
$property = $this->reflection->getProperty('location');
33+
$property->setAccessible(TRUE);
34+
$this->assertSame(__DIR__.'/', $property->getValue($this->class));
35+
}
36+
1637

1738
}

src/PHPDraft/Model/Elements/Tests/ArrayStructureTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ArrayStructureTest extends TestBase
1717
public function setUp()
1818
{
1919
$this->class = new ArrayStructureElement();
20-
$this->reflection = new ArrayStructureElement('PHPDraft\Model\DataStructureElement');
20+
$this->reflection = new \ReflectionClass('PHPDraft\Model\DataStructureElement');
2121
}
2222

2323
public function tearDown()

src/PHPDraft/Out/Tests/TemplateGeneratorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace PHPDraft\Out\Tests;
1010

1111

12-
class TemplateGeneratorTest
12+
use PHPDraft\Core\TestBase;
13+
14+
class TemplateGeneratorTest extends TestBase
1315
{
1416

1517
}

src/PHPDraft/Out/UI.php

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
namespace PHPDraft\Out;
1010

1111

12+
use Exception;
13+
use Phar;
14+
use Throwable;
15+
1216
class UI
1317
{
18+
protected $versionStringPrinted;
19+
1420
static function main($argv = [])
1521
{
16-
$options = getopt("f:t::i::hv");
22+
$options = getopt("f:t::i::hvu");
1723
if(!isset($argv[1]))
1824
{
1925
file_put_contents('php://stderr', 'Not enough arguments'.PHP_EOL);
@@ -35,6 +41,12 @@ static function main($argv = [])
3541
exit(0);
3642
}
3743

44+
if (isset($options['u']))
45+
{
46+
self::handleSelfUpdate();
47+
exit(0);
48+
}
49+
3850
elseif (isset($options['f']))
3951
{
4052
$file = $options['f'];
@@ -60,6 +72,98 @@ static function main($argv = [])
6072
];
6173
}
6274

75+
private function printVersionString()
76+
{
77+
print self::version() . "\n\n";
78+
}
79+
80+
/**
81+
* @since Method available since Release 1.4
82+
*/
83+
protected function handleSelfUpdate()
84+
{
85+
self::printVersionString();
86+
$localFilename = realpath($_SERVER['argv'][0]);
87+
if (!is_writable($localFilename)) {
88+
print 'No write permission to update ' . $localFilename . "\n";
89+
exit(3);
90+
}
91+
if (!extension_loaded('openssl')) {
92+
print "The OpenSSL extension is not loaded.\n";
93+
exit(3);
94+
}
95+
//set POST variables
96+
//https://github.com/SMillerDev/phpdraft/releases/download/1.3.2/phpdraft.phar
97+
$url = 'https://github.com/SMillerDev/phpdraft/releases/latest';
98+
$ch = curl_init();
99+
curl_setopt($ch,CURLOPT_URL, $url);
100+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
101+
$result = curl_exec($ch);
102+
$matches = [];
103+
preg_match("/href=\"https:\/\/github.com\/SMillerDev\/phpdraft\/releases\/tag\/([0-9.]*)\"/", $result, $matches);
104+
curl_close($ch);
105+
$remoteFilename = sprintf(
106+
'https://github.com/SMillerDev/phpdraft/releases/download/%s/phpdraft.phar',
107+
$matches[0]
108+
);
109+
110+
$tempFilename = tempnam(sys_get_temp_dir(), 'phpdraft') . '.phar';
111+
// Workaround for https://bugs.php.net/bug.php?id=65538
112+
$caFile = dirname($tempFilename) . '/ca.pem';
113+
copy(__PHPDRAFT_PHAR_ROOT__ . '/ca.pem', $caFile);
114+
print 'Updating the PHPDraft PHAR ... ';
115+
$options = [
116+
'ssl' => [
117+
'allow_self_signed' => false,
118+
'cafile' => $caFile,
119+
'verify_peer' => true
120+
]
121+
];
122+
file_put_contents(
123+
$tempFilename,
124+
file_get_contents(
125+
$remoteFilename,
126+
false,
127+
stream_context_create($options)
128+
)
129+
);
130+
chmod($tempFilename, 0777 & ~umask());
131+
try {
132+
$phar = new Phar($tempFilename);
133+
unset($phar);
134+
rename($tempFilename, $localFilename);
135+
unlink($caFile);
136+
} catch (Throwable $_e) {
137+
$e = $_e;
138+
} catch (Exception $_e) {
139+
$e = $_e;
140+
}
141+
if (isset($e)) {
142+
unlink($caFile);
143+
unlink($tempFilename);
144+
print " done\n\n" . $e->getMessage() . "\n";
145+
exit(2);
146+
}
147+
print " done\n";
148+
exit(0);
149+
}
150+
/**
151+
* @since Method available since Release 1.4
152+
*/
153+
protected function handleVersionCheck()
154+
{
155+
$this->printVersionString();
156+
$latestVersion = file_get_contents('https://phar.phpdraft.de/latest-version-of/phpdraft');
157+
$isOutdated = version_compare($latestVersion, self::release_id(), '>');
158+
if ($isOutdated) {
159+
print "You are not using the latest version of PHPDraft.\n";
160+
print 'Use "phpdraft --self-upgrade" to install PHPDraft ' . $latestVersion . "\n";
161+
} else {
162+
print "You are using the latest version of PHPDraft.\n";
163+
}
164+
exit(0);
165+
}
166+
63167
static function help()
64168
{
65169
echo 'This is a parser for API Blueprint files in PHP.'.PHP_EOL.PHP_EOL;
@@ -71,8 +175,46 @@ static function help()
71175

72176
static function version()
73177
{
74-
$version = (VERSION === '0') ? @exec('git describe --tags 2>&1') : VERSION;
178+
$version = self::release_id();
75179
echo 'PHPDraft: '.$version;
76180
}
77181

182+
/**
183+
* Get the version number
184+
*
185+
* @return string
186+
*/
187+
static function release_id()
188+
{
189+
return (VERSION === '0') ? @exec('git describe --tags 2>&1') : VERSION;
190+
}
191+
192+
193+
/**
194+
* @return string
195+
*
196+
* @since Method available since Release 4.8.13
197+
*/
198+
public static function series()
199+
{
200+
if (strpos(self::release_id(), '-')) {
201+
$version = explode('-', self::release_id())[0];
202+
} else {
203+
$version = self::release_id();
204+
}
205+
return implode('.', array_slice(explode('.', $version), 0, 2));
206+
}
207+
208+
/**
209+
* @return string
210+
*
211+
* @since Method available since Release 4.0.0
212+
*/
213+
public static function getReleaseChannel()
214+
{
215+
if (strpos(self::release_id(), '-') !== false) {
216+
return '-nightly';
217+
}
218+
return '';
219+
}
78220
}

0 commit comments

Comments
 (0)