Last active
August 30, 2019 16:26
-
-
Save jrich523/59c08696df120439d5805d0e2c973a7d to your computer and use it in GitHub Desktop.
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
<# | |
.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(-not (test-path $env:REPOROOT)){ | |
Write-Error "Env Variable REPOROOT missing or invalid!" -ErrorAction stop | |
} | |
################################ | |
# Should be pulled from Env var's | |
$defaultOrg = "aws-team" | |
$defaultPlatform = "SSTK" | |
################################ | |
$projects = Get-Projects | %{ [pscustomobject]@{Path= $_; Parts = $_ -split '\\'} } | |
$searchData = @{ | |
repo=@("*$repo*",$repo) | |
org=@($org, $defaultOrg) | |
platform=@($platform, $defaultPlatform) | |
} | |
$smallset = $projects | |
$totalCnt = $projects.Count | |
Write-Verbose "Total Project: $totalCnt" | |
for($p=0;$p -lt $searchData.platform.Count;$p++) | |
{ | |
$projectMatches = $projects | |
$projectMatches = $projectMatches | ? { $_.parts[-3] -like $searchData.platform[$p] } | |
Write-Verbose "Platform $p - $($projectMatches.count)" | |
for($o=0;$o -lt $searchData.org.Count;$o++) | |
{ | |
$projectMatches = $projectMatches | ? { $_.parts[-2] -like $searchData.org[$o] } | |
Write-Verbose "Org $o - $($projectMatches.count)" | |
for($r=0;$r -lt $searchData.repo.Count;$r++) | |
{ | |
$projectMatches = $projectMatches | ? { $_.parts[-1] -like $searchData.repo[$r] } | |
Write-Verbose "repo $r($($searchData.repo[$r])) - $($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-Projects { | |
param( | |
$root='D:\Repos', | |
$depth = 5) | |
$projectTypes = @(".git",".vscode") | |
## is this folder a project? | |
if(ls $root -Directory -Name -Force | ? { $projectTypes -contains $_}) | |
{ | |
return $root | |
} | |
if($depth -gt 0){ | |
$folders = ls -Path $root -Directory | |
foreach($folder in $folders) | |
{ | |
find-Projects $folder.fullname $depth-1 | |
} | |
} | |
} | |
function Set-ProjectLocation { | |
param( | |
#man | |
$repo, | |
$org, | |
$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