Source

hpaco / runtest

#!/usr/bin/env php
<?php

$hpaco_binary = '../cabal-dev/bin/hpaco';
$node_binary = 'node';
$json_data = '';
$template = '';
$end_of_json = false;
$mode = 'php';
$dump_src = false;
$dump_tpl = false;
$dump_cmd = false;

foreach (array_slice($argv, 1) as $arg) {
	switch ($arg) {
		case 'php':
		case 'js':
		case 'run':
			$mode = $arg;
			break;
		case 'dump-src':
			$dump_src = true;
			break;
		case 'dump-tpl':
			$dump_tpl = true;
			break;
		case 'dump-cmd':
			$dump_cmd = true;
			break;
	}
}

foreach (file('php://stdin') as $line) {
    if ($end_of_json) {
        $template .= $line;
    }
    elseif ($line == "\n") {
        $end_of_json = true;
    }
    else {
        $json_data .= $line;
    }
}

if ($dump_tpl) {
	echo $template;
}

class FunctionPointer {
	private $callable;
	public function __construct($callable) { $this->callable = $callable; }
	public function __invoke() {
		return call_user_func_array($this->callable, func_get_args());
	}
}

$tmpfilename = tempnam('/tmp', 'hpacotest');

file_put_contents($tmpfilename, $template);

switch ($mode) {
	case 'run':
		$json_data_escaped = escapeshellarg(trim($json_data));
		$hpaco_command = "$hpaco_binary --run --data=$json_data_escaped <$tmpfilename";

		if ($dump_cmd) {
			echo $hpaco_command;
		}

		chdir(dirname(__FILE__) . "/tests");

		exec($hpaco_command, $output, $retval);

		if ($retval) {
			exit($retval);
		}

		echo implode("\n", $output);

		break;

	case 'php':
		$hpaco_command = $hpaco_binary . " --php-pretty --php-func <$tmpfilename";

		if ($dump_cmd) {
			echo $hpaco_command;
		}

		chdir(dirname(__FILE__) . "/tests");
		exec($hpaco_command, $output, $retval);

		if ($retval) {
			exit($retval);
		}

		$src = '?>' . implode("\n", $output);
		if ($dump_src) {
			echo $src;
		}
		eval($src);

		$data = json_decode($json_data, true);
		if (is_array($data)) {
			$data['implode'] = new FunctionPointer('implode');
			$data['count'] = new FunctionPointer('count');
		}

		runTemplate($data);
		break;
	case 'js':
		$hpaco_command = "$hpaco_binary --js --js-pretty <$tmpfilename";

		if ($dump_cmd) {
			echo $hpaco_command;
		}

		chdir(dirname(__FILE__) . "/tests");
		exec($hpaco_command, $output, $retval);
		if ($retval) {
			fprintf(STDERR, "HPaco compilation failed with exit code $retval");
			exit(-1);
		}
		$js_src = "var _util = require('util');\n" .
			"var _write = _util.print;\n" .
			"var context = $json_data;\n" .
			"context['count'] = function(a){return a.length;};\n" .
			"context['implode'] = function(s,a){\n" .
			"\tvar r = '';\n" .
			"\tvar first = true;\n" .
			"\tfor (i in a){\n" .
			"\t\tif (!first) { r += s; }\n" .
			"\t\tr += a[i];\n" .
			"\t\tfirst = false;\n" .
			"\t}\n" .
			"\treturn r;\n" .
			"};\n" .
			"\n" .
			implode("\n" .
			"", $output);
		$node_command = "$node_binary <<EOT\n{$js_src}\nEOT";
		if ($dump_src) {
			echo $js_src;
		}
		exec($node_command, $node_output, $retval);
		if ($retval) {
			fprintf(STDERR, "node.js execution failed with exit code $retval");
			exit(-2);
		}
		echo implode("\n", $node_output);
		echo "\n";
		break;
}

unlink($tmpfilename);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.