Esempio n. 1
0
        protected void CopyDirectory(DirectoryInfoBase source, DirectoryInfoBase target)
        {
            var fs = this.FileSystem;

            fs.Directory.CreateDirectory(target.FullName);

            foreach (var file in source.GetFiles())
            {
                fs.File.Copy(file.FullName, fs.Path.Combine(target.FullName, file.Name), true);
            }

            foreach (var directory in source.GetDirectories())
            {
                var nextTargetSubDir = target.CreateSubdirectory(directory.Name);
                CopyDirectory(directory, nextTargetSubDir);
            }
        }
Esempio n. 2
0
        public async Task <Dictionary <PackageIdentity, FileInfoBase> > LoadPackages(PackagesLock packagesLock, DirectoryInfoBase tempDirectory,
                                                                                     CancellationToken cancellationToken)
        {
            var result             = new Dictionary <PackageIdentity, FileInfoBase>();
            var packagesToDownload = new List <PackageIdentity>();

            foreach (var packageId in packagesLock.Keys)
            {
                var filePath = _versionFolderPathResolver.GetPackageFilePath(packageId.Id, packageId.Version);
                var file     = _fileSystem.FileInfo.FromFileName(filePath);
                if (file.Exists)
                {
                    _logger.LogDebug("Package {packageId} could be resolved locally at {path}", packageId, file.FullName);
                    result.Add(packageId, file);
                }
                else
                {
                    _logger.LogDebug("Package {packageId} could not be resolved locally", packageId);
                    packagesToDownload.Add(packageId);
                }

                cancellationToken.ThrowIfCancellationRequested();
            }

            if (packagesToDownload.Any())
            {
                _logger.LogInformation("{0} package(s) were not found locally and will be downloaded.", packagesToDownload.Count);

                var nugetFolder            = tempDirectory.CreateSubdirectory("nuget");
                var tempFolderPathResolver = new VersionFolderPathResolverFlat(nugetFolder.FullName);

                var serverRepo = new ServerRepository(new Uri(_restClient.Server.ServerUri, "nuget"));
                var downloader = new PackageDownloadManager(new ModulesDirectory(tempFolderPathResolver), serverRepo);

                var results = await downloader.DownloadPackages(
                    new PackagesLock(packagesToDownload.ToDictionary(x => x,
                                                                     _ => (IImmutableList <PackageIdentity>)ImmutableList <PackageIdentity> .Empty)),
                    new PackageDownloadContext(new SourceCacheContext {
                    DirectDownload = true, NoCache = true
                }, nugetFolder.FullName, true),
                    new NuGetLoggerWrapper(_logger), cancellationToken);

                try
                {
                    foreach (var preFetchResult in results.Values)
                    {
                        using (var downloadPackageResult = await preFetchResult.GetResultAsync())
                        {
                            var packageIdentity = await downloadPackageResult.PackageReader.GetIdentityAsync(cancellationToken);

                            _logger.LogInformation("Package {0} was successfully downloaded.", packageIdentity);

                            var fileStream = (FileStream)downloadPackageResult.PackageStream;
                            var file       = _fileSystem.FileInfo.FromFileName(fileStream.Name);
                            if (!file.Exists)
                            {
                                _logger.LogError("Package {0} was not found at {1}. The download may have failed.", packageIdentity, file.FullName);
                                throw new FileNotFoundException($"The file {file.FullName} does not exist.");
                            }

                            var packagePath = _fileSystem.Path.Combine(_fileSystem.Path.GetDirectoryName(file.FullName), packageIdentity + ".nupkg");
                            using (var stream = _fileSystem.FileStream.Create(packagePath, FileMode.CreateNew))
                                await fileStream.CopyToAsync(stream);

                            result.Add(packageIdentity, _fileSystem.FileInfo.FromFileName(packagePath));
                        }
                    }
                }
                finally
                {
                    foreach (var fetcherResult in results.Values)
                    {
                        await fetcherResult.EnsureResultAsync();

                        fetcherResult.Dispose();
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        internal void Arrange(string[] fileNames)
        {
            var lst = new List <FileSystemInfoBase>();
            DirectoryInfoBase parentDir = null;

            foreach (var filename in fileNames)
            {
                DirectoryInfoBase tmpParentDir = null;
                if (this.fileSystem.File.Exists(filename))
                {
                    var fi = this.fileSystem.FileInfo.FromFileName(filename);
                    lst.Add(fi);
                    tmpParentDir = fi.Directory;
                }
                else if (this.fileSystem.Directory.Exists(filename))
                {
                    var di = this.fileSystem.DirectoryInfo.FromDirectoryName(filename);
                    lst.Add(di);
                    tmpParentDir = di.Parent;
                }
                else
                {
                    throw new FileNotFoundException("file or directory not found.", filename);
                }

                if (parentDir == null)
                {
                    parentDir = tmpParentDir;
                }
                else
                {
                    if (parentDir.FullName != tmpParentDir.FullName)
                    {
                        throw new NotSupportedException("all files or folder must be in one same parent folder.");
                    }
                }
            }

            var commonStr = CommonStringExtension.GetCommonSubstring(lst.Select(fsi => fsi.Name.Remove(fsi.Name.Length - fsi.Extension.Length)).ToArray());

            if (string.IsNullOrEmpty(commonStr))
            {
                var newFolderName = DefaultFolderName;
                var count         = 0;
                while (this.fileSystem.Directory.Exists(parentDir.FullName + "\\" + newFolderName))
                {
                    newFolderName = DefaultFolderName + (count++).ToString();
                }
                commonStr = newFolderName;
            }

            DirectoryInfoBase newDir;
            var sameNameFSI = lst.SingleOrDefault(fsi => fsi.Name == commonStr);

            if (sameNameFSI != null)
            {
                var pathName = sameNameFSI.FullName;
                if (pathName.EndsWith("\\"))
                {
                    pathName = pathName.Remove(pathName.Length - 1);
                }
                MoveToPath(sameNameFSI, pathName + ".HGYYFGHFDC");
                newDir = parentDir.CreateSubdirectory(commonStr);
                MoveToPath(sameNameFSI, newDir.FullName + "\\" + commonStr);
                lst.Remove(sameNameFSI);
            }
            else
            {
                newDir = parentDir.CreateSubdirectory(commonStr);
            }

            foreach (var item in lst)
            {
                var newPath = newDir.FullName + "\\" + item.Name;
                MoveToPath(item, newPath);
            }
        }