-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthoral_functions.psm1
366 lines (325 loc) · 13.6 KB
/
authoral_functions.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
Add-Type -AssemblyName PresentationCore, PresentationFramework
function Notify {
<#
.SYNOPSIS
Shows a notification
.DESCRIPTION
Shows a notification modal on terminal, allowing to use as alert.
.Parameter <Title>
The modal title
.Parameter <Message>
The modal message
.Parameter <Quiet>
Allows to run silently
.EXAMPLE
notify -Title "Title" -Message "Some usefull message"
.EXAMPLE
notify -Title Title -Message Usefull_message
.EXAMPLE
notify Title "Some usefull message"
#>
param(
[parameter(ValueFromPipelineByPropertyName, HelpMessage = "Please, enter the message title")][string]$Title,
[parameter(ValueFromPipelineByPropertyName, HelpMessage = "Please, inform the message")][string]$Message,
[parameter(HelpMessage = "Allows to run quietly")][Alias('s', 'q')][Switch]$Quiet
# [parameter(ValueFromPipelineByPropertyName, HelpMessage = "Please, inform the notification type")][ValidateSet("Warning", "Info", "Error")][string]$Type
)
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
if (!$Title) {
$Title = "Alerta !!"
}
if (!$Message) {
$Message = "Terminei algo!"
}
#$msgBody = "Reboot the computer now?"
# $msgTitle = "Confirm Reboot"
# $msgButton = 'YesNoCancel'
# $msgImage = 'Question'
# $Result = [System.Windows.MessageBox]::Show($msgBody,$msgTitle,$msgButton,$msgImage)
# Write-Host "The user chose: $Result [" ($result).value__ "]"
if (!$Quiet) {
[console]::beep(440, 1000)
}
$Result = [System.Windows.MessageBox]::Show($Message, $Title, 0, 0)
}
Export-ModuleMember -Function Notify
function clone {
<#
.SYNOPSIS
Function to customize repositories cloning with some validations.
.DESCRIPTION
Function to customize repositories cloning with some validations. It validates the folder and the repository link.
.Parameter <Path>
Repository link
.Parameter <Folder>
Provides you the possibility of cloning the repository on a different folder. Pass the desired folder path.
.Parameter <Alias>
Provides you the possibility of changing the destiny folder name.
.Parameter <Confirm>
Forces execution
.EXAMPLE
clone https://github.com/user/repo.git
.EXAMPLE
clone https://github.com/user/repo.git -y
.EXAMPLE
clone https://github.com/user/repo.git -Folder test
.EXAMPLE
clone https://github.com/user/repo.git -Alias someTest
.EXAMPLE
clone https://github.com/user/repo.git someTest
#>
param(
[parameter(ValueFromPipelineByPropertyName, HelpMessage = "Please, enter the repository link for download")][string]$Path,
[parameter(ValueFromPipelineByPropertyName, HelpMessage = "Provides you the possibility of changing the destiny folder name.")][string]$Alias,
[parameter(ValueFromPipelineByPropertyName, HelpMessage = "Provides you the possibility of cloning the repository on a different folder. Pass the desired folder path.")][string]$Folder,
[parameter(HelpMessage = "Please, enter the repository link for download")][Alias('y', 'yes')][Switch] $confirm
)
if (!$Path) {
Write-Output "You must provide a repository to clone!"
}
$repository = $Path
$destiny = if ($Folder) { $Folder } else { $pwd }
$localFolder = if ($Alias) { $Alias } else { $(Split-Path -Path $repository -Leaf) }
if ($(Split-Path -Path $destiny -Leaf) -eq 'personal' -Or $(Split-Path -Path $destiny -Leaf) -eq 'pda' -Or $(Split-Path -Path $destiny -Leaf) -eq 'estudos' -Or $confirm) {
if ($folder) { Set-Location $(Resolve-Path -Path $Folder) }
git clone $repository $(if ($Alias) { $Alias })
Set-Location $(Resolve-Path -Path $localFolder)
return
}
$response = Read-Host "You're outside of the predefined projects folders. Do you want to proceed? ([Y]es/[N]o)"
if ($response -eq 'Y' -Or $response -eq 'y' -Or $response -eq 'S' -Or $response -eq 's') {
if ($folder) { Set-Location $(Resolve-Path -Path $Folder) }
git clone $repository $(if ($Alias) { $Alias })
Set-Location $(Resolve-Path -Path $localFolder)
return
}
Write-Host "Cancelling cloning projects. Have a nice day!"
}
Export-ModuleMember -Function clone
function Get-Repositories {
$CurrentLocation = $PWD
$projectFolders = $(Get-ChildItem -Path "~/projetos" -depth 0 -Recurse)
$repos = $($projectFolders | ForEach-Object {
$f = $_
$folders = $(Get-ChildItem -Path "~/projetos/$_" -depth 0 -Recurse) | ForEach-Object { "~/projetos/$f/$_" }
$repositories = @()
$folders | ForEach-Object {
$folder = $_
Set-Location $folder
$git = isInsideGit
if ($(git remote -v | Select-String 'fetch')) {
$remote = $($(git remote -v | Select-String 'fetch').ToString().split('')[1])
$branches = $(git branches | select-string -Pattern " remotes")
$result = [pscustomobject]@{
"repo" = "$remote";
"branches" = @($branches | select-string -Pattern "HEAD" -NotMatch | ForEach-Object { $_.ToString().Replace(" remotes/origin/", '') });
"alias" = "$(Split-Path -Path $(Resolve-Path -Path $folder) -Leaf)"
}
$repositories += $result
}
}
$data = [pscustomobject]@{
"Parent" = "~/projetos/$_";
"repos" = $($repositories | ConvertTo-Json)
}
$data
})
Set-Location $CurrentLocation
$repos
}
Export-ModuleMember -Function Get-Repositories
function Import-Repositories {
param(
[parameter(ValueFromPipelineByPropertyName)]$Repos
)
$Repos | ForEach-Object {
$Folder = $_
if ($($(Test-Path -Path $(Resolve-Path -Path $Folder.Parent)) -eq $True)) {
Set-Location $Folder.Parent
$repos = $($Folder.repos | ConvertFrom-Json)
$repos | ForEach-Object {
clone -Alias $_.alias -Folder $Folder -Path $_.repo
if ($_.branches) {
$_.branches | ForEach-Object {
git checkout $_
git pull --set-upstream origin $_
}
}
git push -u origin --all
}
}
else {
New-Item -Type Directory $Folder.Parent
Set-Location $Folder.Parent
$repos = $($Folder.repos | ConvertFrom-Json)
$repos | ForEach-Object {
clone -Alias $_.alias -Folder $Folder -Path $_.repo
if ($_.branches) {
$_.branches | ForEach-Object {
git checkout $_
git pull --set-upstream origin $_
}
}
git push -u origin --all
}
}
}
}
Export-ModuleMember -Function Download-Repositories
Function Discord {
param(
[parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String]$Content,
[parameter(ValueFromPipelineByPropertyName)][String]$Username,
[parameter(ValueFromPipelineByPropertyName)][String]$Avatar,
[parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String]$Webhook
)
$headers = @{}
$headers.Add("Content-Type", "application/json")
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
if (!$Content) {
$Content = "Some hello"
}
if (!$Avatar) {
$Avatar = "https://rodcordeiro.github.io/shares/img/vader.png"
}
if (!$Username) {
$Username = "Lord Vader"
}
$body = @{
"content" = $Content;
"username" = $Username;
"avatar_url" = $Avatar
}
if (!$Webhook) {
$Webhook = $env:disc_testes
}
Invoke-WebRequest -Uri $Webhook -Method POST -Headers $headers -WebSession $session -Body "$($body | ConvertTo-Json)" -ErrorAction SilentlyContinue
}
Export-ModuleMember -Function Discord
# function hasPdaLib{
# $pkg = $(get-Content -Path .\package.json | ConvertFrom-Json)
# $dependencies = $($pkg.Dependencies | Select-String "pdasolutions")
# if($dependencies){
# return $True
# } else {
# return $False
# }
# }
# function UpdatePDAlib{
# yarn remove @pdasolutions/web
# yarn add @pdasolucoes/web
# $pkg = $(get-Content -Path .\package.json | ConvertFrom-Json)
# $scripts = $pkg.scripts.updateLib
# $scripts
# if($scripts){
# $content = $(get-Content -Path .\package.json).Replace("pdasolutions","pdasolucoes")
# Remove-Item .\package.json -Force
# New-Item -Type File -Name package.json -Value $content.ToString()
# } else {
# return $False
# }
# }
function Update-Repos {
$folders = Get-Repositories
Discord -Avatar "https://rodcordeiro.github.io/shares/img/eu.jpg" -Username "Script do rod" -Webhook $env:disc_testes -Content "Ignorem. Estou rodando um script de atualizacao automatica dos repositorios"
$folders | ForEach-Object {
$folder = $_
$($Folder.repos | ConvertFrom-Json) | ForEach-Object {
$repos = $_
$repo = Resolve-Path -Path "$($folder.Parent)/$($repos.Alias)"
Write-Output "Repo $repo"
Set-Location $repo
$git = isInsideGit
# $lib = hasPdaLib
if ($git -and $(git remote -v | Select-String 'fetch')) {
$branches = $(git branch | select-string -NotMatch "remote")
$currentBranch = ''
$branches | ForEach-Object {
if ($(git status | select-string "Changes not staged for commit") -or $(git status | select-string "Changes to be committed:")) {
git add .
git commit -m '[skip ci] Automatic repositories update'
}
git checkout $branch
$branch = $_.ToString().Replace(' ','')
if ($branch -match '\*') {
$branch = $branch.split(" ")[1]
$currentBranch = $branch
}
git add .
git commit -m '[skip ci] Automatic repositories update'
git pull origin
git push -u origin $branch
}
Write-Output $currentBranch
# UpdatePDAlib
$git_dir = $(Split-Path -Path $(git rev-parse --show-toplevel) -Leaf)
$git_index = $PWD.ToString().IndexOf($git_dir)
$CmdPromptCurrentFolder = $PWD.ToString().Substring($git_index)
Discord -Avatar "https://rodcordeiro.github.io/shares/img/eu.jpg" -Content "Atualizado o $CmdPromptCurrentFolder" -Username "Script do rod" -Webhook $env:disc_testes
}
}
}
}
Export-ModuleMember -Function Update-Repos
Function ConvertTo-B64 {
param(
[parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[String]$Content
)
node -e "const text = '$Content', p = Buffer.from(text.trim()).toString('base64');console.log(p);process.exit();"
}
Export-ModuleMember -Function ConvertTo-B64
function Timer {
param(
[parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[Int]
$Time
)
node -e "const current = new Date();let time_to_await = $Time * 1000;const timer = current.getTime() + time_to_await, timerParsed = new Date(timer);const interval = setInterval(() => { if (time_to_await === 0) { clearInterval(interval); } process.stdout.clearLine(0); process.stdout.cursorTo(0); process.stdout.write('Time remaining: '+time_to_await / 1000+'s'); time_to_await -= 1000;}, 1000);"
}
Export-ModuleMember -Function Timer
function ConvertTo-MarkdownTable {
param(
[Parameter(ValueFromPipelineByPropertyName, Mandatory)]
[ValidateNotNullOrEmpty()]
[string[]]$Columns,
[Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
[string[]]$RowsLabel,
[Parameter(ValueFromPipelineByPropertyName, Mandatory)]
[ValidateNotNullOrEmpty()]
[PSCustomObject[]]$Rows,
[Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)]
[string]$OutFile
)
$table = @()
$table += $(if ($RowsLabel) { '| ' }) + [String]::Join("", $($Columns | foreach-Object { "|$($_)" })) + "|"
$table += $(if ($RowsLabel) { '|:- ' }) + [String]::Join("", $($Columns | foreach-Object { "|:-:" })) + "|"
if ($RowsLabel) {
$table += $RowsLabel | foreach-object {
$app = $_
return "|$app" + [String]::Join("", $($Columns | foreach-Object { "|$($Rows.$_.$app)" })) + "|"
}
}
else {
$table += $Rows | foreach-object {
$row = $_
return [String]::Join("", $($Columns | foreach-Object { "|$($row.$_)" })) + "|"
}
}
if ($OutFile) {
New-Item -Type file -Path $OutFile | Out-Null
$table | ForEach-Object {
Add-Content -Path $OutFile -Value $_
}
}
else {
return $table
}
}
Export-ModuleMember -Function ConvertTo-MarkdownTable