Skip to content

Commit 5d3ea34

Browse files
committed
Use json2hcl binary
1 parent ea44a9d commit 5d3ea34

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

src/HCLParser.php

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,17 @@ public function __construct($hcl)
1111
$this->hcl = $hcl;
1212
}
1313

14-
public function parse()
14+
private function getJSONString()
1515
{
16-
$working = $this->hcl;
17-
18-
// Remove single line comments
19-
$working = preg_replace('/#.*/', '', $working);
20-
21-
// Replace = with :
22-
$working = str_replace('=', ':', $working);
23-
24-
// Surround with { }
25-
$working = '{'.$working.'}';
26-
27-
// Surround keys with " "
28-
preg_match_all('/(?:\s*)(\w+)\s+:/', $working, $matches, PREG_OFFSET_CAPTURE);
29-
30-
for ($i=count($matches[1])-1; $i >= 0; $i--) {
31-
$match = $matches[1][$i];
32-
$matchText = $match[0];
33-
$startPos = $match[1];
34-
$working = substr_replace($working, '"'.$matchText.'"', $startPos, strlen($matchText));
35-
}
16+
$command = __DIR__.'/../bin/json2hcl_v0.0.6_linux_amd64 --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';
3617

37-
// Add : between " and {
38-
$working = preg_replace('/"\s*{/', '": {', $working);
18+
exec($command, $lines);
3919

40-
// Add , between "} and "
41-
$working = preg_replace('/"\s*}\s*"/', '\"}, '.PHP_EOL.PHP_EOL.'"', $working);
42-
43-
// Add , between " and "
44-
$working = preg_replace('/"\s*"/', '",'.PHP_EOL.'"', $working);
45-
46-
// Removing trailing comma before ]
47-
$working = preg_replace('/,\s*]/', PHP_EOL.']', $working);
48-
49-
var_dump($working); die;
20+
return implode(PHP_EOL, $lines);
21+
}
5022

51-
return json_decode($working);
23+
public function parse()
24+
{
25+
return json_decode($this->getJSONString());
5226
}
5327
}

src/Installer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public static function installBinaries()
99
$binaryUrls = ['https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64'];
1010

1111
foreach($binaryUrls as $binaryUrl) {
12-
file_put_contents(__DIR__.'/../bin/'.basename($binaryUrl), file_get_contents($binaryUrl));
12+
$destination = __DIR__.'/../bin/'.basename($binaryUrl);
13+
file_put_contents($destination, file_get_contents($binaryUrl));
14+
chmod($destination, 0755);
1315
}
1416
}
1517
}

0 commit comments

Comments
 (0)