Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions Flow.net/IO/DeleteFiles.cs

This file was deleted.

120 changes: 120 additions & 0 deletions Flow.net/IO/FileActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.Concurrent;

namespace Flow.IO
{


public class GetFiles : PipelineAction
{
public string DirectoryPath { get; set; }
public string SearchPattern { get; set; } = "*";
public SearchOption SearchOption { get; set; } = SearchOption.TopDirectoryOnly;

protected override async Task<IPayload> DefaultHandlerAsync(IExecutionContext context, IPayload input)
{
var directory = Format(DirectoryPath, context, NullResult.Instance, this);
var searchPattern = Format(SearchPattern, context, NullResult.Instance, this);
var files = Directory.GetFiles(directory, searchPattern, SearchOption);
return await Task.FromResult(new FilePathCollection(files));
}
}

public class DeleteFiles : PipelineAction
{
public DeleteFiles()
{
SetTypeHandler<ValueCollection<string>>(async (context, input) => await Handler(this, context, input));
SetTypeHandler<FilePath>(async (context, input) => await Handler(this, context, input));
SetTypeHandler<PayloadCollection>(async (context, input) => await Handler(this, context, input));
}

private static async Task<IPayload> Handler(DeleteFiles that, IExecutionContext context, PayloadCollection paths)
{ return await Handler(that, context, paths.Cast<FilePath>().ToArray()); }

private static async Task<IPayload> Handler(DeleteFiles that, IExecutionContext context, params FilePath[] paths)
{ return await Handler(that, context, paths.Select(p => p.Path).ToArray()); }

private static async Task<IPayload> Handler(DeleteFiles that, IExecutionContext context, IEnumerable<string> paths)
{
return await Task.Run(
() =>
{
Parallel.ForEach(paths, file => File.Delete(file));
return NullResult.Instance;
});
}
}

public class CopyFiles : PipelineAction
{
public string DirectoryPath { get; set; }
public CopyFiles()
{
SetTypeHandler<ValueCollection<string>>(async (context, input) => await Handler(this, context, input));
SetTypeHandler<FilePath>(async (context, input) => await Handler(this, context, input));
SetTypeHandler<PayloadCollection>(async (context, input) => await Handler(this, context, input));
}

private static async Task<IPayload> Handler(CopyFiles that, IExecutionContext context, PayloadCollection paths)
{ return await Handler(that, context, paths.Cast<FilePath>().ToArray()); }

private static async Task<IPayload> Handler(CopyFiles that, IExecutionContext context, params FilePath[] paths)
{ return await Handler(that, context, paths.Select(p => p.Path).ToArray()); }

private static async Task<IPayload> Handler(CopyFiles that, IExecutionContext context, IEnumerable<string> paths)
{
return await Task.Run(
() =>
{
ConcurrentQueue<string> newPaths = new ConcurrentQueue<string>();
Parallel.ForEach(paths, file => {
string newFile = Path.Combine(that.DirectoryPath, Path.GetFileName(file));
File.Copy(file, newFile);
newPaths.Enqueue(newFile);
});
return new FilePathCollection(newPaths);
});
}
}

public class MoveFiles: PipelineAction
{
public string DirectoryPath { get; set; }
public MoveFiles()
{
// Why use value collection and file path?
// benefits of having a FilePathCollection? only I can think of is allowing stronger config valdiations, would only be useful for a flow.gui interface, but that's a worthy cause
SetTypeHandler<ValueCollection<string>>(async (context, input) => await Handler(this, context, input));
SetTypeHandler<FilePath>(async (context, input) => await Handler(this, context, input));
SetTypeHandler<PayloadCollection>(async (context, input) => await Handler(this, context, input));
}

private static async Task<IPayload> Handler(MoveFiles that, IExecutionContext context, PayloadCollection paths)
{ return await Handler(that, context, paths.Cast<FilePath>().ToArray()); }

private static async Task<IPayload> Handler(MoveFiles that, IExecutionContext context, params FilePath[] paths)
{ return await Handler(that, context, paths.Select(p => p.Path).ToArray()); }

private static async Task<IPayload> Handler(MoveFiles that, IExecutionContext context, IEnumerable<string> paths)
{
return await Task.Run(
() =>
{
ConcurrentQueue<string> newPaths = new ConcurrentQueue<string>();
Parallel.ForEach(paths, file => {
string newFile = Path.Combine(that.DirectoryPath, Path.GetFileName(file));
File.Move(file, newFile);
newPaths.Enqueue(newFile);
});
return new FilePathCollection(newPaths);
});
}
}

}
23 changes: 0 additions & 23 deletions Flow.net/IO/GetFiles.cs

