-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclean-cache-folder.psm1
110 lines (103 loc) · 3.99 KB
/
clean-cache-folder.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
[string]$scriptDir = $PSScriptRoot
[string]$rootCacheFolder = (Join-Path -Path $env:LOCALAPPDATA -ChildPath templatereport)
[string]$defaultCacheFolder = (Join-Path -Path $rootCacheFolder -ChildPath extracted)
[string]$defaultZipOutputFolder = (Join-Path $rootCacheFolder 'generated-zips')
function Resolve-FullPath{
[cmdletbinding()]
param
(
[Parameter(Position=0,ValueFromPipeline=$true)]
[string[]] $path
)
process{
foreach($p in $path){
if(-not ([string]::IsNullOrWhiteSpace($p))){
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path)
}
}
}
}
function Clean-CacheFolder{
[cmdletbinding()]
param(
[string]$cacheFolder = $defaultCacheFolder
)
process{
if(-not (test-path $cacheFolder)){
'cache folder not found at "{0}"' -f $cacheFolder | Write-Error
}
else{
'Cleaning cache folder at "{0}"' -f $cacheFolder | Write-Output
Push-Location -Path $cacheFolder
[string[]]$dirsToClean = Get-ChildItem -Path $cacheFolder -Directory
$dirsToClean | Clean-PackageDirectory
# go back to dir the user was on
Pop-Location
}
}
}
function Clean-PackageDirectory{
[cmdletbinding()]
param(
[parameter(ValueFromPipeline = $true)]
[string[]]$pkgFolderToClean
)
process{
foreach($pkgfolder in $pkgFolderToClean){
[string]$fullpath = Resolve-FullPath -path $pkgfolder
if(-not (test-path $fullpath)){
'package folder not found at "{0}"' -f $cacheFolder | Write-Error
}
else{
'cleaning pkg folder "{0}"' -f $fullpath | Write-Output
Get-ChildItem -Path $fullpath -Recurse -File -Exclude 'template.json','*.nuspec','ide.host.json'|Select-Object -ExpandProperty FullName|%{Remove-Item -LiteralPath $_}
# if the folder doesn't have a template.json file somewhere, delete the folder
[string[]]$templatejsonfound = Get-ChildItem -Path $fullpath -Recurse -File template.json
if($templatejsonfound.length -lt 1){
' deleting folder because it doesn''t have any template.json file' | Write-Output
Remove-Item -Path $fullpath -Recurse -Force
}
}
}
}
}
function Zip-CacheFolder{
[cmdletbinding()]
param(
[string]$cacheFolder = $defaultCacheFolder,
[string]$zipOutputFolderRoot = $defaultZipOutputFolder
)
process{
[string]$actualZipOutputPath = (Join-Path $zipOutputFolderRoot -ChildPath ([datetime]::Now.ToString('yyyy.MM.hh.ss.mm')))
$actualZipOutputPath = Resolve-FullPath -path $actualZipOutputPath
if(-not (test-path -Path $actualZipOutputPath)){
New-Item -Path $actualZipOutputPath -ItemType Directory #-Force
}
$compress = @{
Path = $cacheFolder
# CompressionLevel = "Fastest"
DestinationPath = (join-path $actualZipOutputPath nugettemplatecache.zip)
}
Compress-Archive @compress
}
}
function Extract-CacheFolder{
[cmdletbinding()]
param(
[string]$sourceZipFile = (Join-Path $scriptDir nugettemplatecache.zip),
[string]$destination = $rootCacheFolder
)
process{
'extracting to "{0}"' -f $rootCacheFolder | Write-Output
[string]$extractFolder = (Join-Path -Path $destination 'extracted');
if(-not(test-path $extractFolder)){
Expand-Archive -LiteralPath $sourceZipFile -DestinationPath $destination -Force
}
else{
'Skipping extract cache file because folder exists at "{0}"' -f $extractFolder | Write-Output
}
}
}
#Clean-CacheFolder -cacheFolder D:\temp\templates-temp\cache-test
#Zip-CacheFolder -cacheFolder D:\temp\templates-temp\cache-test
#Extract-CacheFolder -destination D:\temp\templates-temp