Skip to content

Commit

Permalink
Updated teh IO modules to allow for async operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Baudin999 committed Dec 26, 2019
1 parent 7fe9b0b commit cc1fb41
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 104 deletions.
5 changes: 3 additions & 2 deletions CLI/Controllers/ModuleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ public async Task<IActionResult> 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();
Expand Down
2 changes: 1 addition & 1 deletion Configuration/CarConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CarConfig>(new JsonTextReader(new StringReader(json)));

Expand Down
70 changes: 70 additions & 0 deletions Configuration/IO.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
60 changes: 30 additions & 30 deletions Project/FileProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Task<Module> 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)
{
Expand All @@ -91,7 +91,7 @@ public Task<Module> CreateModule(string name, string? code = null)
});
}

CreateNewModuleFile(modulePath, name, code);
_ = IO.SaveFile(modulePath, directoryName, $"# {name}");
}
catch (Exception ex)
{
Expand Down Expand Up @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions Project/IO.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
80 changes: 13 additions & 67 deletions Project/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public string Code
{
if (Generator.Code == String.Empty)
{
return ReadModuleText();
return IO.ReadFileText(this.FilePath);
}
else
{
Expand Down Expand Up @@ -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);
Expand All @@ -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...
Expand All @@ -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)
{
Expand All @@ -129,74 +132,17 @@ public void Clean()
}
}

public async Task SaveCode(string code) {
await IO.SaveFile(this.FilePath, code);
}

public IEnumerable<Descriptor> GetDescriptions()
{
var mapper = new DescriptionMapper(this.Generator, this.Name);
var nodes = mapper.Start().SelectMany(s => s);
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);
Expand Down
2 changes: 1 addition & 1 deletion Project/ModuleStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Project
{
public class ModuleStream : IDisposable
{

private Subject<ModuleStreamMessage> moduleMessageSubject;
private IDictionary<string, IDisposable> subscribers;

Expand Down
4 changes: 1 addition & 3 deletions Project/ProjectFilesWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cc1fb41

Please sign in to comment.