Пример #1
0
        private void WriteDepsFile(string depsFilePath)
        {
            ProjectContext projectContext;

            if (AssetsFilePath == null)
            {
                projectContext = null;
            }
            else
            {
                LockFile lockFile = new LockFileCache(this).GetLockFile(AssetsFilePath);
                projectContext = lockFile.CreateProjectContext(
                    NuGetUtils.ParseFrameworkName(TargetFramework),
                    RuntimeIdentifier,
                    PlatformLibraryName,
                    RuntimeFrameworks,
                    IsSelfContained);
            }

            CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);

            SingleProjectInfo mainProject = SingleProjectInfo.Create(
                ProjectPath,
                AssemblyName,
                AssemblyExtension,
                AssemblyVersion,
                AssemblySatelliteAssemblies);

            var userRuntimeAssemblySet = new HashSet <string>(UserRuntimeAssemblies ?? Enumerable.Empty <string>(), StringComparer.OrdinalIgnoreCase);
            Func <ITaskItem, bool> isUserRuntimeAssembly = item => userRuntimeAssemblySet.Contains(item.ItemSpec);

            IEnumerable <ReferenceInfo> referenceAssemblyInfos =
                ReferenceInfo.CreateReferenceInfos(ReferenceAssemblies);

            // If there is a generated asset file. The projectContext will have project reference.
            // So remove it from directReferences to avoid duplication
            var projectContextHasProjectReferences       = projectContext != null;
            IEnumerable <ReferenceInfo> directReferences =
                ReferenceInfo.CreateDirectReferenceInfos(ReferencePaths,
                                                         ReferenceSatellitePaths,
                                                         projectContextHasProjectReferences, isUserRuntimeAssembly);

            IEnumerable <ReferenceInfo> dependencyReferences =
                ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths, isUserRuntimeAssembly);

            Dictionary <string, SingleProjectInfo> referenceProjects =
                SingleProjectInfo.CreateProjectReferenceInfos(ReferencePaths, ReferenceSatellitePaths, isUserRuntimeAssembly);

            IEnumerable <RuntimePackAssetInfo> runtimePackAssets =
                IsSelfContained ?
                RuntimePackAssets.Where(item => !IsSingleFile || !item.GetMetadata(MetadataKeys.DropFromSingleFile).Equals("true"))
                .Select(item => RuntimePackAssetInfo.FromItem(item)) :
                Enumerable.Empty <RuntimePackAssetInfo>();

            DependencyContextBuilder builder;

            if (projectContext != null)
            {
                // Generate the RID-fallback for self-contained builds.
                //
                // In order to support loading components with RID-specific assets,
                // the AssemblyDependencyResolver requires a RID fallback graph.
                // The component itself should not carry the RID fallback graph with it, because
                // it would need to carry graph of all the RIDs and needs updates for newer RIDs.
                // For framework dependent apps, the RID fallback graph comes from the core framework Microsoft.NETCore.App,
                // so there is no need to write it into the app.
                // If self-contained apps, the (applicable subset of) RID fallback graph needs to be written to the deps.json manifest.
                //
                // If a RID-graph is provided to the DependencyContextBuilder, it generates a RID-fallback
                // graph with respect to the target RuntimeIdentifier.

                RuntimeGraph runtimeGraph =
                    IsSelfContained ? new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath) : null;

                builder = new DependencyContextBuilder(mainProject, IncludeRuntimeFileVersions, runtimeGraph, projectContext);
            }
            else
            {
                builder = new DependencyContextBuilder(
                    mainProject,
                    IncludeRuntimeFileVersions,
                    RuntimeFrameworks,
                    isSelfContained: IsSelfContained,
                    platformLibraryName: PlatformLibraryName,
                    runtimeIdentifier: RuntimeIdentifier,
                    targetFramework: TargetFramework);
            }

            builder = builder
                      .WithMainProjectInDepsFile(IncludeMainProject)
                      .WithReferenceAssemblies(referenceAssemblyInfos)
                      .WithDirectReferences(directReferences)
                      .WithDependencyReferences(dependencyReferences)
                      .WithReferenceProjectInfos(referenceProjects)
                      .WithRuntimePackAssets(runtimePackAssets)
                      .WithCompilationOptions(compilationOptions)
                      .WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
                      .WithPackagesThatWereFiltered(GetFilteredPackages());

            if (CompileReferences.Length > 0)
            {
                builder = builder.WithCompileReferences(ReferenceInfo.CreateReferenceInfos(CompileReferences));
            }

            var resolvedNuGetFiles = ResolvedNuGetFiles.Select(f => new ResolvedFile(f, false))
                                     .Concat(ResolvedRuntimeTargetsFiles.Select(f => new ResolvedFile(f, true)));

            builder = builder.WithResolvedNuGetFiles(resolvedNuGetFiles);

            DependencyContext dependencyContext = builder.Build();

            var writer = new DependencyContextWriter();

            using (var fileStream = File.Create(depsFilePath))
            {
                writer.Write(dependencyContext, fileStream);
            }
            _filesWritten.Add(new TaskItem(depsFilePath));
        }
Пример #2
0
        protected override void ExecuteCore()
        {
            LoadFilesToSkip();

            LockFile           lockFile           = new LockFileCache(this).GetLockFile(AssetsFilePath);
            CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);

            SingleProjectInfo mainProject = SingleProjectInfo.Create(
                ProjectPath,
                AssemblyName,
                AssemblyExtension,
                AssemblyVersion,
                AssemblySatelliteAssemblies);

            IEnumerable <ReferenceInfo> referenceAssemblyInfos =
                ReferenceInfo.CreateReferenceInfos(ReferenceAssemblies);

            IEnumerable <ReferenceInfo> directReferences =
                ReferenceInfo.CreateDirectReferenceInfos(ReferencePaths, ReferenceSatellitePaths);

            IEnumerable <ReferenceInfo> dependencyReferences =
                ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths);

            Dictionary <string, SingleProjectInfo> referenceProjects = SingleProjectInfo.CreateProjectReferenceInfos(
                ReferencePaths,
                ReferenceDependencyPaths,
                ReferenceSatellitePaths);

            IEnumerable <string> excludeFromPublishAssets = PackageReferenceConverter.GetPackageIds(ExcludeFromPublishPackageReferences);

            IEnumerable <RuntimePackAssetInfo> runtimePackAssets =
                RuntimePackAssets.Select(item => RuntimePackAssetInfo.FromItem(item));

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

            DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, IncludeRuntimeFileVersions)
                                                  .WithMainProjectInDepsFile(IncludeMainProject)
                                                  .WithReferenceAssemblies(referenceAssemblyInfos)
                                                  .WithDirectReferences(directReferences)
                                                  .WithDependencyReferences(dependencyReferences)
                                                  .WithReferenceProjectInfos(referenceProjects)
                                                  .WithExcludeFromPublishAssets(excludeFromPublishAssets)
                                                  .WithRuntimePackAssets(runtimePackAssets)
                                                  .WithCompilationOptions(compilationOptions)
                                                  .WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
                                                  .WithPackagesThatWhereFiltered(GetFilteredPackages())
                                                  .Build();

            if (compileFilesToSkip.Any() || runtimeFilesToSkip.Any())
            {
                dependencyContext = TrimFilesToSkip(dependencyContext);
            }

            var writer = new DependencyContextWriter();

            using (var fileStream = File.Create(DepsFilePath))
            {
                writer.Write(dependencyContext, fileStream);
            }
            _filesWritten.Add(new TaskItem(DepsFilePath));
        }
Пример #3
0
        private void WriteDepsFileNew(string depsFilePath)
        {
            LockFile           lockFile           = new LockFileCache(this).GetLockFile(AssetsFilePath);
            CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);

            SingleProjectInfo mainProject = SingleProjectInfo.Create(
                ProjectPath,
                AssemblyName,
                AssemblyExtension,
                AssemblyVersion,
                AssemblySatelliteAssemblies);

            IEnumerable <ReferenceInfo> referenceAssemblyInfos =
                ReferenceInfo.CreateReferenceInfos(ReferenceAssemblies);

            IEnumerable <ReferenceInfo> directReferences =
                ReferenceInfo.CreateDirectReferenceInfos(ReferencePaths, ReferenceSatellitePaths);

            IEnumerable <ReferenceInfo> dependencyReferences =
                ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths);

            Dictionary <string, SingleProjectInfo> referenceProjects = SingleProjectInfo.CreateProjectReferenceInfos(
                ReferencePaths,
                ReferenceDependencyPaths,
                ReferenceSatellitePaths);

            IEnumerable <string> excludeFromPublishAssets = PackageReferenceConverter.GetPackageIds(ExcludeFromPublishPackageReferences);

            IEnumerable <RuntimePackAssetInfo> runtimePackAssets =
                RuntimePackAssets.Select(item => RuntimePackAssetInfo.FromItem(item));


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

            var builder = new DependencyContextBuilder2(mainProject, projectContext, IncludeRuntimeFileVersions);

            builder = builder
                      .WithMainProjectInDepsFile(IncludeMainProject)
                      .WithReferenceAssemblies(referenceAssemblyInfos)
                      .WithDirectReferences(directReferences)
                      .WithDependencyReferences(dependencyReferences)
                      .WithReferenceProjectInfos(referenceProjects)
                      .WithExcludeFromPublishAssets(excludeFromPublishAssets)
                      .WithRuntimePackAssets(runtimePackAssets)
                      .WithCompilationOptions(compilationOptions)
                      .WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
                      .WithPackagesThatWereFiltered(GetFilteredPackages());

            if (CompileReferences.Length > 0)
            {
                builder = builder.WithCompileReferences(ReferenceInfo.CreateReferenceInfos(CompileReferences));
            }

            var resolvedNuGetFiles = ResolvedNuGetFiles.Select(f => new ResolvedFile(f, false))
                                     .Concat(ResolvedRuntimeTargetsFiles.Select(f => new ResolvedFile(f, true)));

            builder = builder.WithResolvedNuGetFiles(resolvedNuGetFiles);

            DependencyContext dependencyContext = builder.Build();

            var writer = new DependencyContextWriter();

            using (var fileStream = File.Create(depsFilePath))
            {
                writer.Write(dependencyContext, fileStream);
            }
            _filesWritten.Add(new TaskItem(depsFilePath));
        }