From 43c515a7a439192a899e66d66d782ab438ef45c1 Mon Sep 17 00:00:00 2001 From: ZtRXR Date: Sun, 21 Apr 2024 23:08:33 +0800 Subject: [PATCH] update --- .gitignore | 3 ++- pymake/cmake.py | 53 +++++++++++++++++++++++++------------------------ pymake/tools.py | 6 ++++-- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 587d2c6..128d40b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ __pycache__/ /main.cxx /build -/poetry.lock \ No newline at end of file +/poetry.lock +/test.py \ No newline at end of file diff --git a/pymake/cmake.py b/pymake/cmake.py index bade668..ae467f3 100644 --- a/pymake/cmake.py +++ b/pymake/cmake.py @@ -1,62 +1,63 @@ +from typing import Self from .tools import * from .values import * class cmake: - __write_stack:"list[str]" = [] - __file_name = "" - def __init__(self,version_min:"str",file_name:"str"="CMakeLists.txt") -> None: - self.__file_name = file_name - self.__write_stack.append(f"cmake_minimum_required(VERSION {version_min})") + __write_stack__:List[str] = [] + __file_name__:str + def __init__(self,version_min:str,file_name:str="CMakeLists.txt") -> None: + self.__file_name__ = file_name + self.__write_stack__.append(f"cmake_minimum_required(VERSION {version_min})") def write(self)->None: - with open(file=self.__file_name,mode="+w",encoding="utf8") as f: - for i in self.__write_stack: + with open(file=self.__file_name__,mode="+w",encoding="utf8") as f: + for i in self.__write_stack__: f.write(i) f.write("\n") - def project(self,name:"str"="default",version:"str"="1.0",description:"str"="a default project",languages:"str"="CXX")->"cmake": - self.__write_stack.append(f"project({name} VERSION {version} DESCRIPTION \"{description}\" LANGUAGES {languages})") + def project(self,name:str="default",version:str="1.0",description:str="a default project",languages:str="CXX")->Self: + self.__write_stack__.append(f"project({name} VERSION {version} DESCRIPTION \"{description}\" LANGUAGES {languages})") return self - def add_executable(self,name:"str"=var(project_name),*source:"str")->"cmake": - self.__write_stack.append(f"add_executable({name} {args_to_str(source)})") + def add_executable(self,name:str=var(project_name),*source:str)->Self: + self.__write_stack__.append(f"add_executable({name} {args_to_str(source)})") return self - def add_library(self,name:"str"=var(project_name),mode:"str"=static,*source:"str")->"cmake": + def add_library(self,name:str=var(project_name),mode:str=static,*source:str)->Self: """ ARGS: mode: mode can be STATIC , SHARED or MODULE """ - self.__write_stack.append(f"add_library({name} {mode} {args_to_str(source)})") + self.__write_stack__.append(f"add_library({name} {mode} {args_to_str(source)})") return self - def target_include_directories(self,for_project:"str"=var(project_name),mode:"str"=public,*directories:"str")->"cmake": + def target_include_directories(self,for_project:str=var(project_name),mode:str=public,*directories:str)->Self: """ ARGS: mode : mode can be PUBLIC PRIVATE INTERFACE """ - self.__write_stack.append(f"target_include_directories({for_project} {mode} {args_to_str(directories)})") + self.__write_stack__.append(f"target_include_directories({for_project} {mode} {args_to_str(directories)})") return self - def set(self,name:"str",value:"str")->"cmake": - self.__write_stack.append(f"set({name} {value})") + def set(self,name:str,value:str)->Self: + self.__write_stack__.append(f"set({name} {value})") return self - def target_link_libraries(self,lib_name:"str",name:"str"=var(project_name),mode:"str"=public)->"cmake": - self.__write_stack.append(f"target_link_libraries({name} {mode} {lib_name})") + def target_link_libraries(self,lib_name:str,name:str=var(project_name),mode:str=public)->Self: + self.__write_stack__.append(f"target_link_libraries({name} {mode} {lib_name})") return self - def target_link_directories(self,name:"str"=var(project_name),mode:"str"=public,*directories:"str")->"cmake": - self.__write_stack.append(f"target_link_directories({name} {mode} {args_to_str(directories)})") + def target_link_directories(self,name:str=var(project_name),mode:str=public,*directories:str)->Self: + self.__write_stack__.append(f"target_link_directories({name} {mode} {args_to_str(directories)})") return self - def just_add(self,sentence:"str")->"cmake": - self.__write_stack.append(sentence) + def just_add(self,sentence:str)->Self: + self.__write_stack__.append(sentence) return self - def file(self,new_var_name:"str",mode:"str"=glob_recurse,*search_str:"str")->"cmake": - self.__write_stack.append(f"file({mode} {new_var_name} {args_to_str(search_str)})") + def file(self,new_var_name:str,mode:str=glob_recurse,*search_str:str)->Self: + self.__write_stack__.append(f"file({mode} {new_var_name} {args_to_str(search_str)})") return self def add_subdirectory(self,path:str)->'cmake': - self.__write_stack.append(f"add_subdirectory({path})") + self.__write_stack__.append(f"add_subdirectory({path})") return self \ No newline at end of file diff --git a/pymake/tools.py b/pymake/tools.py index eb957da..71de6b4 100644 --- a/pymake/tools.py +++ b/pymake/tools.py @@ -1,8 +1,10 @@ -from typing import * +from typing import List, Tuple, Union + + def var(name:str)->str: return f"${{{name}}}" -def args_to_str(args:list[str])->str: +def args_to_str(args:Union[List[str],Tuple[str,...]])->str: vars_str = "" for i in args: vars_str=f"{vars_str} {i}"