Skip to content

Commit

Permalink
feat: improve error handlings for built-in code build flows (node-npm…
Browse files Browse the repository at this point in the history
…, python-pip and java-mvn)
  • Loading branch information
RonWang committed Nov 6, 2019
1 parent a4cf370 commit 3f76185
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 221 deletions.
152 changes: 124 additions & 28 deletions lib/builtins/build-flows/java-mvn/build.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,137 @@
# Powershell script for ask-cli code build for java-mvn flow.
# Script Usage: build.ps1 <OUT_FILE> <DO_DEBUG>

# OUT_FILE is the file name for the output (required)
# DO_DEBUG is boolean value for debug logging
#requires -version 3
<#
.SYNOPSIS
PowerShell script for ask-cli's Java-mvn code building flow.
.DESCRIPTION
This is the PowerShell version of the build script, for building the AWS Lambda deployable skill code that is written in Java language. This script is only run by the ask-cli whenever a 'pom.xml' file is found alongside the skill code. The dependencies are installed and packaged using 'mvn'.
.EXAMPLE
build.ps1 archive.zip
This example showcases how to run the build script, to create an AWS Lambda deployable package called 'archive.zip'.
.EXAMPLE
build.ps1 archive.zip $true
This example showcases how to run the previous example, with additional debug information.
#>
#----------------[ Parameters ]----------------------------------------------------
param(
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Name for the AWS Lambda deployable archive")]
[ValidateNotNullOrEmpty()]
[string]
$script:OutFile = "upload.zip",

# Run this script whenever a pom.xml is defined

param(
[string] $OUT_FILE,
[bool] $DO_DEBUG = $False,
# Provide additional debug information during script run
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Enable verbose output")]
[bool]
$script:Verbose = $false
)

function install_dependencies () {
Invoke-Expression "mvn clean org.apache.maven.plugins:maven-assembly-plugin:2.6:assembly -DdescriptorId=jar-with-dependencies package" 2>&1 | Out-Null
$EXEC_RESULT = $?
return $EXEC_RESULT
#----------------[ Declarations ]----------------------------------------------------
$ErrorActionPreference = "Stop"

#----------------[ Functions ]----------------------------------------------------
function Show-Log() {
<#
.SYNOPSIS
Function to log information/error messages to output
.EXAMPLE
Show-Log "Test"
This will log the message as an Information, only if the script is run in Verbose mode
Show-Log "Test" "Error"
This will log the message as an Error and STOP the script execution
#>
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$Message,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet('Info','Error')]
[string]
$Severity = 'Info'
)

begin {}
process {
if ($Severity -eq 'Info') {
if ($Verbose) {
Write-Host $Message
}
} else {
Write-Error $Message
}
}
end {}
}

if ($DO_DEBUG) {
Write-Output "###########################"
Write-Output "####### Build Code ########"
Write-Output "###########################"
function Build-SkillArtifacts() {
<#
.SYNOPSIS
Function to compile the skill project, aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable jar
#>
[CmdletBinding()]
[OutputType([bool])]
param()

begin {
Show-Log "Building skill artifacts based on pom.xml."
}
process {
$DepCmd = "mvn clean org.apache.maven.plugins:maven-assembly-plugin:2.6:assembly -DdescriptorId=jar-with-dependencies package"
if (-not $Verbose) {
$DepCmd += " --quiet"
}
Invoke-Expression -Command $DepCmd | Out-String | Tee-Object -Variable 'result'
if(!($LASTEXITCODE -eq 0)) {
Show-Log "$result `n Failed to build the skill artifacts in the project." "Error"
}
return $true
}
end {}
}

if (install_dependencies) {
Move-Item -Path ./target/*jar-with-dependencies.jar -Destination $OUT_FILE
if ($DO_DEBUG) {
Write-Output "Codebase built successfully."
function Rename-Archive() {
<#
.SYNOPSIS
Function to rename the build archive into cli format.
#>
[CmdletBinding()]
[OutputType([bool])]
param()

begin {
Show-Log "Renaming build archive to $OutFile."
}
} else {
if ($DO_DEBUG) {
Write-Output "There was a problem installing the dependencies."
process {
Move-Item -Path ./target/*jar-with-dependencies.jar -Destination $OutFile
return $?
}
exit 1
end {}

}

#----------------[ Main Execution ]----------------------------------------------------

Show-Log "###########################"
Show-Log "####### Build Code ########"
Show-Log "###########################"

if (Build-SkillArtifacts) {
Show-Log "Skill artifacts built successfully."
}
if ($DO_DEBUG) {
Write-Output "###########################"

if (-Not (Rename-Archive)) {
Show-Log "Failed to rename build archive to $OutFile" "Error"
}

Show-Log "###########################"
Show-Log "Codebase built successfully"
Show-Log "###########################"

exit 0
8 changes: 4 additions & 4 deletions lib/builtins/build-flows/java-mvn/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ main() {
echo "###########################"
fi

if ! install_dependencies; then
display_stderr "Failed to install the dependencies in the project."
if ! build_skill_dependencies; then
display_stderr "Failed to build the skill artifacts in the project."
exit 1
else
[[ $DO_DEBUG == true ]] && display_debug "Dependencies built successfully."
Expand All @@ -45,8 +45,8 @@ display_debug() {
echo "[Debug] $1" >&2
}

install_dependencies() {
[[ $DO_DEBUG == true ]] && display_debug "Installing Java dependencies based on the pom.xml."
build_skill_dependencies() {
[[ $DO_DEBUG == true ]] && display_debug "Building skill artifacts based on the pom.xml."
[[ $DO_DEBUG == false ]] && QQ=true # decide if quiet flag will be appended

mvn clean org.apache.maven.plugins:maven-assembly-plugin:2.6:assembly -DdescriptorId=jar-with-dependencies package ${QQ:+--quiet}
Expand Down
152 changes: 124 additions & 28 deletions lib/builtins/build-flows/nodejs-npm/build.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,137 @@
# Powershell script for ask-cli code build for nodejs-npm flow.
# Script Usage: build.ps1 <OUT_FILE> <DO_DEBUG>

# OUT_FILE is the file name for the output (required)
# DO_DEBUG is boolean value for debug logging
#requires -version 3
<#
.SYNOPSIS
PowerShell script for ask-cli's Nodejs-npm code building flow.
.DESCRIPTION
This is the PowerShell version of the build script, for building the AWS Lambda deployable skill code that is written in JS language. This script is only run by the ask-cli whenever a 'package.json' file is found alongside the skill code. The dependencies are installed using 'npm', and are packaged using 'zip'.
.EXAMPLE
build.ps1 archive.zip
This example showcases how to run the build script, to create an AWS Lambda deployable package called 'archive.zip'.
.EXAMPLE
build.ps1 archive.zip $true
This example showcases how to run the previous example, with additional debug information.
#>
#----------------[ Parameters ]----------------------------------------------------
param(
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Name for the AWS Lambda deployable archive")]
[ValidateNotNullOrEmpty()]
[string]
$script:OutFile = "upload.zip",

# Run this script whenever a package.json is defined

param(
[string] $OUT_FILE,
[bool] $DO_DEBUG = $False
# Provide additional debug information during script run
[Parameter(Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Enable verbose output")]
[bool]
$script:Verbose = $false
)

function install_dependencies () {
Invoke-Expression "npm install" 2>&1 | Out-Null
$EXEC_RESULT = $?
return $EXEC_RESULT
#----------------[ Declarations ]----------------------------------------------------
$ErrorActionPreference = "Stop"

#----------------[ Functions ]----------------------------------------------------
function Show-Log() {
<#
.SYNOPSIS
Function to log information/error messages to output
.EXAMPLE
Show-Log "Test"
This will log the message as an Information, only if the script is run in Verbose mode
Show-Log "Test" "Error"
This will log the message as an Error and STOP the script execution
#>
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$Message,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet('Info','Error')]
[string]
$Severity = 'Info'
)

begin {}
process {
if ($Severity -eq 'Info') {
if ($Verbose) {
Write-Host $Message
}
} else {
Write-Error $Message
}
}
end {}
}

if ($DO_DEBUG) {
Write-Output "###########################"
Write-Output "####### Build Code ########"
Write-Output "###########################"
function Install-Dependencies() {
<#
.SYNOPSIS
Function to install dependencies in package.json from npm.
#>
[CmdletBinding()]
[OutputType([bool])]
param()

begin {
Show-Log "Installing skill dependencies based on package.json."
}
process {
$DepCmd = "npm install --production"
if (-not $Verbose) {
$DepCmd += " --quiet"
}
Invoke-Expression -Command $DepCmd
if(!($LASTEXITCODE -eq 0)) {
Show-Log "Failed to install the dependencies in the project" "Error"
}
return $true
}
end {}
}

if (install_dependencies) {
Compress-Archive -Path ./* -DestinationPath $OUT_FILE
if ($DO_DEBUG) {
Write-Output "Codebase built successfully."
function Compress-Dependencies() {
<#
.SYNOPSIS
Function to compress source code and dependencies for lambda deployment.
#>
[CmdletBinding()]
[OutputType([bool])]
param()

begin {
Show-Log "Zipping source files and dependencies to $OutFile."
}
} else {
if ($DO_DEBUG) {
Write-Output "There was a problem installing the dependencies."
process {
Compress-Archive -Path ./* -DestinationPath $OutFile
return $?
}
exit 1
end {}

}

#----------------[ Main Execution ]----------------------------------------------------

Show-Log "###########################"
Show-Log "####### Build Code ########"
Show-Log "###########################"

if (Install-Dependencies) {
Show-Log "npm install dependencies successfully."
}
if ($DO_DEBUG) {
Write-Output "###########################"

if (-Not (Compress-Dependencies)) {
Show-Log "Failed to zip the artifacts to $OutFile" "Error"
}

Show-Log "###########################"
Show-Log "Codebase built successfully"
Show-Log "###########################"

exit 0
Loading

0 comments on commit 3f76185

Please sign in to comment.