Using PowerShell to automate the build process of your Windows Phone app

Tools, Windows Phone 5 Comments »

Another day another occasion to share a tip which comes with the development of my todo-list app 2Day. This time, I share a small PowerShell script I use in order to build the application. Of course the script will not work out of the box for you but it could be useful if you’re thinking about automating the generation of your application.

Background: managing multiple versions

Since release 1.5.0 there are two versions of 2Day: the lite version (free) and the standard version (paid). I switch from one configuration to another using two Build Configurations. When I want to build the Lite version I build using the Release Lite configuration while for the standard version I build in Release. The difference between the two is a conditional symbol.

2Day-Configurations

I also have a WP7 and a WP8. So for each official release, I must produce 4 XAPs:

  • WP7 Lite
  • WP7 Full
  • WP8 Lite
  • WP8 Full

This is the reason why I was thinking about automating this stuff !

PowerShell script

The script itself is very straightforward: I use MSBuild to build the Visual Studio Solution File (*.sln) using the 2 configurations I described earlier. Once a build is complete I move the generated XAPs to an output folder. As I said this script will NOT work out of the box for your but it could be used as an example. Here is the code:

?View Code POWERSHELL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# usage is ./ScripName -version 1.5.0
param([string]$version = "version")
 
$solution = "2Day.sln"
$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
# get the location of the script
$rootDir = [System.Io.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
# build the location of the sln file. In my case this is under /Branches/WindowsPhone/versionunmber/
$baseDir = $rootDir + "\Branches\WindowsPhone\" + $version + "\"
# and the ouput is in a Build folder
$outputFolder = $rootDir + "\Build\"
 
# if the output folder exists, delete it
if ([System.IO.Directory]::Exists($outputFolder))
{
 [System.IO.Directory]::Delete($outputFolder, 1)
 [System.IO.Directory]::CreateDirectory($outputFolder) | Out-Null
}
else
{
 [System.IO.Directory]::CreateDirectory($outputFolder)| Out-Null
}
 
Write-Host "Building 2Day version: " $version
Write-Host "Solution is at: "  $baseDir$solution
 
# make sure our working directory is correct
cd $baseDir
 
$configurations = "Release", "Release Lite"
foreach($configuration in $configurations)
{
	# prepare build command line
	$options = "/noconsolelogger /m /p:Configuration=""" + $configuration + """ /p:Platform=""Any CPU"""
 
	# prepare output path
	$releasePath1 = $baseDir + "2Day.App.WP7\bin\" + $configuration + "\_2DayWP7.xap"
	$outFile1 = "2Day_" + $version + "_WP7_" + $configuration.Replace(" ", "_") + ".xap"
	$releasePath2 = $baseDir + "2Day.App.WP8\bin\" + $configuration + "\_2DayWP8.xap"
	$outFile2 = "2Day_" + $version + "_WP8_" + $configuration.Replace(" ", "_") + ".xap"
 
	# clean
	Write-Host Perform cleaning...	
	$clean = $msbuild + " " + $solution + " " + $options + " /t:Clean"
	Invoke-Expression $clean | Out-Null
 
	# build
	Write-Host "Building with configuration"$configuration"..."
	$build = $msbuild + " " + $solution + " " + $options + " /t:Build"
	Invoke-Expression $build | Out-Null
 
	# move the files that were built to the output folder
	$out1 = $outputFolder + $outFile1;
	$out2 = $outputFolder + $outFile2;
	[System.IO.File]::Copy($releasePath1, $out1)
	[System.IO.File]::Copy($releasePath2, $out2)
 
	Write-Host "Output: " $out1 $out2
}
 
cd $rootDir
Write-Host "Done"

Fun with ILDASM and ILASM: tweaking the code of an existing library

.Net, Tools 7 Comments »

Today I decided it was the appropriate time to upgrade my work PC from Windows 7 to Windows 8. I’m already using Windows 8 on my personal PC as well as on my //BUILD/ Slate. Re-installing an OS is never really exciting but this time I faced an interesting challenge… In this short blog post I share this story !

Check-list

The PC I’m using at work is a HP Elitebook8560P laptop. I had no doubt Windows8 would work like a charm on it, so I started by writing up the list of major tools I need:

  • VS2005 (yes, the 2005 version – that’s a long story… it’s for C++ work)
  • VS2010 + SP1
  • Office 2010
  • Office 2013

I also would like to install VS2012 but one of the .Net component I’m using in the large WPF app I’m currently working on has a bug with .Net 4.5. If I install VS2012 .Net4.5 will be installed to and prevent the execution of my app. This was at least the behavior I found out on Windows7 while taking a look at VS12.

The team in charge of the component (which are co-workers btw) are aware of the issue and already fixed it. We didn’t integrate their last version yet however.

Installation

This is the part you press and button, grab a cup of cofee and get back in front of the screen to press another button. Nothing really interesting here…

Fun-time

Ok, everything is installed. Time to check I can run my app. Arrrrg !!! Crash !!!

I took me a few seconds to figure out that Windows8 comes with .Net 4.5 pre-installed. Well, that’s a problem, at least for me. I reviewed the option I had:

  • go back to Windows 7 and reinstall everything again. I didn’t have the time for that.
  • remove .Net 4.5 from my laptop. This is actually not possible since .Net 4.5 comes with Windows8.
  • come at work very early monday morning and try to grab the new version of the buggy component. This is a very bad option since I’m in the middle of an important delivery…
Then I realize I knew exactly what was going wrong in the component that lead to the crash. What about disassembling the code to IL (Intermediate Language), recompile it and use this hacked DLL ? This is what I did, and it worked very well :D

Why a crash ?

Just to share the context, the buggy component is a WPF control. It contains UI virtualization stuff. One of the method contains a “throw NotImplementedException”. This method is called when a particular Dispose() method is called. This method was NEVER called prior to .Net 4.5 hiding the problem. The new version of the .Net framework calls the Dispose method properly… and then throws the exception :-)

Introducing ILDASM and its friend ILASM

ILDASM is the MSIL disassembler provider with Visual Studio. It comes with a very basic UI but you can also use it a command line and dump the content of a .Net binary to a file (containing the IL). So I fired up the VS2010 command prompt and typed:

ildasm.exe Company.BuggyComponent.dll /output Company.BuggyComponent.il

I then opened the file using a basic text editor, navigate to the method which contains this code:

.method public hidebysig newslot virtual final 
 instance void Clear() cil managed
{
 // Code size 6 (0x6)
 .maxstack 8
 IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
 IL_0005: throw
} // end of method VirtualizingCollectionSelector`1::Clear

I changed it to:

.method public hidebysig newslot virtual final
 instance void Clear() cil managed
{
 // Code size 1 (0x1)
 .maxstack 8
 IL_0000: ret
} // end of method VirtualizingCollectionSelector`1::Clear

Then I rebuilt a new DLL using ILASM. The tool is able to create an executable or an assembly from IL code in a text fix. So I simply typed:

ilasm Company.BuggyComponent.il /key=company.snk /output=CompanyBuggyComponent.dll

I dropped the newly created assembly in the bin directory, and boom, it’s working again. Of course this is a temporary solution but I had fun coming to it :-)

PS: the 3 screens running Windows8 at home: personal workstation, slate and work laptop

Developping an app for the Windows Phone platform

Tools, Visual Studio, Windows Phone 7 Comments »

For the last few months my colleague Charlotte and I have been working on a Windows Phone 7 app we hope to release soon. Attendants of our “Performance tuning and analysis during the Microsoft TechDays this week had a brief overview of this app.

In this post, I wanted to share with you all the tools we’re using to build this app.Most of them are free, so they might interest you…

Productivity / Code

Libraries

Source Control

Project Management

  • AgileZen (free for managing 1 single project)

Cloud computing

Web site

I personally love all those tools and libraries ! What do YOU use for your Windows Phone 7 development ?

 

ReSharper and code generation

Tools 3 Comments »

I don’t know why I’ve found this feature only today, but I wanted to share another great feature of ReSharper. Let say you need to implement a C# structure. You may start with the following code:

?View Code CSHARP
1
2
3
4
5
6
public struct ServerItem
{
    public string Id { get; private set; }
    public DateTime? Added { get; private set; }
    public int ChildCount { get; private set; }
}

Then you start thinking, “I need to setup a constructor…” You can do it manually, but you can also ask ReSharper do to the job for you. All you have to do is press ALT+Enter (this might depend on your configuration obviously…)

ReSharper will generate the constructor for you:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public struct ServerItem
{
    public ServerItem(string id, DateTime? added, int childCount)
        : this()
    {
        this.Id = id;
        this.Added = added;
        this.ChildCount = childCount;
    }
 
    public string Id { get; private set; }
    public DateTime? Added { get; private set; }
    public int ChildCount { get; private set; }
}

Fine. Then you remember that you also need to setup equality members properly… You have to override Equals, GetHashCode… This is not complicated but it can become cumbersome and it often feels like a waste of time. Here is the ReSharper way of doing this:

1. Press ALT+Enter

2. Choose “Equality members” and setup the code generation:

And BOOOM ! You’re done:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public struct ServerItem
{
    public ServerItem(string id, DateTime? added, int childCount)
        : this()
    {
        this.Id = id;
        this.Added = added;
        this.ChildCount = childCount;
    }
 
    public string Id { get; private set; }
    public DateTime? Added { get; private set; }
    public int ChildCount { get; private set; }
 
    public bool Equals(ServerItem other)
    {
        return Equals(other.Id, this.Id) && other.Added.Equals(this.Added) && other.ChildCount == this.ChildCount;
    }
 
    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (obj.GetType() != typeof(ServerItem))
        {
            return false;
        }
        return Equals((ServerItem)obj);
    }
 
    public override int GetHashCode()
    {
        unchecked
        {
            int result = (this.Id != null ? this.Id.GetHashCode() : 0);
            result = (result * 397) ^ (this.Added.HasValue ? this.Added.Value.GetHashCode() : 0);
            result = (result * 397) ^ this.ChildCount;
            return result;
        }
    }
 
    public static bool operator ==(ServerItem left, ServerItem right)
    {
        return left.Equals(right);
    }
 
    public static bool operator !=(ServerItem left, ServerItem right)
    {
        return !left.Equals(right);
    }
}

The code-generation features of ReSharper has been there for a long time… But because I just found out the power of them, I wanted to briefly showcased them in this post :-)

If you like typing XAML you will love ReSharper 6.1 !

Silverlight, Tools, Windows 8, Windows Phone, WPF 4 Comments »

Resharper is an amazing tool for any .Net developers. The latest version 6.1 has been released just a couple of weeks ago and I wanted to share with you a brief overview of the new workflow available in the XAML world !

Visual Studio 2010 introduced 2 new design time properties: d:DesignInstance and d:DesignData. Those properties can be used in order to specify a design time DataContext in order to have more help during the creation of a binding.

For example, when you create a binding using the Property dialog of VS2010 you can browse your DataContext to select the right property (image from this blog post from Karl Shifflet):

Resharper 6.1 is now able to use those metadata in order to improve the experience you have while typing XAML (which I personally do a LOT!). Here is how it works:

  • you create a new ViewModel with a simple property (this property has just get/set because we don’t need much more in the context of this post…)

  • you setup a binding in your view

At this point the ReSharper magic comes into play…

  • ReSharper warns you the DataContext is unknown

  • Offer the ability to fix this

  • Note that like in C#, you can very easily resolve namespace issues

  • Then notice that the warning is gone (the Title property is no longer underlined)

  • You can now add a new binding

  • You can then ask ReSharper to create the property in your ViewModel

  • Choosing the first option will get you to the ViewModel definition

Now that I’ve upgraded my installation to version 6.1, I think this is a must have !

That’s all for today ! Hope it helps :-)

 

 

Using Reflector to debug a .Net app in Visual Studio without the original source code

Tools, Visual Studio 2 Comments »

Many .Net developers use to say “if you’re a serious developer, then you MUST Reflector”.

I would like to add another statement: “if you want to have an insight and powerful look at how things works internally, use Reflector Professional”.

Last Wednesday, RedGate released a new version of Reflector. You’re probably going to download it soon or later because your actual version is going to expire. When you’ll download the free version, you’ll automatically get a trial (14 days) of the professional edition. Nice, but what’s so special about this edition ?

Let’s see a demonstration of what can be done using Reflector Pro. The following is not an ad for RedGate, I’m just totally amazed by their new feature :-)

1. Reflector is now integrated into Visual Studio (2005, 2008 and 2010 RC):

2. Select the “Choose Assemblie to Debug…” option to select .Net assemblies for which you don’t have the source code. In this example, I’m using one of the Blend3′s assemblies:

3. Once the process is completed, select the “Explore Decompiled Assemblies” option:

4. Browse to your target assembly and select an interesting type:

5. Put a breakpoint in the code, like you do every day when you debug an app:

6. Run the executable

7. Debug Blend3′s source code ! Use breakpoints, step into methods, inspect variables…

And that’s it. With Reflector Professional, you can:

  • Decompile third-party assemblies from within Visual Studio
  • Step through decompiled assemblies and use all the debugging techniques you would use on your own code. This is incredibly powerful as we saw by debugging Blend3″s source code !

Reflector Professional is available for about 195$ on RedGate website

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in