Modifying XML nodes with Cake

|

I am using Cake everywhere I can for my builds. It is such a nice way to define your builds in a C# DSL and run them on any environment.

Recently I had the need to patch a bunch of attributes in some XML files. Of course Cake has a tool for that! This is called XmlPoke. This tool along with a little bit of XPath knowledge and you are ready to update your XML files!

Lets say you want to update your AndroidManifest.xml with the newest versions for your App. This is usually done in the root element of the manifest, funnily enough called manifest:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="dk.ostebaronen.cheesyapp"
    android:versionCode="1"
    android:versionName="1.2.3"

The two attributes android:versionCode and android:versionName are the ones you want to update.

So first thing needed to be done is to load the namespaces. Even if you have a namespace looking like xmlns="http://some.schema.com/" it needs to be loaded and used in the XPath. To load these XmlPoke has a XmlPokeSettings class:

var settings = new XmlPokeSettings
{
    Namespaces = new Dictionary<string, string> 
    {
        {"android", "http://schemas.android.com/apk/res/android"}
    }
};

In the case of the Android Manifest, we only have the android namespace. In the Namespaces dictionary, you would otherwise load all the namespaces that you need for your XPath queries. In the case of a naked xmlns you would also name it something, it could be called root.

With the settings in place we are ready to modify the Android Manifest with the XmlPoke alias. The XPath for the two attributes would be, /manifest/@android:versionCode and /manifest/@android:versionName. Usage in Cake would be as follows:

XmlPoke(manifestFilePath, "/manifest/@android:versionCode",
    versionCode, settings);

XmlPoke(manifestFilePath, "/manifest/@android:versionName",
    versionName, settings);

Tying all this information together in a Cake Task:

Task("Update-Android-Manifest-Version")
    .Does(() => 
{
    var versionCode = "2";
    var versionName = "1.2.4";

    var settings = new XmlPokeSettings
    {
        Namespaces = new Dictionary<string, string> 
        {
            {"android", "http://schemas.android.com/apk/res/android"}
        }
    };

    XmlPoke(manifestFilePath, "/manifest/@android:versionCode", 
        versionCode, settings);
    XmlPoke(manifestFilePath, "/manifest/@android:versionName", 
        versionName, settings);
});

For versionCode and versionName I like to use GitVersion to calculate these, but you could load these from somewhere else, or they could be arguments to your Cake script, it doesn’t really matter as long as they are strings.

Lets take a look at how this looks when you have a naked xmlns declaration. Here is what the root looks like for a Azure Service Fabric ApplicationManifest.xml file:

<ApplicationManifest 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    ApplicationTypeName="CheesyApp"
    ApplicationTypeVersion="1.2.3"
    xmlns="http://schemas.microsoft.com/2011/01/fabric">

In this case we still need some namespace declarations:

var settings = new XmlPokeSettings
{
    Namespaces = new Dictionary<string, string> 
    {
        {"sf", "http://schemas.microsoft.com/2011/01/fabric"}
    }
};

xsi and xsd are not super important, unless you are going to query nodes using these later on. But to update the ApplicationTypeVersion we need the naked namespace.

The query in this case would look like /sf:ApplicationManifest/@ApplicationTypeVersion. These Service Fabric Application Manifests also have some ServiceManifestRef nodes with ServiceManifestVersion for each of the services you have in your app.

<ApplicationManifest ApplicationTypeVersion="1.2.3"
    xmlns="http://schemas.microsoft.com/2011/01/fabric">
    <Parameters>
        <Parameter />
        <Parameter />
        <Parameter />
    </Parameter>

    <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestVersion="1.2.3">
    </ServiceManifestImport>

    <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestVersion="1.2.3">
    </ServiceManifestImport>

    ...

For the scenario here with multiple attributes XPath can also help you. The query for the ServiceManifestVersion would look like //sf:ServiceManifestRef/@ServiceManifestVersion. Notice the two slashes //. In XPath this means select multiple nodes.

Hopefully this gave you a good idea about the usage of XmlPoke in Cake!

Moving The Blog To Jekyll

|

Ever since 3rd July 2012 this blog has been running on the blogger.com platform. However, I’ve grown tired of how things are managed in the blogger CMS and how blog posts are written. Especially when adding code blocks it is a huge pain. Since, this blog contains so much code and is mainly about code, I’ve been looking for a better way of writing.

Enter GitHub Pages and Jekyll! Now this blog is static, content is managed on GitHub and posts and pages are written in markdown, which I am used to with documentation for MvvmCross and issues and pull requests on GitHub. If I need to change something, I can install some of the many Jekyll plugins or just quickly change some HTML or CSS, test it locally and push the changes to GitHub, using git which I use all the time for code anyways!

