Archive for June 3, 2010

Mercurial as a Git client

Days ago I just published a short blog post about using Mercurial as a SVN Client, I know, Subversion is the “now-legacy-vcs-system” and Git with Mercurial are the “cool kids” in the school, why not make them work together?

Well, in the same way we could make Subversion works with Hg (using extensions of course) we could make Git works with Mercurial. It requires a little more work in Windows but it stills easy to do. As usual we start with a TortoiseHg installation. I will try to show its installation in a short set of steps:

  • We need to activate the Mercurial Bookmark and rebase extensions (these extensions are included with standard Mercurial installation). Edit your mercurial.ini file and add the following to the [extensions] section
  • [extensions]
    rebase =
    hgext.bookmarks = 
  • HgGit needs a special python library to handle Git repositories, this library is named dulwich. You need to get it from its source repository, clone it in some directory

    hg clone http://bitbucket.org/abderrahim/dulwich
  • Now we need to get the HgGit source code from its repository, let’s clone it from repository

    hg clone http://bitbucket.org/durin42/hg-git
  • This is the hard part, we need to tell to HgGit the location where the dulwich library is installed. Remember, both HgGit and dalwich are Python libraries, so we’ll edit just a file to show where to get dalwich. Go to the place you cloned HgGit search and open the file __init__.py and add look for the following code and add the code highlighted
    import os
    
    # NOTE: Added for tortoisehg compatibility
    import sys
    sys.path.append('write here the directory where dulwich was cloned')
    
    from mercurial import commands, extensions, hg, util as hgutil
    from mercurial.i18n import _
  • Now let’s activate the extension, open again your mercurial.ini file and add the following line to your [extensions] section

    hggit = directory where hggit where clonedhggit

And that is all, just test your extension installation with hg help extensions And look for hggit

Now you can clone, rebase, push, pull and do whatever you do with mercurial but using remote git repositories, for example, now you can do something like

hg clone http://github.com/castleproject/Castle.Core.git castle

I hope this guide could help you to get more fun from Mercurial, see you later!

.NET y Configuraciones – Parte 3

Ok, hasta el momento ya vimos como crear nuestras propias secciones y valores de configuración, además de darnos una vuelta por las opciones de crear y agregar elementos a nuestras secciones personalizadas. Hoy continuaremos indagando un poco más en el mundo de las configuraciones personalizadas en .NET.

Grupos de Secciones

Imagínense que la configuración de nuestra aplicación es mucho más compleja de lo usual y que además de elementos personalizados necesitamos agregar “subsecciones” de configuración. Dentro de la .Net Framework es algo común en ASP.NET. Crear secciones de configuración no puede ser más fácil, basta con crear las secciones como lo hemos estado haciendo hasta el momento. Tomemos por ejemplo las siguientes dos secciones:

public class SampleSection : ConfigurationSection {
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name {
        get { return (string) base["name"]; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("port", DefaultValue = 80)]
    public int Port {
        get { return (int) base["port"]; }
        set { base["port"] = value; }
    }
}

public class AnotherSection : ConfigurationSection {
    [ConfigurationProperty("times", IsRequired = true)]
    public int TimesPerDay {
        get { return (int) base["times"]; }
        set { base["times"] = value; }
    }
}

Basta ahora con crear un ConfigurationSectionGroup para obtener ambas secciones dentro de un mismo grupo

public class SampleSectionGroup : ConfigurationSectionGroup {
    [ConfigurationProperty("sample")]
    public SampleSection Sample {
        get { return (SampleSection) Sections["sample"]; }
    }

    [ConfigurationProperty("another")]
    public AnotherSection Another {
        get { return (AnotherSection) Sections["another"]; }
    }
}

Agregarla en el web.config es un poco más laborioso pero continúa siendo sencillo

<configuration>
  <configSections>
    <sectionGroup name="sampleGroup" type="WebApplication2.SampleSectionGroup">
      <section name="sample" type="WebApplication2.SampleSection"/>
      <section name="another" type="WebApplication2.AnotherSection"/>
    </sectionGroup>
  </configSections>
  <sampleGroup>
    <sample name="sample" port="31" />
    <another times="5" />
  </sampleGroup>
</configuration>

Creo que para estas alturas el usarla en el código debe ser algo sencillo y "straighforward"

.NET y Configuraciones – Parte 2

Anteriormente habíamos conversado acerca de como trabajar nuestras propias secciones “customizadas” de configuración usando las facilidades de configuración que nos da la .Net Framework. Como vemos es sumamente fácil crear nuestras secciones “personalizadas”. Conversaremos hoy un poco más de como sacarle mayor provecho y extender nuestras secciones de configuración y así sacar mucho mayor provecho.

Elementos personalizados anidados

Observemos el siguiente snippet de código

public class HostInfo : ConfigurationElement {
    [ConfigurationProperty("server", DefaultValue = "localhost")]
    public string Server {
        get { return (string) base["server"]; }
        set { base["server"] = value; }
    }

    [ConfigurationProperty("port", DefaultValue = 80)]
    public int Port {
        get { return (int) base["port"]; }
        set { base["port"] = value; }
    }

    [ConfigurationProperty("ssl", DefaultValue = false)]
    public bool IsSecure {
        get { return (bool) base["ssl"]; }
        set { base["ssl"] = value; }
    }
}

Aunque nos recuerda a la forma en que creamos una sección de configuración, realmente estamos creando un elemento de la configuración (ConfigurationElement). En otras palabras, podemos tener elementos anidados y de esa manera lograr que nuestros elementos de configuración obedezcan a una más clara semántica. Utilizarlo en la sección de configuración no puede ser más fácil

public class ServerConfigurationSection : ConfigurationSection {
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("host")]
    public HostInfo Host {
        get { return (HostInfo)base["host"]; }
        set { base["host"] = value; }
    }
}

Usarlo no puede ser más fácil que esto

<configuration>
  <configSections>
    <section name="server" type="WebApplication2.ServerConfigurationSection"/>
  </configSections>
  <server name="myconfiguration">
    <host server="192.168.1.1" port="81"/>
  </server>
</configuration>