New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add targets to create and manage FakeItEasy.user.props #1688
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @blairconrad, very nice! The regex makes me really uncomfortable, though...
tools/FakeItEasy.Build/Program.cs
Outdated
() => | ||
{ | ||
var originalText = File.ReadAllText("FakeItEasy.user.props", Encoding.UTF8); | ||
var newText = Regex.Replace(originalText, "(?<=<BuildProfile>)[^>]*(?=</BuildProfile>)", profile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I... don't even understand how that regex works. How about using Linq to XML? I think the code would be almost as short, and much easier to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. And at the risk of tooting your horn, I think it's an improvement!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for indulging me, @blairconrad. I have a few more comments... Sorry!
tools/FakeItEasy.Build/Program.cs
Outdated
var newText = Regex.Replace(originalText, "(?<=<BuildProfile>)[^>]*(?=</BuildProfile>)", profile); | ||
if (newText != originalText) | ||
var xmlDoc = XDocument.Load("FakeItEasy.user.props", LoadOptions.PreserveWhitespace); | ||
var buildProfileElement = xmlDoc.Descendants("BuildProfile").FirstOrDefault(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to avoid using Descendants
, because it can match elements unrelated to the one we're looking for (e.g. a BuildProfile
element in an ItemGroup
). I usually try to specify the path more precisely, e.g. xmlDoc.Root.Elements("PropertyGroup").Elements("BuildProfile").FirstOrDefault()
.
Anyway, the current code doesn't handle the case where the PropertyGroup
or BuildProfile
element is missing... You probably need something like this:
var buildProfileElement = xmlDoc.Root.Elements("PropertyGroup").Elements("BuildProfile").FirstOrDefault();
if (buildProfileElement is null)
{
var propertyGroupElement = xmlDoc.Root.Element("PropertyGroup");
if (propertyGroupElement is null)
{
propertyGroupElement = new XElement("PropertyGroup");
xmlDoc.Root.Add(propertyGroupElement);
}
buildProfileElement = new XElement("BuildProfile");
propertyGroupElement.Add(buildProfileElement);
}
...
Well, it ended up being much longer than the original version... But to be fair, it also handles more cases!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured there'd be a low chance that the file would exist and be that malformed. However, I wasn't considering that users would fail to be notified that the transformation wasn't made. Glad you thought of it.
Pushed a fixup. I deviated from your suggestions slightly
- I started checking for missing elements at PropertyGroup, not BuildProfile
- I assumed only a singled BuildProfile element in the PropertyGroup
- (more a deviation from my original), I stopped preserving whitespace on load, since it easily allowed me to indent the output on save. Without that change, newly-added elements were on the same line as their parents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… and added a new fixup, since you seem to not have digested the last one yet. I extracted GetOrAddElement
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there ;)
tools/FakeItEasy.Build/Program.cs
Outdated
var xmlDoc = XDocument.Load("FakeItEasy.user.props"); | ||
|
||
var propertyGroupElement = GetOrAddElement(xmlDoc.Root, "PropertyGroup"); | ||
var buildProfileElement = GetOrAddElement(propertyGroupElement, "BuildProfile"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assumes the BuildProfile
element is in the first PropertyGroup
... it might not be the case. That's why I first looked for BuildProfile
in my suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your error- and future-proofing is infuriating!
Infuriatingly correct and appropriate.
I've pushed a thing. It's exactly your thing, so it probably satisfies.
(Oh, as before, I'm no longer preserving whitespace, as it seemed the lesser of the two formatting evils.)
Sorry to have taken so much of your time with my whim.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Oh, as before, I'm no longer preserving whitespace, as it seemed the lesser of the two formatting evils.)
👍
Sorry to have taken so much of your time with my whim.
You haven't!
bf50eef
to
7e75ae8
Compare
Thanks, @blairconrad! |
Oh, I can't merge yet... you need to squash first |
7e75ae8
to
c454cf1
Compare
Thanks. Squashed. I enjoyed your comments! |
This change has been released as part of FakeItEasy 6.0.0-beta.1. |
Augments #1662