1- using System . Xml ;
1+ using System . Linq ;
2+ using System . Xml ;
23
34namespace ProtoBuf . Project
45{
@@ -8,10 +9,32 @@ public class MSBuildProject
89
910 XmlDocument xd ;
1011
12+ public string ProtoGenPath { get ; private set ; }
13+ public bool HasProtoGenImport { get ; private set ; }
14+
1115 public void Open ( string projectFile )
1216 {
1317 xd = new XmlDocument ( ) ;
1418 xd . Load ( projectFile ) ;
19+
20+ var xnm = new XmlNamespaceManager ( xd . NameTable ) ;
21+ xnm . AddNamespace ( "msbuild" , msbuildNS ) ;
22+ var protoGenPathNode = xd . SelectSingleNode ( "/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoGenPath" , xnm ) ;
23+ if ( protoGenPathNode != null )
24+ ProtoGenPath = protoGenPathNode . InnerText ;
25+
26+ string importProject = @"$(ProtoGenPath)\ProtoBuf.MSBuildTask.targets" ;
27+ var importNode = xd . SelectSingleNode ( "/msbuild:Project/msbuild:Import[@Project='" + importProject + "']" , xnm ) ;
28+ HasProtoGenImport = importNode != null ;
29+ }
30+
31+ public string [ ] LoadItems ( )
32+ {
33+ var xnm = new XmlNamespaceManager ( xd . NameTable ) ;
34+ xnm . AddNamespace ( "msbuild" , msbuildNS ) ;
35+
36+ var nodes = xd . SelectNodes ( "/msbuild:Project/msbuild:ItemGroup/msbuild:ProtoBuf" , xnm ) ;
37+ return nodes . Cast < XmlElement > ( ) . Select ( a => a . GetAttribute ( "Include" ) ) . Where ( a => ! string . IsNullOrEmpty ( a ) ) . ToArray ( ) ;
1538 }
1639
1740 public void Init ( string protoGenPath )
@@ -27,6 +50,7 @@ public void Init(string protoGenPath)
2750 xe . SetAttribute ( "Project" , importProject ) ;
2851 importNode = xe ;
2952 xd . DocumentElement . AppendChild ( xe ) ;
53+ HasProtoGenImport = true ;
3054 }
3155
3256 var protoGenPathNode = xd . SelectSingleNode ( "/msbuild:Project/msbuild:PropertyGroup[not(@Condition)]/msbuild:ProtoGenPath" , xnm ) ;
@@ -38,6 +62,12 @@ public void Init(string protoGenPath)
3862 xe . AppendChild ( xe2 ) ;
3963 // ensure properties come before targets
4064 xd . DocumentElement . InsertBefore ( xe , importNode ) ;
65+ ProtoGenPath = protoGenPath ;
66+ }
67+ else
68+ {
69+ protoGenPathNode . InnerText = protoGenPath ;
70+ ProtoGenPath = protoGenPath ;
4171 }
4272 }
4373
0 commit comments