Skip to content

Commit

Permalink
merge Package and Defition folders
Browse files Browse the repository at this point in the history
  • Loading branch information
mnhng committed Jul 14, 2015
1 parent 0c664c8 commit 537a3c5
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 107 deletions.
6 changes: 2 additions & 4 deletions src/DynamoCore/Core/DynamoMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ protected virtual string PackagesDirectory
get
{
// Only return the default package directory.
var packagesDirectories = pathManager.PackagesDirectories;
return packagesDirectories.ElementAt(0);
return pathManager.DefaultPackagesDirectory;
}
}

Expand All @@ -90,8 +89,7 @@ protected virtual string DefinitionsDirectory
get
{
// Only return the default custom node directory.
var definitionDirectories = pathManager.DefinitionDirectories;
return definitionDirectories.ElementAt(0);
return pathManager.DefaultUserDefinitions;
}
}

Expand Down
86 changes: 46 additions & 40 deletions src/DynamoCore/Core/PathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using Dynamo.Models;
using Dynamo.UI;
using DynamoUtilities;

namespace Dynamo.Core
{
Expand Down Expand Up @@ -68,8 +69,7 @@ class PathManager : IPathManager
private readonly string preferenceFilePath;
private readonly string galleryFilePath;

private readonly List<string> definitionDirectories;
private readonly List<string> packageDirectories;
private readonly List<string> rootDirectories;
private readonly HashSet<string> nodeDirectories;
private readonly HashSet<string> additionalResolutionPaths;
private readonly HashSet<string> preloadedLibraries;
Expand All @@ -88,9 +88,14 @@ public string CommonDataDirectory
get { return commonDataDir; }
}

public string DefaultUserDefinitions
{
get { return TransformPath(rootDirectories[0], DefinitionsDirectoryName); }
}

public IEnumerable<string> DefinitionDirectories
{
get { return definitionDirectories; }
get { return rootDirectories.Select(path => TransformPath(path, DefinitionsDirectoryName)); }
}

public string CommonDefinitions
Expand All @@ -103,9 +108,14 @@ public string LogDirectory
get { return logDirectory; }
}

public string DefaultPackagesDirectory
{
get { return TransformPath(rootDirectories[0], PackagesDirectoryName); }
}

public IEnumerable<string> PackagesDirectories
{
get { return packageDirectories; }
get { return rootDirectories.Select(path => TransformPath(path, PackagesDirectoryName)); }
}

public string ExtensionsDirectory
Expand Down Expand Up @@ -287,15 +297,9 @@ internal PathManager(PathManagerParams pathManagerParams)
var galleryDirectory = GetGalleryDirectory(commonDataDir);
galleryFilePath = Path.Combine(galleryDirectory, GalleryContentsFileName);

definitionDirectories = new List<string>
rootDirectories = new List<string>
{
Path.Combine(userDataDir, DefinitionsDirectoryName)
};


packageDirectories = new List<string>
{
Path.Combine(userDataDir, PackagesDirectoryName)
GetUserDataFolder()
};

nodeDirectories = new HashSet<string>
Expand All @@ -315,28 +319,22 @@ internal PathManager(PathManagerParams pathManagerParams)
/// </summary>
internal void EnsureDirectoryExistence()
{
if (packageDirectories.Count <= 0)
if (rootDirectories.Count <= 0)
{
throw new InvalidOperationException(
"At least one package directory must be specified");
}

if (definitionDirectories.Count <= 0)
{
throw new InvalidOperationException(
"At least one custom node directory must be specified");
"At least one custom package directory must be specified");
}

// User specific data folders.
CreateFolderIfNotExist(userDataDir);
CreateFolderIfNotExist(definitionDirectories[0]);
CreateFolderIfNotExist(logDirectory);
CreateFolderIfNotExist(packageDirectories[0]);
CreateFolderIfNotExist(backupDirectory);
PathHelper.CreateFolderIfNotExist(userDataDir);
PathHelper.CreateFolderIfNotExist(DefaultUserDefinitions);
PathHelper.CreateFolderIfNotExist(logDirectory);
PathHelper.CreateFolderIfNotExist(DefaultPackagesDirectory);
PathHelper.CreateFolderIfNotExist(backupDirectory);

// Common data folders for all users.
CreateFolderIfNotExist(commonDataDir);
CreateFolderIfNotExist(commonDefinitions);
PathHelper.CreateFolderIfNotExist(commonDataDir);
PathHelper.CreateFolderIfNotExist(commonDefinitions);
}

/// <summary>
Expand Down Expand Up @@ -366,16 +364,10 @@ internal string GetBackupFilePath(WorkspaceModel workspace)
return Path.Combine(BackupDirectory, fileName);
}

internal void LoadPackageFolders(IEnumerable<string> folders)
{
packageDirectories.Clear();
packageDirectories.AddRange(folders);
}

