handle mismatched msbuild versions with the installed Visual Studio tools

git-svn-id: https://svn.apache.org/repos/asf/incubator/npanday/trunk@1609950 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/components/dotnet-msbuild/src/main/java/npanday/msbuild/xdt/XmlDocumentTransformer.java b/components/dotnet-msbuild/src/main/java/npanday/msbuild/xdt/XmlDocumentTransformer.java
index b714a5c..5c36e96 100644
--- a/components/dotnet-msbuild/src/main/java/npanday/msbuild/xdt/XmlDocumentTransformer.java
+++ b/components/dotnet-msbuild/src/main/java/npanday/msbuild/xdt/XmlDocumentTransformer.java
@@ -30,6 +30,7 @@
 import npanday.msbuild.MsbuildInvocationParameters;
 import npanday.msbuild.MsbuildInvoker;
 import npanday.vendor.VendorRequirement;
+import org.codehaus.plexus.interpolation.os.Os;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -90,6 +91,7 @@
         parameters.setProperty( "Source", baseFile.getAbsolutePath() );
         parameters.setProperty( "Transform", transformationFile.getAbsolutePath() );
         parameters.setProperty( "Destination", targetFile.getAbsolutePath() );
+        parameters.setProperty( "VisualStudioVersion", findRequiredVSVersion() );
 
         targetFile.getParentFile().mkdirs();
 
@@ -106,6 +108,32 @@
         }
     }
 
+    private static String findRequiredVSVersion() throws XmlDocumentTransformException {
+        if (Os.isArch("amd64")) {
+            return findRequiredVSVersion(System.getenv("PROGRAMFILES(X86)"));
+        }
+        else {
+            return findRequiredVSVersion(System.getenv("PROGRAMFILES"));
+        }
+    }
+
+    private static String findRequiredVSVersion(String programfiles) throws XmlDocumentTransformException {
+        File[] dirs = new File( programfiles, "MSBuild/Microsoft/VisualStudio" ).listFiles();
+        String version = null;
+        if (dirs != null) {
+            for (File dir : dirs) {
+                if (new File(dir, "Web/Microsoft.Web.Publishing.Tasks.dll").exists()) {
+                    version = dir.getName().substring(1);
+                }
+            }
+        }
+        if (version == null) {
+            throw new XmlDocumentTransformException("Unable to find required tasks file in '" + programfiles +
+                    "\\MSBuild\\Microsoft\\VisualStudio'");
+        }
+        return version;
+    }
+
     private File extractResource( String resourceName ) throws IOException
     {
         final URL resource = Resources.getResource( getClass(), resourceName );