検索
ホームページphp教程php手册thrift IDL 生成代码的管理脚本

thrift IDL 生成代码的管理脚本

Jun 06, 2016 pm 07:37 PM
idlthriftコード増加生成する管理脚本とともにプロジェクト

随着项目增多,thriftIDL生成代码的管理也越复杂。此工具用于生成thrift的代码,尽量使其脚本化,可配置,自动化。 无 #!/usr/bin/env php?php/** * 随着项目增多,thrift IDL 生成代码的管理也越复杂。 * 此工具用于生成 thrift 的代码,尽量使其脚本化,自

随着项目增多,thrift IDL 生成代码的管理也越复杂。此工具用于生成 thrift 的代码,尽量使其脚本化,可配置,自动化。
#!/usr/bin/env php
<?php

/**
 * 随着项目增多,thrift IDL 生成代码的管理也越复杂。
 * 此工具用于生成 thrift 的代码,尽量使其脚本化,自动化。
 * 
 * 使用方法:
 * ./cthrift cms-exmaple ./cthrift.config.php
 * 
 * cthrift.config.php 是个配置文件:

<?php

return array(
    'thrift_command' => 'thrift --gen {gen} -out "{out}" "{idl}"',
    'projects' => array(
        'cms-exmaple' => array(
            'gen' => 'php',                       // 代码目标语言,用于 thrift 的 --gen 选项
            'out' => '/path/to/output',           // 代码输出目录,用于 thrift 的 --out 选项
            'idl_git_url' => 'https://git-url',   // IDL 是否使用了 git 管理,如果设置,则自动 pull,
                                                  // 例如 https://github.com/my/thrift-idl.git
            'idl_git_pre' => '/src/master',       // IDL 文件的 git URL 前缀。在本例中:
                                                  // 假设 idl_git_url 为 https://github.com/my/thrift-idl.git
                                                  // IDL 路径为 /path/to/cms.thrift,idl_root_path 为 /root/thrift/idl
                                                  // 则 git 的全路径为 https://github.com/my/thrift-idl/src/master/path/to/cms.thrift
                                                  // 对应的本地路径为 /root/thrift/idl/path/to/cms.thrift
            'idl_root_path' => '/path/to/idl',    // IDL 根目录,与 idls 拼接,如果有 git,此目录应当设置为 git 的根目录(含 .git 的目录)
            'idls' => array(  // IDL 所在的目录或文件
                '/path/to/idl/1',
                '/path/to/idl/2',
            ),
        ),
   ),
);

 *  
 * Author: https://github.com/heiing
 * Date: 2015-03-06T11:06+08:00
 */

define("VERSION", "0.1.0");
 
function usage($self, $ln = PHP_EOL) {
    echo "Usage: {$self} project-name config-file [idl-git-url]{$ln}";
    echo "project-name    Project name{$ln}";
    echo "config-file     Config file path{$ln}";
    echo "idl-git-url     IDL git url{$ln}";
    echo "{$ln}";
    echo "config-file example: {$ln}";
    echo "<?php{$ln}";
    echo "{$ln}";
    echo "return array({$ln}";
    echo "    'thrift_command' => 'thrift --gen {gen} -out \"{out}\" \"{idl}\"',{$ln}";
    echo "    'projects' => array({$ln}";
    echo "        'cms-exmaple' => array({$ln}";
    echo "            'gen' => 'php',                       // 代码目标语言,用于 thrift 的 --gen 选项{$ln}";
    echo "            'out' => '/path/to/output',           // 代码输出目录,用于 thrift 的 --out 选项{$ln}";
    echo "            'idl_git_url' => 'https://git-url',   // IDL 是否使用了 git 管理,如果设置,则自动 pull,{$ln}";
    echo "                                                  // 例如 https://github.com/my/thrift-idl.git{$ln}";
    echo "            'idl_git_pre' => '/src/master',       // IDL 文件的 git URL 前缀。在本例中:{$ln}";
    echo "                                                  // 假设 idl_git_url 为 https://github.com/my/thrift-idl.git{$ln}";
    echo "                                                  // IDL 路径为 /path/to/cms.thrift,idl_root_path 为 /root/thrift/idl{$ln}";
    echo "                                                  // 则 git 的全路径为 https://github.com/my/thrift-idl/src/master/path/to/cms.thrift{$ln}";
    echo "                                                  // 对应的本地路径为 /root/thrift/idl/path/to/cms.thrift{$ln}";
    echo "            'idl_root_path' => '/path/to/idl',    // IDL 根目录,与 idls 拼接,如果有 git,此目录应当设置为 git 的根目录(含 .git 的目录){$ln}";
    echo "            'idls' => array(  // IDL 所在的目录或文件{$ln}";
    echo "                '/path/to/idl/1',{$ln}";
    echo "                '/path/to/idl/2',{$ln}";
    echo "            ),{$ln}";
    echo "        ),{$ln}";
    echo "   ),{$ln}";
    echo ");{$ln}";
    echo "// -------- end of config-file{$ln}";
    echo "{$ln}";
    echo "usage example:{$ln}";
    echo "1. {$self} cms-example /root/cthrift/config.php{$ln}";
    echo "2. {$self} cms-example /root/cthrift/config.php https://github.com/my/thrift-idl/src/master/cms.thrift{$ln}";
    echo "{$ln}";
    echo "GOOD LUCK{$ln}";
    echo "{$ln}";
    exit(1);
}

function error($message, $ln = PHP_EOL) {
    echo "Error: {$message}{$ln}";
    exit(1);
}
 
function info($message, $ln = PHP_EOL) {
    echo "{$message}{$ln}";
}

function config($name, $value = null) {
    static $pool = array();
    if ($value === null) {
        return isset($pool[$name]) ? $pool[$name] : null;
    }
    $pool[$name] = $value;
}

function retend_config($name, $value) {
    if (!is_array($value) || is_numeric(implode('', array_keys($value)))) {
        return config($name, $value);
    }
    foreach ($value as $n => $v) {
        retend_config($name . '/' . $n, $v);
    }
}

function load_config() {
    $file = config('/config-file');
    info('load config: ' . $file);
    if (!is_file($file)) {
        error('Config file not exists!');
    }
    $configs = include $file;
    if (!isset($configs['projects'])) {
        error('Invalid config!');
    }
    if (!isset($configs['projects'][config('/project-name')])) {
        error('Project not set!');
    }
    foreach ($configs as $name => $value) {
        if (!is_array($value)) {
            config($name, $value);
        } else {
            retend_config($name, $value);
        }
    }
}

function do_command($cmd, $argv, $exit_on_error = true) {
    foreach ($argv as $name => $value) {
        $cmd = str_replace('{' . $name . '}', $value, $cmd);
    }
    info($cmd);
    $ret = 0;
    passthru($cmd, $ret);
    if ($ret !== 0 && $exit_on_error) {
        error('faild!');
    }
    return $ret;
}

function process_project() {
    $pre = 'projects/' . config('/project-name');
    
    $out = config("{$pre}/out");
    if (null === ($cmd = config('thrift_command'))) {
        $cmd = 'thrift --gen {gen} ' . ($out ? '--out {out} ' : '') . ' {idl}';
    }
    
    if (null === ($gen = config("{$pre}/gen"))) {
        error('gen not set!');
    }
    
    if (null === ($path = config("{$pre}/idl_root_path"))) {
        error('idl_root_path not set!');
    }
    if (!is_dir($path)) {
        error('idl_root_path not exists!');
    }
    $path = rtrim($path, '/\\');
    
    $git = rtrim(config("{$pre}/idl_git_url"), '/');
    if (!empty($git)) {
        do_command("cd {$path}; git pull;", array(), true);
    }
    
    if (null !== ($url = config('/idl-git-url'))) {
        $git = (strtolower(substr($git, -4)) === '.git' ? substr($git, 0, -4) : $git) . config("{$pre}/idl_git_pre");
        if ($git !== substr($url, 0, strlen($git))) {
            error('Invalid git url!');
        }
        $idls = array(substr($url, strlen($git)));
    } else if (null === ($idls = config("{$pre}/idls")) || empty($idls)) {
        error('idls not set or empty!');
    }
    
    foreach ($idls as $idl) {
        $idl = "{$path}{$idl}";
        if (is_dir($idl)) {
            $idl_files = glob("{$idl}/*.thrift");
        } else if (is_file($idl)) {
            $idl_files = array($idl);
        } else {
            info("Not Found: {$idl}");
            continue;
        }
        foreach ($idl_files as $file) {
            do_command($cmd, array(
                'gen' => $gen,
                'out' => $out,
                'idl' => $file,
            ), true);
        }
    }
}

function run($argv) {
    info("Thrift Creator " . VERSION);
    if (!isset($argv[2])) {
        usage($argv[0]);
    }
    config('/config-file', $argv[2]);
    config('/project-name', $argv[1]);
    if (isset($argv[3])) {
        config('/idl-git-url', $argv[3]);
    }
    load_config();
    process_project();
    info('DONE.');
}

run($argv);
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、