forked from fei-ke/HMSPush
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwatch.ps1
More file actions
200 lines (167 loc) · 5.76 KB
/
watch.ps1
File metadata and controls
200 lines (167 loc) · 5.76 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
class BuildManager {
[bool]$isBuilding = $false
[bool]$pendingBuild = $false
$arguments
$debounceTimer
BuildManager($arguments, $debounceTimer) {
$this.arguments = $arguments
$this.debounceTimer = $debounceTimer
}
[void]Run() {
if ($this.isBuilding) {
$this.pendingBuild = $true
Write-Host "Building, new task will be pendding" -ForegroundColor Magenta
return
}
$this.isBuilding = $true
$this.pendingBuild = $false
Write-Host "Start Build" -ForegroundColor Green
$args = $this.arguments
try {
& ./build @args
}
catch {
Write-Host "Build Error: $_" -ForegroundColor Red
}
finally {
$this.isBuilding = $false
if ($this.pendingBuild) {
Write-Host "run pendding build task" -ForegroundColor Yellow
$this.debounceTimer.Start()
}
}
}
}
class Watcher {
$Data
$ExcludeFolderPatterns
$Watchers
$Callback
$Action = {
$self = $Event.MessageData
$self.DoCallback($Event)
}
Watcher([string[]]$excludeFolderPatterns) {
$this.ExcludeFolderPatterns = $excludeFolderPatterns
$folders = $this.ParseExcludeFolderPatterns($excludeFolderPatterns)
$this.Watchers = @()
if ($folders.excludeSubdirectories.Length -eq 0) {
$this.Watchers += $this.CreateWatcher('.', $true)
} else {
$this.Watchers += $this.CreateWatcher('.', $false)
foreach ($folder in $folders.excludeSubdirectories) {
$this.Watchers += $this.CreateWatcher($folder, $false)
}
foreach ($folder in $folders.includeSubdirectories) {
$this.Watchers += $this.CreateWatcher($folder, $true)
}
}
}
[hashtable]ParseExcludeFolderPatterns([string[]]$excludeFolderPatterns) {
$currentDir = Get-Item .
$folderFilters = $excludeFolderPatterns | %{ $_; "*\$_" }
$needToFilterFolders = ls $folderFilters -ErrorAction Ignore -Directory
$includeSubdirectories = @()
$excludeSubdirectories = @()
foreach ($folder in $needToFilterFolders) {
$f = $folder.Parent
while ($f.FullName -ne $currentDir.FullName) {
$excludeSubdirectories += $f
$includeSubdirectories += ls $f -Exclude $folderFilters -Directory
$f = $f.Parent
}
}
$excludeSubdirectories = $excludeSubdirectories | %{ $_.FullName }
$includeSubdirectories = $includeSubdirectories | %{ $_.FullName }
$includeSubdirectories = $includeSubdirectories | ? { $excludeSubdirectories -notcontains $_ }
return @{
excludeSubdirectories = $excludeSubdirectories
includeSubdirectories = $includeSubdirectories
}
}
[hashtable]CreateWatcher($path, $IncludeSubdirectories = $true) {
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $path
$watcher.IncludeSubdirectories = $IncludeSubdirectories # 包含子目录
$watcher.EnableRaisingEvents = $true # 启用事件触发
$watcher.NotifyFilter = [IO.NotifyFilters]'LastWrite,FileName'
$handlers = @()
$events = @('Created', 'Changed', 'Deleted', 'Renamed')
foreach ($eventName in $events) {
$handler = Register-ObjectEvent -InputObject $watcher -EventName $eventName -Action $this.Action -MessageData $this
$handlers += $handler
}
return @{
watcher = $watcher
handlers = $handlers
}
}
DoCallback($Event) {
$name = $Event.SourceEventArgs.Name
$path = $Event.SourceEventArgs.FullPath
$isDirectory = Test-Path -PathType Container $path
if ($isDirectory) {
return
}
$shouldIgnore = $this.ExcludeFolderPatterns | Where-Object { $name -like $_ }
if ($shouldIgnore) {
return
}
Invoke-Command $this.Callback -ArgumentList $Event
}
SetCallback($callback) {
$this.Callback = $callback
}
Loop() {
try {
Wait-Event -SourceIdentifier $this.Watchers[0].handlers[0].Name
}
finally {
$this.watchers | % {
$_.watcher.Dispose()
$_.handlers | Remove-Job -Force
}
}
}
}
$debounceTimer = New-Object System.Timers.Timer
$debounceTimer.Interval = 1000
$debounceTimer.AutoReset = $false
$buildManager = [BuildManager]::new($args, $debounceTimer)
$data = @{
buildManager = $buildManager
debounceTimer = $debounceTimer
triggerSource = $null
}
$timerHandle = Register-ObjectEvent -InputObject $debounceTimer -EventName Elapsed -Action {
$data = $Event.MessageData
if ($data.triggerSource -eq $null) {
$data.triggerSource = "pendding build task"
}
function TriggerBy($tag) {
Write-Host "$($tag*8) triggered by [" -NoNewline
Write-Host $data.triggerSource -NoNewline -ForegroundColor Green
Write-Host ']'
}
TriggerBy '<'
$data.buildManager.run()
TriggerBy '>'
$data.triggerSource = $null
} -MessageData $data
$watcher = [Watcher]::new(@(".*", "build"))
$watcher.Data = $data
$watcher.SetCallback({ param([System.Management.Automation.PSEventArgs]$Event)
$watcher = $Event.MessageData
$data = $watcher.Data
$data.triggerSource = Resolve-Path -Relative $Event.SourceEventArgs.FullPath
$debounceTimer = $data.debounceTimer
$debounceTimer.Stop()
$debounceTimer.Start()
})
try {
$watcher.Loop()
} finally {
$debounceTimer.Stop()
$debounceTimer.Dispose()
$timerHandle | Remove-Job -Force
}