Skip to content

Commit

Permalink
#1254 Support VB.NET's case-insensitive attributes for GetAttributes …
Browse files Browse the repository at this point in the history
…and UpdateAttributes
  • Loading branch information
Samuel Schuhmacher authored and Samuel Schuhmacher committed Jun 2, 2016
1 parent 60acb2f commit 2d84905
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
34 changes: 19 additions & 15 deletions src/app/FakeLib/AssemblyInfoFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,18 @@ let private removeAtEnd (textToRemove:string) (text:string) =
let GetAttributes assemblyInfoFile =
let text = File.ReadAllText assemblyInfoFile

let regex =
if assemblyInfoFile.ToLower().EndsWith(".cs") then regexAttrNameValueCs
elif assemblyInfoFile.ToLower().EndsWith(".fs") then regexAttrNameValueFs
elif assemblyInfoFile.ToLower().EndsWith(".vb") then regexAttrNameValueVb
elif assemblyInfoFile.ToLower().EndsWith(".cpp") then regexAttrNameValueCpp
// VB.NET is case-insensitive. Handle assembly attributes accordingly
let (regex, additionalRegexOptions) =
if assemblyInfoFile.ToLower().EndsWith(".cs") then (regexAttrNameValueCs, RegexOptions.None)
elif assemblyInfoFile.ToLower().EndsWith(".fs") then (regexAttrNameValueFs, RegexOptions.None)
elif assemblyInfoFile.ToLower().EndsWith(".vb") then (regexAttrNameValueVb, RegexOptions.IgnoreCase)
elif assemblyInfoFile.ToLower().EndsWith(".cpp") then (regexAttrNameValueCpp, RegexOptions.None)
else
failwithf "Assembly info file type not supported: %s" assemblyInfoFile

Regex.Matches(text, regex, RegexOptions.Multiline)
let combinedRegexOptions = RegexOptions.Multiline ||| additionalRegexOptions

Regex.Matches(text, regex, combinedRegexOptions)
|> Seq.cast<Match>
|> Seq.map (fun m -> Attribute(m.Groups.["name"].Value |> removeAtEnd "Attribute",
m.Groups.["value"].Value,
Expand All @@ -314,15 +317,15 @@ let GetAttributeValue attrName assemblyInfoFile =
| Some attr -> Some attr.Value
| None -> None

let private updateAttr regexFactory text (attribute:Attribute) =
let private updateAttr regexFactory additionalRegexOptions text (attribute:Attribute) =
let regex = regexFactory attribute.Name

let m = Regex.Match(text, regex, RegexOptions.Multiline)
let m = Regex.Match(text, regex, RegexOptions.Multiline ||| additionalRegexOptions)

// Replace if found with different value
if m.Success && m.Value <> attribute.Value then
tracefn "Attribute '%s' updated: %s" attribute.Name attribute.Value
Regex.Replace(text, regex, attribute.Value, RegexOptions.Multiline)
Regex.Replace(text, regex, attribute.Value, RegexOptions.Multiline ||| additionalRegexOptions)

// Do nothing if found with the same value
elif m.Success then
Expand All @@ -340,15 +343,16 @@ let private updateAttr regexFactory text (attribute:Attribute) =
let UpdateAttributes assemblyInfoFile (attributes: seq<Attribute>) =
tracefn "Updating attributes in: %s" assemblyInfoFile

let regexFactory =
if assemblyInfoFile.ToLower().EndsWith(".cs") then regexAttrValueCs
elif assemblyInfoFile.ToLower().EndsWith(".fs") then regexAttrValueFs
elif assemblyInfoFile.ToLower().EndsWith(".vb") then regexAttrValueVb
elif assemblyInfoFile.ToLower().EndsWith(".cpp") then regexAttrValueCpp
// VB.NET is case-insensitive. Handle assembly attributes accordingly
let (regexFactory, additionalRegexOptions) =
if assemblyInfoFile.ToLower().EndsWith(".cs") then (regexAttrValueCs, RegexOptions.None)
elif assemblyInfoFile.ToLower().EndsWith(".fs") then (regexAttrValueFs, RegexOptions.None)
elif assemblyInfoFile.ToLower().EndsWith(".vb") then (regexAttrValueVb, RegexOptions.IgnoreCase)
elif assemblyInfoFile.ToLower().EndsWith(".cpp") then (regexAttrValueCpp, RegexOptions.None)
else
failwithf "Assembly info file type not supported: %s" assemblyInfoFile

let text = File.ReadAllText assemblyInfoFile
let newText = attributes |> Seq.fold (updateAttr regexFactory) text
let newText = attributes |> Seq.fold (updateAttr regexFactory additionalRegexOptions) text

File.WriteAllText(assemblyInfoFile, newText)
20 changes: 16 additions & 4 deletions src/test/Test.FAKECore/AssemblyInfoSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,42 +242,54 @@ public class when_using_vb_task_with_default_config
string infoFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".vb");
const string original = "' <auto-generated/>\r\nImports System.Reflection\r\n\r\n" +
"<assembly: AssemblyProduct(\"TestLib\")>\r\n" +
"<assembly: AssemblyVersionAttribute(\"1.0.0.0\")>\r\n";
"<Assembly: AssemblyCompany(\"TestCompany\")>\r\n" +
"<assembly: AssemblyVersionAttribute(\"1.0.0.0\")>\r\n" +
"<Assembly: ComVisibleAttribute(false)>";
File.WriteAllText(infoFile, original.Replace("\r\n", Environment.NewLine));

// Act
var attributes = new[]
{
AssemblyInfoFile.Attribute.Product("TestLibNew"),
AssemblyInfoFile.Attribute.Company("TestCompanyNew"),
AssemblyInfoFile.Attribute.Version("2.0.0.0")
};
AssemblyInfoFile.UpdateAttributes(infoFile, attributes);

// Assert
const string expected = "' <auto-generated/>\r\nImports System.Reflection\r\n\r\n" +
"<assembly: AssemblyProduct(\"TestLibNew\")>\r\n" +
"<assembly: AssemblyVersionAttribute(\"2.0.0.0\")>\r\n";
"<Assembly: AssemblyCompany(\"TestCompanyNew\")>\r\n" +
"<assembly: AssemblyVersionAttribute(\"2.0.0.0\")>\r\n" +
"<Assembly: ComVisibleAttribute(false)>";

File.ReadAllText(infoFile)
.ShouldEqual(expected.Replace("\r\n", Environment.NewLine));
};

It get_attribute_should_read_attribute_from_vb_file = () =>
{
// Arrange. Create attribute both with and without "Attribute" at the end
// Arrange. Create attribute both with and without "Attribute" at the end, and also
// case-insensitive attributes
string infoFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".vb");
const string original = "' <auto-generated/>\r\nImports System.Reflection\r\n\r\n" +
"<assembly: AssemblyProduct(\"TestLib\")>\r\n" +
"<assembly: AssemblyVersionAttribute(\"1.0.0.0\")>\r\n";
"<Assembly: AssemblyCompany(\"TestCompany\")>\r\n" +
"<assembly: AssemblyVersionAttribute(\"1.0.0.0\")>\r\n" +
"<Assembly: ComVisibleAttribute(false)>";
File.WriteAllText(infoFile, original.Replace("\r\n", Environment.NewLine));

// Act
var productAttr = AssemblyInfoFile.GetAttribute("AssemblyProduct", infoFile).Value;
var companyAttr = AssemblyInfoFile.GetAttribute("AssemblyCompany", infoFile).Value;
var versionAttr = AssemblyInfoFile.GetAttribute("AssemblyVersion", infoFile).Value;
var comVisibleAttr = AssemblyInfoFile.GetAttribute("ComVisible", infoFile).Value;

// Assert
productAttr.Value.ShouldEqual("\"TestLib\"");
companyAttr.Value.ShouldEqual("\"TestCompany\"");
versionAttr.Value.ShouldEqual("\"1.0.0.0\"");
comVisibleAttr.Value.ShouldEqual("false");
};
}
}

0 comments on commit 2d84905

Please sign in to comment.