4 using System.Collections.Generic;
5 using System.Diagnostics;
8 using System.Runtime.InteropServices;
10 using System.Text.RegularExpressions;
11 using Microsoft.Build.Execution;
12 using Microsoft.Build.Framework;
13 using Microsoft.VisualStudio;
14 using Microsoft.VisualStudio.Shell;
15 using Microsoft.VisualStudio.Shell.Interop;
16 using SiliconStudio.Paradox.VisualStudio.CodeGenerator;
19 namespace SiliconStudio.
Paradox.VisualStudio.BuildEngine
22 [ClassInterface(ClassInterfaceType.None)]
23 [Guid(GuidList.guidParadox_VisualStudio_DataCodeGenerator)]
24 [ProvideObject(typeof(DataCodeGenerator), RegisterUsing = RegistrationMethod.CodeBase)]
27 public const string DisplayName =
"Paradox Data Code Generator";
28 public const string InternalName =
"ParadoxDataCodeGenerator";
30 protected override byte[]
GenerateCode(
string inputFileName,
string inputFileContent)
36 var lines = Regex.Split(inputFileContent,
"\r\n|\r|\n");
37 if (lines.Length == 0 || lines[0].Length == 0)
39 throw new InvalidOperationException(
"Source should contain project filename.");
42 var projectFullName = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(inputFileName), lines[0]));
43 if (!
File.Exists(projectFullName))
45 throw new InvalidOperationException(
"Project file doesn't exist.");
48 string assemblyOutput, intermediateAssembly;
51 Microsoft.Build.Evaluation.Project msbuildProject = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(projectFullName).First();
54 var property1 = msbuildProject.SetProperty(
"ParadoxBuildStep",
"StepData");
55 var property2 = msbuildProject.SetProperty(
"IntermediateOutputPath",
@"obj\StepData\");
58 msbuildProject.ReevaluateIfNecessary();
62 var outputPane = GetOutputPane();
65 var buildLogger =
new IDEBuildLogger(outputPane,
new TaskProvider(GlobalServiceProvider), VsHelper.GetCurrentHierarchy(GlobalServiceProvider));
66 buildLogger.Verbosity = Microsoft.Build.Framework.LoggerVerbosity.Diagnostic;
68 var evaluatedProperties =
new Dictionary<string, Microsoft.Build.Evaluation.ProjectProperty>();
69 foreach (var evaluatedProperty
in msbuildProject.AllEvaluatedProperties)
71 evaluatedProperties[evaluatedProperty.Name] = evaluatedProperty;
75 foreach (var evaluatedProperty
in evaluatedProperties)
77 outputPane.OutputStringThreadSafe(string.Format(
78 "$({0}) = {1} was evaluated as {2}\n",
79 evaluatedProperty.Key,
80 evaluatedProperty.Value.UnevaluatedValue,
81 evaluatedProperty.Value.EvaluatedValue));
87 using (var manager =
new BuildManager())
89 var pc =
new Microsoft.Build.Evaluation.ProjectCollection();
90 var globalProperties =
new Dictionary<string, string>();
91 globalProperties[
"SolutionName"] = evaluatedProperties[
"SolutionName"].EvaluatedValue;
92 globalProperties[
"SolutionDir"] = evaluatedProperties[
"SolutionDir"].EvaluatedValue;
93 var projectInstance =
new ProjectInstance(projectFullName, globalProperties, null);
94 var buildResult = manager.Build(
95 new BuildParameters(pc)
97 Loggers =
new[] {buildLogger},
98 DetailedSummary =
true,
100 new BuildRequestData(projectInstance,
new[] {
"Compile" }, null));
103 throw new InvalidOperationException(
string.Format(
"Build of {0} failed.", projectFullName));
107 assemblyOutput = msbuildProject.AllEvaluatedProperties.Last(x => x.Name ==
"TargetPath").EvaluatedValue;
108 intermediateAssembly = msbuildProject.AllEvaluatedItems.First(x => x.ItemType ==
"IntermediateAssembly").EvaluatedInclude;
112 msbuildProject.RemoveProperty(property1);
113 msbuildProject.RemoveProperty(property2);
121 var remoteCommands = ParadoxCommandsProxy.CreateProxy(domain);
122 return remoteCommands.GenerateDataClasses(assemblyOutput, projectFullName, intermediateAssembly);
126 GeneratorError(4, ex.ToString(), 0, 0);
140 var outputWindow = (IVsOutputWindow)Package.GetGlobalService(typeof(SVsOutputWindow));
143 IVsOutputWindowPane pane;
144 Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane;
145 outputWindow.CreatePane(ref generalPaneGuid,
"General", 1, 0);
146 outputWindow.GetPane(ref generalPaneGuid, out pane);
override string GetDefaultExtension()
Gets the default extension of the output file from the CodeDomProvider
static AppDomain CreateAppDomain()
Proxies commands to real IParadoxCommands implementation.
Base code generator with site implementation
IVsOutputWindowPane GetOutputPane()
override byte[] GenerateCode(string inputFileName, string inputFileContent)
The method that does the actual work of generating code given the input file