跳到主要内容

tmake

简介

gs模板的处理工具

tmake 是template maker的缩写

本pkg的功能之一是将源代码目录的源文件(.gs)中的模板占位符(如TEMPLATE_REQURE(ABC))替换为对应的值

以一个模板pkg为例,必然有的文件夹如下:

  • src - 源代码目录
  • 源文件1.gs
  • 源文件2.gs
  • ...
  • template
  • template.yaml - 模板配置文件(参考__template__.yaml)
YAML配置文件说明

tokens - 占位符对应的值

  • 配置一组模板占位符

prebuild_files - 前处理文件

  • 配置一组前处理文件(.gs,需要包含main方法)

prebuild_cmds - 前处理命令

  • 配置一组前处理命令(move_file/move_path)
  • move_file <src_file> <dst_file>
  • move_path <src_path> <dst_path>
  • move_path <src_path> <dst_path> <pattern>

postbuild_files - 后处理文件(需要包含main方法)

  • 配置一组后处理文件(.gs, 需要包含main方法)

postbuild_cmds - 后处理命令

  • 配置一组后处理命令(move_file/move_path)
  • move_file <src_file> <dst_file>
  • move_path <src_path> <dst_path>
  • move_path <src_path> <dst_path> <pattern>

packages - 依赖的pkg包

  • 配置一组本模板安装后依赖的pkg包

file_structures - 文件结构

  • 配置一组模板安装时需要生成的文件或者目录,其中目录需要以'/'结尾配置

project_properties - 自定义的项目属性

  • 配置一组自定义的项目属性

mount_points - 挂载点

  • 配置一组挂载点

组件接口

tmake.gs

函数原型函数作用
void set_project_property(string key, mixed value)设置项目属性
mixed get_project_property(string key)获取项目属性
map get_project_properties()获取项目所有属性
void add_token(string token_name, map properties)在env里动态添加一个token(优先级高于文件token)
map get_tokens(string template_path = nil, object yaml_ob = nil)获取指定路径下以及env里的所有token词典(调试接口)
bool build(string template_path, string out_path, string project_name = "tmake")将给定的一个模板目录处理为目标代码

TemplateContext

模板制作环境

成员变量

变量名类型初始值须初始化描述
pathstringnil可选模板目录
out_pathstringnil可选输出目录
namestringnil可选名称(根据模板目录计算)
yaml_obobjectnil可选YAML配置文件对象
source_dictmapnil可选模板源文件词典
token_dictmapnil可选模板占位符词典表
project_propertiesmapnil可选项目属性
file_structuresarraynil可选模板文件结构
packagesarraynil可选依赖包packages安装
prebuild_filesarraynil可选前处理文件
prebuild_cmdsarraynil可选前处理指令
postbuild_filesarraynil可选后处理文件
postbuild_cmdsarraynil可选后处理指令
error_countint0可选错误计数

成员方法

函数原型函数作用
void set_path(string path)设置模板目录
void set_out_path(string out_path)设置输出目录
bool directory_mount(string real_dir, string mount_point)挂载目录
string get_name()获取模板名称
void reset()重置模板制作环境
array check_integrity()检查模板制作环境是否完整
bool refresh()刷新模板制作环境
string parse(string content)解析指定的文本内容(替换文本中的token和project property)
void recursive_create_file_structures(string parent_path, array file_structures)递归创建文件结构
void create_file_structures()模板制作 - 创建文件结构
bool run_command_line(string cmd_line, string working_path = nil)执行命令行
string run_retrieve_stdout(string cmd_line, string working_path = nil)执行命令行并获取标准输出内容
void add_packages()模板制作 - 执行所有pkg包依赖指令
void complete_template_cmd(TemplateCmd template_cmd)填充完成一个模板命令
void prebuild()模板制作 - 前处理
void postbuild()模板制作 - 后处理
void build()模板制作 - 执行
string get_template_src_path()获取模板源文件目录
string get_template_yaml_file()获取模板yaml配置文件
string get_template_path()获取模板目录
void set_project_property(string property, mixed value)设置项目属性
mixed get_project_property(string property)获取项目属性
void assure_path(string path)确保路径存在(如果不存在则创建)
void build_src()模板制作 - 解析模板的资源文件输出到输出目录
void build_src_file(string src_file_path)解析指定目录下的资源文件并输出到输出目录
void build_src_dir(string src_dir)解析指定目录下的资源文件并输出到输出目录
bool load_mount_points()模板制作 - 加载所有挂载点
bool load_file_structures()模板制作 - 加载文件结构
bool load_packages()模板制作 - 加载所有pkg依赖指令
bool load_yaml_file()模板制作 - 加载.yaml配置文件
bool load_prebuild_files()模板制作 - 加载所有前处理文件
bool load_prebuild_cmds()模板制作 - 加载所有前处理指令
bool load_postbuild_files()模板制作 - 加载所有后处理文件
bool load_postbuild_cmds()模板制作 - 加载所有后处理指令
bool load_project_properties()模板制作 - 加载所有项目属性
bool load_tokens()模板制作 - 加载所有token(变量)
mixed get_token_value(string token_name)获取token值
bool contains_error()模板制作 - 制作环境中是否存在错误
bool is_os_path(string path)判断是否为操作系统路径(file://开头的路径当作是系统路径)
string get_os_path(string path)获取操作系统路径
string get_relative_path(string to, string from)获取起始路径到目标路径的相对路径
void drop_file(string src_file)删除文件
void drop_path(string path, string pattern = "**")删除目录
bool is_same_path(string path1, string path2)判断两个路径是否相同
void move_file(string src_file, string to_file)移动文件
void move_path(string src_path, string dst_path, string pattern = "**")移动目录
void build_log(string fmt, ...)模板制作 - 输出普通日志
void build_success(string fmt, ...)模板制作 - 输出成功日志
void build_warning(string fmt, ...)模板制作 - 输出警告日志
void build_error(string fmt, ...)模板制作 - 输出错误日志
void log(string prefix, string color, string fmt, ...)输出日志

TemplateSourceFile

模板源文件

成员变量

变量名类型初始值须初始化描述
pathstringnil可选文件路径

成员方法

函数原型函数作用
string parse(TemplateContext context)解析模板源文件内容
bool build(string out_path, TemplateContext context)解析模板源文件内容并写入目标文件

TemplateCmd

模板前处理/后处理命令

成员变量

变量名类型初始值须初始化描述
cmdstringnil可选名称
argsarraynil可选参数列表
funcfunctionnil可选执行函数

成员方法

函数原型函数作用
mixed exec()执行命令

样例

    tmake.build("/demo", "/output");