Skip to content

Commit 21a1602

Browse files
Merge pull request #2 from pbheemag/add_hook_scripts
Added skill.json, config and hook scripts
2 parents 0c11686 + 40eed56 commit 21a1602

6 files changed

Lines changed: 366 additions & 26 deletions

File tree

.ask/config

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"deploy_settings": {
3+
"default": {
4+
"skill_id": "",
5+
"resources": {
6+
"lambda": [
7+
{
8+
"alexaUsage": [
9+
"custom/default"
10+
],
11+
"handler": "lambda_function.lambda_handler",
12+
"runtime": "python3.6"
13+
}
14+
]
15+
},
16+
"was_cloned": false,
17+
"merge": {}
18+
}
19+
}
20+
}

hooks/post_new_hook.ps1

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

hooks/post_new_hook.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
# Shell script for ask-cli post-new hook for Python
3+
# Script Usage: post_new_hook.sh <SKILL_NAME> <DO_DEBUG>
4+
5+
# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters.
6+
# DO_DEBUG is boolean value for debug logging
7+
8+
# Run this script one level outside of the skill root folder
9+
10+
# The script does the following:
11+
# - Create a '.venv' directory under <SKILL_NAME> folder
12+
# - Find if python3 is installed.
13+
# - If yes, try creating virtual environment using built-in venv
14+
# - If that fails, install virtualenv and create virtualenv
15+
# - If no, install virtualenv and create virtualenv
16+
# - If virtual environment is created, use container pip to install dependencies from ${SOURCE_DIR}/requirements.txt
17+
# - Provide message on activation script location and additional dependencies
18+
19+
create_env () {
20+
# Check for Python3 installation
21+
if command -v python3 &> /dev/null; then
22+
PYTHON=python3
23+
# Use Python3's venv script to create virtualenv.
24+
if $PYTHON -m venv "$ENV_LOC"; then
25+
echo "Using Python3's venv script"
26+
return 0
27+
else
28+
# No venv script present (< Py 3.3). Install using virtualenv
29+
return create_using_virtualenv $PYTHON
30+
fi
31+
else
32+
# Python2 environment. Install using virtualenv
33+
PYTHON=python
34+
return create_using_virtualenv $PYTHON
35+
fi
36+
return 1
37+
}
38+
39+
create_using_virtualenv () {
40+
# Check for virtualenv installation or install
41+
if $1 -m pip install virtualenv; then
42+
echo "Using virtualenv library"
43+
# Try creating env
44+
if $1 -m virtualenv "$ENV_LOC"; then
45+
return 0
46+
else
47+
echo "There was a problem creating virtualenv"
48+
return 1
49+
fi
50+
else
51+
echo "There was a problem installing virtualenv"
52+
return 1
53+
fi
54+
}
55+
56+
install_dependencies() {
57+
# Install dependencies at lambda/py/requirements.txt
58+
return $("$ENV_LOC"/bin/python -m pip -q install -r "$SKILL_DIR"/"$1"/requirements.txt)
59+
}
60+
61+
SKILL_NAME=$1
62+
DO_DEBUG=${2:-false}
63+
SKILL_DIR=$SKILL_NAME
64+
SKILL_ENV_NAME="skill_env"
65+
ENV_LOC="$SKILL_DIR/.venv/$SKILL_ENV_NAME"
66+
67+
if ! $DO_DEBUG ; then
68+
exec > /dev/null 2>&1
69+
fi
70+
71+
echo "###########################"
72+
echo "###### post-new hook ######"
73+
echo "###########################"
74+
echo "Creating virtualenv for $SKILL_NAME"
75+
mkdir "$SKILL_NAME/.venv"
76+
if create_env; then
77+
echo "Created $SKILL_ENV_NAME virtualenv at $ENV_LOC"
78+
echo "###########################"
79+
echo "Installing dependencies based on sourceDir"
80+
grep "sourceDir" "$SKILL_NAME/skill.json" | cut -d: -f2 | sed 's/"//g' | sed 's/,//g' | while read -r SOURCE_DIR; do
81+
if install_dependencies $SOURCE_DIR; then
82+
echo "Codebase ($SOURCE_DIR) built successfully."
83+
else
84+
echo "There was a problem installing dependencies for ($SOURCE_DIR)."
85+
exit 1
86+
fi
87+
done
88+
echo "###########################"
89+
echo "Activate the environment before installing any other dependencies by running 'source $ENV_LOC/bin/activate'"
90+
exit 0
91+
else
92+
exit 1
93+
fi

