提交 cf3fe5cc 编写于 作者: 小代码2016's avatar 小代码2016

模板引擎与多目录打包

上级 2463d1fc
# cm_cli
使用 python 管理 cmake 工程
\ No newline at end of file
使用 python 管理 cmake 工程
```
pip freeze > requirements.txt
pip install - r requirements.txt
```
```
pyinstaller cm.py -p util
```
\ No newline at end of file
# -*- coding: utf-8 -*-
import argparse
import json
import os.path
import sys
import init_action
from build_handler import BuildHandler
from cm_config import CmConfig
from init_handler import InitHandler
from util import collection_init_args, is_windows
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
KEY_CM_ACTION = 'cm_action'
ARG_PREFIX = '--'
SUB_COMMAND_INIT = 'init'
ARG_INIT_NAME = 'name'
ARG_INIT_LANGUAGE = 'language'
ARG_INIT_STANDARD = 'standard'
ARG_INIT_PROJ_TYPE = 'proj-type'
ARG_INIT_BUILD_DIR = 'build-dir'
ARG_INIT_OUTPUT_DIR = 'output-dir'
SUB_COMMAND_BUILD = 'build'
ARG_BUILD_RE = 're'
SUB_COMMAND_RUN = 'run'
def register_init_args():
"""
init 子命令
"""
init_parsers = subparsers.add_parser(SUB_COMMAND_INIT, help='初始化')
init_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_INIT)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_NAME, help='项目名称', action=init_action.NameAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_LANGUAGE, help='语言', choices=['c', 'cpp'],
action=init_action.LanguageAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_STANDARD,
help='语言标准. c:89,90,99,11,18 . cpp: 98,11,14,17,20,23 . c 默认为 11 , cpp 默认为 14',
action=init_action.LanguageStandardAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_PROJ_TYPE,
help='项目类型. simple, simple_app, module_app . 默认为 simple_app',
default='simple_app',
action=init_action.ProjTypeAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_BUILD_DIR, help='构建目录. 默认为 build',
default='build',
action=init_action.BuildDirAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_OUTPUT_DIR, help='二进制或库文件输出目录. 默认为 bin',
default='bin',
action=init_action.OutputDirAction)
def register_build_args():
"""
build 子命令
"""
build_parsers = subparsers.add_parser(SUB_COMMAND_BUILD, help='构建')
build_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_BUILD)
build_parsers.add_argument(ARG_PREFIX + ARG_BUILD_RE, help='重新构建', action='store_const', const=1)
def register_run_args():
"""
run 子命令
"""
run_parsers = subparsers.add_parser(SUB_COMMAND_RUN, help='运行')
run_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_RUN)
def print_config():
config = CmConfig()
print('proj_name:' + config.proj_name)
print('language:' + config.language)
print('standard:' + config.standard)
print('type:' + config.proj_type)
print('build_dir:' + config.build_dir)
print('output_dir:' + config.output_dir)
def init_cm_config():
"""
初始化全局配置
:return:
"""
config = CmConfig()
# 程序所在目录
if hasattr(sys, 'frozen'):
config._application_dir = os.path.dirname(sys.executable)
elif __file__:
config._application_dir = os.path.dirname(os.path.abspath(__file__))
# 工作目录
config._work_dir = os.getcwd()
# 如果工作目录存在 cm.json , 则根据 cm.json 更新 CmConfig
cm_file = config.work_dir + os.path.sep + 'cm.json'
if os.path.exists(cm_file):
with open(cm_file, 'r', encoding='UTF-8') as file:
data = json.load(file)
config.proj_name = data.get('proj_name')
config.language = data.get('language')
config.standard = data.get('standard')
config.proj_type = data.get('proj_type')
config.build_dir = data.get('build_dir')
config.output_dir = data.get('output_dir')
# ['cm.py','base_action.py','build_handler.py','cm_config.py','cm_decorator.py','init_action.py','init_handler.py','util.py'],
if __name__ == '__main__':
# 初始化全局配置
init_cm_config()
config = CmConfig()
# 注册命令
register_init_args()
register_build_args()
register_run_args()
args = parser.parse_args()
if SUB_COMMAND_INIT == args.__dict__[KEY_CM_ACTION]:
if os.path.exists('cm.json'):
print('已完成初始化')
else:
collection_init_args()
print('执行初始化...')
init_handler = InitHandler()
init_handler.handle()
print('初始化完成. 使用 cm build 编译 , cm run 运行')
elif SUB_COMMAND_BUILD == args.__dict__[KEY_CM_ACTION]:
print('开始构建...')
build_handler = BuildHandler()
build_handler.build(re=args.__dict__[ARG_BUILD_RE])
print('构建完成. 使用 cm run 运行')
elif SUB_COMMAND_RUN == args.__dict__[KEY_CM_ACTION]:
print('开始运行...\n')
command = '.' + os.path.sep + config.output_dir + os.path.sep + config.proj_name
if is_windows():
command = command + '.exe'
os.system(command)
print('\n运行完毕')
cmake_minimum_required(VERSION 3.0)
# 设置项目名称和语言
project($%{proj_name} C)
# 指定语言标准
set(CMAKE_C_STANDARD $%{standard})
set(CMAKE_C_STANDARD_REQUIRED True)
# 参与编译的源文件列表
aux_source_directory(. MAIN_SRCS)
# 可执行文件所在目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/$%{output_dir})
add_executable(${PROJECT_NAME} ${MAIN_SRCS})
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
\ No newline at end of file
# -*- coding: utf-8 -*-
import argparse
import json
import os.path
import sys
from jinja2 import Template
import init_action
from build_handler import BuildHandler
from cm_config import CmConfig
from init_handler import InitHandler
from util import collection_init_args, is_windows
from util import util
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
KEY_CM_ACTION = 'cm_action'
ARG_PREFIX = '--'
SUB_COMMAND_INIT = 'init'
ARG_INIT_NAME = 'name'
ARG_INIT_LANGUAGE = 'language'
ARG_INIT_STANDARD = 'standard'
ARG_INIT_PROJ_TYPE = 'proj-type'
ARG_INIT_BUILD_DIR = 'build-dir'
ARG_INIT_OUTPUT_DIR = 'output-dir'
SUB_COMMAND_BUILD = 'build'
ARG_BUILD_RE = 're'
SUB_COMMAND_RUN = 'run'
def register_init_args():
"""
init 子命令
"""
init_parsers = subparsers.add_parser(SUB_COMMAND_INIT, help='初始化')
init_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_INIT)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_NAME, help='项目名称', action=init_action.NameAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_LANGUAGE, help='语言', choices=['c', 'cpp'],
action=init_action.LanguageAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_STANDARD,
help='语言标准. c:89,90,99,11,18 . cpp: 98,11,14,17,20,23 . c 默认为 11 , cpp 默认为 14',
action=init_action.LanguageStandardAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_PROJ_TYPE,
help='项目类型. simple, simple_app, module_app . 默认为 simple_app',
default='simple_app',
action=init_action.ProjTypeAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_BUILD_DIR, help='构建目录. 默认为 build',
default='build',
action=init_action.BuildDirAction)
init_parsers.add_argument(ARG_PREFIX + ARG_INIT_OUTPUT_DIR, help='二进制或库文件输出目录. 默认为 bin',
default='bin',
action=init_action.OutputDirAction)
def register_build_args():
"""
build 子命令
"""
build_parsers = subparsers.add_parser(SUB_COMMAND_BUILD, help='构建')
build_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_BUILD)
build_parsers.add_argument(ARG_PREFIX + ARG_BUILD_RE, help='重新构建', action='store_const', const=1)
def register_run_args():
"""
run 子命令
"""
run_parsers = subparsers.add_parser(SUB_COMMAND_RUN, help='运行')
run_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_RUN)
def print_config():
config = CmConfig()
print('proj_name:' + config.proj_name)
print('language:' + config.language)
print('standard:' + config.standard)
print('type:' + config.proj_type)
print('build_dir:' + config.build_dir)
print('output_dir:' + config.output_dir)
def init_cm_config():
"""
初始化全局配置
:return:
"""
config = CmConfig()
# 程序所在目录
if hasattr(sys, 'frozen'):
config._application_dir = os.path.dirname(sys.executable)
elif __file__:
config._application_dir = os.path.dirname(os.path.abspath(__file__))
# 工作目录
config._work_dir = os.getcwd()
# 如果工作目录存在 cm.json , 则根据 cm.json 更新 CmConfig
cm_file = config.work_dir + os.path.sep + 'cm.json'
if os.path.exists(cm_file):
with open(cm_file, 'r', encoding='UTF-8') as file:
data = json.load(file)
config.proj_name = data.get('proj_name')
config.language = data.get('language')
config.standard = data.get('standard')
config.proj_type = data.get('proj_type')
config.build_dir = data.get('build_dir')
config.output_dir = data.get('output_dir')
# ['cm.py','base_action.py','build_handler.py','cm_config.py','cm_decorator.py','init_action.py','init_handler.py','util.py'],
if __name__ == '__main__':
# 初始化全局配置
init_cm_config()
config = CmConfig()
# 注册命令
register_init_args()
register_build_args()
register_run_args()
args = parser.parse_args()
if SUB_COMMAND_INIT == args.__dict__[KEY_CM_ACTION]:
if os.path.exists('cm.json'):
print('已完成初始化')
else:
collection_init_args()
print('执行初始化...')
init_handler = InitHandler()
init_handler.handle()
print('初始化完成. 使用 cm build 编译 , cm run 运行')
elif SUB_COMMAND_BUILD == args.__dict__[KEY_CM_ACTION]:
print('开始构建...')
build_handler = BuildHandler()
build_handler.build(re=args.__dict__[ARG_BUILD_RE])
print('构建完成. 使用 cm run 运行')
elif SUB_COMMAND_RUN == args.__dict__[KEY_CM_ACTION]:
print('开始运行...\n')
command = '.' + os.path.sep + config.output_dir + os.path.sep + config.proj_name
if is_windows():
command = command + '.exe'
os.system(command)
print('\n运行完毕')
if util.is_windows():
print('running in windows')
elif util.is_linux():
print('running in linux')
print('hello world')
template = Template('Hello {{name}}')
s = template.render(name='老狼')
print(s)
with open('F:/2023/blog_code/cm_cli/resources/template_c_simple/CMakeLists.txt', encoding='UTF-8') as file:
data = file.read()
template = Template(data)
s = template.render({
'proj_name': 'helloc'
})
print(s)
cmake_minimum_required(VERSION 3.0)
# 设置项目名称和语言
project($%{proj_name} C)
# 指定语言标准
set(CMAKE_C_STANDARD $%{standard})
set(CMAKE_C_STANDARD_REQUIRED True)
# 参与编译的源文件列表
aux_source_directory(. MAIN_SRCS)
# 可执行文件所在目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/$%{output_dir})
add_executable(${PROJECT_NAME} ${MAIN_SRCS})
project name: {{proj_name}}
\ No newline at end of file
# -*- coding: utf-8 -*-
import platform
def is_windows():
return 'Windows' == platform.system()
def is_linux():
return 'Linux' == platform.system()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册