Skip to content

Commit

Permalink
Add new_component.py. (#10)
Browse files Browse the repository at this point in the history
The new file `tools/new_component.py` allows users to create a new
component with ease. It is an interactive wizard to create new
components by asking the user questions, and creating the new files
required.
  • Loading branch information
ZCG-coder authored Apr 17, 2024
2 parents 7c3a1b1 + 5069041 commit d53aba5
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 22 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(STP_BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "The source directory of Steppable")


execute_process(COMMAND uname -o COMMAND tr -d '\n' OUTPUT_VARIABLE OPERATING_SYSTEM)
if (${OPERATING_SYSTEM} MATCHES "Android")
set(ANDROID 1)
message(STATUS "Building for Android.")
set(LINUX CACHE INTERNAL "Is Linux or Android" 1)
endif ()

if (WIN32)
add_compile_definitions(WINDOWS)
if (MSVC) # MSVC Has no UTF-8 support by default
Expand Down Expand Up @@ -62,7 +70,11 @@ function(capitalize IN OUT)
${CAPITALIZED}
PARENT_SCOPE)
endfunction()

set(COMPONENTS abs add baseConvert subtract multiply decimalConvert comparison power division)
# NEW_COMPONENT: PATCH
# Do NOT remove the previous comment.

set(TARGETS ${COMPONENTS} util)
set(TEST_TARGETS_TEMP util fraction number ${COMPONENTS})

Expand Down
5 changes: 0 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,3 @@ target_link_libraries(steppable util)
target_link_libraries(calc steppable)
target_compile_definitions(calc PRIVATE NO_MAIN)

execute_process(COMMAND uname -o COMMAND tr -d '\n' OUTPUT_VARIABLE OPERATING_SYSTEM)
if (${OPERATING_SYSTEM} MATCHES "Android")
set(ANDROID 1)
message(STATUS "Building for Android.")
endif ()
45 changes: 34 additions & 11 deletions tools/add_copyright_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
**************************************************************************************************/\
"""

REGEX_CPP = re.compile(r"""/\*+
REGEX_CPP = re.compile(
r"""/\*+
\* Copyright \(c\) 2023-(....) NWSOFT {65}\*
\* {96}\*
\* Permission is hereby granted, free of charge, to any person obtaining a copy {19}\*
Expand All @@ -71,7 +72,9 @@
\* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE {18}\*
\* SOFTWARE\. {86}\*
\*+/
""", flags=re.MULTILINE)
""",
flags=re.MULTILINE,
)

COPYRIGHT_PY_CMAKE = """\
#####################################################################################################
Expand All @@ -97,7 +100,8 @@
#####################################################################################################\
"""

REGEX_PY_CMAKE = re.compile(r"""#{101}
REGEX_PY_CMAKE = re.compile(
r"""#{101}
# {2}Copyright \(c\) 2023-(....) NWSOFT {67}#
# {99}#
# {2}Permission is hereby granted, free of charge, to any person obtaining a copy {21}#
Expand All @@ -118,7 +122,9 @@
# {2}OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE {20}#
# {2}SOFTWARE\. {88}#
#{101}
""", flags=re.MULTILINE)
""",
flags=re.MULTILINE,
)


def count_lines(text: str) -> int:
Expand All @@ -131,13 +137,21 @@ def first_n_lines(text: str, n: int) -> str:


def process(file: Path):
if file.suffix in [".cpp", ".hpp"] or file.name == "cpp.hint": # C++ Source / Header
if (
file.suffix in [".cpp", ".hpp"] or file.name == "cpp.hint"
): # C++ Source / Header
with open(file, "r") as f:
contents = f.read()
results = re.match(REGEX_CPP, first_n_lines(contents, count_lines(COPYRIGHT_CPP) + 1))
results = re.match(
REGEX_CPP, first_n_lines(contents, count_lines(COPYRIGHT_CPP) + 1)
)
year = results.group(1) if results is not None else None
if results is None:
contents = COPYRIGHT_CPP.format(year=datetime.datetime.now().year) + "\n\n" + contents
contents = (
COPYRIGHT_CPP.format(year=datetime.datetime.now().year)
+ "\n\n"
+ contents
)
print(f"Added header to {file}")
elif year != str(datetime.datetime.now().year):
header = COPYRIGHT_CPP.format(year=datetime.datetime.now().year)
Expand All @@ -146,13 +160,22 @@ def process(file: Path):

with open(file, "w") as f:
f.write(contents)
elif file.suffix == ".py" or file.name == "CMakeLists.txt": # Python File or CMake file
elif (
file.suffix == ".py" or file.name == "CMakeLists.txt"
): # Python File or CMake file
with open(file, "r") as f:
contents = f.read()
results = re.match(REGEX_PY_CMAKE, first_n_lines(contents, count_lines(COPYRIGHT_PY_CMAKE) + 1))
results = re.match(
REGEX_PY_CMAKE,
first_n_lines(contents, count_lines(COPYRIGHT_PY_CMAKE) + 1),
)
year = results.group(1) if results is not None else None
if results is None:
contents = COPYRIGHT_PY_CMAKE.format(year=datetime.datetime.now().year) + "\n\n" + contents
contents = (
COPYRIGHT_PY_CMAKE.format(year=datetime.datetime.now().year)
+ "\n\n"
+ contents
)
print(f"Added header to {file}")
elif year != str(datetime.datetime.now().year):
header = COPYRIGHT_PY_CMAKE.format(year=datetime.datetime.now().year)
Expand All @@ -169,7 +192,7 @@ def walk_into_directory(path: Path):
process(subpath)


if __name__ == '__main__':
if __name__ == "__main__":
process(PROJECT_PATH / "CMakeLists.txt")
process(PROJECT_PATH / "setup.py")
process(PROJECT_PATH / "cpp.hint")
Expand Down
Loading

0 comments on commit d53aba5

Please sign in to comment.