Here is a NAnt task I am using to enforce VB.NET project settings during a build. This is useful if you can’t use <vbc> to compile your projects.
Usage example:
<vboptions compare="Binary" strict="true" explicit="true"
path="${projectfile}"
if="${path::get-extension(projectfile) == '.vbproj'}" />
The code:
/// <summary>
/// Enforces specific VB settings on a particular project.
/// </summary>
[TaskName("vboptions")]
public class VbOptionsTask : Task
{
private string _path;
private bool _strict;
private bool _explicit;
private string _compare;
[TaskAttribute("path", Required=true)]
public string Path
{
get { return _path; }
set { _path = value; }
}
[TaskAttribute("explicit", Required=false)]
public bool Explicit
{
get { return _explicit; }
set { _explicit = value; }
}
[TaskAttribute("compare", Required=false)]
public string Compare
{
get { return _compare; }
set { _compare = value; }
}
[TaskAttribute("strict", Required=false)]
public bool Strict
{
get { return _strict; }
set { _strict = value; }
}
private bool CheckOption(string opt, bool val)
{
bool on = (string.Compare(opt, "On", true) == 0);
return (on == val);
}
private void ValidateOption(XmlNode node, string attr, bool val)
{
XmlAttribute a = node.Attributes[attr];
if(a == null || !CheckOption(a.Value, val))
{
throw new BuildException(
string.Format("The build option '{0}' in project {1} is not set to '{2}.'",
attr, this.Path, (val ? "On" : "Off")
)
);
}
}
protected override void ExecuteTask()
{
XmlDocument proj = ProjectFactory.LoadProjectXml(this.Path);
if(proj == null)
{
throw new BuildException("Could not load project xml for: " + this.Path + ".");
}
XmlNode settings = proj.SelectSingleNode("//VisualBasic/Build/Settings");
if(proj == null)
{
throw new BuildException(this.Path + " is not an Everett .vbproj file.");
}
XmlAttribute c = settings.Attributes["OptionCompare"];
if(c == null || string.Compare(c.Value, this.Compare, true) != 0)
{
throw new BuildException(string.Format("The project {0} does not have Option Compare set to {1}.", this.Path, this.Compare));
}
ValidateOption(settings, "OptionStrict", this.Strict);
ValidateOption(settings, "OptionExplicit", this.Explicit);
}
public VbOptionsTask()
{
_compare = "Binary";
_explicit = true;
_strict = true;
}
}