Skip to content

Instantly share code, notes, and snippets.

@noahpeltier
Created October 28, 2019 14:19
Show Gist options
  • Save noahpeltier/5196eefbba3981d1d9603580124ca8b2 to your computer and use it in GitHub Desktop.
Save noahpeltier/5196eefbba3981d1d9603580124ca8b2 to your computer and use it in GitHub Desktop.
A module I am working on to allow for installing modules to a project folder to be packaged for distribution.
function PSEnv {
[CmdletBinding()]
param (
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('Install', 'Init', 'Remove')]
[String]$Operation,
[parameter(ValueFromPipelineByPropertyName)]
[string]$module_name
)
$modules_path = (Join-Path $pwd -ChildPath 'Modules')
$init_content = @'
$modules_path = (Join-Path $psscriptroot -ChildPath 'Modules')
Get-ChildItem -Path ".\Modules\" | ForEach-Object {
Import-Module $_.FullName
}
'@
switch ($Operation) {
'Install' {
if (Test-Path .\Init.ps1) {
if (Find-Module $module_name) {
Write-host ('Installing {0} to Virtual Env...' -f $module_name) -ForegroundColor Cyan
try {
Save-Module $module_name -Path $modules_path
}
catch {
Write-host "[ERROR] " -ForegroundColor Red -NoNewline
Write-host $error[0].Exception.Message
}
}
break
}
else {
write-host "[WARN] " -ForegroundColor Yellow -NoNewline
Write-host "Init.ps1 not foud in this directory. Run PSenv init to initialize.`n" -ForegroundColor Cyan
}
break
}
'Init' {
if (!(Test-Path $modules_path) -or (Test-Path .\Init.ps1)) {
mkdir $modules_path
Set-Content -Path 'Init.ps1' -Value $init_content
Write-host "`n[DONE] " -ForegroundColor green -NoNewline
Write-host "Modules folder and init.ps1 created! start installing dependancies."
}
break
}
'Remove' {
Remove-Item -Path (Join-Path $modules_path -ChildPath $module_name) -Recurse -Force
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment