Skip to content

Instantly share code, notes, and snippets.

@prasannavl
Created September 26, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasannavl/7e1bb52300b54decf87e to your computer and use it in GitHub Desktop.
Save prasannavl/7e1bb52300b54decf87e to your computer and use it in GitHub Desktop.
Updates a module manifest automatically with all the files in the powershell scripts in the module directory as scripts to process.
function Update-AutoModuleManifest
{
<#
.SYNOPSIS
Updates a module manifest automatically with all the script files
in the module directory as scripts to process.
.DESCRIPTION
Looks for the given module's path, and then uses New-ModuleManifest to
create a manifest file which includes all the "*.ps1" files in the
folder as "ScriptsToProcess".
Very helpful in having a module as drop points for "ps1" scripts,
but without slowing down the startup of powershell due to dot sourcing
files one by one, and using it in a module instead.
.EXAMPLE
Update-AutoModuleManifest -Name Utils
.EXAMPLE
Update-AutoModuleManifest -Name ISEUtils -UseProfileLocation -DontReimportModule
.EXAMPLE
Update-AutoModuleManifest -ModulePath "C:\Modules\TestModule"
#>
[CmdletBinding()]
param(
[Parameter(ParameterSetName = "Name", Position=0, Mandatory=$true)]
[string] $Name,
[Parameter(ParameterSetName = "Path", Position=0, Mandatory=$true)]
[string] $ModulePath,
[Parameter(ParameterSetName = "PrintUsage", Position=0, Mandatory=$false)]
[Switch]
[bool] $PrintUsage = $false,
[Parameter(ParameterSetName = "Name")]
[Parameter(ParameterSetName = "Path")]
[string] $Author = $env:USERNAME,
[Parameter(ParameterSetName = "Path")]
[Parameter(ParameterSetName = "Name")]
[string] $Version = "1.0.0",
[Parameter(ParameterSetName = "Name")]
[Switch]
[bool] $UseProfileLocation = $false,
[Parameter(ParameterSetName = "Name")]
[Switch]
[bool] $DontReimportModule = $false
)
process {
$moduleName = $Name
$path = ""
switch ($PSCmdlet.ParameterSetName)
{
"Name"
{
$m = (Get-Module -Name $moduleName)
if (-not $m)
{
$wrapUp = $false
if ($UseProfileLocation)
{
$profileFile = $PROFILE.ToString()
$location = Join-Path "$(Split-Path -Parent $profileFile)" (Join-Path "Modules" "$Name")
if (Test-Path $location)
{
$path = $location
break;
}
else { $wrapUp = $true }
} else {
$wrapUp = $true
}
if ($wrapUp)
{
throw "Module $Name not found"
}
}
$path = (Split-Path -Parent $m.Path)
break;
}
"Path"
{
$path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($ModulePath)
if (-not (Test-Path $path -PathType Container))
{ throw "Invalid path" }
$moduleName = (Split-Path -Leaf $path)
break;
}
default
{
help Update-AutoModuleManifest
return;
}
}
$scriptsToProcess = @(ls -Path $path -Filter "*.ps1" | % { $_.Name })
$outPath = "$($path)\$($moduleName).psd1"
New-ModuleManifest -ModuleVersion $Version -Author $Author -ScriptsToProcess $scriptsToProcess -Path $outPath
if ($PSCmdlet.ParameterSetName -eq "Name" -and -not $DontReimportModule)
{
if (Get-Module -Name $moduleName)
{
Remove-Module $moduleName
}
Import-Module $moduleName
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment