Skip to content

Commit

Permalink
Refactor base directory operations to helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Sep 3, 2024
1 parent b8ece22 commit 39a8fa3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
22 changes: 5 additions & 17 deletions Bonsai.Editor/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public partial class EditorForm : Form
{
const float DefaultEditorScale = 1.0f;
const string EditorUid = "editor";
const string BonsaiExtension = ".bonsai";
const string BonsaiPackageName = "Bonsai";
const string ExtensionsDirectory = "Extensions";
const string DefinitionsDirectory = "Definitions";
Expand Down Expand Up @@ -288,7 +287,7 @@ protected override void OnLoad(EventArgs e)
var initialFileName = FileName;
var validFileName =
!string.IsNullOrEmpty(initialFileName) &&
Path.GetExtension(initialFileName) == BonsaiExtension &&
Path.GetExtension(initialFileName) == Project.BonsaiExtension &&
File.Exists(initialFileName);

var formClosed = Observable.FromEventPattern<FormClosedEventHandler, FormClosedEventArgs>(
Expand All @@ -298,19 +297,8 @@ protected override void OnLoad(EventArgs e)
InitializeWorkflowFileWatcher().TakeUntil(formClosed).Subscribe();
updatesAvailable.TakeUntil(formClosed).ObserveOn(formScheduler).Subscribe(HandleUpdatesAvailable);

var currentDirectory = Path.GetFullPath(Environment.CurrentDirectory).TrimEnd('\\');
var appDomainBaseDirectory = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory).TrimEnd('\\');
var currentDirectoryRestricted = currentDirectory == appDomainBaseDirectory;
if (!EditorSettings.IsRunningOnMono)
{
var systemPath = Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.System)).TrimEnd('\\');
var systemX86Path = Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)).TrimEnd('\\');
currentDirectoryRestricted |= currentDirectory == systemPath || currentDirectory == systemX86Path;
}

var workflowBaseDirectory = validFileName
? Path.GetDirectoryName(initialFileName)
: (!currentDirectoryRestricted ? currentDirectory : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
var currentDirectory = Project.GetCurrentBaseDirectory(out bool currentDirectoryRestricted);
var workflowBaseDirectory = validFileName ? Project.GetWorkflowBaseDirectory(initialFileName) : currentDirectory;
if (currentDirectoryRestricted)
{
currentDirectory = workflowBaseDirectory;
Expand Down Expand Up @@ -570,7 +558,7 @@ static IEnumerable<WorkflowElementDescriptor> FindWorkflows(string basePath)
}
else basePathLength = basePath.Length;

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

Expand Down Expand Up @@ -2287,7 +2275,7 @@ static bool TryGetAssemblyResource(string path, out string assemblyName, out str
{
const char AssemblySeparator = ':';
var separatorIndex = path.IndexOf(AssemblySeparator);
if (separatorIndex >= 0 && !Path.IsPathRooted(path) && path.EndsWith(BonsaiExtension))
if (separatorIndex >= 0 && !Path.IsPathRooted(path) && path.EndsWith(Project.BonsaiExtension))
{
path = Path.ChangeExtension(path, null);
var nameElements = path.Split(new[] { AssemblySeparator }, 2);
Expand Down
37 changes: 37 additions & 0 deletions Bonsai.Editor/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.IO;

namespace Bonsai.Editor
{
static class Project
{
internal const string BonsaiExtension = ".bonsai";

public static string GetCurrentBaseDirectory()
{
return GetCurrentBaseDirectory(out _);
}

public static string GetCurrentBaseDirectory(out bool currentDirectoryRestricted)
{
var currentDirectory = Path.GetFullPath(Environment.CurrentDirectory).TrimEnd('\\');
var appDomainBaseDirectory = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory).TrimEnd('\\');
currentDirectoryRestricted = currentDirectory == appDomainBaseDirectory;
if (!EditorSettings.IsRunningOnMono)
{
var systemPath = Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.System)).TrimEnd('\\');
var systemX86Path = Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)).TrimEnd('\\');
currentDirectoryRestricted |= currentDirectory == systemPath || currentDirectory == systemX86Path;
}

return !currentDirectoryRestricted
? currentDirectory
: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}

public static string GetWorkflowBaseDirectory(string fileName)
{
return Path.GetDirectoryName(fileName);
}
}
}

0 comments on commit 39a8fa3

Please sign in to comment.