Skip to content

Instantly share code, notes, and snippets.

@RyannosaurusRex
Last active March 15, 2017 11:43
Show Gist options
  • Save RyannosaurusRex/fb5e003329f6eae1f0aef10eee96b575 to your computer and use it in GitHub Desktop.
Save RyannosaurusRex/fb5e003329f6eae1f0aef10eee96b575 to your computer and use it in GitHub Desktop.
Cake build watcher task. It includes a Windows 10 toast notification when the build completes.
#addin "nuget:?package=Cake.Watch" // Put this at the top to pull in the Cake.Watch package for use.
// Add this task in your build file somewhere. You can update which target to run, the file pattern to watch, and even the toast notification.
Task("Watch")
.Does(() => {
var settings = new WatchSettings {
Recursive = true,
Path = "./",
Pattern = "*.*"
};
Watch(settings, (changed) => {
RunTarget("Default");
StartPowershellFile(@"./build-notification.ps1", args => {
args.Append("Message", @"""👍 Success!""");
});
});
})
.ReportError(ex => {
StartPowershellFile(@"./build-notification.ps1", args => {
args.Append("Message", @"""FOH Build Error: " + ex + @"""");
});
});
#This powershell script can be modified to change how the native Windows toast notification looks.
[CmdletBinding()]
Param(
[string]$Message = "No message set!"
)
$ErrorActionPreference = "Stop"
$notificationTitle = "Build Status: " + $Message + "`r`n" + "Completed: " + [DateTime]::Now.ToShortTimeString()
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
#Convert to .NET type for XML manipuration
$toastXml = [xml] $template.GetXml()
$toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
#Convert back to WinRT type
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "PowerShell"
$toast.Group = "PowerShell"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)
#$toast.SuppressPopup = $true
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
$notifier.Show($toast);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment