Beispiel #1
0
        private ProjectContextCollection AddProjectContextEntry(string projectDirectory,
                                                                ProjectContextCollection currentEntry)
        {
            if (currentEntry == null)
            {
                // new entry required
                currentEntry = new ProjectContextCollection();
            }

            var projectEntry = GetProject(projectDirectory);

            if (projectEntry.Model == null)
            {
                // project doesn't exist anymore
                currentEntry.Reset();
                return(currentEntry);
            }

            var project = projectEntry.Model;

            if (currentEntry.HasChanged)
            {
                currentEntry.Reset();

                foreach (var framework in project.GetTargetFrameworks())
                {
                    var builder = new ProjectContextBuilder()
                                  .WithProjectResolver(path => GetProject(path).Model)
                                  .WithLockFileResolver(path => GetLockFile(path))
                                  .WithProject(project)
                                  .WithTargetFramework(framework.FrameworkName)
                                  .AsDesignTime();

                    currentEntry.ProjectContexts.Add(builder.Build());
                }

                currentEntry.Project                     = project;
                currentEntry.ProjectFilePath             = project.ProjectFilePath;
                currentEntry.LastProjectFileWriteTimeUtc = File.GetLastWriteTimeUtc(currentEntry.ProjectFilePath);

                var lockFilePath = Path.Combine(project.ProjectDirectory, LockFile.FileName);
                if (File.Exists(lockFilePath))
                {
                    currentEntry.LockFilePath             = lockFilePath;
                    currentEntry.LastLockFileWriteTimeUtc = File.GetLastWriteTimeUtc(lockFilePath);
                }

                currentEntry.ProjectDiagnostics.AddRange(projectEntry.Diagnostics);
            }

            return(currentEntry);
        }
        public ProjectContextBuilder Clone()
        {
            var builder = new ProjectContextBuilder()
                .WithLockFile(LockFile)
                .WithProject(Project)
                .WithProjectDirectory(ProjectDirectory)
                .WithTargetFramework(TargetFramework)
                .WithRuntimeIdentifiers(RuntimeIdentifiers)
                .WithReferenceAssembliesPath(ReferenceAssembliesPath)
                .WithPackagesDirectory(PackagesDirectory)
                .WithRootDirectory(RootDirectory)
                .WithProjectResolver(ProjectResolver)
                .WithLockFileResolver(LockFileResolver)
                .WithProjectReaderSettings(ProjectReaderSettings);
            if (IsDesignTime)
            {
                builder.AsDesignTime();
            }

            return builder;
        }
Beispiel #3
0
        public ProjectContextBuilder Clone()
        {
            var builder = new ProjectContextBuilder()
                          .WithLockFile(LockFile)
                          .WithProject(Project)
                          .WithProjectDirectory(ProjectDirectory)
                          .WithTargetFramework(TargetFramework)
                          .WithRuntimeIdentifiers(RuntimeIdentifiers)
                          .WithReferenceAssembliesPath(ReferenceAssembliesPath)
                          .WithPackagesDirectory(PackagesDirectory)
                          .WithRootDirectory(RootDirectory)
                          .WithProjectResolver(ProjectResolver)
                          .WithLockFileResolver(LockFileResolver)
                          .WithProjectReaderSettings(ProjectReaderSettings);

            if (IsDesignTime)
            {
                builder.AsDesignTime();
            }

            return(builder);
        }
Beispiel #4
0
        /// <summary>
        /// Produce all targets found in the lock file associated with this builder.
        /// Returns an empty enumerable if there is no lock file
        /// (making this unsuitable for scenarios where the lock file may not be present,
        /// such as at design-time)
        /// </summary>
        /// <returns></returns>
        public IEnumerable <ProjectContext> BuildAllTargets()
        {
            ProjectDirectory = Project?.ProjectDirectory ?? ProjectDirectory;
            EnsureProjectLoaded();
            LockFile = LockFile ?? LockFileResolver(ProjectDirectory);

            if (LockFile != null)
            {
                var deduper = new HashSet <string>();
                foreach (var target in LockFile.Targets)
                {
                    var context = Clone()
                                  .WithTargetFramework(target.TargetFramework)
                                  .WithRuntimeIdentifiers(new[] { target.RuntimeIdentifier }).Build();

                    var id = $"{context.TargetFramework}/{context.RuntimeIdentifier}";
                    if (deduper.Add(id))
                    {
                        yield return(context);
                    }
                }
            }
            else
            {
                // Build a context for each framework. It won't be fully valid, since it won't have resolved data or runtime data, but the diagnostics will show that.
                foreach (var framework in Project.GetTargetFrameworks())
                {
                    var builder = new ProjectContextBuilder()
                                  .WithProject(Project)
                                  .WithTargetFramework(framework.FrameworkName);
                    if (IsDesignTime)
                    {
                        builder.AsDesignTime();
                    }
                    yield return(builder.Build());
                }
            }
        }
        // Need to unit test this, so public
        public void GenerateDepsJsonFile(
            LockFile toolLockFile,
            string depsPath)
        {
            Reporter.Verbose.WriteLine($"Generating deps.json at: {depsPath}");

            var projectContext = new ProjectContextBuilder()
                .WithLockFile(toolLockFile)
                .WithTargetFramework(s_toolPackageFramework.ToString())
                .Build();

            var exporter = projectContext.CreateExporter(Constants.DefaultConfiguration);

            var dependencyContext = new DependencyContextBuilder()
                .Build(null,
                    null,
                    exporter.GetAllExports(),
                    true,
                    s_toolPackageFramework,
                    string.Empty);

            var tempDepsFile = Path.GetTempFileName();
            using (var fileStream = File.Open(tempDepsFile, FileMode.Open, FileAccess.Write))
            {
                var dependencyContextWriter = new DependencyContextWriter();

                dependencyContextWriter.Write(dependencyContext, fileStream);
            }

            try
            {
                File.Copy(tempDepsFile, depsPath);
            }
            catch (Exception e)
            {
                Reporter.Verbose.WriteLine($"unable to generate deps.json, it may have been already generated: {e.Message}");
            }
            finally
            {
                try
                {
                    File.Delete(tempDepsFile);
                }
                catch (Exception e2)
                {
                    Reporter.Verbose.WriteLine($"unable to delete temporary deps.json file: {e2.Message}");
                }
            }
        }
        private void EnsureToolJsonDepsFileExists(
            LibraryRange toolLibrary, 
            LockFile toolLockFile, 
            string depsPath)
        {
            if (!File.Exists(depsPath))
            {
                var projectContext = new ProjectContextBuilder()
                    .WithLockFile(toolLockFile)
                    .WithTargetFramework(s_toolPackageFramework.ToString())
                    .Build();

                var exporter = projectContext.CreateExporter(Constants.DefaultConfiguration);

                var dependencyContext = new DependencyContextBuilder()
                    .Build(null, 
                        null, 
                        exporter.GetAllExports(), 
                        true, 
                        s_toolPackageFramework, 
                        string.Empty);

                using (var fileStream = File.Create(depsPath))
                {
                    var dependencyContextWriter = new DependencyContextWriter();

                    dependencyContextWriter.Write(dependencyContext, fileStream);
                }
            }
        }
        /// <summary>
        /// Produce all targets found in the lock file associated with this builder.
        /// Returns an empty enumerable if there is no lock file
        /// (making this unsuitable for scenarios where the lock file may not be present,
        /// such as at design-time)
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ProjectContext> BuildAllTargets()
        {
            ProjectDirectory = Project?.ProjectDirectory ?? ProjectDirectory;
            EnsureProjectLoaded();
            LockFile = LockFile ?? LockFileResolver(ProjectDirectory);

            if (LockFile != null)
            {
                var deduper = new HashSet<string>();
                foreach (var target in LockFile.Targets)
                {
                    var context = Clone()
                        .WithTargetFramework(target.TargetFramework)
                        .WithRuntimeIdentifiers(new[] { target.RuntimeIdentifier }).Build();

                    var id = $"{context.TargetFramework}/{context.RuntimeIdentifier}";
                    if (deduper.Add(id))
                    {
                        yield return context;
                    }
                }
            }
            else
            {
                // Build a context for each framework. It won't be fully valid, since it won't have resolved data or runtime data, but the diagnostics will show that.
                foreach (var framework in Project.GetTargetFrameworks())
                {
                    var builder = new ProjectContextBuilder()
                        .WithProject(Project)
                        .WithTargetFramework(framework.FrameworkName);
                    if (IsDesignTime)
                    {
                        builder.AsDesignTime();
                    }
                    yield return builder.Build();
                }
            }
        }