Private/RunPublishOnChanges.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function RunPublishOnChanges {
    $currentLocation = Get-Location
    Write-Verbose "Initialize file system watcher for $currentLocation."
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = $currentLocation
    $watcher.Filter = "*.*"
    $watcher.NotifyFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true

    $splitProjectPath = Split-Path $PackageProjectPath -Leaf
    $binFolder = Join-Path $splitProjectPath "bin" ""
    $binFolderRegex = [regex]::escape($binFolder)
    $objFolder = Join-Path $splitProjectPath "obj" ""
    $objFolderRegex = [regex]::escape($objFolder)
    $excludeFilterRegex = "$binFolderRegex*|$objFolderRegex*"
    Write-Verbose "Exclude filter: $excludeFilterRegex"

    $action = {
        . $PSScriptRoot/../Private/ExtractCsprojMetaData.ps1
        . $PSScriptRoot/../Private/CreatePackageToLocalFeed.ps1
        . $PSScriptRoot/../Private/UpdateGlobalPackageCache.ps1

        $details = $event.SourceEventArgs
        $name = $details.Name
        $timestamp = $event.TimeGenerated

        $packageProjectPath = $event.MessageData.PackageProjectPath
        $localFeedPath = $event.MessageData.LocalFeedPath
        $verbosePreferenceBackup = $VerbosePreference
        $VerbosePreference = $event.MessageData.VerboseSetting

        if ($name -match $event.MessageData.ExcludeFilterRegex) {
            Write-Host ""
            Write-Verbose "Ignoring change of $name."
            return
        }

        Clear-Host
        Write-Host "[$timestamp] $name changed." -ForegroundColor Cyan
        try {
            (($packageName, $packageVersion) = ExtractCsprojMetaData)
            CreatePackageToLocalFeed
            UpdateGlobalPackageCache $packageName $packageVersion
            Write-Host "✓ Done" -ForegroundColor Cyan
        }
        catch {
            Write-Host $_.Exception.Message -ForegroundColor Red
        }
        finally {
            $VerbosePreference = $verbosePreferenceBackup
            Clear-Variable $packageProjectPath
            Clear-Variable $localFeedPath
        }
    }

    Write-Verbose "Register event handler."
    $eventData = New-Object psobject -Property @{
        PackageProjectPath = $PackageProjectPath;
        LocalFeedName      = $localFeedName;
        LocalFeedPath      = $localFeedPath;
        ExcludeFilterRegex = $excludeFilterRegex;
        VerboseSetting     = $VerbosePreference
    }

    $handlers = . {
        Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action -MessageData $eventData -SourceIdentifier FSCreate
        Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $action -MessageData $eventData -SourceIdentifier FSDelete
        Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action -MessageData $eventData -SourceIdentifier FSChange
        Register-ObjectEvent -InputObject $watcher -EventName Renamed -Action $action -MessageData $eventData -SourceIdentifier FSRename
    }

    try {
        $i = 0
        do {
            Wait-Event -Timeout 1
            Write-Progress -Activity " " -Status "Waiting for changes..." -PercentComplete $i

            if ($i -lt 100) {
                $i = $i + 1
            }
            else {
                $i = 0
            }

        } while ($true)
    }
    finally {
        Write-Host ""
        Write-Verbose "Unregister event handler."
        Unregister-Event -SourceIdentifier FSCreate
        Unregister-Event -SourceIdentifier FSDelete
        Unregister-Event -SourceIdentifier FSChange
        Unregister-Event -SourceIdentifier FSRename

        Write-Verbose "Remove background job."
        $handlers | Remove-Job

        Write-Verbose "Dispose file system watcher."
        $watcher.EnableRaisingEvents = $false
        $watcher.Dispose()
    }
}