public static NuGetPackageResolver CreateResolver(IEnumerable <string> packageFolders, string projectPath)
        {
            NuGetPackageResolver packageResolver;

            string userPackageFolder = packageFolders.FirstOrDefault();

            if (userPackageFolder != null)
            {
                var fallBackFolders = packageFolders.Skip(1);
                packageResolver = new NuGetPackageResolver(userPackageFolder, fallBackFolders);
            }
            else
            {
                NuGetPathContext nugetPathContext = NuGetPathContext.Create(Path.GetDirectoryName(projectPath));
                packageResolver = new NuGetPackageResolver(nugetPathContext);
            }

            return(packageResolver);
        }
Exemple #2
0
        public static NuGetPackageResolver CreateResolver(LockFile lockFile, string projectPath)
        {
            NuGetPackageResolver packageResolver;

            string userPackageFolder = lockFile.PackageFolders.FirstOrDefault()?.Path;

            if (userPackageFolder != null)
            {
                var fallBackFolders = lockFile.PackageFolders.Skip(1).Select(f => f.Path);
                packageResolver = new NuGetPackageResolver(userPackageFolder, fallBackFolders);
            }
            else
            {
                NuGetPathContext nugetPathContext = NuGetPathContext.Create(Path.GetDirectoryName(projectPath));
                packageResolver = new NuGetPackageResolver(nugetPathContext);
            }

            return(packageResolver);
        }
Exemple #3
0
        protected override void ExecuteCore()
        {
            var                       lockFileCache = new LockFileCache(BuildEngine4);
            LockFile                  lockFile      = lockFileCache.GetLockFile(AssetsFilePath);
            IEnumerable <string>      excludeFromPublishPackageIds = PackageReferenceConverter.GetPackageIds(ExcludeFromPublishPackageReferences);
            IPackageResolver          packageResolver      = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);
            HashSet <PackageIdentity> packagestoBeFiltered = null;

            if (RuntimeStorePackages != null && RuntimeStorePackages.Length > 0)
            {
                packagestoBeFiltered = new HashSet <PackageIdentity>();
                foreach (var package in RuntimeStorePackages)
                {
                    packagestoBeFiltered.Add(ItemUtilities.GetPackageIdentity(package));
                }
            }

            ProjectContext projectContext = lockFile.CreateProjectContext(
                NuGetUtils.ParseFrameworkName(TargetFramework),
                RuntimeIdentifier,
                PlatformLibraryName,
                IsSelfContained);

            projectContext.PackagesToBeFiltered = packagestoBeFiltered;

            var assemblyResolver =
                new PublishAssembliesResolver(packageResolver)
                .WithExcludeFromPublish(excludeFromPublishPackageIds)
                .WithPreserveStoreLayout(PreserveStoreLayout);

            IEnumerable <ResolvedFile> resolvedAssemblies = assemblyResolver.Resolve(projectContext);

            foreach (ResolvedFile resolvedAssembly in resolvedAssemblies)
            {
                TaskItem item = new TaskItem(resolvedAssembly.SourcePath);
                item.SetMetadata("DestinationSubPath", resolvedAssembly.DestinationSubPath);
                item.SetMetadata("AssetType", resolvedAssembly.Asset.ToString().ToLower());
                item.SetMetadata(MetadataKeys.PackageName, resolvedAssembly.Package.Id.ToString().ToLower());
                item.SetMetadata(MetadataKeys.PackageVersion, resolvedAssembly.Package.Version.ToString().ToLower());
                _assembliesToPublish.Add(item);
            }
        }
