public static async Task RestoreNugetPackages(ILogger logger, string projectPath) { await Task.Run(() => { var pc = new Microsoft.Build.Evaluation.ProjectCollection(); try { var parameters = new BuildParameters(pc) { Loggers = new[] { new LoggerRedirect(logger, true) } //Instance of ILogger instantiated earlier }; // Run a MSBuild /t:Restore <projectfile> var request = new BuildRequestData(projectPath, new Dictionary <string, string>(), null, new[] { "Restore" }, null, BuildRequestDataFlags.None); mainBuildManager.Build(parameters, request); } finally { pc.UnloadAllProjects(); pc.Dispose(); } }); }
public static async Task RestoreNugetPackagesNonRecursive(ILogger logger, IEnumerable <string> projectPaths) { foreach (var projectPath in projectPaths) { await RestoreNugetPackagesNonRecursive(logger, projectPath); } }
public LoggerRedirect(ILogger logger, bool onlyErrors = false) { if (logger == null) { throw new ArgumentNullException("logger"); } this.logger = logger; this.onlyErrors = onlyErrors; }
public static async Task <DependencyGraphSpec> GenerateRestoreGraphFile(ILogger logger, string projectPath) { DependencyGraphSpec spec = null; using (var restoreGraphResult = new TemporaryFile()) { await Task.Run(() => { var pc = new Microsoft.Build.Evaluation.ProjectCollection(); try { var parameters = new BuildParameters(pc) { Loggers = new[] { new LoggerRedirect(logger, true) } //Instance of ILogger instantiated earlier }; // Run a MSBuild /t:Restore <projectfile> var request = new BuildRequestData(projectPath, new Dictionary <string, string> { { "RestoreGraphOutputPath", restoreGraphResult.Path }, { "RestoreRecursive", "false" } }, null, new[] { "GenerateRestoreGraphFile" }, null, BuildRequestDataFlags.None); mainBuildManager.Build(parameters, request); } finally { pc.UnloadAllProjects(); pc.Dispose(); } }); if (File.Exists(restoreGraphResult.Path) && new FileInfo(restoreGraphResult.Path).Length != 0) { spec = DependencyGraphSpec.Load(restoreGraphResult.Path); File.Delete(restoreGraphResult.Path); } else { spec = new DependencyGraphSpec(); } } return(spec); }
public static async Task RestoreNugetPackages(ILogger logger, string projectPath) { if (Path.GetExtension(projectPath)?.ToLowerInvariant() == ".csproj") { var project = LoadProject(projectPath); try { var addedProjs = new HashSet <string>(); //to avoid worst case circular dependencies. var allProjs = Core.Utilities.IterateTree(project, project1 => { var projs = new List <Project>(); foreach (var item in project1.AllEvaluatedItems.Where(x => x.ItemType == "ProjectReference")) { var path = Path.Combine(project.DirectoryPath, item.EvaluatedInclude); if (!File.Exists(path)) { continue; } if (addedProjs.Add(path)) { projs.Add(project.ProjectCollection.LoadProject(path)); } } return(projs); }); await RestoreNugetPackagesNonRecursive(logger, allProjs.Select(x => x.DirectoryPath)); } finally { project.ProjectCollection.UnloadAllProjects(); project.ProjectCollection.Dispose(); } } else { // Solution or unknown project file await RestoreNugetPackagesNonRecursive(logger, projectPath); } }
public static string GetOrCompileProjectAssembly(string solutionFullPath, string fullProjectLocation, ILogger logger, string targets, bool autoCompileProject, string configuration, string platform = "AnyCPU", Dictionary <string, string> extraProperties = null, bool onlyErrors = false, BuildRequestDataFlags flags = BuildRequestDataFlags.None) { if (fullProjectLocation == null) { throw new ArgumentNullException("fullProjectLocation"); } if (logger == null) { throw new ArgumentNullException("logger"); } var project = LoadProject(fullProjectLocation, configuration, platform, extraProperties); var assemblyPath = project.GetPropertyValue("TargetPath"); try { if (!string.IsNullOrWhiteSpace(assemblyPath)) { if (autoCompileProject) { var asyncBuild = new CancellableAsyncBuild(project, assemblyPath); asyncBuild.Build(project, targets, flags, new LoggerRedirect(logger, onlyErrors)); var buildResult = asyncBuild.BuildTask.Result; } } } finally { project.ProjectCollection.UnloadAllProjects(); project.ProjectCollection.Dispose(); } return(assemblyPath); }