internal void LoadCustomNodeFolders(IEnumerable<string> folders)
internal void LoadCustomPackageFolders(IEnumerable<string> folders)
{
definitionDirectories.Clear();
definitionDirectories.AddRange(folders);
rootDirectories.Clear();
rootDirectories.AddRange(folders);
}

#endregion
Expand Down Expand Up @@ -412,7 +404,7 @@ private void LoadPathsFromResolver(IPathResolver pathResolver)
}
}

private string GetUserDataFolder(IPathResolver pathResolver)
internal string GetUserDataFolder(IPathResolver pathResolver = null)
{
if (pathResolver != null && !string.IsNullOrEmpty(pathResolver.UserDataRootFolder))
return GetDynamoDataFolder(pathResolver.UserDataRootFolder);
Expand All @@ -436,10 +428,24 @@ private string GetDynamoDataFolder(string folder)
String.Format("{0}.{1}", majorFileVersion, minorFileVersion));
}

private static void CreateFolderIfNotExist(string folderPath)
// This method is used to get the locations of packages folder or custom
// nodes folder given the root path. This is necessary because the packages
// may be in the root folder or in a packages subfolder of the root folder.
private string TransformPath(string root, string extension)
{
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
if (root.StartsWith(GetUserDataFolder()))
return Path.Combine(root, extension);
try
{
var subFolder = Path.Combine(root, extension);
if (Directory.Exists(subFolder))
return subFolder;
}
catch (IOException) { }
catch (ArgumentException) { }
catch (UnauthorizedAccessException) { }

return root;
}

private static string GetSamplesFolder(string dataRootDirectory)
Expand Down
15 changes: 4 additions & 11 deletions src/DynamoCore/Core/PreferenceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,9 @@ public int MaxNumRecentFiles
public List<string> BackupFiles { get; set; }

/// <summary>
/// A list of folders containing custom nodes.
/// A list of folders containing zero-touch nodes and custom nodes.
/// </summary>
public List<string> CustomNodeFolders { get; set; }

/// <summary>
/// A list of folders containing zero-touch nodes.
/// </summary>
public List<string> PackageFolders { get; set; }
public List<string> CustomPackageFolders { get; set; }

/// <summary>
/// A list of packages used by the Package Manager to determine
Expand Down Expand Up @@ -198,8 +193,7 @@ public PreferenceSettings()
BackupFilesCount = 1;
BackupFiles = new List<string>();

CustomNodeFolders = new List<string>();
PackageFolders = new List<string>();
CustomPackageFolders = new List<string>();
}

/// <summary>
Expand Down Expand Up @@ -274,8 +268,7 @@ public static PreferenceSettings Load(string filePath)
}
catch (Exception) { }

settings.PackageFolders = settings.PackageFolders.Distinct().ToList();
settings.CustomNodeFolders = settings.CustomNodeFolders.Distinct().ToList();
settings.CustomPackageFolders = settings.CustomPackageFolders.Distinct().ToList();

return settings;
}
Expand Down
11 changes: 11 additions & 0 deletions src/DynamoCore/Interfaces/IPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public interface IPathManager
/// </summary>
string CommonDataDirectory { get; }

/// <summary>
/// The default directory that contains custom nodes created by the user.
/// </summary>
string DefaultUserDefinitions { get; }

/// <summary>
/// Directories from where custom nodes are to be loaded. The implementor
/// of this interface method should always guarantee that a non-empty
Expand All @@ -91,6 +96,12 @@ public interface IPathManager
/// </summary>
string LogDirectory { get; }

/// <summary>
/// The default directory for saving packages downloaded through
/// the package manager. This directory is specific to the current user.
/// </summary>
string DefaultPackagesDirectory { get; }

/// <summary>
/// Directories from where packages are to be loaded. The implementor
/// of this interface method should always guarantee that a non-empty
Expand Down
3 changes: 1 addition & 2 deletions src/DynamoCore/Interfaces/IPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public interface IPreferences
List<string> RecentFiles { get; set; }
List<string> BackupFiles { get; set; }
List<string> PackageDirectoriesToUninstall { get; set; }
List<string> CustomNodeFolders { get; set; }
List<string> PackageFolders { get; set; }
List<string> CustomPackageFolders { get; set; }

/// <summary>
/// Call this method to serialize PreferenceSettings given the output
Expand Down
11 changes: 3 additions & 8 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,10 @@ protected DynamoModel(IStartConfiguration config)
// in AppData. If list of PackageFolders is empty, add the folder in AppData to the list since there
// is no additional location specified. Otherwise, update pathManager.PackageDirectories to include
// PackageFolders
if (PreferenceSettings.PackageFolders.Count == 0)
PreferenceSettings.PackageFolders = new List<string>(pathManager.PackagesDirectories);
if (PreferenceSettings.CustomPackageFolders.Count == 0)
PreferenceSettings.CustomPackageFolders = new List<string> {pathManager.GetUserDataFolder()};
else
pathManager.LoadPackageFolders(PreferenceSettings.PackageFolders);