Exemple #4
0
        protected override void ExecuteCore()
        {
            if (Items.Length == 0 || PackageFolders.Length == 0)
            {
                Output = Items;
                return;
            }

            var packageResolver = NuGetPackageResolver.CreateResolver(PackageFolders);

            int index        = 0;
            var updatedItems = new ITaskItem[Items.Length];

            foreach (var item in Items)
            {
                string packageName    = item.GetMetadata(MetadataKeys.PackageName);
                string packageVersion = item.GetMetadata(MetadataKeys.PackageVersion);

                if (string.IsNullOrEmpty(packageName) || string.IsNullOrEmpty(packageVersion) ||
                    !string.IsNullOrEmpty(item.GetMetadata(MetadataKeys.PackageDirectory)))
                {
                    updatedItems[index++] = item;
                    continue;
                }

                var    parsedPackageVersion = NuGetVersion.Parse(packageVersion);
                string packageDirectory     = packageResolver.GetPackageDirectory(packageName, parsedPackageVersion);

                if (packageDirectory == null)
                {
                    updatedItems[index++] = item;
                    continue;
                }

                var newItem = new TaskItem(item);
                newItem.SetMetadata(MetadataKeys.PackageDirectory, packageDirectory);
                updatedItems[index++] = newItem;
            }

            Output = updatedItems;
        }
Exemple #5
0
        protected override void ExecuteCore()
        {
            if (Items != null)
            {
                NuGetPackageResolver packageResolver = NuGetPackageResolver.CreateResolver(PackageFolders, ProjectPath);

                var updatedItems = new List <ITaskItem>();

                foreach (var item in Items)
                {
                    string packageName    = item.GetMetadata(MetadataKeys.PackageName);
                    string packageVersion = item.GetMetadata(MetadataKeys.PackageVersion);
                    if (!string.IsNullOrEmpty(packageName) && !string.IsNullOrEmpty(packageVersion) &&
                        string.IsNullOrEmpty(item.GetMetadata(MetadataKeys.PackageDirectory)))
                    {
                        var newItem = new TaskItem(item);

                        //  Gracefully handle case where we don't have a packageResolver because we don't have an assets
                        //  file, which happens for design-time builds
                        if (packageResolver != null)
                        {
                            var parsedPackageVersion = NuGetVersion.Parse(packageVersion);

                            string packageDirectory = packageResolver.GetPackageDirectory(packageName, parsedPackageVersion);

                            newItem.SetMetadata(MetadataKeys.PackageDirectory, packageDirectory);
                        }

                        updatedItems.Add(newItem);
                    }
                    else
                    {
                        updatedItems.Add(item);
                    }
                }

                Output = updatedItems.ToArray();
            }
        }
        public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, ProjectContext projectContext, bool includeRuntimeFileVersions)
        {
            _mainProjectInfo            = mainProjectInfo;
            _projectContext             = projectContext;
            _includeRuntimeFileVersions = includeRuntimeFileVersions;

            // This resolver is only used for building file names, so that base path is not required.
            _versionFolderPathResolver = new VersionFolderPathResolver(rootPath: null);

            if (_includeRuntimeFileVersions)
            {
                //  This is used to look up the paths to package files on disk, which is only needed in this class if
                //  it needs to read the file versions
                _packageResolver = NuGetPackageResolver.CreateResolver(projectContext.LockFile);
            }

            if (_projectContext.CompilationLockFileTarget != _projectContext.LockFileTarget)
            {
                _compilationTargetLibraries = _projectContext.CompilationLockFileTarget.Libraries
                                              .ToDictionary(l => l.Name, StringComparer.OrdinalIgnoreCase);
            }
        }
        protected override void ExecuteCore()
        {
            LockFile             lockFile = new LockFileCache(BuildEngine4).GetLockFile(AssetsFilePath);
            IEnumerable <string> privateAssetsPackageIds = PackageReferenceConverter.GetPackageIds(PrivateAssetsPackageReferences);
            IPackageResolver     packageResolver         = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);

            ProjectContext projectContext = lockFile.CreateProjectContext(
                NuGetUtils.ParseFrameworkName(TargetFramework),
                RuntimeIdentifier,
                PlatformLibraryName);

            IEnumerable <ResolvedFile> resolvedAssemblies =
                new PublishAssembliesResolver(packageResolver)
                .WithPrivateAssets(privateAssetsPackageIds)
                .Resolve(projectContext);

            foreach (ResolvedFile resolvedAssembly in resolvedAssemblies)
            {
                TaskItem item = new TaskItem(resolvedAssembly.SourcePath);
                item.SetMetadata("DestinationSubPath", resolvedAssembly.DestinationSubPath);

                _assembliesToPublish.Add(item);
            }
        }
