Codepolice

Random posts from Ola in English. Mainly about programming and the web.

Make the Git GUI appear after you publish a Web Project in Visual Studio 2010

A couple of moths ago i started to use GIT as my primary Source Control. To be honest I hadn’t use any “real” source control at all before for this project. I only used a online backup service that had some basic version control on a file to file basis.

But i love GIT. It’s really simple and everything works more or less exactly as you expect it to do. But there isn’t much tools out there that integrates well with Visual Studio. There is Git Extensions for Windows and there is a project called Git Source Control Provider. The problem with Git Extensions is that it doesn’t do that much. It only ads a menu in Visual Studio where you can execute some command. Git Source Control Provider on the other hand looked like the perfect tool for me. But for some reason it didn’t sync correctly with my Git Repository and i got the feeling the project was a bit to immature to be trusted.

I always forget to commit

So, my biggest problem is that I always forget to fire up the command line / GUI and actually commit the changes i do the code. When you use SVN, TFS you get those nice little icons on all the files in Visual Studio so you are constantly reminded of your changes. This was what Git Source Control Provider promised but as i said, didn’t work for me.

I would be happy just to be reminded every time i publish my website to the ftp that “Hey! Don’t forget to commit your changes!”. And just to be clear. This is a solution for a one man shop that don’t really need “continuous integration” and loads of complicated stuff. I like my super simple solution where i just publish my project to my ftp and boom done!

My stupid hack

I found this post on Stack Overflow that explains how to run a MS Build target after you do a publish from Visual Studio. You just have to add this target at the end of your .csproj file. Your publish method must be a “WebDeploy”. I tried with “File System” but that didn’t work. The “WebDeploy” method was much faster anyway.

<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish" >
    <Exec Command="&quot;C:\Program Files (x86)\Git\bin\wish.exe&quot; &quot;C:\Program Files (x86)\Git\libexec\git-core\git-gui&quot;" />
</Target>

That’s more or less all there is to it. This will fire up the GIT GUI and your Publish task won’t finish until you close that program. Of course you could probably just run the console instead if you prefer that.

Copy a folder with Robocopy

I’m playing around a bit with PhoneGap at the moment. I wanted to make a script that copies all my javascript, html, images and css from my Visual Studio project folder to each of the diffrent PhoneGap projects for iOS, Android and so on.

Visual Studio has a featured to execute commands after the project is built and it felt like the simplest solution. I tried to add a command like robocopy c:\myproj\css c:\myandroidproj it worked but the thing was that i wanted to copy the folder AND the files in it and i couldn’t figure out how to to that. Maybe I’m just stupid, i don’t know.

Anyway i figured out how to do it in a good way finally.

robocopy $(ProjectDir) C:\Java\MyProj\assets\www\ *.html *.js *.css *.png *.gif *.jpg /S

This will copy all the specified filetype, ignore files like .csproj and stuff like that. The S stands for “Copy directory structure but ignore empty folders”.

Batch Optimize Png, Jpg and Gif images in Windows (and Visual Studio)

A couple of days ago i found an awesome extension to Visual Studio that let’s you just right click an a folder in VS and it will optimize all the images in it with the help of SmushIt and PunyPng. I have tried to find a tool like this for a long time but haven’t really found anything that suits my need. I also wanted this functionality in Windows outside of Visual Studio so i downloaded the code and made a extremely simple command prompt application for it. It would of course be even better if you could just right click a folder in Windows to run it but I’m a lazy person and this works for me.

Just run BatchOptimzeImage.exe <foldername> and it will optimize all the images in that folder. I wont give so much more instructions than this. And again .. ALL credits goes to Mads Kristensen for this on. I just wanted to put this out there so you don’t have to spend the hour i spent to make a console app out of it.

Download BatchImageOptimizer

Automatically switch spell check language/dictionary as you type in Firefox

I have been looking for a plugin like this for ages. I constantly switch between Swedish and English when i work and it’s a pain to always remember to switch the spell check to the correct language. But today i found Dictionary Switcher that automatically set the spelling language to the locale of the page but also detect what language you are typing in and switch to the correct language. I’ve made a YouTube Video to show how it works.

