Skip to content

Commit 30c23e6

Browse files
committed
Added support for multiple operating systems and architecture - macOS / Windows
1 parent 014d0dc commit 30c23e6

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/HCLParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct($hcl)
2727
*/
2828
private function getJSONString()
2929
{
30-
$command = __DIR__.'/../bin/json2hcl_v0.0.6_linux_amd64 --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';
30+
$command = __DIR__.'/../bin/' . Installer::getBinaryFilename() . ' --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';
3131

3232
exec($command, $lines);
3333

src/Installer.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,63 @@
22

33
namespace DivineOmega\HCLParser;
44

5+
/**
6+
* Class Installer
7+
* @package DivineOmega\HCLParser
8+
*/
59
abstract class Installer
610
{
11+
/**
12+
* json2hcl Version we want to use
13+
* @see https://github.com/kvz/json2hcl/releases
14+
*/
15+
const JSON2HCL_VERSION = '0.0.6';
16+
17+
/**
18+
* Returns the correct binary filename according to the Operating System and Architecture
19+
*/
20+
public static function getBinaryFilename()
21+
{
22+
$architecture = 'amd64';
23+
24+
# Switch architecture if needed
25+
if (2147483647 == PHP_INT_MAX) {
26+
$architecture = '386';
27+
}
28+
29+
# Default OS
30+
$osString = 'linux';
31+
32+
switch (TRUE) {
33+
case stristr(PHP_OS, 'DAR'):
34+
$osString = 'darwin';
35+
break;
36+
case stristr(PHP_OS, 'WIN'):
37+
$osString = 'windows';
38+
break;
39+
}
40+
41+
return sprintf('json2hcl_v%s_%s_%s',self::JSON2HCL_VERSION, $osString, $architecture);
42+
}
43+
744
public static function installBinaries()
845
{
9-
$binaryUrls = ['https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64'];
46+
$binaryUrls = [
47+
sprintf('https://github.com/kvz/json2hcl/releases/download/v%s/%s', self::JSON2HCL_VERSION, self::getBinaryFilename() )
48+
];
1049

1150
foreach ($binaryUrls as $binaryUrl) {
12-
$destination = __DIR__.'/../bin/'.basename($binaryUrl);
51+
$destination = __DIR__ . '/../bin/' . basename($binaryUrl);
1352

53+
# Skip if file exists
1454
if (file_exists($destination)) {
1555
continue;
1656
}
1757

58+
# Download
1859
file_put_contents($destination, file_get_contents($binaryUrl));
60+
61+
# Make executable
1962
chmod($destination, 0755);
2063
}
2164
}

0 commit comments

Comments
 (0)