Category Archives: Software Development

This category contains all my posts about software development. Tutorials, solutions to problems I’ve met, interesting things I’ve stumbled over, code samples, stuff I don’t want to forget, et cetera.

Helpful Sublime Text 2 packages

Keep forgetting the names of packages I like to install when setting up Sublime Text 2. Or that I should install them to begin with. So here’s a note to self on packages I should install whenever I reinstall this fantastic editor. And a note to you if you use the editor and didn’t know such helpful packages existed.

  • Package Control, the package that adds a package manager to the editor. After that is installed, simply press Ctrl+Shift+p, bring up Install Package by typing “ip”, and hit enter. You can then search for packages and install them super easy.
  • Highlight, for copying syntax highlighted code either as HTML or as RTF. Nice for pasting code into emails, documents, etc.
  • Indent XML, for… indenting XML. Been very helpful for cleaning up messy SOAP requests from logs, etc.
  • LESS, for syntax highlighting of LESS files
  • Markdown Preview, for quick preview of markdown files in the browser
  • Text Pastry, for fun pasting madness. You can for example paste in sequences of numbers.

Use WinMerge for comparing archives with TortoiseSVN

I really like the Diff Viewer that comes with TortoiseSVN, but from what I can gather it doesn’t seem to support anything but text files, which is fair enough, but I need to compare archives as well. Zip and Jar files in particular.

WinMerge supports that, and can be integrated with TortoiseSVN very easily right through its installer. But I prefer the diff viewer that comes with TortoiseSVN and the archive diff isn’t recursive by default, so I decided to set it up manually.

  1. Install WinMerge
    (without the automagic TortoiseSVN integration)

  2. Install the WinMerge 7-zip plugin for archive support
    (I couldn’t get the shared install to work, so had to use the application specific one)
    7zmergeinstall

  3. Open TortoiseSVN Settings, Diff Viewer, Advanced
  4. Add rows with extension set to .zip, .jar and whatever archives you want WinMerge to handle and set external program to the following:
    C:\Program Files (x86)\WinMerge\WinMergeU.exe /r /x /e /u /dl %bname /dr %yname %base %mine

If I now for example ask TortoiseSVN for a Diff with previous version of a jar file, I get WinMerge with a recursive comparison of the archive contents. I also recommend that you deselect Show Identical Items under View.

PowerShell: Write stuff to an XML file

Needed to recursively list all the files of a certain type and save them as an XML file. Turns out this was pretty easy using PowerShell.

# Make a regular .Net XmlTextWriter
$output = "processes.xml";
$xml = New-Object System.Xml.XmlTextWriter($output, $Null);
$xml.Formatting = "Indented"
$xml.IndentChar = " ";
$xml.Indentation = "1";

# Start writing
$xml.WriteStartDocument();
$xml.WriteStartElement("root");

# Do the listing and keep outputting XML
Get-ChildItem .. -Recurse -include *.esbp |
foreach {
    $name = $_.BaseName;
    $path = Resolve-Path $_ -relative;

    $xml.WriteStartElement("process");
    $xml.WriteAttributeString("id", $name);
    $xml.WriteString($path);
    $xml.WriteEndElement();
}

# Close the writer, which also closes the root element and document for us
$xml.Close();

Easy peasy!

PHP: Simple directory recursion

Keep running into scenarios where I need to scan through a file system and it’s actually pretty simple if you just know what classes to use. So… note to self and others:

// This should return false if there is something you want excluded
function filter($file, $key, $iterator)
{
    $exclude = array('.git');
    return ! in_array($file->getFilename(), $exclude);
}

// Recursive directory iterator for current directory, ignoring dots
$it = new RecursiveDirectoryIterator('.', FilesystemIterator::SKIP_DOTS);
// Wrapped by a filtering iterator with our filter function
$it = new RecursiveCallbackFilterIterator($it, 'filter');
// Wrapped by an iterator which automatically traverses children for us
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);

// And then just loop :)
foreach($it as $file)
{
    echo str_repeat("\t", $it->getDepth())
        . $file->getRealPath()
        . PHP_EOL;
}

This skips the annoying dots, properly excludes directories you don’t want and pretty much works the way it should.

XSLT: Load XML or other text from external sources

Just discovered you can pull in external XML and other kinds of text directly through standard XSLT 2.0 methods. So here’s a note to self on how.

Documentation

Example usage

<!-- Single string URI -->
<copy-of select="doc('http://www.w3schools.com/xml/note.xml')"/>

<!-- One or more URI from items, in this case a variable -->
<variable name="uri" select="'http://www.w3schools.com/xml/note.xml'"/>
<copy-of select="document($uri)"/>

<!-- Unparsed text, in this case escaped HTML -->
<copy-of select="unparsed-text('http://example.com')"/>

XSLT: Pull duplicate namespace declarations up towards root node

Sometimes XML becomes a bit weird with namespace declarations all over the place. This XSLT cleans that up. Stumbled upon in a StackOverflow answer I don’t find anymore and put here so I know where to find it in the future.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="@* | text() | processing-instruction() | comment()">
        <xsl:copy/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy copy-namespaces="no">
            <xsl:for-each-group group-by="local-name()" select="descendant-or-self::*/namespace::*">
                <xsl:copy-of select="."/>
            </xsl:for-each-group>
            <xsl:apply-templates select="@* , node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Example

Input

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
   <soap:Header>
      <wsa:MessageID soap:mustUnderstand="0">uuid:7fa12310-5db4-11e3-ae24-a3c913f2629d</wsa:MessageID>
      <wsa:To soap:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
   </soap:Header>
   <soap:Body>
      <ns1:getTicket xmlns:ns1="http://api.example.com/some-webservice">
         <cus:msisdn xmlns:cus="http://api.example.com/some-webservice" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">00000000</cus:msisdn>
         <cus:ticket xmlns:cus="http://api.example.com/some-webservice" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">171</cus:ticket>
      </ns1:getTicket>
   </soap:Body>
</soap:Envelope>

Output

<soap:Envelope xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
       xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
       xmlns:ns1="http://api.example.com/some-webservice"
       xmlns:cus="http://api.example.com/some-webservice"
       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <wsa:MessageID soap:mustUnderstand="0">uuid:7fa12310-5db4-11e3-ae24-a3c913f2629d</wsa:MessageID>
        <wsa:To soap:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
    </soap:Header>
    <soap:Body>
        <ns1:getTicket>
            <cus:msisdn>00000000</cus:msisdn>
            <cus:ticket>171</cus:ticket>
        </ns1:getTicket>
    </soap:Body>
</soap:Envelope>

If anyone know how to adjust it to also merge the duplicated prefixes ns1 and cus, do let me know :)

Tail less

I keep seeing people typing these lines in the console:

$ tail /path/to/some.log
$ tail -f /path/to/some.log

This is often a dumb thing to do. Why? Because you can’t really do anything with tail. What if you discovered you needed to look at something right above the lines you got printed out? Or what if you were -f’ing and something flew past you that you needed to investigate further? You’d have to leave tail and run it again with more lines or use a different tool instead. Not very practical.

What more people should do is to use less tail and more less :)

$ less /path/to/some.log

Things you can do with less

Key

Function

Up one line

Down one line
b

Up one page
space

Down one page
g

Beginning of file
G

End of file
F

Follow
ctrl + c

Stop follow
q

Quit
/

Search forward
?

Search backwards
n

Next search result
N

Previous search result

Much more flexible and handy than tail! Know your tools ;) Now back to work…