Skip to content

Commit

Permalink
feat(scripts): add install script for Windows platforms (#43)
Browse files Browse the repository at this point in the history
* feat(scripts): add Powershell install script

* chore(repo): add Windows installation README instructions
  • Loading branch information
ekkolon authored Feb 5, 2024
1 parent ce4aed6 commit be5a226
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,18 @@ Licensa is a powerful CLI tool designed for seamless source code license managem

As of today, Licensa boasts compatibility with over **65 file types**, making it a versatile and comprehensive solution for license management in diverse coding environments.

## Installation

### Windows

Before running the installation script, ensure that your PowerShell execution policy allows (remote) script execution. You can set the execution policy using the following command:

```powershell
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
```

Executing the following script will walk you through the installation process (you may need to run this command from an elevated shell):

```powershell
Invoke-Expression -Command (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/ekkolon/licensa/main/scripts/install.ps1" -UseBasicParsing).Content
```
98 changes: 98 additions & 0 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

# Logging helper
function Write-Log {
param(
[string]$Level,
[string]$Message
)

switch ($Level) {
"info" { $ForegroundColor = "Blue" }
"success" { $ForegroundColor = "Green" }
"warn" { $ForegroundColor = "Yellow" }
"error" { $ForegroundColor = "Red" }
default { $ForegroundColor = "White" }
}

$TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$FormattedLevel = "[$($Level.ToUpper())]"
$FormattedMessage = "$TimeStamp $FormattedLevel $Message"

Write-Host $FormattedMessage -ForegroundColor $ForegroundColor
}

# GitHub repository details
$version = "0.1.0"
$binName = "licensa"
$releaseTag = "v${version}"

$assetNameUnpacked = "${binName}-${releaseTag}-x86_64-windows"
$assetNameZip = "${assetNameUnpacked}.zip"

$releaseDownloadUrl = "https://github.com/ekkolon/licensa/releases/download/$releaseTag/$assetNameZip"

# Target download directory
$userDownloadsFolder = Join-Path $env:USERPROFILE "Downloads"
$downloadPath = Join-Path $userDownloadsFolder $assetNameZip
# TODO: handle errors for `downloadPath`

# Prompt the user for confirmation
$confirmation = Read-Host "This script is set to download and install Licensa CLI ${releaseTag}.
Are you sure you want to proceed? (Type 'Y' for Yes, 'N' for No)"

if ($confirmation -ne 'Y' -or $confirmation -ne 'y') {
Write-Log -Level "info" -Message "The installation process has been canceled"
Exit
}

# Download binary from GitHub release
Write-Log -Level "info" -Message "Downloading assets from GitHub ..."
Invoke-WebRequest -Uri $releaseDownloadUrl -OutFile $downloadPath
Write-Log -Level "success" -Message "Download succeded!"

# Unpack the binary
Write-Log -Level "info" -Message "Unpacking..."
Expand-Archive -Path $downloadPath -DestinationPath $userDownloadsFolder -Force
Write-Log -Level "success" -Message "Successfully unpacked ${assetNameZip}"

Write-Log -Level "info" -Message "Installing..."
$programFiles = $env:Programfiles
$destinationPath = Join-Path $programFiles "Licensa"

# Check if the program folder exists
if (-not (Test-Path -Path $destinationPath -PathType Container)) {
# Create the program folder if it doesn't exist
New-Item -ItemType Directory -Path $destinationPath | Out-Null
Write-Log -Level "info" -Message "Licensa CLI directory created at: $destinationPath"
}
else {
# TODO: improve error handling
# TODO: Suggest override
Write-Host "Licensa CLI is already installed at: $destinationPath"
Exit
}

$sourceFolder = Join-Path $downloadsFolder $assetNameUnpacked

# Move unpacked source to destination
Get-ChildItem -Path $sourceFolder | Move-Item -Destination $destinationPath

# Remove downloaded zip and unpacked directory
Remove-Item -Path $sourceFolder
Remove-Item -Path $downloadPath

# Confirm adding Licensa CLI to system variables
$confirmation = Read-Host "Would you like to add the Licensa CLI to your system variables? [y/N]"
if ($confirmation -eq 'Y' -or $confirmation -eq 'y') {
# Add directory path to system environment variable
$envPath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::User)
$newPath = $envPath + ";" + $destinationPath
[System.Environment]::SetEnvironmentVariable('Path', $newPath, [System.EnvironmentVariableTarget]::User)
Write-Log -Level "info" -Message "Added Licensa CLI to system variables"
}

Write-Log -Level "success" -Message "Licensa CLI has been installed successfully"

exit


0 comments on commit be5a226

Please sign in to comment.