Home php教程 php手册 thrift IDL 生成代码的管理脚本

thrift IDL 生成代码的管理脚本

Jun 06, 2016 pm 07:37 PM
idl thrift code increase generate manage Script along with project

随着项目增多,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);
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to execute .sh file in Linux system? How to execute .sh file in Linux system? Mar 14, 2024 pm 06:42 PM

How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

How to create a script for editing? Tutorial on how to create a script through editing How to create a script for editing? Tutorial on how to create a script through editing Mar 13, 2024 pm 12:46 PM

Cutting is a video editing tool with comprehensive editing functions, support for variable speed, various filters and beauty effects, and rich music library resources. In this software, you can edit videos directly or create editing scripts, but how to do it? In this tutorial, the editor will introduce the method of editing and making scripts. Production method: 1. Click to open the editing software on your computer, then find the "Creation Script" option and click to open. 2. In the creation script page, enter the "script title", and then enter a brief introduction to the shooting content in the outline. 3. How can I see the "Storyboard Description" option in the outline?

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Apr 09, 2024 pm 03:20 PM

Fermat's last theorem, about to be conquered by AI? And the most meaningful part of the whole thing is that Fermat’s Last Theorem, which AI is about to solve, is precisely to prove that AI is useless. Once upon a time, mathematics belonged to the realm of pure human intelligence; now, this territory is being deciphered and trampled by advanced algorithms. Image Fermat's Last Theorem is a "notorious" puzzle that has puzzled mathematicians for centuries. It was proven in 1993, and now mathematicians have a big plan: to recreate the proof using computers. They hope that any logical errors in this version of the proof can be checked by a computer. Project address: https://github.com/riccardobrasca/flt

A closer look at PyCharm: a quick way to delete projects A closer look at PyCharm: a quick way to delete projects Feb 26, 2024 pm 04:21 PM

Title: Learn more about PyCharm: An efficient way to delete projects. In recent years, Python, as a powerful and flexible programming language, has been favored by more and more developers. In the development of Python projects, it is crucial to choose an efficient integrated development environment. As a powerful integrated development environment, PyCharm provides Python developers with many convenient functions and tools, including deleting project directories quickly and efficiently. The following will focus on how to use delete in PyCharm

PyCharm Practical Tips: Convert Project to Executable EXE File PyCharm Practical Tips: Convert Project to Executable EXE File Feb 23, 2024 am 09:33 AM

PyCharm is a powerful Python integrated development environment that provides a wealth of development tools and environment configurations, allowing developers to write and debug code more efficiently. In the process of using PyCharm for Python project development, sometimes we need to package the project into an executable EXE file to run on a computer that does not have a Python environment installed. This article will introduce how to use PyCharm to convert a project into an executable EXE file, and give specific code examples. head

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

How to use Copilot to generate code How to use Copilot to generate code Mar 23, 2024 am 10:41 AM

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for &quot;PowerPlatformTool&quot; and click the Install button

See all articles