-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentProject.cs
72 lines (61 loc) · 2.84 KB
/
ContentProject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Gnomoria.ContentExtractor.Extensions;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using NLog;
using System.Collections.Generic;
using System.IO;
namespace Gnomoria.ContentExtractor
{
public class ContentProject
{
BuildParameters buildParams;
Project buildProject;
ILogger errorLogger;
public ContentProject(string outputPath, string projectDir = null)
{
var rootElement = ProjectRootElement.Create();
rootElement.AddImport(Path.GetFullPath("lib\\Microsoft.Xna.GameStudio.ContentPipeline.targets"));
buildProject = new Project(rootElement);
buildProject.SetProperty("XnaPlatform", "Windows");
buildProject.SetProperty("XnaProfile", "Reach");
buildProject.SetProperty("XnaFrameworkVersion", "v4.0");
buildProject.SetProperty("Configuration", "Release");
buildProject.SetProperty("OutputPath", outputPath);
if (!projectDir.IsNullOrEmpty())
buildProject.SetProperty("BaseIntermediateOutputPath", projectDir.GetRelativePathFrom(Directory.GetCurrentDirectory()));
errorLogger = new ConfigurableForwardingLogger
{
BuildEventRedirector = new NlogEventRedirector()
};
buildParams = new BuildParameters(ProjectCollection.GlobalProjectCollection);
buildParams.Loggers = new ILogger[] { errorLogger };
}
public void AddItem(string file, string importer, string link, string name, string processor = null)
{
ProjectItem item = buildProject.AddItem("Compile", file)[0];
item.SetMetadataValue("Link", link);
item.SetMetadataValue("Name", name);
item.SetMetadataValue("Importer", importer);
item.SetMetadataValue("Processor", processor.IsNullOrEmpty() ? "PassThroughProcessor" : processor);
}
public void AddReference(string assembly, string hintPath = null)
{
if (hintPath.IsNullOrEmpty())
buildProject.AddItem("Reference", assembly);
else
buildProject.AddItem("Reference", assembly, new Dictionary<string, string> { { "HintPath", hintPath } });
}
public bool Build()
{
BuildManager.DefaultBuildManager.BeginBuild(buildParams);
BuildRequestData request = new BuildRequestData(buildProject.CreateProjectInstance(), new string[0]);
BuildSubmission submission = BuildManager.DefaultBuildManager.PendBuildRequest(request);
var result = submission.Execute();
BuildManager.DefaultBuildManager.EndBuild();
return result.OverallResult == BuildResultCode.Success;
}
}
}