private void GenerateEventFile(string changeFile, FileToGenerate currentFile) { StringBuilder eventFile = CreateStringBuilder(ClassType.Event); currentFile.GenerateEvents(eventFile); string fileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Service\\Events", currentFile.FileName + "Events.Generated.cs"); File.WriteAllText(fileName, eventFile.ToString()); WriteLine("Updated file: {0}", fileName); StringBuilder stateFile = CreateStringBuilder(ClassType.State); currentFile.GenerateState(stateFile); string stateFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Service\\Domain\\States", currentFile.FileName + "State.Generated.cs"); File.WriteAllText(stateFileName, stateFile.ToString()); WriteLine("Updated file: {0}", stateFileName); StringBuilder dtoFile = CreateStringBuilder(ClassType.Dto); currentFile.GenerateDto(dtoFile); string dtoFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Interfaces\\Dto\\Domain", currentFile.FileName + "Dto.Generated.cs"); File.WriteAllText(dtoFileName, dtoFile.ToString()); WriteLine("Updated file: {0}", dtoFileName); StringBuilder messageFile = CreateStringBuilder(ClassType.Message); currentFile.GenerateMessages(messageFile); string messageFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Interfaces\\Messages\\Domain", currentFile.FileName + "Message.Generated.cs"); File.WriteAllText(messageFileName, messageFile.ToString()); WriteLine("Updated file: {0}", messageFileName); StringBuilder facadeFile = CreateStringBuilder(ClassType.ReadModelFacade); currentFile.GenerateReadModelFacades(facadeFile); string facadeFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Client\\Facades\\Domain", currentFile.FileName + "Facade.Generated.cs"); File.WriteAllText(facadeFileName, facadeFile.ToString()); WriteLine("Updated file: {0}", facadeFileName); StringBuilder viewBaseFile = CreateStringBuilder(ClassType.ViewBase); currentFile.GenerateViewBase(currentFile.FileName, viewBaseFile); string viewBaseFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Client\\Views\\Base", currentFile.FileName + "ViewBase.Generated.cs"); File.WriteAllText(viewBaseFileName, viewBaseFile.ToString()); WriteLine("Updated file: {0}", viewBaseFileName); string viewFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Client\\Views", currentFile.FileName + "View.cs"); if (!File.Exists(viewFileName)) { StringBuilder viewFile = CreateStringBuilder(ClassType.View); currentFile.GenerateView(currentFile.FileName, viewFile); File.WriteAllText(viewFileName, viewFile.ToString()); WriteLine("Updated file: {0}", viewFileName); } }
private void GenerateCommandFile(string changeFile, FileToGenerate currentFile) { StringBuilder commandFile = CreateStringBuilder(ClassType.Command); currentFile.GenerateCommands(commandFile); string fileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Commands", currentFile.FileName + "Commands.Generated.cs"); File.WriteAllText(fileName, commandFile.ToString()); WriteLine("Updated file: {0}", fileName); StringBuilder handlerFile = CreateStringBuilder(ClassType.Handler); currentFile.GenerateHandler(handlerFile); string handlerFileName = Path.Combine(Path.GetDirectoryName(changeFile) ?? Environment.CurrentDirectory, "Service\\Handlers", currentFile.FileName + "Handler.Generated.cs"); File.WriteAllText(handlerFileName, handlerFile.ToString()); WriteLine("Updated file: {0}", handlerFileName); }
private void OnFileChanged(string fullPath) { DateTime now = DateTime.UtcNow; DateTime last; if (_lastGenerations.TryGetValue(fullPath, out last)) { TimeSpan timeSpan = now - last; WriteLine(timeSpan); if (timeSpan < TimeSpan.FromSeconds(2)) return; } _lastGenerations[fullPath] = now; Thread.Sleep(TimeSpan.FromSeconds(1)); try { WriteLine("File changed at: {0}", fullPath); // define file // @Service\Events\AddressEvents:eLogistics.Application.CQRS.Service.Events // define event: // !StreetChanged(string street, string houseNo) // define command: // ?ChangeStreet(Guid addressId, string street, string houseNo) IDictionary<string, FileToGenerate> files = new Dictionary<string, FileToGenerate>(StringComparer.InvariantCultureIgnoreCase); IEnumerable<string> lines = File.ReadLines(fullPath); string fileNamespace = string.Empty; FileToGenerate currentFile = null; foreach (string line in lines) { if (!string.IsNullOrWhiteSpace(line)) { char firstChar = line[0]; switch (firstChar) { case '#': { fileNamespace = line.Substring(1); break; } case '@': { FileToGenerate fileToGenerate = new FileToGenerate { Root = fullPath }; string fileName = fileToGenerate.Parse(line); if (!files.ContainsKey(fileName)) { files.Add(fileName, fileToGenerate); } else { fileToGenerate = files[fileName]; } currentFile = fileToGenerate; } break; case '!': case '?': { if (currentFile == null) throw new Exception("File must be defined first!"); ClassToGenerate classToGenerate = new ClassToGenerate(); classToGenerate.Parse(line); if (classToGenerate.ClassType == ClassType.Command || classToGenerate.ClassType == ClassType.Event) { string fileNameLowercase = Char.ToLower(currentFile.FileName[0]) + currentFile.FileName.Substring(1); classToGenerate.Properties.Insert(0, new PropertyToGenerate { Name = fileNameLowercase + "Id", UppercaseName = currentFile.FileName + "Id", Type = "Guid", NotIncludedInWhen = true }); } currentFile.Classes.Add(classToGenerate); } break; default: continue; } } } foreach (KeyValuePair<string, FileToGenerate> pair in files) { pair.Value.Namespace = fileNamespace; } GenerateFiles(fullPath, files); } catch (Exception exc) { WriteLine(exc.Message); } }