mardi 31 décembre 2013

Installing globally Karma test runner on Debian

I was not able to install Karma test runner for JavaScript on my Debian squeeze. It kept failing with the error message:

npm ERR! System Linux 3.11-2-amd64
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "-g" "install" "karma"
npm ERR! cwd /home/ma
npm ERR! node -v v0.10.24
npm ERR! npm -v 1.3.10
npm ERR! path /usr/local/lib/node_modules/karma/node_modules/lodash/dist/lodash.compat.js
npm ERR! fstream_path /usr/local/lib/node_modules/karma/node_modules/lodash/dist/lodash.compat.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)

By default, Debian installs node.js with the name nodejs although the usual name for the node command is node. For some reason, this causes an issue with the npm install of karma. I then created a symbolic link node which points to nodejs:

sudo ln -s /usr/bin/nodejs /usr/bin/node
This solved the issue and I was able to install karma without any problem with the command:
sudo npm -g install karma

jeudi 28 novembre 2013

How to use solarized colorscheme for vim on a Nitrous box

I'm a happy user of Nitrous.IO for my Ruby on Rails development. As a vim user and a fan of the solarized colorscheme, I had to find a way to set it up for my Nitrous box.

I was not able to setup the full solarized colorscheme for vim so I had to use the degraded 256 colorscheme. To do so, I added the following lines in my .vimrc:

set background=dark
let g:solarized_termcolors=256
color solarized

I can now enjoy using my favorite colorscheme on my favorite editor!

lundi 25 février 2013

Powershell script parameters with complex default value

I needed to create a Powershell script with some parameters having a non-trivial default value and I didn't find any quick reference to do that. In here, I have a parameter having the current week number as a default value:

param
(
  [int] $week = $(Get-Date -UFormat %V)
)

MSTest ReSharper live templates

When developing in Visual studio with MSTest, I like using ReSharper live templates to quickly create my test classes and methods.

To create a test class, I map the following template to "tc":

[TestClass]
public class $TestClass$
{
    $END$
}

To create a test initialize, I map "ti" to:

[TestInitialize]
public void TestInitialize()
{
    $END$
}

And to create a test method, I use "tm" with:

[TestMethod]
public void $TestName$()
{
    $END$
}

mardi 29 mai 2012

Using NuGet with mono on Debian

I tried to use NuGet on my Debian installation and I had several issues to solve. Thanks to this and this link, I was finally able to make it work! Here are the steps:

First install mono with all the SDK:
$ sudo apt-get install mono-complete
Install https certificate so that Mono can trust the source for NuGet
$ mozroots --import --sync

Download NuGet command line bootstrapper (NuGet.exe)

Copy MsBuild dll from Microsoft and put it in the same directory as NuGet.exe. You can find this dll in a Windows OS here "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.Build.dll" or you can find it in Windows SDK for .NET framework 4.0

Finally, running this command line should show help information
$ mono --runtime=v4.0.30319 NuGet.exe
And you should be able to download any package with
$ mono --runtime=v4.0.30319 NuGet.exe Install Cassette.Web

jeudi 26 avril 2012

Setup for new rails project with RSpec

I like to use RSpec instead of Test::Unit for my rails projects. Here are the steps I use to setup my projects.
Create new rails project without Test::Unit
rails new MYAPP -T
Add rspec-rails to your Gemfile:
gem 'rspec-rails'
Choose a javascript runtime in the Gemfile:
gem 'therubyracer', :platform => :ruby
Run bundle install:
bundle install
Install rspec in rails:
rails g rspec:install
Let's write the first spec!

mardi 7 février 2012

Debian gem install RMagick

I tried to install RMagick gem on my Debian box and got the following error:
checking for Magick-config... no
Can't install RMagick 2.3.0. Can't find Magick-config in [...]
I installed libmagickcore-dev:
sudo apt-get install libmagickcore-dev
But I still had an error with:
Can't find MagickWand.h
The problem was solved by installing libmagickwand-dev:
sudo apt-get install libmagickwand-dev
Hope that helps!

jeudi 2 février 2012

Review of "JavaScript & jQuery: The Missing Manual, Second Edition" by David Sawyer McFarland; O’Reilly Media

Disclaimer: I'm a member of the O'Reilly Blogger Review Program and as such, this book was freely provided to me by the editor.

David Sawyer McFarland has been building websites since 1995. In "JavaScript & jQuery: The Missing Manual", he tries to teach javascript and jQuery to web designers or beginner programmers so that they can build highly interactive web pages. This book is clearly aimed to people who know a little bit of HTML and CSS but want to add some dynamics to their pages.

The first two parts of the book teach the reader basics of javascript and jQuery. It begins with very basic things like "What's a Computer Program" and finishes with a tutorial to write an animated dashboard in jQuery. I have to say that I skipped most of these 200 pages as I already know most of what is written here.

The third part of the book gives lots of tips for improving images and navigation in your website. There are lots of tutorial which cover the most frequent cases that you find on modern websites. I liked the chapter on web forms for it introduce some very useful techniques for having smarter forms and interfaces.

The fourth part was clearly the most interesting to me: Ajax. David goes back to the basics of request/response and explains trough simple tutorials how Ajax works with jQuery. All you need to know to be able to make Ajax with jQuery is here! Moreover, the next chapter show how to use these new skills with popular web services from Flickr or Google Maps.

Finally, the last part "Tips, Tricks, and Troubleshooting" explain how to work with jQuery and javascript efficiently in a daily basis with things like traversing the DOM, regex and debugging.

I was a bit disappointed at the beginning as the first 200 pages are really very basic but then, I learned lots of practical tips that I will be able to use in all my web projects.

jeudi 29 septembre 2011

Ayende tax calculation challenge

I had some fun doing this little challenge from ayende, it's a good exercise to practice TDD. Here is my solution in C#.
The tests
[TestFixture]
public class TaxCalculatorTests
{
	[Test]
	public void Tax_for_0()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(0);
		
		Assert.That(tax, Is.EqualTo(0));
	}
			
	[Test]
	public void Tax_for_5000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(5000);
		
		Assert.That(tax, Is.EqualTo(500));
	}
	
	[Test]
	public void Tax_for_5800()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(5800);
		
		Assert.That(tax, Is.EqualTo(609.2).Within(0.001));
	}

	[Test]
	public void Tax_for_9000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(9000);
		
		Assert.That(tax, Is.EqualTo(1087.8).Within(0.001));
	}

	[Test]
	public void Tax_for_15000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(15000);
		
		Assert.That(tax, Is.EqualTo(2532.9).Within(0.001));
	}

	[Test]
	public void Tax_for_50000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(50000);
		
		Assert.That(tax, Is.EqualTo(15068.1).Within(0.001));
	}
}
And the implementation
public class TaxCalculator
{
	private readonly Tuple<decimal, decimal>[] slices = 
	{
		new Tuple<decimal, decimal>(40230, 0.45M),
		new Tuple<decimal, decimal>(21240, 0.33M),
		new Tuple<decimal, decimal>(14070, 0.30M),
		new Tuple<decimal, decimal>(8660, 0.23M),
		new Tuple<decimal, decimal>(5070, 0.14M),
		new Tuple<decimal, decimal>(0, 0.10M),
	};
	
	public decimal TaxFor(decimal sum)
	{
		if (sum == 0)
		{
			return 0;
		}
		
		var tax = 0M;
		foreach (var slice in slices)
                {
			if (sum > slice.Item1)
                        {
				var amount = sum - slice.Item1;
				tax += amount * slice.Item2;
				sum -= amount;
			}
		}
		
		return tax;
	}
}

mardi 20 septembre 2011

ReSharper test runner with MSTest

The current project I'm working on at my job uses MSTest for testing (and I don't like it!). Anyway, I had an error while running the test suite in Resharper test runner while there were none in Visual Studio test runner.

Actually, the ReSharper test runner create a specific directory where it copies all assemblies under test and runs the test from this location. One of my tests had to load all assemblies in the executing assembly path and some of them were missing. The problem comes from the fact that by default ReSharper is configured to shallow-copy assemblies being tested.

What you need to do is go to Resharper -> Options.

From left side pane select tools -> Unit Test

Uncheck Shallow-copy assemblies being tested

And now, all my tests pass as expected!

dimanche 18 septembre 2011

Bad URI error with heroku on windows

I wanted to push my little rails app to Heroku but after asking for my credentials, the heroku command failed with the following message:
C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:156:in `split': bad URI(is not URI?): 10.35.10.249:8080 (URI::InvalidURIError)
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:174:in `parse'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:628:in `parse'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:99:in `net_http_class'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:142:in `transmit'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/resource.rb:67:in `post'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:554:in `process'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:536:in `post'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:35:in `auth'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:96:in `ask_for_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:129:in `ask_for_and_save_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:71:in `get_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:19:in `login'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/command/auth.rb:12:in `login'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/command.rb:114:in `run'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/bin/heroku:14:in `'
        from C:/RailsInstaller/Ruby1.9.2/bin/heroku:19:in `load'
        from C:/RailsInstaller/Ruby1.9.2/bin/heroku:19:in `
'
Actually, the problem comes once again from my proxy. My environment variable for the proxy was set to HTTP_PROXY=host:port while the Heroku gem expects HTTP_PROXY=http://host:port
I took me one hour to find this, I'd rather read be more carefully the error messages...

vendredi 16 septembre 2011

How to setup github access through corporate firewall

I had troubles cloning and pushing to my github account. My company has a firewall and all internet accesses are through the corporate firewall. Hopefully, I found an easy way to get access to my account with the following steps. I'm using msysgit 1.7.6 on a Windows 7 machine.
Configure the github global property http.proxy:
git config --global http.proxy http://host:port
Access your git repository through the http protocol. For public repositories it's
git clone http://github.com/username/project.git
And for private repositories, use the following command (you'll be prompted for your password):
git clone https://username@github.com/username/project.git
If you want to push your changes to your github account, you have to change the URL for your remotes and use the same format as specified above.

lundi 10 janvier 2011

Debian autotest setup

I spent some time learning Ruby on Rails these days and I had some trouble setting up autotest on my Debian box.
I finally managed to make it work thanks to these instructions.
$ sudo gem install ZenTest
$ sudo gem install autotest-rails
$ sudo apt-get install libnotify-bin
$ sudo gem install test_notifier
And then add require test_notifier/runner/autotest" to the file ~/.autotest

vendredi 22 octobre 2010

jQuery Autocomplete : set autocomplete field to a value

I struggled to find this and the solution is so simple…

In the select event, you can set the value of the field and if you return false, the value won’t be the selected item.

 $(function() {
$("input#ZipCode").autocomplete({
source: function(request, response) {
$.ajax({
url: '<%= Url.Action("ZipCodeAutoComplete", "Home", new { area = "" }) %>',
data: { term: request.term },
success: function(data) {
response($.map(data, function(item) {
return { label: item.ZipCodel + " - " + item.City, ZipCode: item.ZipCodel, City: item.City }
}))
}
})
},
minLength: 3,
select: function(event, ui) {
$("#City").val(ui.item.Ville);
$("#ZipCode").val(ui.item.ZipCode);
return false;
}
});
});

jeudi 21 octobre 2010

Connect to Oracle using NHibernate

Here is my setup to connect to an Oracle database using the ODP.NET provider.

Install Oracle client v9 R2 or greater.

Reference Oracle.DataAccess dll in your project.

Configure your database connection in the tnsnames.ora file. This file is located in $ORACLE_HOME/network/admin/tnsnames.ora.

Configure your connection string in your application. I usually use this syntax:

<add name="default" connectionstring="Data Source=tnsnames_alias;User ID=username;Password=pwd">

Then use the OracleDataClientDriver in your NHibernate configuration or OracleDataClientConfiguration if you use FluentNHibernate.


This should do it!

mercredi 21 juillet 2010

User/role extension methods in an ASP.NET MVC view

This is a small Html helper that I use when I need to check the role in a view

public static class PageHelper
{
public static bool IsAdmin(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Admin");
}

public static bool IsCustomer(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Customer");
}
}
Don't forget to add the namespace for your extension methods in your Web.config :
<namespaces>
<add namespace="MyProject.Web.Helpers" />
</namespaces>

samedi 3 juillet 2010

Blogger open link in new window

In quest for the perfect setup for my blog, I know want to have my links open in a new browser window. The simplest way to do it is to add this tag in the <head></head> part of your template :
<base target="_blank" />

vendredi 2 juillet 2010

Source code syntax highlighting in blogger

I finally figured out how to have syntax highlighting for the source code in blogger thanks to this blog post.
This is the list of brushes I use :
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>

AvalonEdit in a WPF application with MVVM

According to this forum message, it's not possible to bind the Text property from the Avalon Editor to a simple property of a ViewModel.
Instead, you can use the Document property from the editor and bind it to a property of your ViewModel.
Here is the code for the view :
<Window x:Class="AvalonEditIntegration.UI.View"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:AvalonEdit="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit"
        Title="Window1"
        WindowStartupLocation="CenterScreen"
        Width="500"
        Height="500">
    <DockPanel>
        <Button Content="Show code"
                Command="{Binding ShowCode}"
                Height="50"
                DockPanel.Dock="Bottom" />
        <AvalonEdit:TextEditor ShowLineNumbers="True"
                               Document="{Binding Path=Document}"
                               FontFamily="Consolas"
                               FontSize="10pt" />
    </DockPanel>
</Window>

And the code for the ViewModel :
namespace AvalonEditIntegration.UI
{
    using System.Windows;
    using System.Windows.Input;
    using ICSharpCode.AvalonEdit.Document;

    public class ViewModel
    {
        public ViewModel()
        {
            ShowCode = new DelegatingCommand(Show);
            Document = new TextDocument();
        }

        public ICommand ShowCode { get; private set; }
        public TextDocument Document { get; set; }

        private void Show()
        {
            MessageBox.Show(Document.Text);
        }
    }
}