public void RebuildCacheFromSettingsIfNotCurrent(bool forceRebuild) { EnsureLoaded(); if (IsVersionCurrent && !forceRebuild) { return; } // load up the culture neutral cache // and get the mount points for templates from the culture neutral cache IReadOnlyList <TemplateInfo> cultureNeutralTemplates = _userTemplateCache.GetTemplatesForLocale(null, _userSettings.Version); HashSet <Guid> templateMountPointIds = new HashSet <Guid>(cultureNeutralTemplates.Select(x => x.ConfigMountPointId)); _userTemplateCache.TemplateInfo.Clear(); HashSet <Guid> scannedMountPoints = new HashSet <Guid>(); // Scan the unique mount points for the templates. foreach (MountPointInfo mountPoint in MountPoints) { if (templateMountPointIds.Contains(mountPoint.MountPointId) && scannedMountPoints.Add(mountPoint.MountPointId)) { _userTemplateCache.Scan(mountPoint.Place); } } // loop through the localized caches and get all the locale mount points HashSet <Guid> localeMountPointIds = new HashSet <Guid>(); foreach (string locale in _userTemplateCache.AllLocalesWithCacheFiles) { IReadOnlyList <TemplateInfo> templatesForLocale = _userTemplateCache.GetTemplatesForLocale(locale, _userSettings.Version); localeMountPointIds.UnionWith(templatesForLocale.Select(x => x.LocaleConfigMountPointId)); } // Scan the unique local mount points foreach (MountPointInfo mountPoint in MountPoints) { if (localeMountPointIds.Contains(mountPoint.MountPointId) && scannedMountPoints.Add(mountPoint.MountPointId)) { _userTemplateCache.Scan(mountPoint.Place); } } Save(); ReloadTemplates(); }
public void RebuildCacheFromSettingsIfNotCurrent(bool forceRebuild) { EnsureLoaded(); MountPointInfo[] mountPointsToScan = FindMountPointsToScan(forceRebuild).ToArray(); if (!mountPointsToScan.Any()) { // Nothing to do return; } TemplateCache workingCache = new TemplateCache(_environmentSettings); foreach (MountPointInfo mountPoint in mountPointsToScan) { workingCache.Scan(mountPoint.Place); } Save(workingCache); ReloadTemplates(); }
public static void InstallPackage(IReadOnlyList <string> packages, bool quiet) { Init(); RemoteWalkContext context = new RemoteWalkContext(); ILogger logger = new NullLogger(); SourceCacheContext cacheContext = new SourceCacheContext { IgnoreFailedSources = true }; foreach (SourceRepository repo in Repos) { if (!repo.PackageSource.IsLocal) { context.RemoteLibraryProviders.Add(new SourceRepositoryDependencyProvider(repo, logger, cacheContext)); } else { context.LocalLibraryProviders.Add(new SourceRepositoryDependencyProvider(repo, logger, cacheContext)); } } Paths.User.Content.CreateDirectory(); RemoteDependencyWalker walker = new RemoteDependencyWalker(context); HashSet <Package> remainingPackages = new HashSet <Package>(packages.Select(x => new Package(x, VersionRange.All))); HashSet <Package> encounteredPackages = new HashSet <Package>(); List <string> templateRoots = new List <string>(); List <KeyValuePair <string, string> > componentRoots = new List <KeyValuePair <string, string> >(); while (remainingPackages.Count > 0) { HashSet <Package> nextRound = new HashSet <Package>(); foreach (Package package in remainingPackages) { string name = package.PackageId; GraphNode <RemoteResolveResult> result = walker.WalkAsync(new LibraryRange(name, package.Version, LibraryDependencyTarget.All), NuGetFramework.AnyFramework, "", RuntimeGraph.Empty, true).Result; RemoteMatch match = result.Item.Data.Match; PackageIdentity packageIdentity = new PackageIdentity(match.Library.Name, match.Library.Version); nextRound.UnionWith(result.Item.Data.Dependencies.Select(x => new Package(x.Name, x.LibraryRange.VersionRange))); VersionFolderPathContext versionFolderPathContext = new VersionFolderPathContext( packageIdentity, Paths.User.PackageCache, new NullLogger(), packageSaveMode: PackageSaveMode.Defaultv3, xmlDocFileSaveMode: XmlDocFileSaveMode.Skip, fixNuspecIdCasing: true, normalizeFileNames: true); if (match.Library.Version == null) { if (!quiet) { throw new Exception($"Package '{package.PackageId}' version {package.Version} could not be located."); } else { continue; } } string source = Path.Combine(Paths.User.PackageCache, match.Library.Name, match.Library.Version.ToString()); if (!source.Exists() && match.Provider != null) { PackageExtractor.InstallFromSourceAsync( stream => match.Provider.CopyToAsync(match.Library, stream, CancellationToken.None), versionFolderPathContext, CancellationToken.None).Wait(); string target = Path.Combine(Paths.User.Content, match.Library.Name); target.CreateDirectory(); target = Path.Combine(target, match.Library.Version.ToString()); target.CreateDirectory(); Paths.Copy(source, target); target.Delete("*.nupkg", "*.nupkg.*"); string nuspec = target.EnumerateFiles("*.nuspec").FirstOrDefault(); //If there's a nuspec, figure out whether this package is a template and walk the dependency graph if (nuspec?.Exists() ?? false) { XDocument doc = XDocument.Load(nuspec); IReadOnlyList <PackageType> types = NuspecUtility.GetPackageTypes(doc.Root.Element(XName.Get("metadata", "http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd")), false); //If the thing we got is a template... if (types.Any(x => string.Equals(x.Name, "template", StringComparison.OrdinalIgnoreCase))) { templateRoots.Add(target); } else { componentRoots.Add(new KeyValuePair <string, string>(match.Library.Name, match.Library.Version.ToString())); } } } } encounteredPackages.UnionWith(remainingPackages); nextRound.ExceptWith(encounteredPackages); remainingPackages = nextRound; } foreach (KeyValuePair <string, string> package in componentRoots) { foreach (string path in Path.Combine(Paths.User.Content, package.Key, package.Value).EnumerateFiles($"{package.Key}.dll", SearchOption.AllDirectories)) { if (path.IndexOf($"{Path.DirectorySeparatorChar}lib{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase) < 0 || (path.IndexOf($"{Path.DirectorySeparatorChar}netstandard1.", StringComparison.OrdinalIgnoreCase) < 0 && path.IndexOf($"{Path.DirectorySeparatorChar}netcoreapp1.", StringComparison.OrdinalIgnoreCase) < 0)) { continue; } #if !NET451 Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(path); #else Assembly asm = Assembly.LoadFile(path); #endif foreach (Type type in asm.GetTypes()) { SettingsLoader.Components.Register(type); } } } TemplateCache.Scan(templateRoots); }