Exemple #1
0
        public static string GetOrCompileProjectAssembly(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 is null)
            {
                throw new ArgumentNullException(nameof(fullProjectLocation));
            }
            if (logger is null)
            {
                throw new ArgumentNullException(nameof(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));
                        asyncBuild.BuildTask.Wait();
                    }
                }
            }
            finally
            {
                project.ProjectCollection.UnloadAllProjects();
                project.ProjectCollection.Dispose();
            }

            return(assemblyPath);
        }
Exemple #2
0
        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 LoggerRedirect(ILogger logger, bool onlyErrors = false)
 {
     if (logger == null)
     {
         throw new ArgumentNullException("logger");
     }
     this.logger     = logger;
     this.onlyErrors = onlyErrors;
 }
Exemple #4
0
        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
                            DisableInProcNode = true,
                        };

                        // 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 ICancellableAsyncBuild CompileProjectAssemblyAsync(string solutionFullPath, string fullProjectLocation, ILogger logger, string targets = "Build", string configuration = "Debug", string platform = "AnyCPU", Dictionary <string, string> extraProperties = null, 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))
                {
                    var asyncBuild = new CancellableAsyncBuild(project, assemblyPath);
                    asyncBuild.Build(project, targets, flags, new LoggerRedirect(logger));
                    return(asyncBuild);
                }
            }
            finally
            {
                project.ProjectCollection.UnloadAllProjects();
                project.ProjectCollection.Dispose();
            }

            return(null);
        }