This workflow suits me much better and hopefully this means more focus on writing blog posts for you to read! Also hope you like the makeover of the blog!

Cake build issue reporting with PRCA

|
I have covered Cake build in one of my talks I did for a local user group I participate in Copenhagen. However, most of that just explained what Cake build is. If you are reading this and have no idea what Cake build is, I highly recommend looking at the Cake homepage and also watching this introduction by Gary Ewan Park, which explains very well what is great about Cake.

Once you have set up your Cake build, you may wonder how to process all the warnings and issues that MSBuild throws at you. You may also be running the ReSharper command line analysis tool which finds issues with your code. How do you combine all these results and report them or go through them to figure out what is going on?

Pascal Berger has written some great add-ins for Cake, which helps you with this, called Pull Request Code Analysis (PRCA). Let's try set it up for both MSBuild and ReSharper code Analysis.

Add-ins and tools

In your build script you need to add a couple of tools and addins.


In the snippet above, we start by adding the ReSharper CommandLineTools, for analyzing with either the default ReSharper rules or one or more rule sets that you have defined for your solution.
We also add MSBuild Extension Pack, which allows us to use an XML file format that PRCA knows how to parse. The default logging format does not work.

Then we add the relevant PRCA addins. In this case I am only interested in catching issues with my code. However, PRCA also supports reporting the issues it finds into TFS/VSTS pull requests.

MSBuild

Having added the required add-ins and tools for all this to work. Let's add some settings to our MSBuild, such that it can log the build in the format PRCA understands.

Here the important parts is the call to WithLogger, which sets up file logging to XML. The rest is up to you to figure out. I like to pass along the configuration as arguments in my scripts, which is where the configuration and platform variables come from. In this case we are logging to msbuildLogPath which is up to you to decide where is. It could for instance be in an artifacts folder.

ReShaper Inspect Code

Now let's add some static analysis into the mix and lets get some results from the ReSharper CLI tools. A ReShaper license is not required to use the CLI tool, so there is basically no reason not to use it if you want to improve your code.

The CLI tool just like the plugin for Visual Studio also supports extensions, which is great for adding additional analysis. This can help you catch spelling mistakes and other cool additions through the extensions.


We pass along similar MSBuild properties as we did in our build. The Profile property here is the ReSharper settings file. You can configure multiple layers of settings here, similarly to what ReShaper allows you in the VS extension.
I have chosen not to throw exceptions when finding violations, so I can report them at the end of a build if the rest of it succeeds. You can see I added a couple of extensions in the Extensions property. One for checking spelling and ensuring async suffix on async methods.
An important note here is the contents of ArgumentCustomization. On my environment, the CLI tool seems confused about which MSBuild version to use. Even though the documentation claims to always use the latest version, it still picked up an old version. Hence, I had to specify --toolset=15.0 myself to make it work.
The InspectCode tool takes a little while to run. In my case here it dumps the results in a log file at inspectReportFilePath. Just like with MSBuild, you could put that in artifacts or where ever you like.

Reading issues

Now to tie this blog post up, we now need to read the issues logged from both the build and the ReSharper code inspection. This is what PRCA excels at.


Here I simply check existence of the msbuild log and the code inspection report and run the ReadIssues tool. From this I get a IEnumerable<ICodeAnalysisIssue> which gives you the path to the file where the issue is in, the line, the message, which rule it violates and url to the rule documentation. From this you can get a very good idea about how to resolve the issue being reported.

There are plans on adding tools to create reports in various formats from this enumerable, currently you need to do something about the issues yourself, which highly depends on the environment you use to report issues, make pull requests etc.
If you are using TFS/VSTS there are built in methods to report these. More information can be found in the PRCA docs.

When you run the PRCA tool it will look something like this in your cake output.
As you see this project I've run it on has a lot of issues 😢

LiveXAML and Xamarin.Forms

|
Full disclosure. This blog post is sponsored by LiveXAML. I was sent a free license to try it out and mention it in a blog post. All opinions are my own and I was not paid to say nice things about the product.
I do not usually work with Xamarin.Forms, but Mihhail from LiveXAML contacted me and asked whether I wanted a license for LiveXAML to test out and mention them in a blog post. I thought "why not", and here are some brief thoughts and experiences with the tool.

First of all, LiveXAML is not the only product on the market. There are other players, such as Continuous, Gorilla Player and now Xamarin also have their Live Player. They all function a little bit different from each other, but their goals are very much the same. To show changes in the UI, while you are writing code.

I tried LiveXAML with the following setup:
  • Visual Studio 2017 15.2
  • Xamarin.Forms 2.3.4.247
  • LiveXAML 1.2.12
  • Android x86 7.1 image running in a Hyper-V VM
The project I worked with LiveXAML in was a sample project, where I have a ListView showing some images and names of cheeses. All the code was contained in a PCL. To get LiveXAML working I simply installed the Visual Studio 2017 extension. I also installed the LiveXAML NuGet into the PCL project.
For projects using Shared projects, you need to install LiveXAML into your application projects, which consume your Shared project.

This is basically it. Once the very small setup is done, you are ready to play around. Just start debugging your application. Fire up a XAML page and start editing. Every time you press save, LiveXAML will pick up the changes and they will quickly be reflected in the UI in the App.


See the video above to see it in action. In this video I edit some simple XAML, where I change the height and width of the images, then apply a transformation to add rounded borders. Every time I press CTRL + S to save, the emulator updates the view, which is super usefull when playing around with UI in XAML.

LiveXAML seems to work pretty well and seems stable, I only tried it in a Xamarin.Forms Android project, but the LiveXAML web page says that iOS projects work as well. There is also some preliminary VS4Mac support for those of you who use that. It installed and ran fine in an emulator, that is not mentioned as supported on the web page, which is great. It ran with external assemblies, in this case FFImageLoading, where images and transformations where shown correctly. To me it seems that any valid XAML will work with LiveXAML and be rendered on the device!

The difference between LiveXAML and Xamarin Live Player, is that with the former, you install a server in your app, which a plugin in Visual Studio can send commands to. So when saving your XAML it sends a payload which then is rendered on the screen.
With Xamarin Live Player, you don't add dependencies to your app. However, instead you install an App on your device you want to run on, then there is a pairing process with the IDE. Running the app, it seems like it is running inside of the Xamarin Live Player app and changes to any part of your code is reflected. This works with coded UI as well.
Like LiveXAML, Gorilla Player seems to be limited to XAML based UI and it would seem it works in a similar fashion, where your project takes a dependency on a Gorilla Player nuget and you need to set up Gorilla Player in your App startup.

All in all I was quite pleased how well LiveXAML works and how easy it is to get started. It just works. I have not tried it on a real device, but given that the device is on the same network as your PC, I think it may work just fine. If I were making Xamarin.Forms applications professionally, I would really consider LiveXAML as a means to create and debug XAML layouts.

Upgrade notes for MvvmCross 5.x on iOS

|
This post is just a couple of notes about some of the changes that affected some of my apps when updating to MvvmCross 5.x on Xamarin.iOS.

IMvxModalIosView or MvxModalNavSupportIosViewPresenter could not be found

MvvmCross has replaced the presenter logic, which before looked for the IMvxModalIosView interface in order to figure out how to present a ViewController. You should instead use the MvxModalPresentation attribute for your ViewController.

Previously you also had to use MvxModalNavSupportIosViewPresenter for it to understand the IMvxModalIosView interface. This is now all baked into the default presenter.

For this attribute you can also give it a couple of hints about how to display itself through a couple of properties.

  • Animated, set this to false to disable animations when presenting your modal ViewController, defaults to true
  • PreferredContentSize, set this to your content size on iPad, since they are not shown full screen default there
  • ModalTransitionStyle, set this to the desired transition style, cover, flip, cross dissolve or curl, this will not work when Animated is set to false, defaults to CoverVertical
  • ModalPresentationStyle, set this to the desired presentation style. This depends on the LayoutSize and in most cases on iPhone will default to FullScreen. This is mostly to change presentation on iPads.
Here is how the changes will reflect in your code:


Changing to the new presenter for Modals is pretty painless.

CreateNavigationController has changed signature

The MvxIosViewPresenter has changed the signature of CreateNavigationController, this is luckily a very simple change.

MvxTabBarViewController is presented as child?

Another thing in the new presenter for iOS, is that some ViewControllers, cannot be presented as children and need to be presented as root instead. This is something that will be fixed in MvvmCross 5.0.4 since this is unwanted behavior. You should be in control of how you want your ViewControllers presented. For now in 5.0.0-5.0.3 you can make a small change to fix this.


MvvmCross.Dialog is gone?

Yes. It was unmaintained and upstream MonoTouch.Dialog has been unmaintained for just as long time. The MvvmCross team does not want to support something that no one wants to fix.

What should you use instead? How about Xamarin.Forms? Or plain iOS Tables and Views? Sorry, no shortcuts here. You could recompile Dialogs from MvvmCross 4.x against 5.x yourself.

What I have opted for is using MvxTableViewController and making my own Adapters for cases where I need to display different kind of cells, groupings etc. It is not that much work and you are not dependent on a 3rd party library here. You can still bind MvxTableViewCells to whatever you want and TwoWay bindings work much better than MonoTouch.Dialogs ever did.