-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.ps1
85 lines (75 loc) · 1.69 KB
/
add.ps1
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
#!/usr/bin/env pwsh
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromRemainingArguments)]
[object[]] $url,
[switch] $noCommit,
[switch] $noPush,
[switch] $noPull
)
begin {
if(!$noPull) {
write-information -infa continue "pulling for changes"
git -C $PSScriptRoot pull
}
$file = "$PSScriptRoot/list.txt"
$ErrorActionPreference = "stop"
$list = [Collections.Generic.SortedDictionary[string, bool]]::new()
$new = [Collections.Generic.List[string]]::new(64)
function get-domain([Uri] $x) {
$dom = $x.host
if($dom.startsWith("www.")) {
$dom = $dom.substring(4)
}
$dom
}
foreach($s in get-content -lp $file) {
$list.add($s, $false)
}
}
process {
foreach($x in $url) {
if($x -notlike "?*://*") {
$x = "https://$x"
}
$dom = script:get-domain $x
if($list.ContainsKey($dom)) {
write-warning "list already contains $dom"
} else {
[void] $list.Add($dom, $false)
[void] $new.Add($dom)
}
}
}
end {
if($new.count -eq 0) {
write-warning "no changes"
return
}
$list.keys `
| join-string -separator "`n" -outputSuffix "`n" `
| out-file -NoNewLine -Encoding UTF8 $file
& "$PSScriptRoot/generate-lists.ps1"
$msg = $new | sort | join-string -separator ", " -outputPrefix "add: "
if($msg.length -gt 128) {
$msg = "add: $($new.count) sites"
}
write-host $msg
if($noCommit) {
return
}
git -C $PSScriptRoot add list.txt lists
if($LastExitCode -ne 0) {
write-error "failed to add list.txt in git"
}
git -C $PSScriptRoot commit -m $msg
if($LastExitCode -ne 0) {
write-error "failed to commit changes"
}
if(!$noPush) {
git -C $PSScriptRoot push
if($LastExitCode -ne 0) {
write-error "failed to push commit"
}
}
}