Custom Action Result do not run ExecuteResult and just returning ToString

All of a sudden all of my RssFeeds stopped working on AlternativeTo. All i got was the name of the type i returned from my MVC methods that should return a "RssResult". After some Googling i finally found the answer on Stack Overflow as usally.

The problem was that i used version ;MVC 2 in some of my projects (the one where the code for the Custom Action Result was located) and version MVC 3 in some other (the web project).

The thread on Stack Overflow about the issue.

RouteDebugger and 404 MVC Json requests

I got this really strange bug where i got a 404 error on all request i made via Ajax to methods that returned Json via MVC. Turned out that this was caused by RouteDebug that i had laying around in my bin folder. I have got this bug twice now so i just wanted to make a note of this here so i remember it next time.

IE9 mess up color theme in Visual Studio when started

I have a really weird issue with Internet Explorer 9 and my Visual Studio Color Scheme. Each time i start IE9 some of the colors in my IDE is changed. I have no idea why this is and hopefully i can find some other person who has the same issue? I suppose it has something todo with IE9 messing with the underlying colors cheme in Windows just as some games are doing probably because of the hardware accelerated graphics in Internet Explorer 9? Would be awesome if anyone have a solution! A video demonstrating the issue.

Creating a empty/blank text widget for WordPress without additional markup and divs

WordPress provide a simple text widget that you can use on your blog to add some random HTML, Ads, images and so on. But i has one problem and that is that it’s always adding some extra HTML like this.

<div class="widget">
 <div class="textwidget">
 </div>
</div>

Sometimes you really do not want this code if you for example want to add a widget to a ul and want the outer tag to be a li. I tried to Google for this but surprisingly i didn’t find a single “Super Empty Text Widget” so i created one myself.

/**
 * SuperEmptyWidget Class
 */
class SuperEmptyWidget extends WP_Widget {
    /** constructor */
    function SuperEmptyWidget() {
        parent::WP_Widget(false, $name = 'SuperEmptyWidget');
    }
 
    /** @see WP_Widget::widget */
    function widget($args, $instance) {
        extract( $args );
        $content = $instance['content'];
        echo $content;
    }
 
    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
	$instance = $old_instance;
	$instance['content'] = $new_instance['content'];
        return $instance;
    }
 
    /** @see WP_Widget::form */
    function form($instance) {
        $content = esc_attr($instance['content']);
        ?&gt;
         &lt;p&gt;
          &lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id('content'); ?&gt;&quot;&gt;&lt;?php _e('Content:'); ?&gt;&lt;/label&gt;
		          &lt;/p&gt;
          &lt;textarea class=&quot;widefat&quot; cols=&quot;20&quot; rows=&quot;16&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id('content'); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name('content'); ?&gt;&quot;&gt;&lt;?php echo $content; ?&gt;&lt;/textarea&gt;
 
        &lt;?php
    }
 
} // class SuperEmptyWidget
 
// register SuperEmptyWidget widget
add_action('widgets_init', create_function('', 'return register_widget(&quot;SuperEmptyWidget&quot;);'));

Just add that to your functions.php and you should have a widget that do not add any crap either before or after the content.

Entity Framework Scaffolding: The type is not mapped issue

This was a tricky one. I was just watching Scott Hanselmans session from MIX 11 “Microsofts Web Stack Of Love” and as always the guys it totally brilliant. It’s like stand up mixed with development. Anyway, he talked a bit about MVC Scaffolding and about how awesome it was. I fired up Visual Studio and installed MVCScaffolding via NuGet added some EF Models and boom! This was what i saw.

The type was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

Hmm, that was not what i expected. Turns out that i had not installed Entity Framework 4.1 completely. I found this post and i had the exact same problem. Just download Entity Framework from here and everything should work as excpected. Now back to the session!

MS SQL: Saving changes is not premitted if the table need to be re-created

This is a classic. Microsoft SQL Server 2008 introduced a feature that prevents you from changing a table in the designer if it need to be re-created. And whenever i get this i always forget where to turn this feature off. This time i recorded a 14 sec video that shows where this option is. Enjoy :)