GetDestinationPath() public static method

public static GetDestinationPath ( string sourceRootPath, string destinationRootPath, System.IO.Abstractions.FileSystemInfoBase info ) : string
sourceRootPath string
destinationRootPath string
info System.IO.Abstractions.FileSystemInfoBase
return string
Esempio n. 1
0
        private void SmartCopy(string sourcePath,
                               string destinationPath,
                               string targetSubFolder,
                               DirectoryInfoBase sourceDirectory,
                               DirectoryInfoBase destinationDirectory)
        {
            if (IgnorePath(sourceDirectory))
            {
                return;
            }

            // No need to copy a directory to itself
            if (destinationPath == sourceDirectory.FullName)
            {
                return;
            }

            if (!destinationDirectory.Exists)
            {
                destinationDirectory.Create();
                if (_options.CopyMetaData)
                {
                    destinationDirectory.Attributes = sourceDirectory.Attributes;
                }
            }

            var destFilesLookup   = FileSystemHelpers.GetFiles(destinationDirectory);
            var sourceFilesLookup = FileSystemHelpers.GetFiles(sourceDirectory);

            foreach (var destFile in destFilesLookup.Values)
            {
                if (IgnorePath(destFile))
                {
                    continue;
                }

                // If the file doesn't exist in the source, only delete if:
                // 1. We have no previous directory
                // 2. We have a previous directory and the file exists there

                // Trim the start destinationFilePath
                string previousPath = FileSystemHelpers.GetRelativePath(destinationPath, destFile.FullName);
                if (!sourceFilesLookup.ContainsKey(destFile.Name) && DoesPathExistsInManifest(previousPath, targetSubFolder))
                {
                    _logger.Log("Deleting file: '{0}'", previousPath);
                    OperationManager.Attempt(() => SmartDeleteFile(destFile));
                }
            }

            foreach (var sourceFile in sourceFilesLookup.Values)
            {
                if (IgnorePath(sourceFile))
                {
                    continue;
                }

                _nextManifest.AddPath(sourcePath, sourceFile.FullName, targetSubFolder);

                // if the file exists in the destination then only copy it again if it's
                // last write time is different than the same file in the source (only if it changed)
                FileInfoBase targetFile;
                if (destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) &&
                    sourceFile.LastWriteTimeUtc == targetFile.LastWriteTimeUtc)
                {
                    continue;
                }

                string path = FileSystemHelpers.GetDestinationPath(sourcePath, destinationPath, sourceFile);

                var details = FileSystemHelpers.GetRelativePath(sourcePath, sourceFile.FullName) + (_options.CopyMetaData ? " " + ShorthandAttributes(sourceFile) : String.Empty);

                if (sourceFile.IsWebConfig())
                {
                    // If current file is web.config check the content sha1.
                    if (!destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) ||
                        !sourceFile.ComputeSha1().Equals(targetFile.ComputeSha1()))
                    {
                        // Save the file path to copy later for copying web.config forces an appDomain
                        // restart right away without respecting waitChangeNotification
                        _filesToCopyLast.Add(Tuple.Create(sourceFile, path, details));
                    }
                    continue;
                }

                // Otherwise, copy the file
                _logger.Log("Copying file: '{0}'", details);
                OperationManager.Attempt(() => SmartCopyFile(sourceFile, path));
            }

            var sourceDirectoryLookup = FileSystemHelpers.GetDirectories(sourceDirectory);
            var destDirectoryLookup   = FileSystemHelpers.GetDirectories(destinationDirectory);

            foreach (var destSubDirectory in destDirectoryLookup.Values)
            {
                // If the directory doesn't exist in the source, only delete if:
                // 1. We have no previous directory
                // 2. We have a previous directory and the file exists there

                if (!sourceDirectoryLookup.ContainsKey(destSubDirectory.Name))
                {
                    SmartDirectoryDelete(destSubDirectory, destinationPath, targetSubFolder);
                }
            }

            foreach (var sourceSubDirectory in sourceDirectoryLookup.Values)
            {
                DirectoryInfoBase targetSubDirectory;
                if (!destDirectoryLookup.TryGetValue(sourceSubDirectory.Name, out targetSubDirectory))
                {
                    string path = FileSystemHelpers.GetDestinationPath(sourcePath, destinationPath, sourceSubDirectory);
                    targetSubDirectory = CreateDirectoryInfo(path);
                }

                _nextManifest.AddPath(sourcePath, sourceSubDirectory.FullName, targetSubFolder);

                // Sync all sub directories
                SmartCopy(sourcePath, destinationPath, targetSubFolder, sourceSubDirectory, targetSubDirectory);
            }
        }
