Created
September 20, 2019 21:41
-
-
Save jrich523/49e09ec3de99786260197ca90409cefb to your computer and use it in GitHub Desktop.
CDR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Global Values | |
$pathSplitRegex = "[\\|/]" | |
<# | |
.Synopsis | |
Short description | |
.DESCRIPTION | |
Long description | |
.EXAMPLE | |
Example of how to use this cmdlet | |
.EXAMPLE | |
Another example of how to use this cmdlet | |
#> | |
function find-project { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, Position=0)] | |
$repo, | |
[Parameter(Position=1)] | |
$org='*', | |
[Parameter(Position=2)] | |
$platform='*' | |
) | |
if($env:REPOROOT -and -not (test-path $env:REPOROOT)){ | |
Write-Error "Env Variable REPOROOT missing or invalid!" -ErrorAction stop | |
} | |
################################ | |
# Should be pulled from Env var's | |
$defaultOrg = "myOrg" | |
$defaultPlatform = "Corp" | |
################################ | |
$projects = Get-Project -root $env:REPOROOT | %{ [pscustomobject]@{Path= $_; Parts = $_ -split $pathSplitRegex} } | |
$searchData = @{ | |
repo=@("*$repo*",$repo) | |
org=if($org -eq "*"){ @($org, $defaultOrg) }else{@("*$org*",$org, $defaultOrg)} | |
platform=if($platform -eq "*"){ @($platform, $defaultPlatform) }else{ @("*$platform*", $platform, $defaultPlatform) } | |
} | |
$smallset = $projects | |
$totalCnt = $projects.Count | |
Write-Verbose "Total Project: $totalCnt" | |
for($r=0;$r -lt $searchData.repo.Count;$r++) | |
{ | |
$projectMatches = $projects | |
Write-Verbose "ProjectMatches reset to contain $($projectMatches.count)" | |
$projectMatches = $projectMatches | ? { $_.parts[-1] -like $searchData.repo[$r] } | |
Write-Verbose "Repo $r($($searchData.repo[$r])) - $(@($projectMatches).count)" | |
for($o=0;$o -lt $searchData.org.Count;$o++) | |
{ | |
$projectMatches = $projectMatches | ? { $_.parts[-2] -like $searchData.org[$o] } | |
Write-Verbose "Org $o($($searchData.org[$o])) - $(@($projectMatches).count)" | |
for($p=0;$p -lt $searchData.platform.Count;$p++) | |
{ | |
$projectMatches = $projectMatches | ? { $_.parts[-3] -like $searchData.platform[$p] } | |
Write-Verbose "Platform $p($($searchData.platform[$p])) - $(@($projectMatches).count)" | |
if($projectMatches -and $projectMatches -isnot [array]) | |
{ | |
## single match | |
return $projectMatches.path | |
} | |
if($projectMatches -and $projectMatches.count -lt $smallset.count) | |
{ | |
Write-Verbose "SmallSet update" | |
$smallset = $projectMatches | |
} | |
} | |
} | |
} | |
## this will only happen if there are still more than 1 option | |
write-host "unable to find best match, closest set:`n$($smallset.path -join "`n")" | |
} | |
function Get-Project { | |
param( | |
$root, | |
$depth = 5) | |
$projectTypes = @(".git",".vscode") | |
## is this folder a project? | |
if(gci $root -Directory -Name -Force | ? { $projectTypes -contains $_}) | |
{ | |
return $root | |
} | |
if($depth -gt 0){ | |
$folders = gci -Path $root -Directory | |
foreach($folder in $folders) | |
{ | |
Get-Project $folder.fullname $depth-1 | |
} | |
} | |
} | |
function Set-ProjectLocation { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, Position=0)] | |
$repo, | |
[Parameter(Position=1)] | |
$org, | |
[Parameter(Position=2)] | |
$platform | |
) | |
find-project @psboundparameters | Set-Location | |
} | |
Set-Alias -Name cdr -Value Set-ProjectLocation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment