Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active March 17, 2020 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/c4553adbc638db7dd6502de97835fb46 to your computer and use it in GitHub Desktop.
Save amirrajan/c4553adbc638db7dd6502de97835fb46 to your computer and use it in GitHub Desktop.
File Watcher for Windows
ECHO ON
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\Roslyn\csc.exe" /r:"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\ManagedLanguages\VBCSharp\InteractiveComponents\Microsoft.CodeAnalysis.CSharp.dll" sidekick.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp;
namespace SyncDeploy
{
class Program
{
static string path = null;
static FileSystemWatcher watcher;
static string[] fileBlackList = new []
{
"#",
"g.cs",
"0.cs",
"g.i.cs"
};
static string[] fileExtensionsWhiteList = new string[]
{
".cs",
".coffee",
".rb",
".html",
".cshtml",
".js",
".css",
".fs"
};
static void Main(string[] args)
{
path = Directory.GetCurrentDirectory() + "\\..";
watcher = new FileSystemWatcher(path, "*.*");
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Created += new FileSystemEventHandler(watcher_Changed);
watcher.Renamed += watcher_Renamed;
Console.WriteLine("Watching for changes to the following file types: " + string.Join(", ", fileExtensionsWhiteList));
Console.WriteLine("Watching " + path + " for changes, press Enter to stop...");
Shell("tutorial");
Console.ReadLine();
}
static void Shell(params string[] args)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo("ruby", "./sidekick/sidekick.rb " + string.Join(" ", args));
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo = processStartInfo;
process.OutputDataReceived += (sender, args1) => System.Console.WriteLine(args1.Data);
process.ErrorDataReceived += (sender, args2) => System.Console.WriteLine(args2.Data);
bool processStarted = process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
System.Console.WriteLine("---");
}
static void watcher_Renamed(object source, RenamedEventArgs e)
{
CallWatcher(e.FullPath);
}
static void watcher_Changed(object sender, FileSystemEventArgs e)
{
CallWatcher(e.FullPath);
}
static void CallWatcher(string path)
{
foreach(var p in fileBlackList)
{
if(path.Contains(p)) return;
}
if (fileExtensionsWhiteList.Contains(Path.GetExtension(path)) && System.IO.File.Exists(path))
{
watcher.EnableRaisingEvents = false;
var fullPath = System.IO.Path.GetFullPath(path);
var relativeFile = path.Replace(Directory.GetCurrentDirectory(), "");
System.Console.WriteLine("Changed: " + fullPath);
Shell("file_changed", fullPath);
watcher.EnableRaisingEvents = true;
}
}
}
}
file = ARGV[1]
puts "#{ARGV}"
puts "#{file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment