diff --git a/README.md b/README.md index a3fbb75..0d751a0 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,5 @@ Start using Rangolio today to create beautiful, professional portfolio websites Ofcourse I could create instructions here, but what better tool to showcase ability to create content than the tool itself! Head over to [instrctions](https://barunes.io/blog/975fda0e-6f2b-4f7b-9268-2d75dea61b0f) on prepping your github account to setup rangolio. + + diff --git a/backend/apimanager/apps.py b/backend/apimanager/apps.py index 5092f92..4acf213 100644 --- a/backend/apimanager/apps.py +++ b/backend/apimanager/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class ApimanagerConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'apimanager' + + def ready(self): + import apimanager.check_updates diff --git a/backend/apimanager/check_updates.py b/backend/apimanager/check_updates.py new file mode 100644 index 0000000..5688b15 --- /dev/null +++ b/backend/apimanager/check_updates.py @@ -0,0 +1,69 @@ +from django.conf import settings +import shutil + +from .utilities import ( + copy_content, + run_git_command, + run_npm_command, + run_django_command +) + +from .publish_methods_github import ( + git_update_viewable_ui +) + +from .dialogue_box import ( + draw_dialogue_box +) + + +def update_rangolio(rangolio_location): + run_git_command('git_pull', rangolio_location) + + # Install dependencies and build viewable-ui + run_npm_command('npm_install', f'{rangolio_location}/frontend/viewable-ui') + + # Update server + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server.old') + + run_npm_command('npm_run', f'{rangolio_location}/frontend/viewable-ui', ['build:server']) + git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server.old', 'server', False) + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server.old', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server') + + + # Update ghpages + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages.old') + + run_npm_command('npm_run', f'{rangolio_location}/frontend/viewable-ui', ['build:ghpages']) + git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages.old', 'ghpages', False) + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages.old', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages') + + # Update editor-ui + run_npm_command('npm_run', f'{rangolio_location}/frontend/editable-ui', ['build']) + run_django_command('collectstatic', f'{rangolio_location}/backend') + copy_content( + f'{rangolio_location}/backend/static/index.html', + f'{rangolio_location}/backend/templates/index.html', + 'file', + 'remove_and_copy' + ) + run_django_command('makemigrations', f'{rangolio_location}/backend') + run_django_command('migrate', f'{rangolio_location}/backend') + + +print ('Checking for updates') +rangolio_location = settings.DEPLOY_CONFIG["RANGOLIO_LOCATION"] + +run_git_command('git_fetch_origin', rangolio_location) +updates = run_git_command('git_diff', rangolio_location, ['origin/development']) +print (updates) +if updates['subprocess_output'] and updates['subprocess_returncode'] == 0: + update_confirmation = draw_dialogue_box('Software Update', 'Would you like to update rangolio?', 'confirmation') + if update_confirmation: + update_rangolio(rangolio_location) +else: + print('No updates') \ No newline at end of file diff --git a/backend/apimanager/publish_methods.py b/backend/apimanager/publish_methods.py index 74174b0..dd4d235 100644 --- a/backend/apimanager/publish_methods.py +++ b/backend/apimanager/publish_methods.py @@ -37,20 +37,6 @@ def server_deploy(): def github_deploy(): print("Deploying via github") - git_commands = { - "git_init": ['git', 'init'], - "git_add": ['git', 'add', '.'], - "git_pull": ['git', 'pull'], - "git_config_email": ['git', 'config', '--local', 'user.email'], - "git_config_name": ['git', 'config', '--local', 'user.name'], - "git_commit": ['git', 'commit', '-m', 'Update website'], - "git_branch": ['git', 'branch', '-m', 'main'], - "git_get_origin_url": ['git', 'remote', 'get-url', 'origin'], - "git_set_origin_url": ['git', 'remote', 'set-url', 'origin'], - "git_add_url": ['git', 'remote', 'add', 'origin'], - "git_push": ['git', 'push', '-u', 'origin', 'main'], - "git_clone": ['git', 'clone'] - } deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages' copy_data_and_html('ghpages') @@ -62,10 +48,10 @@ def github_deploy(): 'confirmation' ) if existing_repo: - git_existing_repo_setup(deploy_location, git_commands) + git_existing_repo_setup(deploy_location) else: - github_init(deploy_location, git_commands) - github_pages_deploy(deploy_location, git_commands) + github_init(deploy_location) + github_pages_deploy(deploy_location) return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK} except Exception as e: print(f"An error occurred: {str(e)}") @@ -73,7 +59,7 @@ def github_deploy(): else: try: - github_pages_deploy(deploy_location, git_commands) + github_pages_deploy(deploy_location) return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK} except Exception as e: print(f"An error occurred: {str(e)}") diff --git a/backend/apimanager/publish_methods_github.py b/backend/apimanager/publish_methods_github.py index 9e41719..45a19da 100644 --- a/backend/apimanager/publish_methods_github.py +++ b/backend/apimanager/publish_methods_github.py @@ -1,5 +1,4 @@ from django.conf import settings -import os import shutil import subprocess import urllib.parse @@ -14,10 +13,10 @@ from .dialogue_box import ( ) -def github_init(deploy_location, git_commands): - user_details_defined = git_check_user_details(deploy_location, git_commands) +def github_init(deploy_location): + user_details_defined = git_check_user_details(deploy_location) if not user_details_defined: - git_set_user_details(deploy_location, git_commands) + git_set_user_details(deploy_location) username, password = git_get_username_password() remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git' @@ -30,7 +29,7 @@ def github_init(deploy_location, git_commands): run_git_command('git_push', deploy_location) -def git_set_user_details(deploy_location, git_commands): +def git_set_user_details(deploy_location): while True: email = draw_dialogue_box('Github Deploy', 'Enter your github email', 'textbox') name = draw_dialogue_box('Github Deploy', 'Enter your name', 'textbox') @@ -46,7 +45,7 @@ def git_set_user_details(deploy_location, git_commands): run_git_command('git_config_name', deploy_location, [name]) -def git_existing_repo_setup(deploy_location, git_commands): +def git_existing_repo_setup(deploy_location): while True: repo_url = draw_dialogue_box('Github Deploy', 'Enter Repository URL', 'textbox') @@ -65,35 +64,36 @@ def git_existing_repo_setup(deploy_location, git_commands): git_update_viewable_ui(deploy_location, dist_folder_name) -def git_check_user_details(deploy_location, git_commands): +def git_check_user_details(deploy_location): config_email = run_git_command('git_config_name', deploy_location) config_name = run_git_command('git_config_email', deploy_location) - if not config_email or not config_name: + if not config_email['subprocess_output'] or not config_name['subprocess_output']: return False return True -def git_update_viewable_ui(deploy_location, dist_folder_name, build_frontend=False): +def git_update_viewable_ui(deploy_location, dist_folder_name, copy_index_and_asset=True): + # deploy_location = server.old ; dist_folder_name = server + + # server.old -> server.old.temp shutil.move(deploy_location, f'{deploy_location}.temp') - if build_frontend: - subprocess.run(["npm", 'run', 'build:ghpages'], cwd=settings.DEPLOY_CONFIG["VIEWABLE_UI_LOCATION"], check=True, - text=True, capture_output=True) - + # server -> server.old shutil.move(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/{dist_folder_name}', f'{deploy_location}') - copy_content( - f'{deploy_location}.temp/index.html', - deploy_location, - 'file', - 'remove_and_copy' - ) - copy_content( - f'{deploy_location}.temp/assets', - f'{deploy_location}/assets', - 'folder', - 'remove_and_copy' - ) + if copy_index_and_asset: + copy_content( + f'{deploy_location}.temp/index.html', + deploy_location, + 'file', + 'remove_and_copy' + ) + copy_content( + f'{deploy_location}.temp/assets', + f'{deploy_location}/assets', + 'folder', + 'remove_and_copy' + ) copy_content( f'{deploy_location}.temp/data', f'{deploy_location}/data', @@ -131,10 +131,10 @@ def git_get_username_password(): return username, password -def github_pages_deploy(deploy_location, git_commands): - user_details_defined = git_check_user_details(deploy_location, git_commands) +def github_pages_deploy(deploy_location): + user_details_defined = git_check_user_details(deploy_location) if not user_details_defined: - git_set_user_details(deploy_location, git_commands) + git_set_user_details(deploy_location) user_email = run_git_command("git_config_email", deploy_location) user_name = run_git_command("git_config_name", deploy_location) @@ -142,7 +142,7 @@ def github_pages_deploy(deploy_location, git_commands): deploy_confirmation = draw_dialogue_box( 'Github Deploy', - f'Deploying as {user_name} with email {user_email} to {repo_name}', + f'Deploying as {user_name.subprocess_output} with email {user_email.subprocess_output} to {repo_name.subprocess_output}', 'confirmation' ) @@ -150,8 +150,8 @@ def github_pages_deploy(deploy_location, git_commands): run_git_command('git_pull', deploy_location) print("completed git pull") origin_url = run_git_command('git_get_origin_url', deploy_location) - print("Got origin as "+str(origin_url)) - parsed_url = urllib.parse.urlparse(origin_url) + print("Got origin as "+str(origin_url.subprocess_output)) + parsed_url = urllib.parse.urlparse(origin_url.subprocess_output) print(parsed_url) if not '@' in parsed_url.netloc: username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox') diff --git a/backend/apimanager/utilities.py b/backend/apimanager/utilities.py index 8c11e6b..751e3fa 100644 --- a/backend/apimanager/utilities.py +++ b/backend/apimanager/utilities.py @@ -21,12 +21,11 @@ def copy_content(source, destination, content_type, copy_type='merge'): print(f'Error occurred: {e}') -def run_git_command(operation, deploy_location, parameter=None): - if not parameter: - parameter=[] +def run_git_command(operation, command_location, parameter=None): git_commands = { "git_init": ['git', 'init'], "git_add": ['git', 'add', '.'], + "git_fetch_origin": ['git', 'fetch', 'origin'], "git_pull": ['git', 'pull'], "git_config_email": ['git', 'config', '--local', 'user.email'], "git_config_name": ['git', 'config', '--local', 'user.name'], @@ -36,12 +35,38 @@ def run_git_command(operation, deploy_location, parameter=None): "git_set_origin_url": ['git', 'remote', 'set-url', 'origin'], "git_add_url": ['git', 'remote', 'add', 'origin'], "git_push": ['git', 'push', '-u', 'origin', 'main'], - "git_clone": ['git', 'clone'] + "git_clone": ['git', 'clone'], + "git_diff": ['git', 'diff'] } - try: - subprocess_operation = subprocess.run(git_commands[operation] + parameter, cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess_output = subprocess_operation.stdout.strip() - except subprocess.CalledProcessError as e: - subprocess_output = None + return run_command(operation, command_location, git_commands, parameter) - return subprocess_output + +def run_npm_command(operation, command_location, parameter=None): + npm_commands = { + "npm_install": ['npm', 'install', '-i'], + "npm_run": ['npm', 'run'], + } + return run_command(operation, command_location, npm_commands, parameter) + + +def run_django_command(operation, command_location, parameter=None): + npm_commands = { + "collectstatic": ['python', 'manage.py', 'collectstatic', '--no-input'], + "makemigrations": ['python', 'manage.py', 'makemigrations'], + "migrate": ['python', 'manage.py', 'migrate'], + } + return run_command(operation, command_location, npm_commands, parameter) + + +def run_command(operation, command_location, command_map_list, parameter=None): + if not parameter: + parameter=[] + + try: + subprocess_operation = subprocess.run(command_map_list[operation] + parameter, cwd=command_location, check=True, text=True, capture_output=True) + subprocess_output = subprocess_operation.stdout.strip() + subprocess_returncode = subprocess_operation.returncode + subprocess_result = {'subprocess_output': subprocess_output, 'subprocess_returncode': subprocess_returncode} + return subprocess_result + except subprocess.CalledProcessError as e: + return None diff --git a/backend/backend/settings.py b/backend/backend/settings.py index 99891d4..c7ed9e9 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -42,6 +42,7 @@ INSTALLED_APPS = [ ] DEPLOY_CONFIG = { + "RANGOLIO_LOCATION": os.path.join(BASE_DIR, '..'), "VIEWABLE_UI_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui'), "DEPLOY_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui/dist'), "EDITOR_HTML_LOCATION": os.path.join(BASE_DIR, 'deploy/html'),