Skip to content

Commit 1def2fd

Browse files
committed
Separate ProtoBufTaskPath and ProtoGenPath MSBuild properties
This allows this build task to be placed in a separate location
1 parent b07fa17 commit 1def2fd

3 files changed

Lines changed: 130 additions & 23 deletions

File tree

ProtoBuf.Project/MSBuildProject.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public class MSBuildProject
99

1010
XmlDocument xd;
1111

12+
public string ProtoBufTaskPath { get; private set; }
1213
public string ProtoGenPath { get; private set; }
13-
public bool HasProtoGenImport { get; private set; }
14+
public bool HasProtoBufTaskImport { get; private set; }
1415

1516
public void Open(string projectFile)
1617
{
@@ -19,13 +20,17 @@ public void Open(string projectFile)
1920

2021
var xnm = new XmlNamespaceManager(xd.NameTable);
2122
xnm.AddNamespace("msbuild", msbuildNS);
23+
var protoTaskPathNode = xd.SelectSingleNode("/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoBufTaskPath", xnm);
24+
if (protoTaskPathNode != null)
25+
ProtoBufTaskPath = protoTaskPathNode.InnerText;
26+
2227
var protoGenPathNode = xd.SelectSingleNode("/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoGenPath", xnm);
2328
if (protoGenPathNode != null)
2429
ProtoGenPath = protoGenPathNode.InnerText;
2530

26-
string importProject = @"$(ProtoGenPath)\ProtoBuf.MSBuildTask.targets";
31+
string importProject = @"$(ProtoBufTaskPath)\ProtoBuf.MSBuildTask.targets";
2732
var importNode = xd.SelectSingleNode("/msbuild:Project/msbuild:Import[@Project='" + importProject + "']", xnm);
28-
HasProtoGenImport = importNode != null;
33+
HasProtoBufTaskImport = importNode != null;
2934
}
3035

3136
public string[] LoadItems()
@@ -37,31 +42,48 @@ public string[] LoadItems()
3742
return nodes.Cast<XmlElement>().Select(a => a.GetAttribute("Include")).Where(a => !string.IsNullOrEmpty(a)).ToArray();
3843
}
3944

