forked from sayedihashimi/xcompile-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ps1
More file actions
286 lines (251 loc) · 9.94 KB
/
helper.ps1
File metadata and controls
286 lines (251 loc) · 9.94 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<#
https://github.com/xunit/xunit/blob/master/src/abstractions/xunit.abstractions/Properties/AssemblyInfo.cs#L8
$res1.tostring().substring($res1.tostring().lastindexof(':')+1).Replace(' (fetch)','').trim()
$originstr = (git remote -v|Select-String 'origin.*fetch'|Select-Object -First 1).tostring();
$originstr |%{}
$res1.tostring().substring($res1.tostring().lastindexof(':')+1).Replace(' (fetch)','').trim()
.tostring().substring($originstr.lastindexof(':')+1).Replace(' (fetch)','').trim()
$res[0].RelativePath($pwd).replace('\','/')
#>
<#
$baseurl = 'https://github.com/'
$originstr = (git remote -v|Select-String 'origin.*fetch'|Select-Object -First 1).tostring()
$repofrag = $originstr.tostring().tostring().substring($originstr.lastindexof(':')+1).Replace(' (fetch)','').replace('.git','').trim()
#>
function Get-IfdefUrl{
[cmdletbinding()]
param(
[string[]]$path = ($pwd),
[string]$baseUrl = 'https://github.com/'
)
process{
foreach($p in $path){
try{
Push-Location
Set-Location $p
$originstr = (git remote -v|Select-String 'origin.*fetch'|Select-Object -First 1).tostring()
$repofrag = $originstr.tostring().tostring().substring($originstr.lastindexof(':')+1).Replace(' (fetch)','').replace('.git','').trim()
Get-ChildItem .\ *.cs -Recurse -File|select-string '#if' -SimpleMatch -Context 0 | %{
'{0}{1}/blob/master/{2}#L{3}' -f $baseurl,$repofrag, ($_.RelativePath($pwd).replace('\','/')),$_.LineNumber
}
}
finally{
Pop-Location
}
}
}
}
function Get-Ifdef{
[cmdletbinding()]
param(
[string[]]$path = ($pwd),
[int]$context = 5
)
process{
foreach($p in $path){
if(-not (Test-Path $p)){
'not found [{0}]' -f $p | Write-Warning
continue
}
try{
Push-Location
Set-Location $p
$branchName = ((git branch|Select-String '^\*').tostring().substring(2).trim())
$baseurl = 'https://github.com/'
$originstr = (git remote -v|Select-String 'origin.*fetch'|Select-Object -First 1).tostring()
$repofrag = $originstr.tostring().tostring().substring($originstr.lastindexof(':')+1).Replace(' (fetch)','').replace('.git','').trim()
Get-ChildItem .\ *.cs -Recurse -File|select-string '#if' -SimpleMatch -Context $context|%{
'{0}{1}/blob/{2}/{3}#L{4}' -f $baseurl,$repofrag,$branchName, ($_.RelativePath($pwd).replace('\','/')),$_.LineNumber
"$_`r`n"
}
}
finally{
Pop-Location
}
}
}
}
function Get-RepoUrl{
[cmdletbinding()]
param(
[string[]]$path
)
process{
foreach($p in $path){
if(-not (Test-Path $p)){
'not found [{0}]' -f $p | Write-Warning
continue
}
try{
Push-Location | Out-Null
Set-Location $p
$originstr = (git remote -v|Select-String 'origin.*fetch'|Select-Object -First 1).tostring()
if($originstr.Contains('https://')){
$originstr.substring($originstr.IndexOf('https://')).replace('(fetch)','').trim()
}
else{
$repofrag = $originstr.substring($originstr.lastindexof(':')+1).Replace(' (fetch)','').replace('.git','').trim()
'{0}{1}' -f $baseurl,$repofrag,$branchName
}
}
finally{
Pop-Location | Out-Null
}
}
}
}
Function Get-StringHash{
[cmdletbinding()]
param(
[String] $text,
$HashName = "MD5"
)
process{
$sb = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($text))|%{
[Void]$sb.Append($_.ToString("x2"))
}
$sb.ToString()
}
}
function CloneRepo{
[cmdletbinding()]
param(
[string[]]$url,
[string]$rootDir = (Join-Path $env:LOCALAPPDATA 'xcompile\gitrepos')
)
process{
if(-not (Test-Path $rootDir)){
New-Item -Path $rootDir -ItemType Directory | Write-Verbose
}
foreach($u in $url){
if(-not ([string]::IsNullOrWhiteSpace($u))){
try{
Push-Location | Out-Null
$urlhash = (Get-StringHash $u)
$finalDest = (Join-Path $rootDir $urlhash)
if(-not (Test-Path $finalDest)){
New-Item -Path $finalDest -ItemType Directory | Out-Null
Set-Location $finalDest | Out-Null
# https://github.com/dahlbyk/posh-git/issues/109#issuecomment-21638678
git clone $url $finalDest 2>&1 | % { $_.ToString() } | Out-Null
}
$finalDest
}
finally{
Pop-Location | Out-Null
}
}
}
}
}
function InternalGet-RelativePath{
[cmdletbinding()]
param(
[Parameter(Position=0,Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$fromPath,
[Parameter(Position=0,Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$toPath
)
process{
$fromPathToUse = (Resolve-Path $fromPath).Path
if( (Get-Item $fromPathToUse) -is [System.IO.DirectoryInfo]){
$fromPathToUse += [System.IO.Path]::DirectorySeparatorChar
}
$toPathToUse = (Resolve-Path $toPath).Path
if( (Get-Item $toPathToUse) -is [System.IO.DirectoryInfo]){
$toPathToUse += [System.IO.Path]::DirectorySeparatorChar
}
[uri]$fromUri = New-Object -TypeName 'uri' -ArgumentList $fromPathToUse
[uri]$toUri = New-Object -TypeName 'uri' -ArgumentList $toPathToUse
[string]$relPath = $toPath
# if the Scheme doesn't match just return toPath
if($fromUri.Scheme -eq $toUri.Scheme){
[uri]$relUri = $fromUri.MakeRelativeUri($toUri)
$relPath = [Uri]::UnescapeDataString($relUri.ToString())
if([string]::Equals($toUri.Scheme, [Uri]::UriSchemeFile, [System.StringComparison]::OrdinalIgnoreCase)){
$relPath = $relPath.Replace([System.IO.Path]::AltDirectorySeparatorChar,[System.IO.Path]::DirectorySeparatorChar)
}
}
if([string]::IsNullOrWhiteSpace($relPath)){
$relPath = ('.{0}' -f [System.IO.Path]::DirectorySeparatorChar)
}
# return the result here
$relPath
}
}
# Heavily modified from: http://ss64.com/ps/zip.html / http://ss64.com/ps/zip.txt
Add-Type -As System.IO.Compression.FileSystem
function New-ZipFile {
#.Synopsis
# Create a new zip file, optionally appending to an existing zip...
[CmdletBinding()]
param(
# The path of the zip to create
[Parameter(Position=0, Mandatory=$true)]
$ZipFilePath,
# Items that we want to add to the ZipFile
[Parameter(Position=1, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias("PSPath","Item")]
[string[]]$InputObject = $Pwd,
[string]$rootFolder = $pwd,
[string]$relpathinzip,
# Append to an existing zip file, instead of overwriting it
[Switch]$Append,
# The compression level (defaults to Optimal):
# Optimal - The compression operation should be optimally compressed, even if the operation takes a longer time to complete.
# Fastest - The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.
# NoCompression - No compression should be performed on the file.
[System.IO.Compression.CompressionLevel]$Compression = "Optimal"
)
begin {
# Make sure the folder already exists
[string]$File = Split-Path $ZipFilePath -Leaf
[string]$Folder = $(if($Folder = Split-Path $ZipFilePath) { Resolve-Path $Folder } else { $Pwd })
$ZipFilePath = Join-Path $Folder $File
# If they don't want to append, make sure the zip file doesn't already exist.
if(!$Append) {
if(Test-Path $ZipFilePath) { Remove-Item $ZipFilePath }
}
$Archive = [System.IO.Compression.ZipFile]::Open( $ZipFilePath, "Update" )
}
process {
foreach($path in $InputObject) {
foreach($item in Resolve-Path $path) {
# Push-Location so we can use Resolve-Path -Relative
# This will get the file, or all the files in the folder (recursively)
foreach($file in Get-ChildItem $item -Recurse -File -Force | % FullName) {
# Calculate the relative file path
$relative = InternalGet-RelativePath -fromPath $rootFolder -toPath $file
if(-not [string]::IsNullOrWhiteSpace($relpathinzip)){
$relative = $relpathinzip.TrimEnd('\').TrimEnd('/') + '\' + $relative.TrimEnd('\').TrimEnd('/') + '\'
}
# Add the file to the zip
$null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Archive, $file, $relative, $Compression)
}
}
}
}
end {
$Archive.Dispose()
Get-Item $ZipFilePath
}
}
<#
[string[]]$paths = 'C:\Data\mycode\xunit',
'C:\data\mycode\Newtonsoft.Json',
'C:\data\mycode\dapper-dot-net',
'C:\data\mycode\AutoMapper',
'c:\data\mycode\EntityFramework',
'c:\data\mycode\Scaffolding',
'c:\data\mycode\log4net',
'c:\data\mycode\MVVMCross'
#Get-Ifdef -path $paths -context 10
#Get-IfdefUrl -path $paths
#Get-Ifdef -path 'c:\data\mycode\MVVMCross'
#Get-RepoUrl -path 'c:\data\mycode\MVVMCross','C:\data\mycode\MvvmCross2'
#CloneRepo -url git@github.com:sayedihashimi/riddlerps.git
#>