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

thrift IDL 生成代码的管理脚本

Jun 06, 2016 pm 07:37 PM
idl thrift 程式碼 產生 管理 腳本 隨著 專案

随着项目增多,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 Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何在Linux系統中執行.sh檔? 如何在Linux系統中執行.sh檔? Mar 14, 2024 pm 06:42 PM

如何在Linux系統中執行.sh檔?在Linux系統中,.sh文件是一種被稱為Shell腳本的文件,用於執行一系列的命令。執行.sh檔案是非常常見的操作,本文將介紹如何在Linux系統中執行.sh文件,並提供具體的程式碼範例。方法一:使用絕對路徑執行.sh文件要在Linux系統中執行一個.sh文件,可以使用絕對路徑來指定該文件的位置。以下是具體的步驟:打開終

剪映怎麼製作腳本?剪下製作腳本的方法教程 剪映怎麼製作腳本?剪下製作腳本的方法教程 Mar 13, 2024 pm 12:46 PM

  剪映是影片編輯工具,具有全面的剪輯功能,支援變速,有多元濾鏡和美顏的效果,還有豐富的曲庫資源。在這款軟體中,可以直接對影片進行剪輯,也可以建立剪輯腳本,但是該怎麼操作呢?本篇教學小編就來介紹一下剪映製作腳本的方法。  製作方法  1、在電腦中點選開啟剪映軟體,然後找到「創作腳本」選項,點選開啟。  2、在創作腳本頁面中,輸入“腳本標題”,然後在大綱中輸入拍攝內容的簡介。  3、如何在大綱中就能看到「分鏡描述」選項,在框內可以

藍色畫面代碼0x0000001怎麼辦 藍色畫面代碼0x0000001怎麼辦 Feb 23, 2024 am 08:09 AM

藍屏代碼0x0000001怎麼辦藍屏錯誤是電腦系統或硬體出現問題時的一種警告機制,代碼0x0000001通常表示出現了硬體或驅動程式故障。當使用者在使用電腦時突然遇到藍色畫面錯誤,可能會感到驚慌失措。幸運的是,大多數藍色畫面錯誤都可以透過一些簡單的步驟來排除和處理。本文將為讀者介紹一些解決藍屏錯誤代碼0x0000001的方法。首先,當遇到藍色畫面錯誤時,我們可以嘗試重

AI攻克費馬大定理?數學家放棄5年職業生涯,將100頁證明變代碼 AI攻克費馬大定理?數學家放棄5年職業生涯,將100頁證明變代碼 Apr 09, 2024 pm 03:20 PM

費馬大定理,即將被AI攻克?而整件事最有意義的地方在於,AI即將解決的費馬大定理,正是為了證明AI無用。曾經,數學屬於純粹的人類智力王國;如今,這片疆土正被先進的演算法所破解,所踐踏。圖片費馬大定理,是一個「臭名昭著」的謎題,在幾個世紀以來,一直困擾著數學家。它在1993年被證明,而現在,數學家們有一個偉大計畫:用電腦把證明過程重現。他們希望在這個版本的證明中,如果有任何邏輯上的錯誤,都可以由電腦檢查出來。專案網址:https://github.com/riccardobrasca/flt

深入了解PyCharm:快速刪除項目的方法 深入了解PyCharm:快速刪除項目的方法 Feb 26, 2024 pm 04:21 PM

標題:深入了解PyCharm:刪除專案的高效方式近年來,Python作為一種強大而靈活的程式語言,受到越來越多開發者的青睞。在Python專案的開發中,選擇一個高效的整合開發環境至關重要。 PyCharm作為一款功能強大的整合開發環境,為Python開發者提供了許多便利的功能和工具,其中包括快速、有效率地刪除專案目錄。以下將著重介紹如何使用PyCharm中的刪除

PyCharm實用技巧:將項目轉換為可執行EXE文件 PyCharm實用技巧:將項目轉換為可執行EXE文件 Feb 23, 2024 am 09:33 AM

PyCharm是一款功能強大的Python整合開發環境,提供了豐富的開發工具和環境配置,讓開發者更有效率地編寫和除錯程式碼。在使用PyCharm進行Python專案開發的過程中,有時候我們需要將專案打包成可執行的EXE文件,以便在沒有安裝Python環境的電腦上執行。本文將介紹如何使用PyCharm將專案轉換為可執行的EXE文件,同時給出具體的程式碼範例。首

GE通用遠端程式碼可在任何裝置上編程 GE通用遠端程式碼可在任何裝置上編程 Mar 02, 2024 pm 01:58 PM

如果您需要遠端編程任何設備,這篇文章會為您帶來幫助。我們將分享編程任何設備的頂級GE通用遠端代碼。通用電氣的遙控器是什麼? GEUniversalRemote是一款遙控器,可用於控制多個設備,如智慧電視、LG、Vizio、索尼、藍光、DVD、DVR、Roku、AppleTV、串流媒體播放器等。 GEUniversal遙控器有各種型號,具有不同的功能和功能。 GEUniversalRemote最多可以控制四台設備。頂級通用遙控器代碼,可在任何裝置上編程GE遙控器配備一組代碼,使其能夠與不同設備配合。您可

如何使用Copilot產生程式碼 如何使用Copilot產生程式碼 Mar 23, 2024 am 10:41 AM

身為一名程式設計師,對於能夠簡化程式設計體驗的工具,我感到非常興奮。借助人工智慧工具的幫助,我們可以產生演示程式碼,並根據需求進行必要的修改。在VisualStudioCode中新引入的Copilot工具讓我們能夠創建具有自然語言聊天互動的AI生成程式碼。透過解釋功能,我們可以更好地理解現有程式碼的含義。如何使用Copilot產生程式碼?要開始,我們首先需要取得最新的PowerPlatformTools擴充。要實現這一點,你需要進入擴充頁面,搜尋“PowerPlatformTool”,然後點擊Install按鈕

See all articles