Exemple #8
0
        protected override void ExecuteCore()
        {
            var lockFile = new LockFileCache(BuildEngine4).GetLockFile(ProjectAssetsFile);

            _packageResolver = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);

            var targetFramework   = NuGetUtils.ParseFrameworkName(TargetFrameworkMoniker);
            var compileTimeTarget = lockFile.GetTargetAndThrowIfNotFound(targetFramework, runtime: null);
            var runtimeTarget     = lockFile.GetTargetAndThrowIfNotFound(targetFramework, RuntimeIdentifier);

            CheckRuntimePackageDependencies(compileTimeTarget, runtimeTarget);

            Analyzers = RaiseAnalyzers(
                lockFile,
                runtimeTarget);

            CompileTimeAssemblies = RaisePackageAssets(
                compileTimeTarget,
                package => package.CompileTimeAssemblies,
                setup: (asset, item) =>
            {
                item.SetMetadata(MetadataKeys.Private, "false");
                item.SetMetadata(MetadataKeys.HintPath, item.ItemSpec);

                if (MarkPackageReferencesAsExternallyResolved)
                {
                    item.SetMetadata(MetadataKeys.ExternallyResolved, "true");
                }
            });

            if (!DisableFrameworkAssemblies)
            {
                FrameworkAssemblies = RaiseFrameworkAssemblies(
                    compileTimeTarget,
                    setup: (asset, item) =>
                {
                    item.SetMetadata(MetadataKeys.Pack, "false");
                    item.SetMetadata(MetadataKeys.Private, "false");
                    item.SetMetadata(MetadataKeys.NuGetIsFrameworkReference, "true");
                });
            }

            ContentFilesToPreprocess = RaisePackageAssets(
                runtimeTarget,
                p => p.ContentFiles,
                filter: asset => !string.IsNullOrEmpty(asset.PPOutputPath),
                setup: (asset, item) =>
            {
                item.SetMetadata(MetadataKeys.BuildAction, asset.BuildAction.ToString());
                item.SetMetadata(MetadataKeys.CopyToOutput, asset.CopyToOutput.ToString());
                item.SetMetadata(MetadataKeys.PPOutputPath, asset.PPOutputPath);

                if (!string.IsNullOrEmpty(MetadataKeys.OutputPath))
                {
                    item.SetMetadata(MetadataKeys.OutputPath, asset.OutputPath);
                }

                if (!string.IsNullOrEmpty(asset.CodeLanguage))
                {
                    item.SetMetadata(MetadataKeys.CodeLanguage, asset.CodeLanguage);
                }
            });

            NativeLibraries = RaisePackageAssets(
                runtimeTarget,
                package => package.NativeLibraries);

            ResourceAssemblies = RaisePackageAssets(
                runtimeTarget,
                package => package.ResourceAssemblies,
                setup: (asset, item) =>
            {
                string locale = asset.Properties["locale"];
                item.SetMetadata(MetadataKeys.Culture, locale);
                item.SetMetadata(MetadataKeys.DestinationSubDirectory, locale + Path.DirectorySeparatorChar);
            });

            RuntimeAssemblies = RaisePackageAssets(
                runtimeTarget,
                package => package.RuntimeAssemblies);

            RuntimeTargets = RaisePackageAssets(
                runtimeTarget,
                package => package.RuntimeTargets,
                setup: (asset, item) =>
            {
                string directory = Path.GetDirectoryName(asset.Path);
                item.SetMetadata(MetadataKeys.DestinationSubDirectory, directory + Path.DirectorySeparatorChar);
            });

            if (!DisableTransitiveProjectReferences)
            {
                TransitiveProjectReferences = RaiseTransitiveProjectReferences(lockFile, runtimeTarget);
            }
        }