NAnt.MSBuild.MSBuildProject.DetermineProductVersion C# (CSharp) Method

DetermineProductVersion() protected method

Determines the version of the target msbuild file.

This method identifies the msbuild version by reviewing the following tags in order:

<ProductVersion> <TargetFrameworkVersion> ToolsVersion attribute
/// version string found in the tags listed above is not recognized. ///
protected DetermineProductVersion ( System docElement ) : ProductVersion
docElement System /// A representing the msbuild project file. ///
return ProductVersion
        protected override ProductVersion DetermineProductVersion(System.Xml.XmlElement docElement)
        {
            XmlNamespaceManager _nsMgr = new XmlNamespaceManager(new NameTable());
            _nsMgr.AddNamespace("ms", docElement.NamespaceURI);

            // <ProductVersion> element node
            XmlNode _productVerNode = docElement.SelectSingleNode("ms:PropertyGroup/ms:ProductVersion", _nsMgr);
            // <TargetFrameworkVersion> element node
            XmlNode _targetNetVerNode = docElement.SelectSingleNode("ms:PropertyGroup/ms:TargetFrameworkVersion", _nsMgr);

            // If the <ProductVersion> element exists and it is not empty, get the
            // product version from it.
            if (_productVerNode != null && !StringUtils.IsNullOrEmpty(_productVerNode.InnerText)) {
                Version _ver = new Version(_productVerNode.InnerText);

                switch (_ver.Major) {
                    case 8:
                        //    <ProductVersion>8.0.50727</ProductVersion>
                        return ProductVersion.Whidbey;
                    case 9:
                        //    <ProductVersion>9.0.21022</ProductVersion>
                        if (_ver.Build <= 21022) {
                            return ProductVersion.Orcas;
                        }
                        return ProductVersion.Rosario;
                }

            // If the <TargetFrameworkVersion> element exists, get the product version from it.
            } else if (_targetNetVerNode != null) {
                string targetFrameworkVer = _targetNetVerNode.InnerText;

                switch (targetFrameworkVer.ToUpper().Trim()) {
                    case "V4.0":
                        return ProductVersion.Rosario;
                    case "V3.5":
                        return ProductVersion.Orcas;
                    case "V2.0":
                        return ProductVersion.Whidbey;
                }

            // If neither of the above mentioned tags exist, look for the "ToolsVersion"
            // attribute in the <Project> tag.
            } else {
                XmlAttribute toolsVersionAttribute = docElement.Attributes["ToolsVersion"];

                // If the ToolsVersion attribute does not exist at this point,
                // assume that the project is 2.0.
                if (toolsVersionAttribute == null) {
                    return ProductVersion.Whidbey;
                }

                switch (toolsVersionAttribute.Value) {
                    case "4.0":
                        return ProductVersion.Rosario;
                    case "3.5":
                        return ProductVersion.Orcas;
                    case "2.0":
                        return ProductVersion.Whidbey;
                }
            }

            // Throw a buildexception if none of the version numbers above are found.
            throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                "Unknown Project version '{0}'.", ProjectPath), Location.UnknownLocation);
        }