40-
public void Init(string protoGenPath)
45+
public void Init(string protoBufTaskPath, string protoGenPath)
4146
{
4247
var xnm = new XmlNamespaceManager(xd.NameTable);
4348
xnm.AddNamespace("msbuild", msbuildNS);
4449

45-
string importProject = @"$(ProtoGenPath)\ProtoBuf.MSBuildTask.targets";
50+
string importProject = @"$(ProtoBufTaskPath)ProtoBuf.MSBuildTask.targets";
4651
var importNode = xd.SelectSingleNode("/msbuild:Project/msbuild:Import[@Project='" + importProject + "']", xnm);
4752
if (importNode == null)
4853
{
4954
var xe = xd.CreateElement("Import", msbuildNS);
5055
xe.SetAttribute("Project", importProject);
5156
importNode = xe;
5257
xd.DocumentElement.AppendChild(xe);
53-
HasProtoGenImport = true;
58+
HasProtoBufTaskImport = true;
5459
}
5560

56-
var protoGenPathNode = xd.SelectSingleNode("/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoGenPath", xnm);
57-
if (protoGenPathNode == null)
61+
XmlElement propertyGroupNode;
62+
var protoBufTaskPathNode = xd.SelectSingleNode("/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoBufTaskPath", xnm);
63+
if (protoBufTaskPathNode == null)
5864
{
5965
var xe = xd.CreateElement("PropertyGroup", msbuildNS);
60-
var xe2 = xd.CreateElement("ProtoGenPath", msbuildNS);
61-
xe2.InnerText = protoGenPath;
66+
var xe2 = xd.CreateElement("ProtoBufTaskPath", msbuildNS);
67+
xe2.InnerText = protoBufTaskPath;
6268
xe.AppendChild(xe2);
69+
propertyGroupNode = xe;
6370
// ensure properties come before targets
6471
xd.DocumentElement.InsertBefore(xe, importNode);
72+
ProtoBufTaskPath = protoBufTaskPath;
73+
}
74+
else
75+
{
76+
propertyGroupNode = protoBufTaskPathNode.ParentNode as XmlElement;
77+
protoBufTaskPathNode.InnerText = protoBufTaskPath;
78+
ProtoBufTaskPath = protoBufTaskPath;
79+
}
80+
81+
var protoGenPathNode = xd.SelectSingleNode("/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoGenPath", xnm);
82+
if (protoGenPathNode == null)
83+
{
84+
var xe2 = xd.CreateElement("ProtoGenPath", msbuildNS);
85+
xe2.InnerText = protoGenPath;
86+
propertyGroupNode.AppendChild(xe2);
6587
ProtoGenPath = protoGenPath;
6688
}
6789
else

ProtoBuf.ProjectInserter.WinForms/Form1.Designer.cs

Lines changed: 82 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProtoBuf.ProjectInserter.WinForms/Form1.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public partial class Form1 : Form
1313
public Form1()
1414
{
1515
InitializeComponent();
16+
lblFileCount.Text = "0";
1617
}
1718

1819
private void btnOpen_Click(object sender, EventArgs e)
@@ -22,21 +23,33 @@ private void btnOpen_Click(object sender, EventArgs e)
2223
project.Open(ofd1.FileName);
2324
projectFile = ofd1.FileName;
2425
txtProject.Text = projectFile;
26+
txtProtoBufTaskPath.Text = project.ProtoBufTaskPath;
2527
txtProtoGenPath.Text = project.ProtoGenPath;
2628
btnInit.Enabled = true;
27-
btnAddFiles.Enabled = btnSave.Enabled = (project.HasProtoGenImport && !string.IsNullOrEmpty(project.ProtoGenPath));
29+
btnAddFiles.Enabled = btnSave.Enabled = (project.HasProtoBufTaskImport && !string.IsNullOrEmpty(project.ProtoBufTaskPath) && !string.IsNullOrEmpty(project.ProtoGenPath));
2830
lsbFiles.Items.Clear();
2931
lsbFiles.Items.AddRange(project.LoadItems());
32+
lblFileCount.Text = lsbFiles.Items.Count.ToString();
3033
}
3134

3235
private void btnInit_Click(object sender, EventArgs e)
3336
{
37+
fbd1.Description = "ProtoBufTaskPath";
38+
fbd1.SelectedPath = MakeAbsoluteFileName(project.ProtoGenPath ?? "");
39+
if (fbd1.ShowDialog() != DialogResult.OK) return;
40+
string protobufTaskPath = GetRelativeFileName(fbd1.SelectedPath + Path.DirectorySeparatorChar);
41+
protobufTaskPath = protobufTaskPath == "" ? "." : protobufTaskPath;
42+
protobufTaskPath += protobufTaskPath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? "" : Path.DirectorySeparatorChar.ToString();
43+
44+
fbd1.Description = "ProtoGenPath";
3445
fbd1.SelectedPath = MakeAbsoluteFileName(project.ProtoGenPath ?? "");
3546
if (fbd1.ShowDialog() != DialogResult.OK) return;
3647
string protoGenPath = GetRelativeFileName(fbd1.SelectedPath + Path.DirectorySeparatorChar);
3748
protoGenPath = protoGenPath == "" ? "." : protoGenPath;
3849
protoGenPath += protoGenPath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? "" : Path.DirectorySeparatorChar.ToString();
39-
project.Init(protoGenPath);
50+
51+
project.Init(protobufTaskPath, protoGenPath);
52+
txtProtoBufTaskPath.Text = project.ProtoBufTaskPath;
4053
txtProtoGenPath.Text = project.ProtoGenPath;
4154
btnAddFiles.Enabled = btnSave.Enabled = true;
4255
}
@@ -62,6 +75,7 @@ private void btnAddFiles_Click(object sender, EventArgs e)
6275
project.AddItem(GetRelativeFileName(file));
6376
lsbFiles.Items.Clear();
6477
lsbFiles.Items.AddRange(project.LoadItems());
78+
lblFileCount.Text = lsbFiles.Items.Count.ToString();
6579
}
6680

6781
private void btnSave_Click(object sender, EventArgs e)

0 commit comments

Comments
 (0)