Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current dir option #46

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"advanced_contribution_utilities": ["basic contribution [DEFAULT]", "advanced contribution (dynamic versioning, GitHub actions, pre-commit hooks)"],
"__contrib_utilities": "{% if cookiecutter.advanced_contribution_utilities == 'basic contribution [DEFAULT]' -%}basic{% else -%}advanced{% endif -%}",
"license": ["MIT license [DEFAULT]", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"],
"use_current_directory": ["no [DEFAULT]", "yes"],
"__license": "{{ cookiecutter.license.replace( ' [DEFAULT]', '') }}",
"__contrib_type_modulename": "{{ cookiecutter.__autora_contribution_type.replace(' [DEFAULT]', '').lower().replace(' ', '_').replace('-', '_') }}",
"__contrib_subtype_modulename": "{{ cookiecutter.__contrib_subtype.replace(' [DEFAULT]', '').lower().replace(' ', '_').replace('-', '_') }}",
Expand Down
25 changes: 11 additions & 14 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,33 @@

PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)


def move_files_to_parent_folder(folder_path):
parent_folder = os.path.dirname(folder_path)
# Move all files and directories to the parent folder
for item in os.listdir(folder_path):
source_path = os.path.join(folder_path, item)
target_path = os.path.join(parent_folder, item)
shutil.move(source_path, target_path)

for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)

if os.path.isfile(file_path):
shutil.move(file_path, parent_folder)
elif os.path.isdir(file_path):
move_files_to_parent_folder(file_path)

# Check if the folder is empty and remove it
if not os.listdir(folder_path):
os.rmdir(folder_path)



if __name__ == '__main__':
# Move file to upper level if no contribution subtype
if 'not_applicable' == '{{ cookiecutter.__contrib_subtype }}':
shutil.move(
'src/autora/{{ cookiecutter.__contrib_type_modulename }}/not_applicable/{{ cookiecutter.__contrib_name_modulename }}',
'src/autora/{{ cookiecutter.__contrib_type_modulename }}/{{ cookiecutter.__contrib_name_modulename }}',)
'src/autora/{{ cookiecutter.__contrib_type_modulename }}/{{ cookiecutter.__contrib_name_modulename }}', )
os.rmdir('src/autora/{{ cookiecutter.__contrib_type_modulename }}/not_applicable')
# Remove .pre-commit-config.yaml file if not using pre-commit hooks
if 'basic' == '{{ cookiecutter.__contrib_utilities }}':
os.remove('.pre-commit-config.yaml')
# Remove .github directory if not using github actions
if 'basic' == '{{ cookiecutter.__contrib_utilities }}':
shutil.rmtree('.github')





if '{{ cookiecutter.use_current_directory }}' == 'yes':
move_files_to_parent_folder(PROJECT_DIRECTORY)
15 changes: 15 additions & 0 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import re
import sys
import os


MODULE_REGEX = r'^[_a-zA-Z][_a-zA-Z0-9]+$'

module_name = '{{ cookiecutter.__contrib_name_modulename}}'

is_current_directory = '{{ cookiecutter.use_current_directory }}'
parent_dir = os.path.basename(os.path.realpath(os.path.curdir))
slug = '{{ cookiecutter.__project_slug }}'


if not re.match(MODULE_REGEX, module_name):
print('ERROR: The project name (%s) is not a valid Python module name.' % module_name)

#Exit to cancel project
sys.exit(1)

if is_current_directory == 'yes':
if parent_dir != slug:
print(f'ERROR: When using `use_current_directory`, '
f'the parent directory `{parent_dir} '
f'must match the contribution name `{slug}`')
sys.exit(1)


Loading