3
0

upd(codegen): generate subpackages

This commit is contained in:
Tit Petric
2018-07-16 00:13:45 +02:00
parent 657ef15cf5
commit b68337c111
62 changed files with 3705 additions and 38 deletions

View File

@@ -1,15 +1,34 @@
#!/usr/bin/env php
<?php
if (count($argv) < 2) {
echo "Usage: ./codegen [path]\n";
exit(255);
}
if (!is_dir(__DIR__ . "/" . $argv[1])) {
echo "No folder " . $argv[1] . "\n";
die;
}
$project = $argv[1];
error_reporting(E_ALL^E_NOTICE);
include("docs/vendor/autoload.php");
include(__DIR__ . "/vendor/autoload.php");
function capitalize($name) {
$names = explode("/", $name);
return implode("", array_map("ucfirst", $names));
}
function expose($name) {
if ($name == "id") {
return "ID";
}
return ucfirst($name);
}
function array_change_key_case_recursive($arr) {
return array_map(function ($item) {
if (is_array($item)) {
@@ -20,11 +39,13 @@ function array_change_key_case_recursive($arr) {
}
$tpl = new Monotek\MiniTPL\Template;
$tpl->set_paths(array(__DIR__ . "/templates/"));
$tpl->set_compile_location("/tmp", true);
$tpl->add_default("newline", "\n");
$api_files = glob("docs/src/spec/*.json");
$generators = array();
exec("find -L " . __DIR__ . "/" . $project . " -name index.php", $generators);
$api_files = glob($project . "/docs/src/spec/*.json");
$apis = array_map(function($filename) {
return array_change_key_case_recursive(json_decode(file_get_contents($filename), true));
}, $api_files);
@@ -33,17 +54,34 @@ usort($apis, function($a, $b) {
return strcmp($a['interface'], $b['interface']);
});
foreach (array("structs", "handlers", "interfaces", "request", "") as $type) {
$parsers = array(
"uint64" => "parseUInt64",
"bool" => "parseBool",
);
foreach ($generators as $generator) {
$tpl->set_paths(array(dirname($generator) . "/", __DIR__ . "/templates/"));
$dirname = strstr(dirname($generator), $project. "/");
if (empty($dirname)) {
$dirname = $project;
}
// echo "generator=". dirname($generator) . " project=$project, dirname=$dirname\n";
if (!is_dir($dirname) && !empty($dirname)) {
mkdir($dirname, 0777, true);
}
$common = compact("parsers");
include($generator);
}
/* foreach (array("structs", "handlers", "interfaces", "request", "") as $type) {
foreach ($apis as $api) {
if (is_array($api['struct'])) {
$name = ucfirst($api['interface']);
$filename = str_replace("..", ".", strtolower($name) . "." . $type . ".go");
$tpl->load("http_$type.tpl");
$tpl->assign("parsers", array(
"uint64" => "parseUInt64",
"bool" => "parseBool",
));
$tpl->assign("parsers",
$tpl->assign("package", $api['package']);
$tpl->assign("name", $name);
$tpl->assign("self", strtolower(substr($name, 0, 1)));
@@ -82,6 +120,7 @@ foreach (array("routes") as $type) {
file_put_contents($filename, $contents);
}
*/
// camel case to snake case
function decamel($input) {