|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Text; |
| 3 | +using CoreServices; |
| 4 | +using FSEvents.Workers; |
| 5 | +using Marille; |
| 6 | +using Marille.FileSystem; |
| 7 | +using Marille.FileSystem.Workers; |
| 8 | +using Mono.Options; |
| 9 | +using ObjCRuntime; |
| 10 | +using Serilog; |
| 11 | + |
| 12 | +namespace FSEvents; |
| 13 | + |
| 14 | +static class MainClass { |
| 15 | + #region Fields |
| 16 | + |
| 17 | + public static int Verbosity { get; set; } = 1; |
| 18 | + public static bool ShowHelp { get; set; } = false; |
| 19 | + |
| 20 | + public static List<string> Paths { get; } = new (); |
| 21 | + |
| 22 | + #endregion |
| 23 | + |
| 24 | + #region Helpers |
| 25 | + |
| 26 | + static void PrintHelp (OptionSet options) |
| 27 | + { |
| 28 | + Console.WriteLine ("Usage: fsevents [OPTIONS]+ message"); |
| 29 | + Console.WriteLine ("Example of a file system watcher using Marille."); |
| 30 | + Console.WriteLine (); |
| 31 | + Console.WriteLine ("Options:"); |
| 32 | + options.WriteOptionDescriptions (Console.Out); |
| 33 | + } |
| 34 | + |
| 35 | + static void InitializeLog () |
| 36 | + { |
| 37 | + LoggerConfiguration logConfiguration = new LoggerConfiguration () |
| 38 | + .Enrich.WithThreadId () |
| 39 | + .Enrich.WithThreadName (); |
| 40 | + |
| 41 | + // cast verbosity to a log level and set it as the minimum level |
| 42 | + var minLevel = (LogLevel) Verbosity; |
| 43 | + |
| 44 | + switch (minLevel) { |
| 45 | + case LogLevel.Fatal: |
| 46 | + logConfiguration.MinimumLevel.Fatal (); |
| 47 | + break; |
| 48 | + case LogLevel.Error: |
| 49 | + logConfiguration.MinimumLevel.Error (); |
| 50 | + break; |
| 51 | + case LogLevel.Information: |
| 52 | + logConfiguration.MinimumLevel.Information (); |
| 53 | + break; |
| 54 | + case LogLevel.Debug: |
| 55 | + logConfiguration.MinimumLevel.Debug (); |
| 56 | + break; |
| 57 | + default: |
| 58 | + logConfiguration.MinimumLevel.Information (); |
| 59 | + break; |
| 60 | + } |
| 61 | + |
| 62 | + logConfiguration.MinimumLevel.Debug (); |
| 63 | + |
| 64 | + // thread id == min level of log |
| 65 | + Log.Logger = logConfiguration |
| 66 | + .WriteTo.Console (outputTemplate: "{Timestamp:HH:mm:ss} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception}") |
| 67 | + .CreateLogger (); |
| 68 | + } |
| 69 | + |
| 70 | + #endregion |
| 71 | + |
| 72 | + static int Main (string [] args) |
| 73 | + { |
| 74 | + try { |
| 75 | + Console.OutputEncoding = new UTF8Encoding (false, false); |
| 76 | + NSApplication.Init (); |
| 77 | + Environment.CurrentDirectory = Runtime.OriginalWorkingDirectory!; |
| 78 | + return Main2 (args); |
| 79 | + } catch (Exception e) { |
| 80 | + Console.WriteLine (e); |
| 81 | + Exit (1); |
| 82 | + } |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + [DllImport ("/usr/lib/libSystem.dylib")] |
| 87 | + static extern void exit (int exitcode); |
| 88 | + public static void Exit (int exitCode = 0) |
| 89 | + { |
| 90 | + // This is ugly. *Very* ugly. The problem is that: |
| 91 | + // |
| 92 | + // * The Apple API we use will create background threads to process messages. |
| 93 | + // * Those messages are delivered to managed code, which means the mono runtime |
| 94 | + // starts tracking those threads. |
| 95 | + // * The mono runtime will not terminate those threads (in fact the mono runtime |
| 96 | + // will do nothing at all to those threads, since they're not running managed |
| 97 | + // code) upon shutdown. But the mono runtime will wait for those threads to |
| 98 | + // exit before exiting the process. This means mtouch will never exit. |
| 99 | + // |
| 100 | + // So just go the nuclear route. |
| 101 | + exit (exitCode); |
| 102 | + } |
| 103 | + |
| 104 | + static int Main2 (string [] args) |
| 105 | + { |
| 106 | + // Use mono.options to parse the args, this is a sample so we won't do too complex things |
| 107 | + var os = new OptionSet () { |
| 108 | + { "h|?|help", "Displays the help", v => ShowHelp = v != null }, |
| 109 | + { "v", "Verbose", v => Verbosity++ }, |
| 110 | + { "q", "Quiet", v => Verbosity = 0 }, |
| 111 | + { "p|path=", "Add a path to monitor", v => Paths.Add (v) }, |
| 112 | + }; |
| 113 | + |
| 114 | + try { |
| 115 | + var extra = os.Parse (args); |
| 116 | + } catch (Exception e) { |
| 117 | + // We could not parse the argumets, print the error and suggest to call help |
| 118 | + Console.WriteLine("fsevents:"); |
| 119 | + Console.WriteLine (e.Message); |
| 120 | + Console.WriteLine ("Try `fsevents --help' for more information."); |
| 121 | + return 1; |
| 122 | + } |
| 123 | + |
| 124 | + if (ShowHelp) { |
| 125 | + PrintHelp (os); |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + // Add logging so that we can see what is going on |
| 130 | + InitializeLog (); |
| 131 | + |
| 132 | + // create the app data dir in which we will store the diffs |
| 133 | + var baseDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "Marille", "Diffs"); |
| 134 | + Directory.CreateDirectory (baseDir); |
| 135 | + Log.Information ("Created base directory {BaseDir}", baseDir); |
| 136 | + |
| 137 | + // we are going to create a uuid7 for the new snapshot. uuid7 is time shortable and unique so that way |
| 138 | + // we will be able to read see the snapshot and the diff in the future. |
| 139 | + var snapshot = SnapshotManager.Create (baseDir, Paths); |
| 140 | + |
| 141 | + Log.Information ("Starting fsevents with {Paths}", Paths); |
| 142 | + |
| 143 | + // we need to create several things to be able to get the events: |
| 144 | + // 1. Hub: will be used to deliver the events to the consumers. |
| 145 | + // 2. Worker: will be used to process the events. |
| 146 | + // 3. FSMonitor: will be used to monitor the file system and deliver the events to the hub. |
| 147 | + var hub = new Hub (); |
| 148 | + |
| 149 | + // because we are going to start a main loop in the main thread, we need the channel initialization to be |
| 150 | + // done in a background thread else we will be blocked. |
| 151 | + Task.Run (async () => { |
| 152 | + // workers will be disposed by the hub |
| 153 | + //var worker = new LogEventToConsole (); |
| 154 | + var eventFilter = new FSEventFilterer (hub); |
| 155 | + var diffGenerator = new DiffGenerator (snapshot); |
| 156 | + |
| 157 | + var fsEventsErrorHandler = new FSEventsErrorHandler (); |
| 158 | + var textFileChangedErrorHandler = new TextFileChangedErrorHandler (); |
| 159 | + |
| 160 | + var fsEventsConfig = new TopicConfiguration { |
| 161 | + Mode = ChannelDeliveryMode.AtLeastOnceSync |
| 162 | + }; |
| 163 | + var txtEventsConfig = new TopicConfiguration { |
| 164 | + Mode = ChannelDeliveryMode.AtMostOnceAsync |
| 165 | + }; |
| 166 | + await hub.CreateAsync (nameof (FSMonitor), txtEventsConfig, textFileChangedErrorHandler, diffGenerator); |
| 167 | + await hub.CreateAsync (nameof (FSMonitor), fsEventsConfig, fsEventsErrorHandler, eventFilter); |
| 168 | + Log.Information ("Channels created"); |
| 169 | + Console.WriteLine ("Feel free to edit your files, we will keep a history of the edits in the session!!!"); |
| 170 | + Console.WriteLine ("Press Ctrl+C to stop the watcher."); |
| 171 | + }); |
| 172 | + |
| 173 | + var monitor = new FSMonitor (Paths, hub, FSEventStreamCreateFlags.FileEvents | FSEventStreamCreateFlags.WatchRoot); |
| 174 | + monitor.ScheduleWithRunLoop (NSRunLoop.Current); |
| 175 | + Log.Information ("FSEvent monitor scheduled"); |
| 176 | + monitor.Start (); |
| 177 | + Console.WriteLine($"Starting watch on {string.Join (',', Paths)}"); |
| 178 | + NSRunLoop.Main.Run (); |
| 179 | + |
| 180 | + return 0; |
| 181 | + } |
| 182 | +} |
0 commit comments