<filename>src/utopia/cmake/cli.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# MIT License
# Copyright (c) 2021 MingMoe

import sys
import subprocess
from .. import u_version
from .. import write_okay
from .. import write_error
from .. import done_exit
from .. import failed_exit


# 打印帮助信息
def print_help():
    print(f"cmake build tools ver{u_version.UTOOLS_CMAKE_VERSION}")
    print("usage:utools-cmake [Options]")
    print("options:")
    print("\t-h,--help\t\t\t\t打印帮助，之后退出")

    print("\tdebug\t\t\t\t\t开启debug模式")
    print("\trelease\t\t\t\t\t开启release模式")

    print("\tpack-bin\t\t\t\t开启二进制包")
    print("\tpack-src\t\t\t\t开启源码包")

    print("\ttest\t\t\t\t\t开启测试")

    print("\t--generate-options [option]\t\t添加一个生成选项")

    print("\t--build-options    [option]\t\t添加一个构建选项")

    print("\t--pack-bin-options [option]\t\t添加一个二进制打包选项")
    print("\t--pack-src-options [option]\t\t添加一个源码打包选项")

    print("\t--test-options     [option]\t\t添加一个测试选项")

    print("\t--cmake-check              \t\t开启cmake警告")


def cmake_main():
    """
    cmake构建工具入口函数
    :return: None
    """
    # 构建类型
    build_type = "Unknown"

    # 打包类型
    pack_bin_type = False
    pack_src_type = False

    # 测试类型
    test_type = False

    # cmake检查
    cmake_check = False

    # 生成命令行
    generate_arguments = ['cmake', '-S', '.', '-B', 'build']

    # 构建命令行
    build_arguments = ['cmake', '--build', '.']

    # 打包命令行
    pack_bin_arguments = ['cpack', "--config", "CPackConfig.cmake"]
    pack_src_arguments = ['cpack', "--config", "CPackSourceConfig.cmake"]

    # 测试命令行
    test_arguments = ['ctest']

    # 解析参数
    args = sys.argv[1:]

    index = 0

    # 从第一个参数开始解析
    while index < len(args):
        arg = args[index]

        # 打印帮助
        if arg == "-h" or arg == "--help":
            print_help()
            # 之后退出
            done_exit()

        # cmake检查
        elif arg == "--cmake-check":
            cmake_check = True

        # 设置编译模式
        elif arg == "debug":
            build_type = "Debug"

        elif arg == "release":
            build_type = "Release"

        # 设置打包
        elif arg == "pack-bin":
            pack_bin_type = True

        elif arg == "pack-src":
            pack_src_type = True

        # 设置测试
        elif arg == "test":
            test_type = True

        # 设置生成参数
        elif arg == "--generate-options":
            index += 1
            generate_arguments.append(args[index])

        elif arg == "--build-options":
            index += 1
            build_arguments.append(args[index])

        # 设置打包参数
        elif arg == "--pack-bin-options":
            index += 1
            pack_bin_arguments.append(args[index])

        elif arg == "--pack-src-options":
            index += 1
            pack_src_arguments.append(args[index])

        # 设置测试参数
        elif arg == "--test-options":
            index += 1
            test_arguments.append(args[index])

        else:
            # 错误:未知的参数
            write_error(f"未知的参数:{arg}")
            print_help()
            failed_exit()

        index += 1

    # 如果未指定build_type则打印错误并退出
    if build_type == "Unknown":
        write_error("未指定构建类型")
        write_error("需要debug或release参数")
        print_help()
        failed_exit()

    # 添加构建类型参数
    generate_arguments.append("-D")
    generate_arguments.append(f"CMAKE_BUILD_TYPE={build_type}")

    build_arguments.append("--config")
    build_arguments.append(build_type)

    test_arguments.append("-C")
    test_arguments.append(build_type)

    pack_bin_arguments.append("-C")
    pack_bin_arguments.append(build_type)

    pack_src_arguments.append("-C")
    pack_src_arguments.append(build_type)

    # 开启cmake检查
    if cmake_check:
        generate_arguments.append("-Wdev")
        generate_arguments.append("-Wdeprecated")

    # 构建
    write_okay("开始构建")

    write_okay(f"构建类型:{build_type}")
    write_okay(f"Test:{test_type}")
    write_okay(f"Pack-Src:{pack_src_type}")
    write_okay(f"Pack-Bin:{pack_bin_type}")

    write_okay(f"生成命令行:{generate_arguments}")
    write_okay(f"构建命令行:{build_arguments}")

    write_okay(f"测试命令行:{test_arguments}")

    write_okay(f"打包源码命令行:{pack_src_arguments}")
    write_okay(f"打包输出命令行:{pack_bin_arguments}")

    # 构建
    try:
        subprocess.run(generate_arguments, check=True)

        write_okay("生成完成")
        write_okay("设置工作目录到build")

        subprocess.run(build_arguments, check=True, cwd="./build")
        write_okay("构建完成")

        if pack_bin_type:
            subprocess.run(pack_bin_arguments, check=True, cwd="./build")
            write_okay("打包输出完成")

        if pack_src_type:
            subprocess.run(pack_src_arguments, check=True, cwd="./build")
            write_okay("打包源码完成")

        if test_type:
            subprocess.run(test_arguments, check=True, cwd="./build")
            write_okay("测试完成")

    except subprocess.CalledProcessError as err:
        print(err)
        write_error("构建失败!")
        failed_exit()

    write_okay("构建成功!")
    done_exit()

<filename>src/utopia/cmake/cli.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# MIT License
# Copyright (c) 2021 MingMoe

import sys
import subprocess
from .. import u_version
from .. import write_okay
from .. import write_error
from .. import done_exit
from .. import failed_exit


# 打印帮助信息
def print_help():
    print(f"cmake build tools ver{u_version.UTOOLS_CMAKE_VERSION}")
    print("usage:utools-cmake [Options]")
    print("options:")
    print("\t-h,--help\t\t\t\t打印帮助，之后退出")

    print("\tdebug\t\t\t\t\t开启debug模式")
    print("\trelease\t\t\t\t\t开启release模式")

    print("\tpack-bin\t\t\t\t开启二进制包")
    print("\tpack-src\t\t\t\t开启源码包")

    print("\ttest\t\t\t\t\t开启测试")

    print("\t--generate-options [option]\t\t添加一个生成选项")

    print("\t--build-options    [option]\t\t添加一个构建选项")

    print("\t--pack-bin-options [option]\t\t添加一个二进制打包选项")
    print("\t--pack-src-options [option]\t\t添加一个源码打包选项")

    print("\t--test-options     [option]\t\t添加一个测试选项")

    print("\t--cmake-check              \t\t开启cmake警告")


def cmake_main():
    """
    cmake构建工具入口函数
    :return: None
    """
    # 构建类型
    build_type = "Unknown"

    # 打包类型
    pack_bin_type = False
    pack_src_type = False

    # 测试类型
    test_type = False

    # cmake检查
    cmake_check = False

    # 生成命令行
    generate_arguments = ['cmake', '-S', '.', '-B', 'build']

    # 构建命令行
    build_arguments = ['cmake', '--build', '.']

    # 打包命令行
    pack_bin_arguments = ['cpack', "--config", "CPackConfig.cmake"]
    pack_src_arguments = ['cpack', "--config", "CPackSourceConfig.cmake"]

    # 测试命令行
    test_arguments = ['ctest']

    # 解析参数
    args = sys.argv[1:]

    index = 0

    # 从第一个参数开始解析
    while index < len(args):
        arg = args[index]

        # 打印帮助
        if arg == "-h" or arg == "--help":
            print_help()
            # 之后退出
            done_exit()

        # cmake检查
        elif arg == "--cmake-check":
            cmake_check = True

        # 设置编译模式
        elif arg == "debug":
            build_type = "Debug"

        elif arg == "release":
            build_type = "Release"

        # 设置打包
        elif arg == "pack-bin":
            pack_bin_type = True

        elif arg == "pack-src":
            pack_src_type = True

        # 设置测试
        elif arg == "test":
            test_type = True

        # 设置生成参数
        elif arg == "--generate-options":
            index += 1
            generate_arguments.append(args[index])

        elif arg == "--build-options":
            index += 1
            build_arguments.append(args[index])

        # 设置打包参数
        elif arg == "--pack-bin-options":
            index += 1
            pack_bin_arguments.append(args[index])

        elif arg == "--pack-src-options":
            index += 1
            pack_src_arguments.append(args[index])

        # 设置测试参数
        elif arg == "--test-options":
            index += 1
            test_arguments.append(args[index])

        else:
            # 错误:未知的参数
            write_error(f"未知的参数:{arg}")
            print_help()
            failed_exit()

        index += 1

    # 如果未指定build_type则打印错误并退出
    if build_type == "Unknown":
        write_error("未指定构建类型")
        write_error("需要debug或release参数")
        print_help()
        failed_exit()

    # 添加构建类型参数
    generate_arguments.append("-D")
    generate_arguments.append(f"CMAKE_BUILD_TYPE={build_type}")

    build_arguments.append("--config")
    build_arguments.append(build_type)

    test_arguments.append("-C")
    test_arguments.append(build_type)

    pack_bin_arguments.append("-C")
    pack_bin_arguments.append(build_type)

    pack_src_arguments.append("-C")
    pack_src_arguments.append(build_type)

    # 开启cmake检查
    if cmake_check:
        generate_arguments.append("-Wdev")
        generate_arguments.append("-Wdeprecated")

    # 构建
    write_okay("开始构建")

    write_okay(f"构建类型:{build_type}")
    write_okay(f"Test:{test_type}")
    write_okay(f"Pack-Src:{pack_src_type}")
    write_okay(f"Pack-Bin:{pack_bin_type}")

    write_okay(f"生成命令行:{generate_arguments}")
    write_okay(f"构建命令行:{build_arguments}")

    write_okay(f"测试命令行:{test_arguments}")

    write_okay(f"打包源码命令行:{pack_src_arguments}")
    write_okay(f"打包输出命令行:{pack_bin_arguments}")

    # 构建
    try:
        subprocess.run(generate_arguments, check=True)

        write_okay("生成完成")
        write_okay("设置工作目录到build")

        subprocess.run(build_arguments, check=True, cwd="./build")
        write_okay("构建完成")

        if pack_bin_type:
            subprocess.run(pack_bin_arguments, check=True, cwd="./build")
            write_okay("打包输出完成")

        if pack_src_type:
            subprocess.run(pack_src_arguments, check=True, cwd="./build")
            write_okay("打包源码完成")

        if test_type:
            subprocess.run(test_arguments, check=True, cwd="./build")
            write_okay("测试完成")

    except subprocess.CalledProcessError as err:
        print(err)
        write_error("构建失败!")
        failed_exit()

    write_okay("构建成功!")
    done_exit()

#!/usr/bin/python3 # -*- coding: utf-8 -*- # MIT License # Copyright (c) 2021 MingMoe # 打印帮助信息 cmake构建工具入口函数     :return: None # 构建类型 # 打包类型 # 测试类型 # cmake检查 # 生成命令行 # 构建命令行 # 打包命令行 # 测试命令行 # 解析参数 # 从第一个参数开始解析 # 打印帮助 # 之后退出 # cmake检查 # 设置编译模式 # 设置打包 # 设置测试 # 设置生成参数 # 设置打包参数 # 设置测试参数 # 错误:未知的参数 # 如果未指定build_type则打印错误并退出 # 添加构建类型参数 # 开启cmake检查 # 构建 # 构建