Esempio n. 2
0
        private void SmartCopy(string sourcePath,
                               string destinationPath,
                               DirectoryInfoBase sourceDirectory,
                               DirectoryInfoBase destinationDirectory)
        {
            if (IgnorePath(sourceDirectory))
            {
                return;
            }

            // No need to copy a directory to itself
            if (destinationPath == sourceDirectory.FullName)
            {
                return;
            }

            if (!destinationDirectory.Exists)
            {
                destinationDirectory.Create();
                if (_options.CopyMetaData)
                {
                    destinationDirectory.Attributes = sourceDirectory.Attributes;
                }
            }

            var destFilesLookup   = FileSystemHelpers.GetFiles(destinationDirectory);
            var sourceFilesLookup = FileSystemHelpers.GetFiles(sourceDirectory);

            foreach (var destFile in destFilesLookup.Values)
            {
                if (IgnorePath(destFile))
                {
                    continue;
                }

                // If the file doesn't exist in the source, only delete if:
                // 1. We have no previous directory
                // 2. We have a previous directory and the file exists there

                // Trim the start path
                string previousPath = FileSystemHelpers.GetRelativePath(destinationPath, destFile.FullName);
                if (!sourceFilesLookup.ContainsKey(destFile.Name) && DoesPathExistsInManifest(previousPath))
                {
                    _logger.Log("Deleting file: '{0}'", previousPath);
                    OperationManager.Attempt(() => destFile.Delete());
                }
            }

            foreach (var sourceFile in sourceFilesLookup.Values)
            {
                if (IgnorePath(sourceFile))
                {
                    continue;
                }

                _nextManifest.AddPath(sourcePath, sourceFile.FullName);

                // if the file exists in the destination then only copy it again if it's
                // last write time is different than the same file in the source (only if it changed)
                FileInfoBase targetFile;
                if (destFilesLookup.TryGetValue(sourceFile.Name, out targetFile) &&
                    sourceFile.LastWriteTimeUtc == targetFile.LastWriteTimeUtc)
                {
                    continue;
                }

                // Otherwise, copy the file
                string path = FileSystemHelpers.GetDestinationPath(sourcePath, destinationPath, sourceFile);

                var details = FileSystemHelpers.GetRelativePath(sourcePath, sourceFile.FullName) + (_options.CopyMetaData ? " " + ShorthandAttributes(sourceFile) : String.Empty);
                _logger.Log("Copying file: '{0}'", details);
                OperationManager.Attempt(() => SmartCopyFile(sourceFile, path));
            }

            var sourceDirectoryLookup = FileSystemHelpers.GetDirectories(sourceDirectory);
            var destDirectoryLookup   = FileSystemHelpers.GetDirectories(destinationDirectory);

            foreach (var destSubDirectory in destDirectoryLookup.Values)
            {
                // If the directory doesn't exist in the source, only delete if:
                // 1. We have no previous directory
                // 2. We have a previous directory and the file exists there

                if (!sourceDirectoryLookup.ContainsKey(destSubDirectory.Name))
                {
                    SmartDirectoryDelete(destSubDirectory, destinationPath);
                }
            }

            foreach (var sourceSubDirectory in sourceDirectoryLookup.Values)
            {
                DirectoryInfoBase targetSubDirectory;
                if (!destDirectoryLookup.TryGetValue(sourceSubDirectory.Name, out targetSubDirectory))
                {
                    string path = FileSystemHelpers.GetDestinationPath(sourcePath, destinationPath, sourceSubDirectory);
                    targetSubDirectory = CreateDirectoryInfo(path);
                }

                _nextManifest.AddPath(sourcePath, sourceSubDirectory.FullName);

                // Sync all sub directories
                SmartCopy(sourcePath, destinationPath, sourceSubDirectory, targetSubDirectory);
            }
        }