Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Mar 28, 2024
1 parent 55733c2 commit c7ae87a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,4 @@ dmypy.json
.pyre/
.activate.sh
.DS_Store
.MyAppTest

.MyAppTest/
50 changes: 50 additions & 0 deletions activate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

#!/bin/bash
set -e

# Get the directory of the current script
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Change to that directory
cd "$script_dir"

# Confirm the current directory
echo "Changed directory to: $(pwd)"
echo "activate.sh change working directory to $(pwd)"


if [ "$IN_ACTIVATED_ENV" = "1" ]; then
IN_ACTIVATED_ENV=1
else
IN_ACTIVATED_ENV=0
fi

# If the 'venv' directory doesn't exist, print a message and exit.
if [ ! -d "venv" ]; then
cwd=$(pwd)
echo "The 'venv' directory in $cwd does not exist, creating..."
echo "OSTYPE: $OSTYPE"
case "$OSTYPE" in
darwin*|linux-gnu*)
python3 ./install.py
;;
*)
python ./install.py
;;
esac

. ./venv/bin/activate
export IN_ACTIVATED_ENV=1
this_dir=$(pwd)
export PATH="$this_dir:$PATH"
echo "Environment created."
pip install -e .
exit 0
fi

if [[ "$IN_ACTIVATED_ENV" != "1" ]]; then
. ./venv/bin/activate
export IN_ACTIVATED_ENV=1
this_dir=$(pwd)
export PATH="$this_dir:$PATH"
fi
20 changes: 7 additions & 13 deletions src/create_python_cmd/createapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
def check_name(app_name: str) -> None:
"""Check the name of the application."""
if not app_name.isidentifier():
raise ValueError(
"The name of the application is not a valid Python identifier."
)
raise ValueError("The name of the application is not a valid Python identifier.")


def check_semantic_version(version: str) -> None:
"""Check the version of the application."""
version_list = version.split(".")
for v in version_list:
if not v.isnumeric():
raise ValueError(
"The version of the application is not a valid semantic version."
)
raise ValueError("The version of the application is not a valid semantic version.")


def remove_double_blank_lines(lines: list) -> list:
Expand All @@ -49,6 +45,7 @@ def remove_double_blank_lines(lines: list) -> list:


def do_create_python_app(
app_name: str,
app_description: str,
app_author: str,
app_keywords: str, # Example "keyword1, keyword2, keyword3"
Expand All @@ -61,7 +58,6 @@ def do_create_python_app(
# get app name from the github url
cwd = cwd or os.getcwd()
os.makedirs(cwd, exist_ok=True)
app_name = github_url.split("/")[-1]
app_name_underscore = app_name.replace("-", "_")
with tempfile.TemporaryDirectory() as tmpdir:
# download https://github.com/zackees/template-python-cmd
Expand All @@ -72,9 +68,7 @@ def do_create_python_app(
for root, dirs, files in os.walk(tmpdir):
for d in dirs:
if d == "template-python-cmd" or d == "template_python_cmd":
shutil.move(
os.path.join(root, d), os.path.join(root, app_name_underscore)
)
shutil.move(os.path.join(root, d), os.path.join(root, app_name_underscore))
pyproject = os.path.join(tmpdir, "pyproject.toml")
with open(pyproject, encoding="utf-8", mode="r") as pyproject_file:
pyproject_lines = pyproject_file.read().splitlines()
Expand All @@ -94,9 +88,7 @@ def do_create_python_app(
pyproject_lines[i] = ""
else:
if line.startswith("test_cmd ="):
pyproject_lines[
i
] = f'{command_name} = "{app_name_underscore}.cli:main"'
pyproject_lines[i] = f'{command_name} = "{app_name_underscore}.cli:main"'
########
# Transform pyproject file with the new information
pyproject_lines = remove_double_blank_lines(pyproject_lines)
Expand Down Expand Up @@ -154,6 +146,7 @@ def create_python_app() -> None:
# check if git exists
if not shutil.which("git"):
raise RuntimeError("Git is not installed.")
app_name: str
while True:
try:
app_name = input("Python app name: ").replace("-", "_")
Expand Down Expand Up @@ -190,6 +183,7 @@ def create_python_app() -> None:
print(f"Error: {e}, try again")
continue
do_create_python_app(
app_name=app_name,
app_description=app_description,
app_author=app_author,
app_keywords=app_keywords,
Expand Down
4 changes: 4 additions & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e

pytest tests
1 change: 1 addition & 0 deletions tests/test_createapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_imports(self) -> None:
if os.path.exists(outdir):
shutil.rmtree(outdir)
do_create_python_app(
"my-app",
app_description="MyAppTest description",
app_author="Firstname Lastname",
app_keywords="myapp test",
Expand Down

0 comments on commit c7ae87a

Please sign in to comment.