diff --git a/CLI/Controllers/ModuleController.cs b/CLI/Controllers/ModuleController.cs index 42133ad..4b0cb49 100644 --- a/CLI/Controllers/ModuleController.cs +++ b/CLI/Controllers/ModuleController.cs @@ -108,11 +108,12 @@ public async Task SaveModuleTextAsync(string module) { return await Task.Run(NotFound); } - else { + else { + using (var reader = new StreamReader(Request.Body, Encoding.UTF8)) { var code = await reader.ReadToEndAsync(); - m.SaveCode(code); + await m.SaveCode(code); var result = Ok(m.Generator.Code); reader.Close(); reader.Dispose(); diff --git a/Configuration/CarConfig.cs b/Configuration/CarConfig.cs index b6531a7..43b900e 100644 --- a/Configuration/CarConfig.cs +++ b/Configuration/CarConfig.cs @@ -20,7 +20,7 @@ public static CarConfig Load(string path) { if (File.Exists(path)) { - var json = File.ReadAllText(path); + var json = IO.ReadFileText(path); var serializer = new JsonSerializer(); var result = serializer.Deserialize(new JsonTextReader(new StringReader(json))); diff --git a/Configuration/IO.cs b/Configuration/IO.cs new file mode 100644 index 0000000..81585b2 --- /dev/null +++ b/Configuration/IO.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using System.Text; + +namespace Configuration +{ + internal static class IO + { + internal static string ReadFileText(string filePath) + { + try + { + if (!File.Exists(filePath)) return ""; + using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + using (var sr = new StreamReader(fs)) + { + return sr.ReadToEnd(); + } + } + } + catch (IOException ioe) + { + Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + throw ioe; + } + } + + internal static async Task SaveFile(string filePath, string source) + { + try + { + using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) + { + using (var sr = new StreamWriter(fs)) + { + await sr.WriteAsync(source); + sr.Flush(); + sr.Close(); + sr.Dispose(); + } + fs.Close(); + fs.Dispose(); + } + } + catch (IOException ioe) + { + Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + throw ioe; + } + } + + internal static async Task SaveFile(string fileName, string directoryName, string source) + { + try + { + var filePath = System.IO.Path.GetFullPath(fileName); + Directory.CreateDirectory(directoryName); + await SaveFile(filePath, source); + } + catch (IOException ioe) + { + Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + throw ioe; + } + } + } +} diff --git a/Project/FileProject.cs b/Project/FileProject.cs index b009d62..9eb9d28 100644 --- a/Project/FileProject.cs +++ b/Project/FileProject.cs @@ -73,7 +73,7 @@ public Task CreateModule(string name, string? code = null) var fileName = String.Join("/", name.Split(".").Select(UppercaseFirst)) + ".car"; var modulePath = Path.GetFullPath(fileName, this.BasePath); var directoryName = Path.GetDirectoryName(modulePath); - Directory.CreateDirectory(directoryName); + //Directory.CreateDirectory(directoryName); if (this.ProjectWatcher != null) { @@ -91,7 +91,7 @@ public Task CreateModule(string name, string? code = null) }); } - CreateNewModuleFile(modulePath, name, code); + _ = IO.SaveFile(modulePath, directoryName, $"# {name}"); } catch (Exception ex) { @@ -170,34 +170,34 @@ public void Dispose() } } - private void CreateNewModuleFile(string modulePath, string moduleName, string? code = null) - { - try - { - using (var fs = new FileStream(modulePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)) - { - using (var sr = new StreamWriter(fs)) - { - sr.Write(code ?? $@" -# {moduleName} - -Have fun with your module! -"); - sr.Flush(); - sr.Close(); - sr.Dispose(); - } - fs.Close(); - fs.Dispose(); - - } - } - catch (IOException ioe) - { - Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); - throw ioe; - } - } +// private void CreateNewModuleFile(string modulePath, string moduleName, string? code = null) +// { +// try +// { +// using (var fs = new FileStream(modulePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)) +// { +// using (var sr = new StreamWriter(fs)) +// { +// sr.Write(code ?? $@" +//# {moduleName} + +//Have fun with your module! +//"); +// sr.Flush(); +// sr.Close(); +// sr.Dispose(); +// } +// fs.Close(); +// fs.Dispose(); + +// } +// } +// catch (IOException ioe) +// { +// Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); +// throw ioe; +// } +// } static string UppercaseFirst(string s) diff --git a/Project/IO.cs b/Project/IO.cs new file mode 100644 index 0000000..1ac939d --- /dev/null +++ b/Project/IO.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using System.Text; + +namespace Project +{ + internal static class IO + { + internal static string ReadFileText(string filePath) + { + try + { + if (!File.Exists(filePath)) return ""; + using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + using (var sr = new StreamReader(fs)) + { + return sr.ReadToEnd(); + } + } + } + catch (IOException ioe) + { + Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + throw ioe; + } + } + + internal static async Task SaveFile(string filePath, string source) + { + try + { + using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) + { + using (var sr = new StreamWriter(fs)) + { + await sr.WriteAsync(source); + sr.Flush(); + sr.Close(); + sr.Dispose(); + } + fs.Close(); + fs.Dispose(); + } + } + catch (IOException ioe) + { + Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + throw ioe; + } + } + + internal static async Task SaveFile(string fileName, string directoryName, string source) + { + try + { + Directory.CreateDirectory(directoryName); + await SaveFile(fileName, source); + } + catch (IOException ioe) + { + Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + throw ioe; + } + } + } +} diff --git a/Project/Module.cs b/Project/Module.cs index 46c0d69..2e46b1f 100644 --- a/Project/Module.cs +++ b/Project/Module.cs @@ -40,7 +40,7 @@ public string Code { if (Generator.Code == String.Empty) { - return ReadModuleText(); + return IO.ReadFileText(this.FilePath); } else { @@ -74,7 +74,7 @@ public Module(string path, string basePath, FileProject project) public void Parse() { - var code = ReadModuleText() + Environment.NewLine; + var code = IO.ReadFileText(this.FilePath) + Environment.NewLine; this.Generator = new ASTGenerator(code, this.Name); this.LastParsed = DateTime.Now; this.Transpiler = new Transpiler(this.Generator, this.Project); @@ -89,12 +89,12 @@ public async Task SaveModuleOutput(bool decend = true, bool suppressMessage = fa { Console.WriteLine($"Perfectly parsed: {Name}"); } - await SaveResult("Model.xsd", Transpiler.XsdToString()); - await SaveResult("index.html", Transpiler.HtmlToString()); + await IO.SaveFile("Model.xsd", this.OutPath, Transpiler.XsdToString()); + await IO.SaveFile("index.html", this.OutPath, Transpiler.HtmlToString()); foreach (var (key, value) in Transpiler.JsonToString()) { - await SaveResult(key, value); + await IO.SaveFile(key, this.OutPath, value); } // Trickery to not parse infinately... @@ -120,7 +120,10 @@ public void Clean() { try { - Task.Factory.StartNew(path => Directory.Delete((string)path, true), OutPath); + if (OutPath != null && Directory.Exists(OutPath)) + { + Task.Factory.StartNew(() => Directory.Delete(OutPath, true)); + } } catch (Exception ex) { @@ -129,6 +132,10 @@ public void Clean() } } + public async Task SaveCode(string code) { + await IO.SaveFile(this.FilePath, code); + } + public IEnumerable GetDescriptions() { var mapper = new DescriptionMapper(this.Generator, this.Name); @@ -136,67 +143,6 @@ public IEnumerable GetDescriptions() return nodes.ToList(); } - public bool SaveCode(string source) - { - try - { - - File.WriteAllText(this.FilePath, source); - return true; - } - catch (Exception ex) - { - Console.WriteLine(ex); - return false; - } - } - - private string ReadModuleText() - { - try - { - if (!File.Exists(this.FilePath)) return ""; - using (var fs = new FileStream(this.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - using (var sr = new StreamReader(fs)) - { - return sr.ReadToEnd(); - } - } - } - catch (IOException ioe) - { - Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); - throw ioe; - } - } - - private async Task SaveResult(string fileName, string source) - { - try - { - var filePath = System.IO.Path.GetFullPath(fileName, OutPath); - Directory.CreateDirectory(OutPath); - using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) - { - using (var sr = new StreamWriter(fs)) - { - await sr.WriteAsync(source); - sr.Flush(); - sr.Close(); - sr.Dispose(); - } - fs.Close(); - fs.Dispose(); - } - } - catch (IOException ioe) - { - Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); - throw ioe; - } - } - private string CreateModuleName() { return Module.FromPathToName(this.FilePath, this.BasePath); diff --git a/Project/ModuleStream.cs b/Project/ModuleStream.cs index 3233efa..0d12a78 100644 --- a/Project/ModuleStream.cs +++ b/Project/ModuleStream.cs @@ -7,7 +7,7 @@ namespace Project { public class ModuleStream : IDisposable { - + private Subject moduleMessageSubject; private IDictionary subscribers; diff --git a/Project/ProjectFilesWatcher.cs b/Project/ProjectFilesWatcher.cs index 7b73bd7..f73b0fc 100644 --- a/Project/ProjectFilesWatcher.cs +++ b/Project/ProjectFilesWatcher.cs @@ -24,9 +24,7 @@ public void Start() // Watch for changes in LastAccess and LastWrite times, and // the renaming of files or directories. - NotifyFilter = NotifyFilters.LastWrite - | NotifyFilters.FileName - | NotifyFilters.DirectoryName, + NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName, // Only watch text files. Filter = "*.car"