|
| 1 | +# Powershell script for ask-cli post-new hook for Python |
| 2 | +# Script Usage: post_new_hook.ps1 <SKILL_NAME> <DO_DEBUG> |
| 3 | + |
| 4 | +# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters. |
| 5 | +# DO_DEBUG is boolean value for debug logging |
| 6 | + |
| 7 | +# Run this script one level outside of the skill root folder |
| 8 | + |
| 9 | +# The script does the following: |
| 10 | +# - Create a '.venv' directory under <SKILL_NAME> folder |
| 11 | +# - Find if python3 is installed. |
| 12 | +# - If yes, try creating virtual environment using built-in venv |
| 13 | +# - If that fails, install virtualenv and create virtualenv |
| 14 | +# - If no, install virtualenv and create virtualenv |
| 15 | +# - If virtual environment is created, use container pip to install dependencies from ${SOURCE_DIR}/requirements.txt |
| 16 | +# - Provide message on activation script location and additional dependencies |
| 17 | + |
| 18 | +param( |
| 19 | + [string] $SKILL_NAME, |
| 20 | + [bool] $DO_DEBUG = $False |
| 21 | +) |
| 22 | + |
| 23 | +if ($DO_DEBUG) { |
| 24 | + Write-Output "###########################" |
| 25 | + Write-Output "###### post-new hook ######" |
| 26 | + Write-Output "###########################" |
| 27 | +} |
| 28 | + |
| 29 | +function create_env () { |
| 30 | + # Check for Python3 installation |
| 31 | + python -V | Select-String -Pattern "Python 3." 2>&1 | Out-Null |
| 32 | + if ($?) { |
| 33 | + python -m venv $ENV_LOC 2>&1 | Out-Null |
| 34 | + if ($?) { |
| 35 | + return $true |
| 36 | + } |
| 37 | + } |
| 38 | + return create_using_virtualenv |
| 39 | +} |
| 40 | + |
| 41 | +function create_using_virtualenv() { |
| 42 | + # Check for virtualenv installation or install |
| 43 | + python -m pip install virtualenv 2>&1 | Out-Null |
| 44 | + if ($?) { |
| 45 | + python -m virtualenv $ENV_LOC 2>&1 | Out-Null |
| 46 | + if ($?) { |
| 47 | + return $true |
| 48 | + } |
| 49 | + } |
| 50 | + if ($DO_DEBUG) { |
| 51 | + Write-Output "There was a problem installing virtualenv" |
| 52 | + } |
| 53 | + return $false |
| 54 | +} |
| 55 | + |
| 56 | +function install_dependencies($PARAM_SOURCE_DIR) { |
| 57 | + # Install dependencies at lambda/py/requirements.txt |
| 58 | + $PYTHON_PATH = $ENV_LOC + "\Scripts\python" |
| 59 | + $REQUIREMENTS_PATH = $SKILL_NAME + "\" + $PARAM_SOURCE_DIR + "\requirements.txt" |
| 60 | + $CMD = "$PYTHON_PATH -m pip -q install -r $REQUIREMENTS_PATH" |
| 61 | + return Invoke-Expression $CMD 2>&1 | Out-Null |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +$SKILL_ENV_NAME = "skill_env" |
| 66 | +$ENV_LOC = $SKILL_NAME + "\.venv\" + $SKILL_ENV_NAME |
| 67 | +if (create_env) { |
| 68 | + $SKILL_FILE_PATH = $SKILL_NAME + "\skill.json" |
| 69 | + $ALL_SOURCE_DIRS = Get-Content -Path $SKILL_FILE_PATH | select-string -Pattern "sourceDir" -CaseSensitive |
| 70 | + if ($DO_DEBUG) { |
| 71 | + Write-Output "Created $SKILL_ENV_NAME virtualenv at $ENV_LOC" |
| 72 | + Write-Output "###########################" |
| 73 | + Write-Output "Installing dependencies based on sourceDir" |
| 74 | + } |
| 75 | + Foreach ($SOURCE_DIR in $ALL_SOURCE_DIRS) { |
| 76 | + $FILTER_SOURCE_DIR = $SOURCE_DIR -replace "`"", "" -replace "\s", "" -replace ",","" -replace "sourceDir:", "" |
| 77 | + if (-Not (install_dependencies $FILTER_SOURCE_DIR)) { |
| 78 | + if ($DO_DEBUG) { |
| 79 | + Write-Output "Codebase ($FILTER_SOURCE_DIR) built successfully." |
| 80 | + } |
| 81 | + } else { |
| 82 | + if ($DO_DEBUG) { |
| 83 | + Write-Output "There was a problem installing dependencies for ($FILTER_SOURCE_DIR)." |
| 84 | + } |
| 85 | + exit 1 |
| 86 | + } |
| 87 | + } |
| 88 | + if ($DO_DEBUG) { |
| 89 | + Write-Output "###########################" |
| 90 | + Write-Output "Activate the environment before installing any other dependencies by running 'source $ENV_LOC/bin/activate'" |
| 91 | + } |
| 92 | + exit 0 |
| 93 | +} else { |
| 94 | + exit 1 |
| 95 | +} |
0 commit comments