if (PreferenceSettings.CustomNodeFolders.Count == 0)
PreferenceSettings.CustomNodeFolders = new List<string>(pathManager.DefinitionDirectories);
else
pathManager.LoadCustomNodeFolders(PreferenceSettings.CustomNodeFolders);
pathManager.LoadCustomPackageFolders(PreferenceSettings.CustomPackageFolders);


SearchModel = new NodeSearchModel();
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ public void ShowSaveDialogAndSaveResult(object parameter)
else if (vm.Model.CurrentWorkspace is CustomNodeWorkspaceModel)
{
var pathManager = vm.model.PathManager;
_fileDialog.InitialDirectory = pathManager.DefinitionDirectories.ElementAt(0);
_fileDialog.InitialDirectory = pathManager.DefaultUserDefinitions;
}

if (_fileDialog.ShowDialog() == DialogResult.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,9 @@ private void ShowAddFileDialogAndAdd()
else // use the definitions directory
{
var pathManager = dynamoViewModel.Model.PathManager;
if (Directory.Exists(pathManager.DefinitionDirectories.ElementAt(0)))
if (Directory.Exists(pathManager.DefaultUserDefinitions))
{
fDialog.InitialDirectory = pathManager.DefinitionDirectories.ElementAt(0);
fDialog.InitialDirectory = pathManager.DefaultUserDefinitions;
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/DynamoPackages/PackageDownloadHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public bool Extract(DynamoModel dynamoModel, out Package pkg)
throw new Exception(Properties.Resources.PackageEmpty);
}

var packagesDirectories = dynamoModel.PathManager.PackagesDirectories;
var defaultPackageDirectory = packagesDirectories.ElementAt(0);
var installedPath = BuildInstallDirectoryString(defaultPackageDirectory);
var packagesDirectory = dynamoModel.PathManager.DefaultPackagesDirectory;
var installedPath = BuildInstallDirectoryString(packagesDirectory);
Directory.CreateDirectory(installedPath);

// Now create all of the directories
Expand Down
12 changes: 4 additions & 8 deletions src/DynamoPackages/PackageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ public PackageLoader(IEnumerable<string> packagesDirectories)
throw new ArgumentNullException("packagesDirectories");

this.packagesDirectories.AddRange(packagesDirectories);
try
{
if (!Directory.Exists(DefaultPackagesDirectory))
Directory.CreateDirectory(DefaultPackagesDirectory);
}
catch (IOException) { }
catch (ArgumentException) { }
catch (UnauthorizedAccessException) { }
var error = PathHelper.CreateFolderIfNotExist(DefaultPackagesDirectory);

if (error != null)
Log(error);
}

private void OnPackageAdded(Package pkg)
Expand Down
1 change: 1 addition & 0 deletions src/DynamoUtilities/DynamoUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="AssemblyHelper.cs" />
<Compile Include="DataMarshaler.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="PathHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypeSwitch.cs" />
<Compile Include="XmlHelper.cs" />
Expand Down
27 changes: 27 additions & 0 deletions src/DynamoUtilities/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace DynamoUtilities
{
public class PathHelper
{
// This return an exception if any operation failed and the folder
// wasn't created. It's the resposibility of the caller of this function
// to check whether the folder creation is successful or not.
public static Exception CreateFolderIfNotExist(string folderPath)
{
try
{
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
}
catch (IOException ex) { return ex; }
catch (ArgumentException ex) { return ex; }
catch (UnauthorizedAccessException ex) { return ex; }

return null;
}
}
}
18 changes: 5 additions & 13 deletions test/DynamoCoreTests/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ public void LoadInvalidLocationsFromSetting()
var settings = PreferenceSettings.Load(filePath);

Assert.NotNull(settings);
Assert.AreEqual(4, settings.PackageFolders.Count);
Assert.AreEqual(3, settings.CustomNodeFolders.Count);
Assert.AreEqual(4, settings.CustomPackageFolders.Count);

var expectedPackageFolders = new List<string> { @"C:\folder_name_with_invalid_:*?|_characters\foobar",
@"C:\this_folder_doesn't_exist",
@"X:\this_drive_doesn't_exist",
@"\\unreachable_machine\share_packages" };
@"C:\this_folder_doesn't_exist",
@"X:\this_drive_doesn't_exist",
@"\\unreachable_machine\share_packages" };

IEnumerable<bool> comparisonResult = settings.PackageFolders.Zip(expectedPackageFolders, string.Equals);
Assert.IsFalse(comparisonResult.Any(isEqual => !isEqual));

var expectedCustomNodeFolders = new List<string> { @"\\test_machine\non-existent_folder",
@"D:\custom_nodes",
@"E:\this_folder_doesn't_exist" };

comparisonResult = settings.CustomNodeFolders.Zip(expectedCustomNodeFolders, string.Equals);
IEnumerable<bool> comparisonResult = settings.CustomPackageFolders.Zip(expectedPackageFolders, string.Equals);
Assert.IsFalse(comparisonResult.Any(isEqual => !isEqual));
}

Expand Down
Loading

0 comments on commit 537a3c5

Please sign in to comment.