Beispiel #1
0
        /***************************************************/
        /**** Public Methods                            ****/
        /***************************************************/

        public IFSContainer Create(ILocatableResource resource, PushType pushType, PushConfig pushConfig)
        {
            if (resource == null)
            {
                return(null);
            }

            IFSContainer fileOrDir = resource.ToFiling();

            return(Create(fileOrDir, pushType, pushConfig));
        }
Beispiel #2
0
        private static string FullPath(this ILocatableResource fdr)
        {
            if (fdr?.Location == null)
            {
                return(null);
            }

            string location = Path.Combine(fdr.Location, string.IsNullOrWhiteSpace(fdr.Name) ? "" : fdr.Name);

            if (location.Contains("/") && location.Contains("\\"))
            {
                // If the location has inconsistent separators, assume the format of local windows FileSystem.
                location = location.Replace("/", "\\");
            }

            return(location);
        }
        public static oM.Adapters.File.IFSContainer ToFiling(this ILocatableResource iLocRes)
        {
            try
            {
                IFSContainer fileOrDir = null;

                if (iLocRes is IFile)
                {
                    fileOrDir = (FSFile)Path.Combine(iLocRes.Location, iLocRes.Name ?? "");
                }

                if (iLocRes is IDirectory)
                {
                    fileOrDir = (FSDirectory)Path.Combine(iLocRes.Location, iLocRes?.Name ?? "");
                }

                IContainableResource iContRes = iLocRes as IContainableResource;
                if (iContRes != null && fileOrDir != null)
                {
                    fileOrDir.Content = iContRes.Content;
                }

                return(fileOrDir);
            }
            catch { }

            try
            {
                FSDirectory dir = (FSDirectory)Path.Combine(iLocRes.Location, iLocRes.Name ?? "");
                return(dir);
            }
            catch { }

            BH.Engine.Base.Compute.RecordError($"The resource {iLocRes.IFullPath()} has an invalid path.");

            return(null);
        }
Beispiel #4
0
        /***************************************************/
        /**** Public Methods                            ****/
        /***************************************************/

        public IFSContainer CreateDirectory(FSDirectory dir, PushType pushType, PushConfig pushConfig)
        {
            List <BH.oM.Adapters.File.IFSContainer> createdDirs = new List <oM.Adapters.File.IFSContainer>();

            bool clearfile = pushType == PushType.DeleteThenCreate ? true : false;

            string dirFullPath = dir.IFullPath();
            bool   existed     = System.IO.Directory.Exists(dirFullPath);

            bool directoryCreated = true;

            try
            {
                if (pushType == PushType.DeleteThenCreate) // Deletes and recreates the directory.
                {
                    if (existed)
                    {
                        System.IO.Directory.Delete(dirFullPath, true); // Deletes the directory and all contents. To make things safer, a Warning is exposed in the Push before proceeding.
                    }
                    System.IO.Directory.CreateDirectory(dirFullPath);
                }
                else if (pushType == PushType.CreateOnly || pushType == PushType.CreateNonExisting || pushType == PushType.UpdateOrCreateOnly || pushType == PushType.UpdateOnly)
                {
                    // Create only directories that didn't exist.
                    if (pushType != PushType.UpdateOnly)
                    {
                        if (!existed)
                        {
                            System.IO.Directory.CreateDirectory(dirFullPath);
                        }
                        else
                        {
                            BH.Engine.Base.Compute.RecordNote($"Directory {dirFullPath} was not created as it existed already (Pushtype {pushType.ToString()} was specified).");
                            directoryCreated = false;
                        }
                    }

                    if (dir.Content != null && dir.Content.Any())
                    {
                        for (int i = 0; i < dir.Content.Count; i++)
                        {
                            ILocatableResource item = (dir.Content[i] as ILocatableResource).DeepClone();
                            if (item == null)
                            {
                                BH.Engine.Base.Compute.RecordWarning($"Cannot push Directory content {dir.Content[i].GetType().Name}.");
                            }

                            string itemFullPath = item.IFullPath();
                            if (string.IsNullOrWhiteSpace(itemFullPath) && !string.IsNullOrWhiteSpace(item.Name))
                            {
                                itemFullPath  = Path.Combine(dirFullPath, item.Name); // Default to Container Directory path.
                                item.Location = Path.GetDirectoryName(itemFullPath);
                            }

                            if (item.Location == dirFullPath)
                            {
                                Create(item, pushType, pushConfig);
                            }
                            else
                            {
                                BH.Engine.Base.Compute.RecordWarning($"The content of the Directory {dirFullPath} can't be Pushed because the content Path {itemFullPath} does not match the container Directory path.");
                            }
                        }
                    }
                }
                else
                {
                    BH.Engine.Base.Compute.RecordWarning($"The specified Pushtype of {pushType.ToString()} is not supported for {nameof(BH.oM.Adapters.File.FSDirectory)} objects.");
                    directoryCreated = false;
                }
            }
            catch (Exception e)
            {
                BH.Engine.Base.Compute.RecordError(e.Message);
            }

            if (directoryCreated || existed)
            {
                System.IO.DirectoryInfo      dirInfo    = new System.IO.DirectoryInfo(dirFullPath);
                oM.Adapters.File.FSDirectory createdDir = dirInfo.ToFiling();

                return(createdDir);
            }

            BH.Engine.Base.Compute.RecordError($"Could not create the Directory {dir.ToString()}.");
            return(null);
        }