hooks/pre_deploy_hook.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Powershell script for ask-cli pre-deploy hook for Python
2+
# Script Usage: pre_deploy_hook.ps1 <SKILL_NAME> <DO_DEBUG> <TARGET>
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+
# TARGET is the deploy TARGET provided to the CLI. (eg: all, skill, lambda etc.)
7+
8+
# Run this script under the skill root folder
9+
10+
# The script does the following:
11+
# - Create a temporary 'lambda_upload' directories under each SOURCE_DIR folder
12+
# - Copy the contents of '<SKILL_NAME>/SOURCE_DIR' folder into '<SKILL_NAME>/SOURCE_DIR/lambda_upload'
13+
# - Copy the contents of site packages in $VIRTUALENV created in <SKILL_NAME>/.venv/ folder
14+
# - Update the location of this 'lambda_upload' folder to skill.json for zip and upload
15+
16+
param(
17+
[string] $SKILL_NAME,
18+
[bool] $DO_DEBUG = $False,
19+
[string] $TARGET = "all"
20+
)
21+
22+
if ($DO_DEBUG) {
23+
Write-Output "###########################"
24+
Write-Output "##### pre-deploy hook #####"
25+
Write-Output "###########################"
26+
}
27+
28+
if ($TARGET -eq "all" -Or $TARGET -eq "lambda") {
29+
$ALL_SOURCE_DIRS = Get-Content -Path "skill.json" | select-string -Pattern "sourceDir" -CaseSensitive
30+
Foreach ($SOURCE_DIR in $ALL_SOURCE_DIRS) {
31+
# Step 1: Decide source path and upload path
32+
$FILTER_SOURCE_DIR = $SOURCE_DIR -replace "`"", "" -replace "\s", "" -replace ",","" -replace "sourceDir:", ""
33+
if ($FILTER_SOURCE_DIR.endsWith("/lambda_upload")) {
34+
$UPLOAD_DIR_PATH = $FILTER_SOURCE_DIR
35+
$CODE_PATH = $FILTER_SOURCE_DIR.replace("/lambda_upload", "")
36+
} else {
37+
$UPLOAD_DIR_PATH = $FILTER_SOURCE_DIR + "/lambda_upload"
38+
$CODE_PATH = $FILTER_SOURCE_DIR
39+
}
40+
# Step 2: Create empty lambda_upload folder
41+
Remove-Item -Recurse -Force $UPLOAD_DIR_PATH -ErrorAction Ignore
42+
New-Item -Force $UPLOAD_DIR_PATH -ItemType "directory" 2>&1 | Out-Null
43+
44+
# Step 3: Copy source code in sourceDir to lambda_upload
45+
$EXCLUDE_PATH = Resolve-Path -Path ((pwd).Path + "/" + $UPLOAD_DIR_PATH)
46+
robocopy $CODE_PATH $UPLOAD_DIR_PATH /s /e /ndl /XD $EXCLUDE_PATH 2>&1 | Out-Null
47+
48+
# Step 4: Find virtual environment site packages, copy contents to lambda_upload
49+
$SITE = $(.venv\skill_env\Scripts\python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
50+
Copy-Item "$SITE\*" -Destination $UPLOAD_DIR_PATH -Recurse
51+
52+
# Step 5: Update the "manifest.apis.custom.endpoint.sourceDir" value in skill.json if necessary
53+
if (!$FILTER_SOURCE_DIR.endsWith("/lambda_upload")) {
54+
$RAW_SOURCE_DIR_LINE = "`"sourceDir`": `"$FILTER_SOURCE_DIR`""
55+
$NEW_SOURCE_DIR_LINE = "`"sourceDir`": `"$UPLOAD_DIR_PATH`""
56+
(Get-Content "skill.json").replace($RAW_SOURCE_DIR_LINE, $NEW_SOURCE_DIR_LINE) | Set-Content "skill.json"
57+
}
58+
}
59+
60+
if ($DO_DEBUG) {
61+
Write-Output "###########################"
62+
}
63+
64+
exit 0
65+
}

hooks/pre_deploy_hook.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
# Shell script for ask-cli pre-deploy hook for Python
3+
# Script Usage: pre_deploy_hook.sh <SKILL_NAME> <DO_DEBUG> <TARGET>
4+
5+
# SKILL_NAME is the preformatted name passed from the CLI, after removing special characters.
6+
# DO_DEBUG is boolean value for debug logging
7+
# TARGET is the deploy TARGET provided to the CLI. (eg: all, skill, lambda etc.)
8+
9+
# Run this script under skill root folder
10+
11+
# The script does the following:
12+
# - Create a temporary 'lambda_upload' directories under each SOURCE_DIR folder
13+
# - Copy the contents of '<SKILL_NAME>/SOURCE_DIR' folder into '<SKILL_NAME>/SOURCE_DIR/lambda_upload'
14+
# - Copy the contents of site packages in $VIRTUALENV created in <SKILL_NAME>/.venv/ folder
15+
# - Update the location of this 'lambda_upload' folder to skill.json for zip and upload
16+
17+
SKILL_NAME=$1
18+
DO_DEBUG=${2:-false}
19+
TARGET=${3:-"all"}
20+
SKILL_ENV_NAME="skill_env"
21+
22+
if ! $DO_DEBUG ; then
23+
exec > /dev/null 2>&1
24+
fi
25+
26+
echo "###########################"
27+
echo "##### pre-deploy hook #####"
28+
echo "###########################"
29+
30+
if [[ $TARGET == "all" || $TARGET == "lambda" ]]; then
31+
grep "sourceDir" ./skill.json | cut -d: -f2 | sed 's/"//g' | sed 's/,//g' | while read -r SOURCE_DIR; do
32+
# Step 1: Decide source path and upload path
33+
if [[ $SOURCE_DIR == */lambda_upload ]]; then
34+
ADJUSTED_SOURCE_DIR=${SOURCE_DIR%"/lambda_upload"}
35+
UPLOAD_DIR=$SOURCE_DIR
36+
else
37+
ADJUSTED_SOURCE_DIR=$SOURCE_DIR
38+
UPLOAD_DIR="$SOURCE_DIR/lambda_upload"
39+
fi
40+
41+
# Step 2: Create empty lambda_upload folder
42+
echo "Checking for lambda_upload folder existence in sourceDir $ADJUSTED_SOURCE_DIR"
43+
rm -rf $UPLOAD_DIR
44+
mkdir $UPLOAD_DIR
45+
46+
# Step 3: Copy source code in sourceDir to lambda_upload
47+
echo "Copying source code in $SKILL_NAME/$ADJUSTED_SOURCE_DIR folder to $SKILL_NAME/$UPLOAD_DIR"
48+
rsync -avzq --exclude '*lambda_upload' $ADJUSTED_SOURCE_DIR/* $UPLOAD_DIR
49+
50+
# Step 4: Find virtual environment site packages, copy contents to lambda_upload
51+
echo "Copying dependencies installed in $SKILL_NAME/.venv/$SKILL_ENV_NAME to $SKILL_NAME/$UPLOAD_DIR"
52+
SITE=$(.venv/$SKILL_ENV_NAME/bin/python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
53+
cp -r $SITE/* $UPLOAD_DIR
54+
55+
# Step 4: Update the "manifest.apis.custom.endpoint.sourceDir" value in skill.json if necessary
56+
if ! [[ $SOURCE_DIR == */lambda_upload ]]; then
57+
echo "Updating sourceDir to point to lambda_upload folder in skill.json"
58+
RAW_SOURCE_DIR_LINE="\"sourceDir\": \"$SOURCE_DIR\""
59+
NEW_SOURCE_DIR_LINE="\"sourceDir\": \"$UPLOAD_DIR\""
60+
sed -in "s#$RAW_SOURCE_DIR_LINE#$NEW_SOURCE_DIR_LINE#g" ./skill.json
61+
fi
62+
done
63+
echo "###########################"
64+
fi
65+
66+
exit 0

0 commit comments

Comments
 (0)