Tuesday, November 17, 2009

Closure Compiler Externs Extractor

Many programmers are trying to use the Google Closure Compiler in their own applications but quickly they are hitting a wall: suddenly their third party APIs got renamed and the application doesn't work anymore, because they did not define the correct Externs!

If you already know what Externs are you can go directly to the Closure Compiler Externs Extractor example.

Closure compiler is an amazing tool if it is used as it was designed. The difference between it and other tools is that it really compiles the javascript and not just "minify" like YUI Compressor or Ajax Minifier does.

One of the concepts behind is that we can rename all the symbols and not just the local variables.
Nice to hear that, but this is very bad for the current javascript pieces of code that were not written with Closure Compiler's rules in mind.


MyApp.MyNamespace.MyObject.MyMethod = function() {...}
var o = MyApp.MyNamespace.MyObject.MyMethod();
gets compiled into
b.c.d.e = function() {...}
var a = b.c.d.e();

Well that is very good if I own the source code of MyMethod, so it will also be renamed along the road to the same short name e.
However, if I would like to make this code available for other people to use into their application as a plugin - it might not be that good, as on every compilation I would get a different method name.
Google's solution is to export your symbols.
Yeah, I can do that, because it's my code, right?

MyApp.MyNamespace.MyObject.MyMethod =
window["MyApp"]["MyNamespace"]["MyObject"]["MyMethod"] = function() {...}

This would still compress my code on all the location where it is used (as above), but will also make it available of other users to use it with public friendly names.

Well, this is good, but is very bad if I need to use an external API that is maintained by a third party.
In that case Google's solutions is to use an Externs file on the compilation.

An externs file is basically a javascript file that instructs the closure compiler about some symbols that are defined in a third party code that it is not aware of.

i.e. If I'm using jQuery, but I can't include it into my compilation, because it is already used by other users on my site
- and jQuery is not yet friendly with the closure-compiler
- and I don't want to have two jQuery files on my site - one compiled and one not
I could define the externs I'm using in my compiled code like this:

