diff --git a/project.json b/project.json index 08e84bf..be7ace8 100644 --- a/project.json +++ b/project.json @@ -1,7 +1,7 @@ { "ProjectName": "ModuleTools", "Description": "ModuleTools is a versatile, standalone PowerShell module builder. Create anything from simple to robust modules with ease. Built for CICD and Automation.", - "Version": "1.4.1", + "Version": "1.5.0", "copyResourcesToModuleRoot": false, "Manifest": { "Author": "Manjunath Beli", diff --git a/src/private/GetLocalModulePath.ps1 b/src/private/GetLocalModulePath.ps1 new file mode 100644 index 0000000..6712f8c --- /dev/null +++ b/src/private/GetLocalModulePath.ps1 @@ -0,0 +1,24 @@ +function Get-LocalModulePath { + $sep = [IO.Path]::PathSeparator + + $ModulePaths = $env:PSModulePath -split $sep | ForEach-Object { $_.Trim() } | Select-Object -Unique + + if ($IsWindows) { + $MatchPattern = '\\Documents\\PowerShell\\Modules' + $Result = $ModulePaths | Where-Object { $_ -match $MatchPattern } | Select-Object -First 1 + if ($Result -and (Test-Path $Result)) { + return $Result + } else { + throw "No windows module path matching $MatchPattern found" + } + } else { + # For Mac and Linux + $MatchPattern = '/\.local/share/powershell/Modules$' + $Result = $ModulePaths | Where-Object { $_ -match $MatchPattern } | Select-Object -First 1 + if ($Result -and (Test-Path $Result)) { + return $Result + } else { + throw "No macOS/Linux module path matching $MatchPattern found in PSModulePath." + } + } +} \ No newline at end of file diff --git a/src/public/PublishMTLocal.ps1 b/src/public/PublishMTLocal.ps1 new file mode 100644 index 0000000..e9f7418 --- /dev/null +++ b/src/public/PublishMTLocal.ps1 @@ -0,0 +1,30 @@ +function Publish-MTLocal { + [CmdletBinding()] + param( + [string]$ModuleDirectoryPath + ) + + if ($ModuleDirectoryPath) { + if (-not (Test-Path $ModuleDirectoryPath -PathType Container)) { + New-Item $ModuleDirectoryPath -ItemType Directory -Force | Out-Null + } + } else { + $ModuleDirectoryPath = Get-LocalModulePath + } + + $ProjectInfo = Get-MTProjectInfo + + # Ensure module is locally built and ready + if (-not (Test-Path $ProjectInfo.OutputModuleDir)) { + trhow 'Dist folder is empty, build the module before running publish command' + } + + # Cleanup old files + $OldModule = Join-Path -Path $ModuleDirectoryPath -ChildPath $ProjectInfo.ProjectName + if (Test-Path -Path $OldModule) { + Remove-Item -Recurse $OldModule -Force + } + + # Copy New Files + Copy-Item -Path $ProjectInfo.OutputModuleDir -Destination $ModuleDirectoryPath -Recurse -ErrorAction Stop +} \ No newline at end of file