4 using System.Collections.Generic;
6 using System.Threading.Tasks;
7 using Microsoft.Build.Evaluation;
8 using Microsoft.Build.Execution;
9 using Microsoft.Build.Framework;
10 using SiliconStudio.Core;
11 using SiliconStudio.Core.Diagnostics;
14 namespace SiliconStudio.Assets
18 string AssemblyPath {
get; }
22 bool IsCanceled {
get; }
29 private const string SiliconStudioProjectType =
"SiliconStudioProjectType";
30 private const string SiliconStudioPlatform =
"SiliconStudioPlatform";
32 private static BuildManager mainBuildManager =
new BuildManager();
34 public static Guid
GetProjectGuid(Microsoft.Build.Evaluation.Project project)
36 if (project == null)
throw new ArgumentNullException(
"project");
37 return Guid.Parse(project.GetPropertyValue(
"ProjectGuid"));
42 return GetEnumFromProperty<PlatformType>(project, SiliconStudioPlatform);
47 return GetEnumFromProperty<ProjectType>(project, SiliconStudioProjectType);
50 private static T? GetEnumFromProperty<T>(Microsoft.Build.Evaluation.Project project,
string propertyName) where T :
struct
52 if (project == null)
throw new ArgumentNullException(
"project");
53 if (propertyName == null)
throw new ArgumentNullException(
"propertyName");
54 var value = project.GetPropertyValue(propertyName);
55 if (
string.IsNullOrEmpty(value))
59 return (T)Enum.Parse(typeof(T), value);
62 public static string GetOrCompileProjectAssembly(
string fullProjectLocation,
ILogger logger,
bool autoCompileProject,
string configuration =
"Debug",
string platform =
"AnyCPU", Dictionary<string, string> extraProperties = null,
bool onlyErrors =
false, BuildRequestDataFlags
flags = BuildRequestDataFlags.None)
64 if (fullProjectLocation == null)
throw new ArgumentNullException(
"fullProjectLocation");
65 if (logger == null)
throw new ArgumentNullException(
"logger");
67 var project = LoadProject(fullProjectLocation, configuration, platform, extraProperties);
68 var assemblyPath = project.GetPropertyValue(
"TargetPath");
70 if (!
string.IsNullOrWhiteSpace(assemblyPath))
72 if (autoCompileProject)
74 var asyncBuild =
new CancellableAsyncBuild(project, assemblyPath);
75 asyncBuild.Build(project,
"Build",
flags,
new LoggerRedirect(logger, onlyErrors));
76 var buildResult = asyncBuild.BuildTask.Result;
85 if (fullProjectLocation == null)
throw new ArgumentNullException(
"fullProjectLocation");
86 if (logger == null)
throw new ArgumentNullException(
"logger");
88 var project = LoadProject(fullProjectLocation, configuration, platform, extraProperties);
89 var assemblyPath = project.GetPropertyValue(
"TargetPath");
91 if (!
string.IsNullOrWhiteSpace(assemblyPath))
93 var asyncBuild =
new CancellableAsyncBuild(project, assemblyPath);
94 asyncBuild.Build(project, targets,
flags,
new LoggerRedirect(logger));
101 public static Microsoft.Build.Evaluation.Project
LoadProject(
string fullProjectLocation,
string configuration =
"Debug",
string platform =
"AnyCPU", Dictionary<string, string> extraProperties = null)
103 configuration = configuration ??
"Debug";
104 platform = platform ??
"AnyCPU";
106 var globalProperties =
new Dictionary<string, string>();
107 globalProperties[
"Configuration"] = configuration;
108 globalProperties[
"Platform"] = platform;
110 if (extraProperties != null)
112 foreach (var extraProperty
in extraProperties)
114 globalProperties[extraProperty.Key] = extraProperty.Value;
118 var projectCollection =
new Microsoft.Build.Evaluation.ProjectCollection(globalProperties);
119 projectCollection.LoadProject(fullProjectLocation);
120 var project = projectCollection.LoadedProjects.First();
126 private readonly
ILogger logger;
127 private readonly
bool onlyErrors;
129 public LoggerRedirect(
ILogger logger,
bool onlyErrors =
false)
131 if (logger == null)
throw new ArgumentNullException(
"logger");
132 this.logger = logger;
133 this.onlyErrors = onlyErrors;
136 public override void Initialize(Microsoft.Build.Framework.IEventSource eventSource)
138 if (eventSource == null)
throw new ArgumentNullException(
"eventSource");
141 eventSource.MessageRaised += MessageRaised;
142 eventSource.WarningRaised += WarningRaised;
144 eventSource.ErrorRaised += ErrorRaised;
147 void MessageRaised(
object sender, BuildMessageEventArgs e)
150 if (loggerResult != null)
152 loggerResult.Module = string.Format(
"{0}({1},{2})",
e.File, e.LineNumber, e.ColumnNumber);
154 switch (e.Importance)
156 case MessageImportance.High:
157 logger.Info(e.Message);
159 case MessageImportance.Normal:
160 logger.Verbose(e.Message);
162 case MessageImportance.Low:
163 logger.Debug(e.Message);
168 void WarningRaised(
object sender, BuildWarningEventArgs e)
171 if (loggerResult != null)
173 loggerResult.Module = string.Format(
"{0}({1},{2})",
e.File, e.LineNumber, e.ColumnNumber);
175 logger.Warning(e.Message);
178 void ErrorRaised(
object sender, Microsoft.Build.Framework.BuildErrorEventArgs e)
181 if (loggerResult != null)
183 loggerResult.Module = string.Format(
"{0}({1},{2})",
e.File, e.LineNumber, e.ColumnNumber);
185 logger.Error(e.Message);
189 private class CancellableAsyncBuild : ICancellableAsyncBuild
191 public CancellableAsyncBuild(
Project project,
string assemblyPath)
194 AssemblyPath = assemblyPath;
197 public string AssemblyPath {
get;
private set; }
203 public bool IsCanceled {
get;
private set; }
205 internal void Build(Microsoft.Build.Evaluation.Project project,
string targets, BuildRequestDataFlags
flags, Microsoft.Build.Utilities.Logger logger)
207 if (project == null)
throw new ArgumentNullException(
"project");
208 if (logger == null)
throw new ArgumentNullException(
"logger");
212 var projectInstance =
new ProjectInstance(project.Xml, project.ProjectCollection.GlobalProperties, null, project.ProjectCollection);
214 BuildTask = Task.Run(() =>
216 var buildResult = mainBuildManager.Build(
217 new BuildParameters(project.ProjectCollection)
219 Loggers =
new[] { logger }
221 new BuildRequestData(projectInstance, targets.Split(
';'), null,
flags));
229 var localManager = mainBuildManager;
230 if (localManager != null)
232 localManager.CancelAllSubmissions();
static ICancellableAsyncBuild CompileProjectAssemblyAsync(string fullProjectLocation, ILogger logger, string targets="Build", string configuration="Debug", string platform="AnyCPU", Dictionary< string, string > extraProperties=null, BuildRequestDataFlags flags=BuildRequestDataFlags.None)
PlatformType
Describes the platform operating system.
SiliconStudio.Core.Diagnostics.ILogger ILogger
static PlatformType GetPlatformTypeFromProject(Microsoft.Build.Evaluation.Project project)
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
SiliconStudio.Core.Diagnostics.LoggerResult LoggerResult
static string GetOrCompileProjectAssembly(string fullProjectLocation, ILogger logger, bool autoCompileProject, string configuration="Debug", string platform="AnyCPU", Dictionary< string, string > extraProperties=null, bool onlyErrors=false, BuildRequestDataFlags flags=BuildRequestDataFlags.None)
static ProjectType GetProjectTypeFromProject(Microsoft.Build.Evaluation.Project project)
Base implementation for ILogger.
static Microsoft.Build.Evaluation.Project LoadProject(string fullProjectLocation, string configuration="Debug", string platform="AnyCPU", Dictionary< string, string > extraProperties=null)
ProjectType
Type of the project.
The dialog has been closed by a cancellation from the user.
static Guid GetProjectGuid(Microsoft.Build.Evaluation.Project project)