var jQuery, $;
$.fn = jQuery.fn = {
bind: function() {
trigger: function() {
prepend: function() {
show: function() {
hide: function() {
append: function() {

All good, but I don't know all the public methods/properties of the APIs I'm using! Nor I want to waste time to write externs for each and every piece of code for all the extern APIs I'm using!

So, I have written a small example of how to extract all the properties / methods of a javascript object as externs.

Please leave a comment if this helps you or if you have suggestions to improve it.

Tuesday, November 10, 2009

EasyXDM, the best Cross-Domain XSS / XDM javascript solution

There are tons of posts and articles about cross-domain communication and Same Origin Policy limitations on the web, but none of them are providing you a real browser-transparent solution.

After I dug over a month in this area, I have finally found the best solution!
It is called EasyXDM, and I really want to thank to oyvind for it.

I'm not going to write just yet of how easy can it be used but you can read here

There was a bug on the Tests suite that was crashing Google Chrome and Safari browsers but I have fixed it here. Be aware that the tests are forced to run in all the transports and some of them are expected to fail in certain browsers. However all of them should pass on the "Best Transport" tests suite.
[Update: Oyvind has also updated the test suite here]

For those who don't want to spend a month doing the research they can start with these resources:

Enjoy cross domain re-usable applications!

Wednesday, November 4, 2009

Exclude a folder from solution explorer in VS2008 website

In any project of Visual Studio.Net environment excluding a folder is very simple. We just need to right click on the folder and select Exclude from Project. But in Website of Visual Studio.NET it is not that easy to hide a folder and also there is no direct way to exclude a folder from Website.

The easy way to exclude an unwanted folder from a website is to physically delete the folder from the website structure. But what if you should not delete the folder from the website structure? Here is a simple trick to hide(not deleting) a folder from Visual Studio.NET editor as well as from Visual Source Safe.

Set Hidden attribute for the folder which you want to exclude from project, and the refresh the Website(project) in Visual Studio.NET Solution Explorer. Here are the detailed steps to be followed

1. Open Windows Explorer
2. Locate the physical folder of your website
3. Right click the folder which you want to exclude, and then select Properties
4. Check the Hidden attribute
5. Click the Refresh button in the Visual Studio.NET Solution Explorer

Using this simple trick you can even hide files of a website from Visual Studio.NET environment.

Thanks for TechDreams for letting us know this easy but very useful trick!

Wednesday, September 30, 2009

Visual Studio 2008 Custom Nested Files

After googling for months I have finally succeeded to nest custom files on Visual Studio 2008.

There are different techniques posted by other users, all are good but they only apply to specific situations.
  1. Using a macro for nesting the files on the fly as Christoph De Baene describes here
    Very nice, but this doesn't work for WebSites, as there is no .csproj involved to store the nesting information.

  2. Creating a project template -or- item template i.w. with GAT (Guidance Automation Toolkit) and add the ability to nest custom files as Mads Kristensen describes here
    Again nice, but not suitable for simple cases and still doesn't apply to WebSites

  3. Defining custom nesting rules in registry - suitable for websites as Bill Hie describes here.
    Below is the quoted tip updated for VS2008 and on a Windows Server 2008 x64
    Note that the root path of the VisualStudio might be different on another version of Windows.

    Defining Related Files

    The web project system has the ability to nest one file type under another file type in the solution explorer. These relationships are all done by pattern matching the file names and are driven by entries under the RelatedFiles key.

    NoRemove 'Projects'
    NoRemove '{E24C65DC-7377-472b-9ABA-BC803B73C61A}' = s '#2143'
    val 'RelationType' = d 'typeValue'
    val ‘Suffix’ = s ’somesuffix’

    PrimaryExtension - This is the extension of the “primary” file – the one that will contain the nested files.

    RelationType - This value defines the pattern that is used to form the relationship. The supported typeValue’s are:

    • Value = 1 this defines a simple relationship based on the base name of the parent file. basename.PrimaryExtension will have one or more related files called basename.RelatedExt1, basename.RelatedExt2, etc.
    This can be very nice as you can define multiple nest levels.
    For example if .lang would be the extension for the language specific files and .min for the minified javascript files we can define:
    So your files can be nested like this:

    • Value = 2 – this defines a basename.primaryextension to basename.primaryext.relatedextension relationship.

    Suffix - Optional. Only used by the simple relationship (RelationType = 1), to extend the simple relationship to include semi-colon delimited list of possible suffixes to the base name. If Suffix was set to ‘_bar;_foobar’, the simple relationship example above becomes:

    To include foo.xsx and foo.xsc as well as the suffix varieties, one of the suffixes needs to be the empty string as denoted by the leading semi-colon: ‘;_bar;foobar’

    .RelatedExt1…n - The file extensions which will appear under the primary file. In the example above, it would register two extensions .xsx, and .xsc.

Monday, August 31, 2009

JIT Compiler encountered an internal limitation.

Finally, the summer of holidays ended and we have got back to work.
After not too much codding when we were hit by this phenomenal error :)

Server Error in '/' Application.
JIT Compiler encountered an internal limitation.

Nice one, Doc!

Saturday, May 23, 2009

Code Comment Tracking - A must have tool

23 June 2010: You might find this macro also intereting. It contains two useful methods:
Attach to W3WP so you never need to open the Attach To Process dialog.
Note that the macro is trying to attach to the first w3wp.exe process that is runs under your credentials. If you call the function again, it will attach to the next w3wp available, as sometimes you might have more than just one.
Of course if it doesn't find a w3wp.exe running under your account, it will attach to the first that it finds.

It also contains an Attach To IE function so you can easily debug javascript and attaching to IE.exe.
Note that calling the function multiple times, will attach to the next available IE.exe process.

For an easier use of these macros I suggest you should map your custom shortcuts - I am using alt+3 and alt+4 for these.

How many times have are you looking through some old code and you can't figure out who created that 'weird' thing?
What about when is not your code , isn't hard to look after the fields, methods, properties etc. as they are not in the right order you are used with?

Here is a tool which can help you tracking and arranging the the code.

Have a look on this simple example:
It is weird aligned and it is not commented at all.
there is a certain order every developer respect. For example:
  1. Fields
  2. Constructors
  3. Properties
  4. Events
  5. Util methods
  6. Virtual/Override methods
  7. Business/Static methods
  8. Other classes, delegates, structs etc.
In the left image the code is messed up in a random order, hard to follow.
Of course this is just a small example, but on real code it gets very messy.

Well, that's about to be changed.

As you can notice the code is nicely arranged and well commented.
Now you know when a piece of code was actually created and who to blame on

  • Move code in regions
  • Group regions in an useful order
  • Author and date-time stamp for source-code tracking
  • Automatic xml-doc comment generated for properties i.e. Gets the Name - or - Gets or Sets the Date
  • Automatic xml-doc comment generated for event handlers i.e. Handles the Page's Load event
  • Automatic xml-doc comment generated for event raisers i.e Raises the Init event
  • Automatic xml-doc comment generated for constructors, classes, interfaces, delegates, structs, enums
  • The ability to add the comments/author/timestamp without moving all the code in regions
  • The ability to arrange the code (move in regions) without commenting it
  • The ability to move the code and comment it at the same time
  • The ability to comment the code with the currently logged in user (Environment.UserName)
  • The ability to comment the code with a customized/predefined username or timestamp

How to install it:
  1. Download the attached macro version for VS2008
    or for VS2010
  2. Open Visual Studio 2008/2010, and go to Tools->Macros->Macro Explorer (Alt+F8)
  3. Locate Samples -> Utilities, right click -> Edit
  4. Copy the code from the downloaded file and paste it right after Public Module Utilities
  5. Under project exporer right click References and Add Reference to System.Web.dll version 2.0

  6. Make sure you add on the imports section these lines:
    Imports System.Collections
    Imports System.Text
    Imports System.Text.RegularExpressions
  7. Save the samples (Ctrl+S) and go back in Visual Studio 2008/2010 on Tools->Options->Environment->Keyboard
  8. On 'Show command containing' type PutInRegionsWithoutCreatedAuthor
  9. On 'Press shortcut keys' type your favorite shortcut i.e. Ctrl+Alt+\ (Ctrl+Alt+Backshash) and press Assign
  10. On 'Show command containing' type CommentWholeCode
  11. Select CommentWholeCode if you like your windows login account name to be used for comments, or CommentWholeCodeSpecific if you want to override with your custom name
    To change the specific name locate the CommentWholeCodeSpecific method in the macros editor and fill in your desired name.
  12. On 'Press shortcut keys' type your favorite shortcut i.e. Ctrl+Alt+/ (Ctrl+Alt+Slash) and press Assign
  13. Now go to any class file (*.cs) and test the macros by pressing your shortcuts. eg. Ctrl+Alt+/ and Ctrl+Alt+\

  1. Sometimes VisualStudio goes crazy and shows up an error. To get around it just open the File Search dialog (Ctrl+F) and do a dummy search and run the macros again. The error should be gone. Monday, 6 July 2009, Laurentiu Macovei: This has been fixed and should work smoothly. Also tested on VS2010 and works.
  2. This version only works for .cs files located in CSharp (.cs) projects, or files from App_Code folder within a website
  3. Does not work for stand-alone files (out of solution files)
  4. Does not work for Visual Basic .vb files - just yet

The comment/put in regions operations can be undone by pressing Undo on your document so your document is safe.

The comment feature is intended to be used very often, by all the developers with no exceptions.
Basically every time when you add a new method, property, etc. you should run the CommentWholeCode macro (Ctrl+Alt+/) so it will be commented with your username. Otherwise, some other developer may 'take' your code under his username, and that's not desired.

If you have any questions, please let me know.

Happy code tracking!

Friday, May 22, 2009

Visual Studio 2008 SP1 + Windows Updates and you can't step through .NET Sources!

Visual Studio 2008 with SP1 is great! Especially for the easiness you can debug and step through .NET Framework sources. I'm not going to insist here about how to configure it, there are a lot of articles out there.

Anyway, the happiness is going to end right after you accept Windows Update's KBs. Microsoft has published the sources of a few .NET Framework versions, but of course, unfortunately they are out of date!

As you can see below they only have published the sources of .NET Framework 3.5 SP1 3053 and an update to 3074.

The big issue is that Windows Update automatically brings you the KB51847 update for .NET Framework 3.5 SP1. What they don't tell you is that they are changing .NET Framework 2.0 version from 2.0.50727.3053 to 2.0.50727.3082 and later after other KBs on 2.0.50727.4016.

This change will break your debug and step through .NET Framework sources availability because Microsoft did not publish the PDB (Program Database) symbol files.
After a week of research and wasted time I ended up writing this article and hopefully will help you not to waste it as well.
Of course, Microsoft did not do anything even if I shout on all the channels at them!

In my case, I could not debug System.Web.dll as the downloaded System.Web.pdb was only 912,384 bytes instead of 10,694,144 bytes, the original with the source code included.

The BIGGEST problem is that you cannot ROLL-BACK to 2.0.50727.3053 anymore!

One way to get around this (until M$ will publish out the correct versions) is to download the VHD image with VS2008 sp1 pre-installed and do your quick step-through .NET Framework source code in there. Of course this method requires you to have Windows Server 2008 with Hyper V.

This is how looks in Visual Studio:

Before Modules
After Modules

Source code

Hey Microsoft, are you sleeping on one ear?

Tuesday, May 19, 2009

Visual Studio 2010 Beta 1

Visual Studio 2010 Beta 1 was just launched yesterday, 18th May 09!

Update: 23th May 2009 - Now it's available for everyone for download!

If you are MSDN subscription then you can download it right away. Otherwise you'll just have to wait for a few more days.

Wednesday, April 15, 2009

Bloggy style

DotNetWise has just stepped into the blog world.
Here will post .NET related issues and pieces of advices.

Happy codding!
Blog powered up by Blogger