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); }
/// <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()); } } }
/// <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(); } } }