Skip to content

Commit

Permalink
feat: optimize the post process script
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Sep 30, 2024
1 parent 97a3471 commit be32a09
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
22 changes: 12 additions & 10 deletions template/cuda/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os
import subprocess

def remove_file_if_exists(file_path):
if os.path.exists(file_path):
os.remove(file_path)

cxx_build_tool = "{{cookiecutter.cxx_build_tool}}"

cmake_root = "CMakeLists.txt"
Expand All @@ -9,22 +13,20 @@
xmake_test = "tests/xmake.lua"

if cxx_build_tool == "cmake":
if os.path.exists(xmake_root):
os.remove(xmake_root)
if os.path.exists(xmake_test):
os.remove(xmake_test)
remove_file_if_exists(xmake_root)
remove_file_if_exists(xmake_test)
elif cxx_build_tool == "xmake":
if os.path.exists(cmake_root):
os.remove(cmake_root)
if os.path.exists(cmake_test):
os.remove(cmake_test)
remove_file_if_exists(cmake_root)
remove_file_if_exists(cmake_test)
else:
raise ValueError(f"Unknown cxx_build_tool: {cxx_build_tool}")

# Initialize Git repository
try:
subprocess.run(["git", "init"], check=True)
except subprocess.CalledProcessError:
print("Failed to initialize Git repository. Please make sure Git is installed and try manually.")
except subprocess.CalledProcessError as e:
print(f"Failed to initialize Git repository. Error: {e}")
except FileNotFoundError:
print("Git command not found. Please install Git and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
22 changes: 12 additions & 10 deletions template/cxx/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os
import subprocess

def remove_file_if_exists(file_path):
if os.path.exists(file_path):
os.remove(file_path)

cxx_build_tool = "{{cookiecutter.cxx_build_tool}}"

cmake_root = "CMakeLists.txt"
Expand All @@ -9,22 +13,20 @@
xmake_test = "tests/xmake.lua"

if cxx_build_tool == "cmake":
if os.path.exists(xmake_root):
os.remove(xmake_root)
if os.path.exists(xmake_test):
os.remove(xmake_test)
remove_file_if_exists(xmake_root)
remove_file_if_exists(xmake_test)
elif cxx_build_tool == "xmake":
if os.path.exists(cmake_root):
os.remove(cmake_root)
if os.path.exists(cmake_test):
os.remove(cmake_test)
remove_file_if_exists(cmake_root)
remove_file_if_exists(cmake_test)
else:
raise ValueError(f"Unknown cxx_build_tool: {cxx_build_tool}")

# Initialize Git repository
try:
subprocess.run(["git", "init"], check=True)
except subprocess.CalledProcessError:
print("Failed to initialize Git repository. Please make sure Git is installed and try manually.")
except subprocess.CalledProcessError as e:
print(f"Failed to initialize Git repository. Error: {e}")
except FileNotFoundError:
print("Git command not found. Please install Git and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
6 changes: 4 additions & 2 deletions template/go/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Initialize Git repository
try:
subprocess.run(["git", "init"], check=True)
except subprocess.CalledProcessError:
print("Failed to initialize Git repository. Please make sure Git is installed and try manually.")
except subprocess.CalledProcessError as e:
print(f"Failed to initialize Git repository. Error: {e}")
except FileNotFoundError:
print("Git command not found. Please install Git and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
6 changes: 4 additions & 2 deletions template/py/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Initialize Git repository
try:
subprocess.run(["git", "init"], check=True)
except subprocess.CalledProcessError:
print("Failed to initialize Git repository. Please make sure Git is installed and try manually.")
except subprocess.CalledProcessError as e:
print(f"Failed to initialize Git repository. Error: {e}")
except FileNotFoundError:
print("Git command not found. Please install Git and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
27 changes: 25 additions & 2 deletions template/rs/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import os
import subprocess

# Function to remove a file if it exists
def remove_file_if_exists(file_path):
if os.path.exists(file_path):
os.remove(file_path)

# Get crate type from the cookiecutter template
crate_type = "{{cookiecutter.crate_type}}"

# Define paths to main.rs and lib.rs
main_rs = "src/main.rs"
lib_rs = "src/lib.rs"

# Handle crate type and remove unnecessary files
if crate_type == "bin":
remove_file_if_exists(lib_rs)
elif crate_type == "lib":
remove_file_if_exists(main_rs)
else:
raise ValueError(f"Unknown crate type: {crate_type}")

# Initialize Git repository
try:
subprocess.run(["git", "init"], check=True)
except subprocess.CalledProcessError:
print("Failed to initialize Git repository. Please make sure Git is installed and try manually.")
except subprocess.CalledProcessError as e:
print(f"Failed to initialize Git repository. Error: {e}")
except FileNotFoundError:
print("Git command not found. Please install Git and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
6 changes: 4 additions & 2 deletions template/ts/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Initialize Git repository
try:
subprocess.run(["git", "init"], check=True)
except subprocess.CalledProcessError:
print("Failed to initialize Git repository. Please make sure Git is installed and try manually.")
except subprocess.CalledProcessError as e:
print(f"Failed to initialize Git repository. Error: {e}")
except FileNotFoundError:
print("Git command not found. Please install Git and try again.")
except Exception as e:
print(f"An unexpected error occurred: {e}")

0 comments on commit be32a09

Please sign in to comment.