Skip to content
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

Merged
merged 1 commit into from Nov 6, 2019

Conversation

blairconrad
Copy link
Member

Augments #1662

Copy link
Member

@thomaslevesque thomaslevesque left a 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...

() =>
{
var originalText = File.ReadAllText("FakeItEasy.user.props", Encoding.UTF8);
var newText = Regex.Replace(originalText, "(?<=<BuildProfile>)[^>]*(?=</BuildProfile>)", profile);
Copy link
Member

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.

Copy link
Member Author

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!

Copy link
Member

@thomaslevesque thomaslevesque left a 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 Show resolved Hide resolved
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();
Copy link
Member

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!

Copy link
Member Author

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

  1. I started checking for missing elements at PropertyGroup, not BuildProfile
  2. I assumed only a singled BuildProfile element in the PropertyGroup
  3. (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

Copy link
Member Author

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.

Copy link
Member

@thomaslevesque thomaslevesque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there ;)

var xmlDoc = XDocument.Load("FakeItEasy.user.props");

var propertyGroupElement = GetOrAddElement(xmlDoc.Root, "PropertyGroup");
var buildProfileElement = GetOrAddElement(propertyGroupElement, "BuildProfile");
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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!

thomaslevesque
thomaslevesque previously approved these changes Nov 6, 2019
@thomaslevesque
Copy link
Member

Thanks, @blairconrad!

@thomaslevesque
Copy link
Member

Oh, I can't merge yet... you need to squash first

@blairconrad
Copy link
Member Author

Thanks. Squashed. I enjoyed your comments!

@blairconrad blairconrad added this to the vNext milestone Nov 6, 2019
@thomaslevesque thomaslevesque merged commit 6c63457 into FakeItEasy:master Nov 6, 2019
@blairconrad blairconrad deleted the use-profile branch November 6, 2019 21:20
@afakebot
Copy link

This change has been released as part of FakeItEasy 6.0.0-beta.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants