//================================================================ #region Getting the package from unpacked source folder private InstalledPackage TryGetPackageFromUnpackedSourceFolder(PackageSource source, PackageRequest request, FileSyncer binFileSyncer) { if (request.Source == null) // Unpacked source folder must be explicitly set in the package request. { return(null); } if (source.Path == null || !Directory.Exists(source.Path)) { return(null); } foreach (string packedExtension in new[] { "zip", "nupkg" }) { if (Directory.GetFiles(source.Path, "*." + packedExtension).Length > 0) { _logger.Trace(() => "Package " + request.Id + " source folder is not considered as unpacked source because it contains ." + packedExtension + " files."); return(null); } } var metadataFiles = Directory.GetFiles(source.Path, "*.nuspec"); // Disambiguation by name: if (metadataFiles.Length > 1) { var standardFileName = metadataFiles.Where(f => string.Equals(Path.GetFileName(f), request.Id + ".nuspec", StringComparison.OrdinalIgnoreCase)).ToArray(); if (standardFileName.Length == 1) { metadataFiles = standardFileName; } } if (metadataFiles.Length > 1) { _logger.Info(() => "Package " + request.Id + " source folder '" + source.ProvidedLocation + "' contains multiple .nuspec metadata files."); return(null); } else if (metadataFiles.Length == 1) { _logger.Trace(() => "Reading package " + request.Id + " from unpacked source folder with metadata " + Path.GetFileName(metadataFiles.Single()) + "."); return(UseFilesFromUnpackedSourceWithMetadata(metadataFiles.Single(), request, binFileSyncer)); } else { var rhetosPackageSubfolders = new[] { "DslScripts", "DataMigration", "Plugins", "Resources" }; if (rhetosPackageSubfolders.Any(subfolder => Directory.Exists(Path.Combine(source.Path, subfolder)))) { _logger.Trace(() => "Reading package " + request.Id + " from unpacked source folder without metadata file."); return(UseFilesFromUnpackedSourceWithoutMetadata(source.Path, request, binFileSyncer)); } else { return(null); } } }
//================================================================ #region Getting the package from NuGet private InstalledPackage TryGetPackageFromNuGetCache(PackageRequest request, FileSyncer binFileSyncer) { var sw = Stopwatch.StartNew(); // Use cache only if not deploying from source and an exact version is specified: if (request.Source != null) { return(null); } var requestVersionsRange = !string.IsNullOrEmpty(request.VersionsRange) ? VersionUtility.ParseVersionSpec(request.VersionsRange) : new VersionSpec(); if (requestVersionsRange.MinVersion == null || requestVersionsRange.MinVersion.Equals(new SemanticVersion("0.0")) || requestVersionsRange.MinVersion != requestVersionsRange.MaxVersion) { _logger.Trace(() => $"Not looking for {request.ReportIdVersionsRange()} in packages cache because the request does not specify an exact version."); return(null); } // Find the NuGet package: var nugetRepository = new LocalPackageRepository(Paths.PackagesCacheFolder, enableCaching: false); IPackage package = nugetRepository.FindPackage(request.Id, requestVersionsRange, allowPrereleaseVersions: true, allowUnlisted: true); _performanceLogger.Write(sw, () => $"PackageDownloader: {(package == null ? "Did not find" : "Found")} the NuGet package {request.ReportIdVersionsRange()} in cache."); if (package == null) { return(null); } // Copy binary files and resources: string packageSubfolder = nugetRepository.PathResolver.GetPackageDirectory(request.Id, requestVersionsRange.MinVersion); _deployPackagesLogger.Trace(() => $"Reading package from cache '{packageSubfolder}'."); string targetFolder = Path.Combine(Paths.PackagesCacheFolder, packageSubfolder); foreach (var file in FilterCompatibleLibFiles(package.GetFiles())) { binFileSyncer.AddFile(Path.Combine(targetFolder, file.Path), Paths.PluginsFolder, Path.Combine(Paths.PluginsFolder, file.EffectivePath)); } binFileSyncer.AddFolderContent(Path.Combine(targetFolder, "Plugins"), Paths.PluginsFolder, recursive: false); // Obsolete bin folder; lib should be used instead. binFileSyncer.AddFolderContent(Path.Combine(targetFolder, "Resources"), Paths.ResourcesFolder, SimplifyPackageName(package.Id), recursive: true); return(new InstalledPackage(package.Id, package.Version.ToString(), GetNuGetPackageDependencies(package), targetFolder, request, Paths.PackagesCacheFolder)); }
/// <summary> /// Downloads the packages from the provided sources, if not already downloaded. /// Unpacks the packages, if not already unpacked. /// </summary> public List <InstalledPackage> GetPackages() { var sw = Stopwatch.StartNew(); var installedPackages = new List <InstalledPackage>(); installedPackages.Add(new InstalledPackage("Rhetos", SystemUtility.GetRhetosVersion(), new List <PackageRequest>(), Paths.RhetosServerRootPath, new PackageRequest { Id = "Rhetos", VersionsRange = "", Source = "", RequestedBy = "Rhetos framework" }, ".", new List <ContentFile> { })); var binFileSyncer = new FileSyncer(_logProvider); binFileSyncer.AddDestinations(Paths.PluginsFolder, Paths.ResourcesFolder); // Even if there are no packages, those folders must be created and emptied. _filesUtility.SafeCreateDirectory(Paths.PackagesCacheFolder); var packageRequests = _deploymentConfiguration.PackageRequests; while (packageRequests.Any()) { var newDependencies = new List <PackageRequest>(); foreach (var request in packageRequests) { if (!CheckAlreadyDownloaded(request, installedPackages)) { var installedPackage = GetPackage(request, binFileSyncer); ValidatePackage(installedPackage, request, installedPackages); installedPackages.Add(installedPackage); newDependencies.AddRange(installedPackage.Dependencies); } } packageRequests = newDependencies; } DeleteObsoletePackages(installedPackages); SortByDependencies(installedPackages); binFileSyncer.UpdateDestination(); foreach (var package in installedPackages) { _packagesLogger.Trace(() => package.Report()); } _performanceLogger.Write(sw, "PackageDownloader.GetPackages."); return(installedPackages); }
//================================================================ #region Getting the package from unpacked source folder private InstalledPackage TryGetPackageFromUnpackedSourceFolder(PackageSource source, PackageRequest request, FileSyncer binFileSyncer) { if (request.Source == null) // Unpacked source folder must be explicitly set in the package request. { return(null); } if (source.Path == null || !Directory.Exists(source.Path)) { return(null); } var packageExtensions = new[] { "*.nupkg", GetZipPackageName(request) }; var existingPackageFiles = packageExtensions.SelectMany(ext => Directory.GetFiles(source.Path, ext)); var existingMetadataFiles = Directory.GetFiles(source.Path, "*.nuspec"); if (existingMetadataFiles.Length > 1) { // If any nuspec file exactly matches the packages name, use that one. var standardFileName = existingMetadataFiles.Where(f => string.Equals(Path.GetFileName(f), request.Id + ".nuspec", StringComparison.OrdinalIgnoreCase)).ToArray(); if (standardFileName.Length == 1) { existingMetadataFiles = standardFileName; } } var packageSourceSubfolders = new[] { "DslScripts", "DataMigration", "Plugins", "Resources" }; var existingSourceSubfolders = packageSourceSubfolders.Where(subfolder => Directory.Exists(Path.Combine(source.Path, subfolder))); if (existingPackageFiles.Count() > 0) { string ambiguousAlternative = null; if (existingMetadataFiles.Count() > 0) { ambiguousAlternative = $".nuspec file '{Path.GetFileName(existingMetadataFiles.First())}'"; } else if (existingSourceSubfolders.Count() > 0) { ambiguousAlternative = $"source folder '{existingSourceSubfolders.First()}'"; } if (ambiguousAlternative != null) { throw new FrameworkException($"Ambiguous source for package {request.Id}. Source folder '{source.Path}' contains both" + $" package file '{Path.GetFileName(existingPackageFiles.First())}' and {ambiguousAlternative}."); } _logger.Trace(() => $"Package {request.Id} source folder is not considered as unpacked source because" + $" it contains a package file '{Path.GetFileName(existingPackageFiles.First())}'."); return(null); } else if (existingMetadataFiles.Length > 1) { _logger.Info(() => "Package " + request.Id + " source folder '" + source.ProvidedLocation + "' contains multiple .nuspec metadata files."); return(null); } else if (existingMetadataFiles.Length == 1) { _logger.Trace(() => "Reading package " + request.Id + " from unpacked source folder with metadata " + Path.GetFileName(existingMetadataFiles.Single()) + "."); return(UseFilesFromUnpackedSourceWithMetadata(existingMetadataFiles.Single(), request, binFileSyncer)); } else if (existingSourceSubfolders.Any()) { _logger.Trace(() => "Reading package " + request.Id + " from unpacked source folder without metadata file."); return(UseFilesFromUnpackedSourceWithoutMetadata(source.Path, request, binFileSyncer)); } else { return(null); } }
/// <summary> /// Downloads the packages from the provided sources, if not already downloaded. /// Unpacks the packages, if not already unpacked. /// </summary> public InstalledPackages GetPackages() { var sw = Stopwatch.StartNew(); var installedPackages = new List <InstalledPackage>(); // These Rhetos framework packages are already integrated in Rhetos application when using DeployPackages build process. installedPackages.Add(new InstalledPackage("Rhetos", SystemUtility.GetRhetosVersion(), new List <PackageRequest>(), Paths.RhetosServerRootPath, new PackageRequest { Id = "Rhetos", VersionsRange = null, Source = null, RequestedBy = "Rhetos framework" }, ".", new List <ContentFile> { })); installedPackages.Add(new InstalledPackage("Rhetos.MSBuild", SystemUtility.GetRhetosVersion(), new List <PackageRequest>(), Paths.RhetosServerRootPath, new PackageRequest { Id = "Rhetos.MSBuild", VersionsRange = null, Source = null, RequestedBy = "Rhetos framework" }, ".", new List <ContentFile> { })); var binFileSyncer = new FileSyncer(_logProvider); binFileSyncer.AddDestinations(Paths.PluginsFolder); // Even if there are no packages, this folder must be emptied. _filesUtility.SafeCreateDirectory(_packagesCacheFolder); var packageRequests = _deploymentConfiguration.PackageRequests; while (packageRequests.Any()) { var newDependencies = new List <PackageRequest>(); foreach (var request in packageRequests) { _logger.Trace(() => $"Getting package {request.ReportIdVersionRequestSource()}."); if (!CheckAlreadyDownloaded(request, installedPackages)) { var installedPackage = GetPackage(request, binFileSyncer); ValidatePackage(installedPackage, request); installedPackages.Add(installedPackage); newDependencies.AddRange(installedPackage.Dependencies); } } packageRequests = newDependencies; } DeleteObsoletePackages(installedPackages); SortByDependencies(installedPackages); binFileSyncer.UpdateDestination(); _performanceLogger.Write(sw, "GetPackages."); return(new InstalledPackages { Packages = installedPackages }); }