Skip to content

Commit

Permalink
Added awaiting to the DeleteModule, lets see if the tests succeede
Browse files Browse the repository at this point in the history
  • Loading branch information
Baudin999 committed Dec 26, 2019
1 parent 535e5d6 commit 9d7b6fb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 47 deletions.
6 changes: 3 additions & 3 deletions Project/FileProject.Watch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public void Watch()
// }
//});

ProjectWatcher.ModuleStream.Subscribe("OnDelete", MessageType.ModuleDeleted, msm =>
ProjectWatcher.ModuleStream.Subscribe("OnDelete", MessageType.ModuleDeleted, async msm =>
{
var module = Modules.FirstOrDefault(m => m.Name == msm.ModuleName);
if (!(module is null))
{
module.Clean();
Modules.Remove(module);
await module.Clean();
}
});

Expand All @@ -54,7 +54,7 @@ public void Watch()
var moduleOld = Modules.FirstOrDefault(m => m.Name == msm.ModuleName);
if (!(moduleOld is null))
{
moduleOld.Clean();
await moduleOld.Clean();
Modules.Remove(moduleOld);
}
var module = new Module(msm.FileFullPath, this.BasePath, this);
Expand Down
84 changes: 42 additions & 42 deletions Project/FileProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,35 @@ public List<IASTNode> GetAstForModule(string moduleName)
return this.Modules.FirstOrDefault(m => m.Name == name);
}

public Task<bool> DeleteModule(string moduleName)
public async Task<bool> DeleteModule(string moduleName)
{
var tcs = new TaskCompletionSource<bool>();
try
{
var module = FindModule(moduleName);
if (module is null) return Task.FromResult(false);
if (module is null) return await Task.FromResult(false);

if (this.ProjectWatcher != null)
{
Task.Factory.StartNew(() =>
{
Action cleanup = () => { };
var listenerName = "CREATEOR: " + DateTime.Now + new Random().Next(1000000).ToString();
cleanup = this.ProjectWatcher.ModuleStream.Subscribe(listenerName, MessageType.ModuleDeleted, msm =>
{
tcs.TrySetResult(true);
Task.Run(cleanup);
});
});
await Task.Factory.StartNew(() =>
{
Action cleanup = () => { };
var listenerName = "CREATEOR: " + DateTime.Now + new Random().Next(1000000).ToString();
cleanup = this.ProjectWatcher.ModuleStream.Subscribe(listenerName, MessageType.ModuleDeleted, msm =>
{
tcs.TrySetResult(true);
Task.Run(cleanup);
});
});
}

File.Delete(module.FilePath);
await IO.DeleteFile(module.FilePath);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
return tcs.Task;
return await tcs.Task;
}

public void Dispose()
Expand All @@ -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
10 changes: 10 additions & 0 deletions Project/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,15 @@ internal static async Task SaveFile(string fileName, string directoryName, strin
throw ioe;
}
}

internal static async Task DeleteFile(string fileName)
{
await Task.Run(() => File.Delete(fileName));
}

internal static async Task DeleteDirectory(string directoryName)
{
await Task.Run(() => Directory.Delete(directoryName, true));
}
}
}
5 changes: 3 additions & 2 deletions Project/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ public async Task SaveModuleOutput(bool decend = true, bool suppressMessage = fa
});
}
}
public void Clean()
public async Task Clean()
{
try
{
if (OutPath != null && Directory.Exists(OutPath))
{
Task.Factory.StartNew(() => Directory.Delete(OutPath, true));
//Directory.Delete(OutPath, true);
await Task.Run(() => Directory.Delete(OutPath, true));
}
}
catch (Exception ex)
Expand Down

0 comments on commit 9d7b6fb

Please sign in to comment.