Public/New-PyPackage.ps1
|
function New-PyPackage { <# .SYNOPSIS Creates a Python package directory. .DESCRIPTION Creates a valid Python package directory with a UTF-8 __init__.py file. .PARAMETER Name Python package name. It must be a valid Python identifier. .PARAMETER Path Parent directory in which the package is created. .PARAMETER Force Overwrites an existing __init__.py file. .EXAMPLE mkpkg widgets -Path src #> [CmdletBinding(SupportsShouldProcess = $true)] [Alias('mkpkg')] [OutputType([System.IO.DirectoryInfo])] param( [Parameter(Mandatory = $true, Position = 0)] [string]$Name, [Parameter()] [string]$Path = (Get-Location).Path, [switch]$Force ) $packageName = ConvertTo-DtPythonIdentifier -Name $Name $packageRoot = Resolve-DtContainedPath -BasePath $Path -ChildName $packageName if (-not $PSCmdlet.ShouldProcess($packageRoot, 'Create Python package')) { return } New-DtDirectory -Path $packageRoot -Confirm:$false | Out-Null $initPath = Join-Path -Path $packageRoot -ChildPath '__init__.py' $content = '"""' + $packageName + ' package."""' Set-DtFileContent -Path $initPath -Content $content -Force:$Force -Confirm:$false | Out-Null Get-Item -LiteralPath $packageRoot } |