찾다
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 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기