PSDT.App.psm1

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
$host.ui.RawUI.WindowTitle = "PSDT | PowerShell Developer Tools";

if (Test-Path Function:\TabExpansion) {
    Rename-Item Function:\TabExpansion PreGetVSSolutionTabExpansion
}

Function global:TabExpansion($line, $lastWord) {
    $filter = ($line -split " " | Select-Object -Skip 1) -join ".*";
   
  switch -regex ($line) {
        "^(Get-VSSolution|gvss) .*" {
      Get-VSSolution $filter | Select-Object -ExpandProperty FullName
    }
        "^(Get-File|f) .*" {
      Get-File $filter | Select-Object -ExpandProperty FullName
    }
        "^(Get-Directory|d) .*" {
      Get-Directory $filter | Select-Object -ExpandProperty FullName
    }
        default {
            if (Test-Path Function:\PreGetVSSolutionTabExpansion) {
                PreGetVSSolutionTabExpansion $line $lastWord
            }
        }
    }
}

$global:PSDTChildItemCache = @{};
$global:PSDTChildItemCacheFiltered = @{};

Function Find-ChildItem {
  Param (
    [string]$Filter = "*.*",
    [switch]$File,
    [switch]$Directory
  )

  $fullNameFilter = $args -join ".*";
  $fullNameFilter = ".*{0}.*" -f $fullNameFilter;
  $itemCache = $global:PSDTChildItemCache;
  $itemCacheFilterd = $global:PSDTChildItemCacheFiltered;

  $key = "$((Get-Location).Path)-File:$File-Directory:$Directory-Filter:$Filter";
  $keyOfFilteredItems = "$key-$fullNameFilter";
  
  If(-not $itemCache.ContainsKey($key)) { 
      $searchProgressActivity = "Find child items...";
      
      Write-Progress -Activity $searchProgressActivity -Status $fullNameFilter;
      $childItems = (Get-ChildItem .\ -Recurse -Filter $Filter -File:$File -Directory:$Directory);
      Write-Progress -Activity $searchProgressActivity -Completed;
      
      $itemCache.Add($key, $childItems); 
  }

  If($args.Length -gt 0 -and (Test-Path $args[$args.Length - 1])) {
      return Get-Item $args[$args.Length - 1];
  }
  
  If(-not $itemCacheFilterd.ContainsKey($keyOfFilteredItems)) {
      $filteredItems = $itemCache[$key] | Where-Object { ($_.FullName -match $fullNameFilter) };
      $itemCacheFilterd.Add($keyOfFilteredItems, $filteredItems);
  }

  return $itemCacheFilterd[$keyOfFilteredItems];
}

Set-Alias fci Find-ChildItem;

<#
.Synopsis
    Gets all files under the current path, which match the filter parameters.
    The cmdlet's default alias is: f
.DESCRIPTION
    The cmdlet is using an internal cache, which is stored in the script scope.
.EXAMPLE
    The example gets all files on the drive R, where the FullName matches the pattern *common*full*.sln*.
   
    PS R:\Get-File common full .sln
   
        Directory: R:\Source\cool-project\master\common\Sources\Builds\FullBuild

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a---- 1/9/2017 8:03 AM 21870 FullBuild.sln

.EXAMPLE
    The example shows all matching files using tab completion.

    Type the command below and press the TAB.
        
        PS R:\Get-File common full .sln

    Pressing the tab will replace the last word with the first matching file.
        
        PS R:\Get-File common full R:\Source\cool-project\master\common\Sources\Builds\FullBuild.sln

.EXAMPLE
    The example shows all matching files in a completion list.

    After typing the following line:
        
        PS R:\Get-File common f .sln

    Pressing the CTRL+SPACE will replace the last word with the first matching file's full name, and will list each other matches.
        
        PS R:\Get-File common R:\Source\cool-project\master\common\Sources\Builds\FullBuild.sln
        R:\Source\cool-project\master\common\Sources\Builds\FeedbackBuild.sln
        R:\Source\cool-project\master\common\Sources\Builds\FeatureBuild.sln

#>

Function Get-File {
  return Find-ChildItem -Filter "*.*" -File @args;
}

Set-Alias f Get-File;

<#
.Synopsis
    Gets all directories under the current path, which match the filter parameters.
    The cmdlet's default alias is: d
.DESCRIPTION
    The cmdlet is using an internal cache, which is stored in the script scope.
.EXAMPLE
    The example gets all directories on the drive R, where the FullName matches the pattern *common*full*.sln*.
   
    PS R:\Get-Directory common full
   
        Directory: R:\Source\cool-project\master\common\Sources\Builds

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    d----- 1/9/2017 8:03 AM 21870 FullBuild

.EXAMPLE
    The example shows all matching directories using tab completion.

    Type the command below and press the TAB.
        
        PS R:\Get-Directory common full

    Pressing the tab will replace the last word with the first matching directory.
        
        PS R:\Get-Directory common full R:\Source\cool-project\master\common\Sources\Builds\FullBuild

.EXAMPLE
    The example shows all matching directories in a completion list.

    After typing the following line:
        
        PS R:\Get-Directory common f

    Pressing the CTRL+SPACE will replace the last word with the first matching directory's full name, and will list each other matches.
        
        PS R:\Get-Directory common R:\Source\cool-project\master\common\Sources\Builds\FullBuild
        R:\Source\cool-project\master\common\Sources\Features
        R:\Source\cool-project\master\common\Sources\Features\Feature1
        R:\Source\cool-project\master\common\Sources\Features\Feature1\Project1
        R:\Source\cool-project\master\common\Sources\Features\Feature2

#>

Function Get-Directory {
  return Find-ChildItem -Filter "*.*" -Directory @args;
}

Set-Alias d Get-Directory;