This file was deleted.

44 changes: 44 additions & 0 deletions Flow.net/IO/MoveFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.Concurrent;

namespace Flow.IO
{
//public class MoveFiles : PipelineAction
//{
// public string Directory { get; set; }
// public MoveFiles()
// {
// // Why use value collection and file path?
// // benefits of having a FilePathCollection? only I can think of is allowing stronger config valdiations, would only be useful for a flow.gui interface, but that's a worthy cause
// SetTypeHandler<ValueCollection<string>>(async (context, input) => await Handler(this, context, input));
// SetTypeHandler<FilePath>(async (context, input) => await Handler(this, context, input));
// SetTypeHandler<PayloadCollection>(async (context, input) => await Handler(this, context, input));
// }

// private static async Task<IPayload> Handler(MoveFiles that, IExecutionContext context, PayloadCollection paths)
// { return await Handler(that, context, paths.Cast<FilePath>().ToArray()); }

// private static async Task<IPayload> Handler(MoveFiles that, IExecutionContext context, params FilePath[] paths)
// { return await Handler(that, context, paths.Select(p => p.Path).ToArray()); }

// private static async Task<IPayload> Handler(MoveFiles that, IExecutionContext context, IEnumerable<string> paths)
// {
// return await Task.Run(
// () =>
// {
// ConcurrentQueue<string> newPaths = new ConcurrentQueue<string>();
// Parallel.ForEach(paths, file => {
// string newFile = Path.Combine(that.Directory, Path.GetFileNameWithoutExtension(file));
// File.Move(file, newFile);
// newPaths.Enqueue(newFile);
// });
// return new FilePathCollection(newPaths);
// });
// }
//}
}
24 changes: 24 additions & 0 deletions Flow.net/Logging/TraceLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flow;
using Flow.Logging;

namespace FlowTest
{

public class TraceLogger : ILogger
{
public async Task LogErrorAsync(string message)
=> await Task.Run(() => Trace.TraceError(message));

public async Task LogInfoAsync(string message)
=> await Task.Run(() => Trace.TraceInformation(message));

public async Task LogWarningAsync(string message)
=> await Task.Run(() => Trace.TraceWarning(message));
}
}
25 changes: 24 additions & 1 deletion Flow.net/PipelineAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text.Json.Serialization;
Expand All @@ -15,6 +16,14 @@ private readonly IDictionary<Type, Func<IExecutionContext, IPayload, Task<IPaylo

public PipelineAction() { Name = GetType().Name; }


public string ScopedName { get; set; }

[DefaultValue(false)]
public bool ContextLogged { get; set; }



private string Name { get; }

[JsonIgnore]
Expand All @@ -35,7 +44,8 @@ public async Task<IPayload> ExecuteAsync(IExecutionContext context)
await context.LogInfoAsync($"{Name}:{Id} executing");
var result = await GetFormatter(type)(context, input);
await context.LogInfoAsync($"{Name}:{Id} compleated");
return result;
return await SetActionAccessibilityAsync(context, result);
//return result;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -84,5 +94,18 @@ private static SmartFormat.SmartFormatter CreateDefaultFormater()
formatter.Settings.ConvertCharacterStringLiterals = false;
return formatter;
}



private async Task<IPayload> SetActionAccessibilityAsync(IExecutionContext context, IPayload result)
{
if (!(ScopedName is null))
context.Scope[ScopedName] = result;

if (ContextLogged)
await context.LogInfoAsync(context.Serialize(true));

return result;
}
}
}
Loading