Public/Get-ProjectName.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
function Get-ProjectName { <# .SYNOPSIS Get the name for this project .FUNCTIONALITY CI/CD .DESCRIPTION Get the name for this project Evaluates based on the following scenarios: * Subfolder with the same name as the current folder * Subfolder with a <subfolder-name>.psd1 file in it * Current folder with a <currentfolder-name>.psd1 file in it + Subfolder called "Source" or "src" (not case-sensitive) with a psd1 file in it If no suitable project name is discovered, the function will return the name of the root folder as the project name. .PARAMETER Path Path to project root. Defaults to the current working path .NOTES We assume you are in the project root, for several of the fallback options .EXAMPLE Get-ProjectName .LINK https://github.com/RamblingCookieMonster/BuildHelpers .LINK Get-BuildVariable .LINK Set-BuildEnvironment .LINK about_BuildHelpers #> [cmdletbinding()] param( $Path = $PWD.Path, [validatescript({ if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) { throw "Could not find command at GitPath [$_]" } $true })] $GitPath = 'git' ) if(!$PSboundParameters.ContainsKey('GitPath')) { $GitPath = (Get-Command $GitPath -ErrorAction SilentlyContinue)[0].Path } $WeCanGit = ( (Test-Path $( Join-Path $Path .git )) -and (Get-Command $GitPath -ErrorAction SilentlyContinue) ) $Path = ( Resolve-Path $Path ).Path $CurrentFolder = Split-Path $Path -Leaf $ExpectedPath = Join-Path -Path $Path -ChildPath $CurrentFolder if(Test-Path $ExpectedPath) { $result = $CurrentFolder } else { # Look for properly organized modules $ProjectPaths = Get-ChildItem $Path -Directory | Where-Object { Test-Path $(Join-Path $_.FullName "$($_.name).psd1") } | Select-Object -ExpandProperty Fullname if( @($ProjectPaths).Count -gt 1 ) { Write-Warning "Found more than one project path via subfolders with psd1 files" $result = Split-Path $ProjectPaths -Leaf } elseif( @($ProjectPaths).Count -eq 1 ) { $result = Split-Path $ProjectPaths -Leaf } # PSD1 in Source or Src folder elseif( Get-Item "$Path\S*rc*\*.psd1" -OutVariable SourceManifests) { If ( $SourceManifests.Count -gt 1 ) { Write-Warning "Found more than one project manifest in the Source folder" } $result = $SourceManifests.BaseName } #PSD1 in root of project - ick, but happens. elseif( Test-Path "$ExpectedPath.psd1" ) { $result = $CurrentFolder } #PSD1 in root of project but name doesn't match #very ick or just an icky time in Azure Pipelines elseif ( $PSDs = Get-ChildItem -Path $Path "*.psd1" ) { if ($PSDs.count -gt 1) { Write-Warning "Found more than one project manifest in the root folder" } $result = $PSDs.BaseName } #Last ditch, are you in Azure Pipelines or another CI that checks into a folder unrelated to the project? #let's try some git elseif ( $WeCanGit ) { $result = (Invoke-Git -Path $Path -GitPath $GitPath -Arguments "remote get-url origin").Split('/')[-1] -replace "\.git","" } else { Write-Warning "Could not find a project from $($Path); defaulting to project root for name" $result = Split-Path $Path -Leaf } } if ($env:APPVEYOR_PROJECT_NAME -and $env:APPVEYOR_JOB_ID -and ($result -like $env:APPVEYOR_PROJECT_NAME)) { $env:APPVEYOR_PROJECT_NAME } else { $result } } |