Skip to content
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
43 changes: 43 additions & 0 deletions helper/invokeCommand.helper.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# SET MY INVOKE COMMAND ALIAS
#
# Allows calling constitely InvokeHelper with the module tag
# Need to define a variable called $MODULE_INVOKATION_TAG
#
# Sample:
# $MODULE_INVOKATION_TAG = "SfHelperModule"

$moduleRootPath = $PSScriptRoot | Split-Path -Parent
$MODULE_NAME = (Get-ChildItem -Path $moduleRootPath -Filter *.psd1 | Select-Object -First 1).BaseName
$MODULE_INVOKATION_TAG = "$($MODULE_NAME)Module"

function Set-MyInvokeCommandAlias{
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory,Position=0)][string]$Alias,
[Parameter(Mandatory,Position=1)][string]$Command
)

# throw if MODULE_INVOKATION_TAG is not set or is "MyModuleModule"
if (-not $MODULE_INVOKATION_TAG) {
throw "MODULE_INVOKATION_TAG is not set. Please set it to a unique value before calling Set-MyInvokeCommandAlias."
}

if ($PSCmdlet.ShouldProcess("InvokeCommandAliasList", ("Add Command Alias [{0}] = [{1}]" -f $Alias, $Command))) {
InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG
}
}

function Reset-MyInvokeCommandAlias{
[CmdletBinding(SupportsShouldProcess)]

Check warning

Code scanning / PSScriptAnalyzer

'Reset-MyInvokeCommandAlias' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue. Warning

'Reset-MyInvokeCommandAlias' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue.
param()

# throw if MODULE_INVOKATION_TAG is not set or is "MyModuleModule"
if (-not $MODULE_INVOKATION_TAG) {
throw "MODULE_INVOKATION_TAG is not set. Please set it to a unique value before calling Set-MyInvokeCommandAlias."
}
InvokeHelper\Reset-InvokeCommandAlias -Tag $MODULE_INVOKATION_TAG
}

# Reset all aliases for this module on each refresh
Reset-MyInvokeCommandAlias
153 changes: 106 additions & 47 deletions public/Install-SalesforceClient.ps1
Original file line number Diff line number Diff line change
@@ -1,87 +1,146 @@

Set-MyInvokeCommandAlias -Alias GetUserEmail -Command "gh api user --jq '.email'"
Set-MyInvokeCommandAlias -Alias GetNpmVersion -Command "npm --version"
Set-MyInvokeCommandAlias -Alias SalesforceCliInstall -Command "npm install @salesforce/cli --global"
Set-MyInvokeCommandAlias -Alias GetSalesforceCliVersion -Command "sf --version"
Set-MyInvokeCommandAlias -Alias SalesforceCliSetConfig -Command "sf config set --global target-org {email} --json"
Set-MyInvokeCommandAlias -Alias GetUserEmail -Command '$env:GITHUB_USER ? "$($env:GITHUB_USER)@github.com" : $null'
Set-MyInvokeCommandAlias -Alias SfCliInstall -Command "npm install @salesforce/cli --global"
Set-MyInvokeCommandAlias -Alias SfCliVersion -Command "sf --version"
Set-MyInvokeCommandAlias -Alias SfCliDisplay -COmmand "sf org display --target-org={email} --json"
Set-MyInvokeCommandAlias -Alias SfCliGetConfig -Command "sf config get target-org --json"
Set-MyInvokeCommandAlias -Alias SfCliSetConfig -Command "sf config set --global target-org {email} --json"

# Set-MyInvokeCommandAlias -Alias SalesforceCliLogin -Command "sf org login device"
Set-MyInvokeCommandAlias -Alias SalesforceCliDisplay -COmmand "sf org display --json"
Set-MyInvokeCommandAlias -Alias SalesforceCliGetConfig -Command "sf config get target-org --json"

