Public/New-PyModule.ps1
|
function New-PyModule { <# .SYNOPSIS Creates a Python module file. .DESCRIPTION Creates a UTF-8 Python module in the requested directory. The module name must be a valid Python identifier; an optional .py suffix is accepted. .PARAMETER Name Python module name, with or without the .py suffix. .PARAMETER Path Directory in which the module is created. .PARAMETER Force Overwrites an existing module file. .EXAMPLE mkmod utilities -Path src #> [CmdletBinding(SupportsShouldProcess = $true)] [Alias('mkmod')] [OutputType([System.IO.FileInfo])] param( [Parameter(Mandatory = $true, Position = 0)] [string]$Name, [Parameter()] [string]$Path = (Get-Location).Path, [switch]$Force ) $moduleName = if ($Name.EndsWith('.py', [System.StringComparison]::OrdinalIgnoreCase)) { $Name.Substring(0, $Name.Length - 3) } else { $Name } $moduleName = ConvertTo-DtPythonIdentifier -Name $moduleName $modulePath = Resolve-DtContainedPath -BasePath $Path -ChildName "$moduleName.py" if (-not $PSCmdlet.ShouldProcess($modulePath, 'Create Python module')) { return } $content = '"""' + $moduleName + ' module."""' Set-DtFileContent -Path $modulePath -Content $content -Force:$Force -Confirm:$false | Out-Null if (Test-Path -LiteralPath $modulePath -PathType Leaf) { Get-Item -LiteralPath $modulePath } } |