Skip to content

Commit

Permalink
Refactor extension workflow search to helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Aug 6, 2024
1 parent 3a76150 commit c992a45
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 56 deletions.
59 changes: 3 additions & 56 deletions Bonsai.Editor/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public partial class EditorForm : Form
const float DefaultEditorScale = 1.0f;
const string EditorUid = "editor";
const string BonsaiPackageName = "Bonsai";
const string ExtensionsDirectory = "Extensions";
const string DefinitionsDirectory = "Definitions";
const string WorkflowCategoryName = "Workflow";
const string SubjectCategoryName = "Subject";
const string DefaultWorkflowNamespace = "Unspecified";
static readonly AttributeCollection DesignTimeAttributes = new AttributeCollection(BrowsableAttribute.Yes, DesignTimeVisibleAttribute.Yes);
static readonly AttributeCollection RuntimeAttributes = AttributeCollection.FromExisting(DesignTimeAttributes, DesignOnlyAttribute.No);
static readonly char[] ToolboxArgumentSeparator = new[] { ' ' };
Expand Down Expand Up @@ -307,7 +305,7 @@ protected override void OnLoad(EventArgs e)

directoryToolStripItem.Text = currentDirectory;
openWorkflowDialog.InitialDirectory = saveWorkflowDialog.InitialDirectory = currentDirectory;
extensionsPath = new DirectoryInfo(Path.Combine(workflowBaseDirectory, ExtensionsDirectory));
extensionsPath = new DirectoryInfo(Path.Combine(workflowBaseDirectory, Project.ExtensionsDirectory));
if (extensionsPath.Exists) OnExtensionsDirectoryChanged(EventArgs.Empty);

InitializeEditorToolboxTypes();
Expand Down Expand Up @@ -506,7 +504,7 @@ IObservable<Unit> RefreshWorkflowExtensions()
.Merge(start, changed, created, deleted, renamed)
.Throttle(TimeSpan.FromSeconds(1), Scheduler.Default)
.Select(evt => workflowExtensions
.Concat(FindWorkflows(ExtensionsDirectory))
.Concat(Project.EnumerateExtensionWorkflows(extensionsPath.FullName))
.GroupBy(x => x.Namespace)
.ToList())
.ObserveOn(formScheduler)
Expand Down Expand Up @@ -546,57 +544,6 @@ IObservable<Unit> RefreshWorkflowExtensions()
.Select(xs => Unit.Default);
}

static IEnumerable<WorkflowElementDescriptor> FindWorkflows(string basePath)
{
int basePathLength;
string[] workflowFiles;
if (!Path.IsPathRooted(basePath))
{
var currentDirectory = Environment.CurrentDirectory;
basePath = Path.Combine(currentDirectory, basePath);
basePathLength = currentDirectory.Length;
}
else basePathLength = basePath.Length;

try { workflowFiles = Directory.GetFiles(basePath, "*" + Project.BonsaiExtension, SearchOption.AllDirectories); }
catch (UnauthorizedAccessException) { yield break; }
catch (DirectoryNotFoundException) { yield break; }

foreach (var fileName in workflowFiles)
{
var description = string.Empty;
try
{
using (var reader = XmlReader.Create(fileName, new XmlReaderSettings { IgnoreWhitespace = true }))
{
reader.ReadStartElement(typeof(WorkflowBuilder).Name);
if (reader.Name == nameof(WorkflowBuilder.Description))
{
reader.ReadStartElement();
description = reader.Value;
}
}
}
catch (SystemException) { continue; }

var relativePath = fileName.Substring(basePathLength)
.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var fileNamespace = Path.GetDirectoryName(relativePath)
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.Replace(Path.DirectorySeparatorChar, ExpressionHelper.MemberSeparator.First());
if (string.IsNullOrEmpty(fileNamespace)) fileNamespace = DefaultWorkflowNamespace;

yield return new WorkflowElementDescriptor
{
Name = Path.GetFileNameWithoutExtension(relativePath),
Namespace = fileNamespace,
FullyQualifiedName = relativePath,
Description = description,
ElementTypes = new[] { ~ElementCategory.Workflow }
};
}
}

IObservable<Unit> InitializeTypeVisualizers()
{
var visualizerMapping = from typeVisualizer in visualizerElements
Expand Down Expand Up @@ -643,7 +590,7 @@ IEnumerable<WorkflowElementDescriptor> InitializeWorkflowExtensions(IEnumerable<

static string GetPackageDisplayName(string packageKey)
{
if (packageKey == null) return ExtensionsDirectory;
if (packageKey == null) return Project.ExtensionsDirectory;
if (packageKey == BonsaiPackageName) return packageKey;
return packageKey.Replace(BonsaiPackageName + ".", string.Empty);
}
Expand Down
45 changes: 45 additions & 0 deletions Bonsai.Editor/Project.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Bonsai.Design;

namespace Bonsai.Editor
{
static class Project
{
const string DefaultWorkflowNamespace = "Unspecified";
internal const string BonsaiExtension = ".bonsai";
internal const string LayoutExtension = ".layout";
internal const string ExtensionsDirectory = "Extensions";

public static string GetCurrentBaseDirectory()
{
Expand Down Expand Up @@ -50,5 +56,44 @@ internal static string GetLegacyLayoutConfigPath(string fileName)
{
return Path.ChangeExtension(fileName, Path.GetExtension(fileName) + LayoutExtension);
}

internal static IEnumerable<WorkflowElementDescriptor> EnumerateExtensionWorkflows(string basePath)
{
IEnumerable<string> workflowFiles;
try { workflowFiles = Directory.EnumerateFiles(basePath, "*" + BonsaiExtension, SearchOption.AllDirectories); }
catch (UnauthorizedAccessException) { yield break; }
catch (DirectoryNotFoundException) { yield break; }

foreach (var fileName in workflowFiles)
{
var description = string.Empty;
try
{
using var reader = XmlReader.Create(fileName, new XmlReaderSettings { IgnoreWhitespace = true });
reader.ReadStartElement(typeof(WorkflowBuilder).Name);
if (reader.Name == nameof(WorkflowBuilder.Description))
{
reader.ReadStartElement();
description = reader.Value;
}
}
catch (SystemException) { continue; }

var relativePath = PathConvert.GetProjectPath(fileName);
var fileNamespace = Path.GetDirectoryName(relativePath)
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.Replace(Path.DirectorySeparatorChar, ExpressionHelper.MemberSeparator.First());
if (string.IsNullOrEmpty(fileNamespace)) fileNamespace = DefaultWorkflowNamespace;

yield return new WorkflowElementDescriptor
{
Name = Path.GetFileNameWithoutExtension(relativePath),
Namespace = fileNamespace,
FullyQualifiedName = relativePath,
Description = description,
ElementTypes = new[] { ~ElementCategory.Workflow }
};
}
}
}
}

0 comments on commit c992a45

Please sign in to comment.