function Install-SalesforceClient{
[CmdletBinding(SupportsShouldProcess)]
param()
param(
[Parameter()][string]$Email
)

# check that npm is install in the system
$result = Invoke-MyCommand -Command GetNpmVersion
if($null -eq $result){
throw "0. npm not installed. Please install npm to allow the installation of Salesforce Cli through npm."
} else {
# 0. NPM setup check
if(-not (Test-NpmSetup)){return}

# 1. Sf Install
if(-not (Invoke-SfInstall)){return}

# 2. Test Sf Login
if(-not (Test-SfLogin)){return}

# 3. Sf Config
if(-not(Invoke-SfConfig -Email:$Email)){return}

} Export-ModuleMember -Function Install-SalesforceClient

function Test-NpmSetup{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Test-NpmSetup' does not have a help comment. Note

The cmdlet 'Test-NpmSetup' does not have a help comment.
[CmdletBinding()]

$result = Invoke-MyCommand -Command GetNpmVersion -ErrorAction SilentlyContinue

if($result){
"0. npm installed. version: $result" | Write-ToConsole -Color "Green"
return $true
} else {
throw "0. npm not installed. Please install npm to allow the installation of Salesforce Cli through npm."
return $false
}
} Export-ModuleMember -Function Test-NpmSetup

# Check for Salesforce CLI installed and install if not
$sfversion = Invoke-MyCommand -Command GetSalesforceCliVersion -ErrorAction SilentlyContinue
function Invoke-SfInstall{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-SfInstall' does not have a help comment. Note

The cmdlet 'Invoke-SfInstall' does not have a help comment.
[CmdletBinding(SupportsShouldProcess)]
param()

# Check and Install for Salesforce CLI installed and install if not
$sfversion = Invoke-MyCommand -Command SfCliVersion -ErrorAction SilentlyContinue
if($null -eq $sfversion){
if ($PSCmdlet.ShouldProcess("Target", "Operation")) {
"Installing Salesforce CLI using npm..." | Write-MyHost
Invoke-MyCommand -Command SalesforceCliInstall
Invoke-MyCommand -Command SfCliInstall
}
}

# Sf installation check
$sfversion = Invoke-MyCommand -Command GetSalesforceCliVersion -ErrorAction SilentlyContinue
$sfversion = Invoke-MyCommand -Command SfCliVersion -ErrorAction SilentlyContinue
if($null -eq $sfversion){
"1. Salesforce Cli installation failed. Run ""npm install @salesforce/cli --global"" to install manually!!!" | Write-MyError
return
return $false

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-SfInstall' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Invoke-SfInstall' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
} else{
"1. Salesforce CLI installed. Version: $sfversion} " | Write-ToConsole -Color "Green"
return $true

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-SfInstall' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Invoke-SfInstall' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
}
} Export-ModuleMember -Function Invoke-SfInstall

function Test-SfLogin{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Test-SfLogin' does not have a help comment. Note

The cmdlet 'Test-SfLogin' does not have a help comment.
[CmdletBinding(SupportsShouldProcess)]

Check warning

Code scanning / PSScriptAnalyzer

'Test-SfLogin' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue. Warning

'Test-SfLogin' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue.
param(
[Parameter()][string]$Email
)

$email = Resolve-Email -Email $Email

# Sf logging
$result = Invoke-MyCommand -Command SalesforceCliDisplay -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
$result = Invoke-MyCommand -Command SfCliDisplay -Parameters @{ email = $email } -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
$result | Write-MyVerbose
$user = $result.result
if($user.connectedStatus -eq "Connected"){
"2. Salesforce CLI already connected with user $($user.username)" | Write-ToConsole -Color "Green"
return $user.username
} else {
"2. Run the following command to loging to Salesforce CLI: ""sf org login device""" | Write-ToConsole -Color "Magenta"
return
return $null
}

# # Logging in to Salesforce CLI
# "Loging in to Salesforce CLI..." | Write-MyHost
# if ($PSCmdlet.ShouldProcess("Target", "Operation")) {
# Invoke-MyCommand -Command SalesforceCliLogin
# }
# Invoke-MyCommand -Command SalesforceCliLogin
# }
} Export-ModuleMember -Function Test-SfLogin

function Invoke-SfConfig{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-SfConfig' does not have a help comment. Note

The cmdlet 'Invoke-SfConfig' does not have a help comment.
[CmdletBinding(SupportsShouldProcess)]

Check warning

Code scanning / PSScriptAnalyzer

'Invoke-SfConfig' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue. Warning

'Invoke-SfConfig' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue.
param(
[Parameter()][string]$Email
)

$result = Invoke-MyCommand -Command SalesforceCliGetConfig -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
$Email = Resolve-Email -Email $Email

"Using email $Email to configure Salesforce CLI" | Write-MyVerbose

$result = Invoke-MyCommand -Command SfCliGetConfig -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue
$result | Write-MyVerbose
$config = $result.result
if($config.success){
"3. Salesforce CLI configured with user $($config.value)" | Write-ToConsole -Color "Green"
if($result.result.value -eq $Email){
"3. Salesforce CLI already configured with user $($config.value)" | Write-ToConsole -Color "Green"
return $true
}

# Configure Salesforce CLI
"Configuring Salesforce CLI ..." | Write-MyVerbose

$result = Invoke-MyCommand -Command SfCliSetConfig -Parameters @{ email = $Email } | ConvertFrom-Json -Depth 10 -ErrorAction SilentlyContinue
if($result.result.successes.value -eq $Email){
"3. Salesforce CLI configured with user $($Email) " | Write-ToConsole -Color "Green"
return $Email
} else {
"3. Salesforce CLI not configured. Run the following command to configure Salesforce CLI: ""sf config set --global target-org {email}""" | Write-ToConsole -Color "Magenta"
"3. Salesforce CLI configuration failed with email $Email. Logging to Salesforce Cli and try again. " | Write-MyError
$result | ConvertTo-Json -Depth 10 | Write-MyVerbose
return
}

# # Configure Salesforce CLI
# $email = Invoke-MyCommand -Command GetUserEmail
# "Configuring Salesforce CLI ..." | Write-MyHost
# if($email){
# $json = Invoke-MyCommand -Command SalesforceCliSetConfig -Parameters @{ email = $email }
# $result = $json | ConvertFrom-Json -ErrorAction SilentlyContinue
# if($user.result.successes.success){
# "Configured Salesforce CLI with user $($user.result.successes.value) " | Write-MyHost
# } else {
# "Salesforce CLI configuration failed with email $email" | Write-MyError
# return
# }
# } else {
# "Can not find your github user. Run the following command to configure your Salesforce CLI: sf config set --global target-org {email}" | Write-MyHost
# "Salesforce CLI installed but pending to be configured." | Write-MyWarning
# return
# }
} Export-ModuleMember -Function Invoke-SfConfig

function Resolve-Email{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Resolve-Email' does not have a help comment. Note

The cmdlet 'Resolve-Email' does not have a help comment.
[CmdletBinding(SupportsShouldProcess)]

Check warning

Code scanning / PSScriptAnalyzer

'Resolve-Email' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue. Warning

'Resolve-Email' has the ShouldProcess attribute but does not call ShouldProcess/ShouldContinue.
param(
[Parameter()][string]$Email
)

if(-not [string]::IsNullOrEmpty($Email)){
"Resolving email [$Email] from parameter" | Write-MyVerbose
return $Email
}

$Email = Invoke-MyCommand -Command GetUserEmail -ErrorAction SilentlyContinue
"Resolved email [$Email] from github" | Write-MyVerbose

if ($null -eq $Email){
throw "Unable to resolve user email. Please provide email as parameter or set proper gh cli credentials and user github profile email." | Write-MyError
}

return $Email

} Export-ModuleMember -Function Install-SalesforceClient
} Export-ModuleMember -Function Resolve-Email
Loading