Check In That F*ckin Code

Check In That F*ckin Code

Ugh. So apparently I've dropped the ball on this blog software. I've made some changes that were deployed and others that were not since October. None of the changes were committed. Now I don't know where I'm at. Some stuff made it to panteravb.com, and some stuff didn't. So apparently NOW is a good time to do some refactoring. New design coming soon(like no later than this Sunday).

This code was generated by a tool.

If you've ever generated code using the System.CodeDom, you'll notice that the generated code always has the above message stamped at the top of the generated code compile unit. There's nothing amazing about this, and yes, everytime I read This code was generated by a tool, I laugh internally like a little 12 year old boy.

I did some digging with Reflector and opened up System.dll to find the CSharpCodeGenerator. The following is a little snippet from the private method GenerateCompileUnitStart, where the message at the top of each file is generated.

On my current gig, our development process starts with the database. Make a change to the database schema, regenerate all of the ActiveRecord classes with HyperActive, rebuild, check in changed classes, continue developing. This works fine but one day I noticed that after making one small change to the schema and regenerating classes, upon checking in the changes, I was actually checking in every single generated class. How could this be? I only made one change. It turns out that the little message at the top of the gen'd code is different depending on what operating system you're running. Even though we all have .NET 3.5 SP1 and VS 2008 SP1 installed, the different operating systems cause the Runtime Version(in the above image) to be slightly different, differing only in the revision component of the version number.

Since checking in files that really have not changed seemed stupid to me, I set out to find a way of turning that message off. I was unable to find any method. So that's when I came up with this hack. I added a config option for HyperActive named UseMicrosoftsHeader, by default this is set to true. Setting this to false means that after generating the usual code, HyperActive then walks line by line and removes the generated message, and instead writes in a HyperActive generated message. This message stamps in the version of HyperActive that generated the code.

private string UseHyperActiveHeader(NamespaceDeclaration ns)
{
  string source = "";

  // Generate source to a string for post parsing.
  string tempSource = String.Empty;
  using (StringWriter tempWriter = new StringWriter())
  {
    new CodeBuilder().GenerateCode(tempWriter, ns);
    tempSource = tempWriter.ToString();
  }

  // Now while walking through each line of the file, intercept where
  // there is Microsoft header shite and put in HyperActive header shite.
  using (StringReader reader = new StringReader(tempSource))
  using (StringWriter tempWriter = new StringWriter())
  {
    bool insideHeader = false;
    bool hyperActiveMessageWritten = false;
    string line;
    while ((line = reader.ReadLine()) != null)
    {
      if (line == "// ")
      {
        insideHeader = false;
      }
      if (!insideHeader)
      {
        tempWriter.WriteLine(line);
      }
      else if (!hyperActiveMessageWritten)
      {
        tempWriter.WriteLine("// HyperActive {0} generated this file.",
          Assembly.GetExecutingAssembly().GetName().Version.ToString());
        hyperActiveMessageWritten = true;
      }
      if (line.Trim() == "// ")
      {
        insideHeader = true;
      }
    }
    source = tempWriter.ToString();
  }
  return source;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

After applying the above method to the output source code, we get a nice consistent message like the one below:

On Good Taste:Charleston, SC

My wife, Anja, and I are in the relocation mode, just looking for possible places to move to mix things up. One of the places on the short list is Charleston, SC. I figured that before actually doing any serious job searching I'd check out the town first. So I googled and found this site: http://www.charlestoncity.info/home/home.aspx

I'm pretty sure there hasn't been a need to support 640x480 resolutions from websites since the mid 90's. This is not designed for a mobile device. This....is....horrible:

Automate Everying: How I Restore Databases With A Double Click

"Lifehackers go to great lengths to avoid tedious, mundane work: they'd rather spend 2 hours and 59 minutes automating a 3-hour job than doing it manually. "

...from Lifehacker: 88 tech tricks to turbocharge your day, gina trapani

I work on a team where pretty much everything is remote. There's no central development sql server, each developer has their own local copy of each database. When a developer makes a schema or data change, they backup their local database copy, check it into source control, and all of the other devs have to update their local copies. The process requires only a few steps, but they are repetitive steps I don't care to do one or more times throughout the day.

So short story long, I asked my buddy Google for an answer, basically I needed to know how to restore a database from command line without any interaction from me. I found this script. The script works great, however, I also wanted to be able to use this in our CruiseControl build.

This is where NAnt comes in. I wrote a little script like the following to wrap the commandline. Originally in this post, I put the actual script but after some consideration I wanted to post something that can be downloaded and actually tried out, so I modified stuff a little bit to use the Northwind database, and supplied the code at the end of the post.



    
    
    
    

    
        
        

        
        
        
        

        
    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

The file restore.bat contains the command line info for kicking off the restore and looks like this:

c:\tools\nant\bin\nant.exe -buildfile:nant.build -D:database=Northwind restore
1

Since we store a zipped up .bak file in source control, there are three steps to restoring the database: unzip, restore, delete zip. Here's the output you'll see after running the restore.bat script:

We also have a need to generate a schema script for the target database. Another application uses this script to create an offline structure of the database. I use the buried version of SqlPubWiz located on your machine right now if you have VS 2008 installed: C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2\SqlPubWiz.exe (note, I'm running a 64 bit Windows 7, if you're running 32 bit you won't have (x86) in the path to that tool). If you double click this file, you'll notice it looks alot like the built in one used by Sql Server Management Studio, but since I want this scripted we don't need to double click. Instead we'll add a little to our restore.bat script. Once the database is restored, we'll script the schema out. The script now looks like this:

call c:\tools\nant\bin\nant.exe -buildfile:nant.build -D:database=Northwind restore
call "C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2\SqlPubWiz.exe" script -d Northwind "c:\dev\RestoreNwind\NorthwindSchema.sql" -schemaonly -f
1
2

The output will look something like this:

Here are the files: http://panteravb.com/downloads/RestoreNwind.zip

The zip contains four files. attachdb.sql which is the sql script for attaching a backup, nant.build is the build script which can be integrated into your build system, northwind.zip is a zipped up backup of the Northwind database, and restore.bat is the script that orchestrates everything. Check file paths before running this as all I'm confident of is this:

Image ripped from thoughtex.net

Writing Is Hard

I've come to the conclusion that writing is hard. Especially when you're a perfectionist like myself. I get tied up in the tools, since those are the things I'm most interested. I had completely forgotten about DarkRoom until this morning. I need to figure out how to integrate this tool as my blog editing software. Right now I'm just saving this post as a text file. Since I use MarkDown as my markup tool, this works out pretty well.

Writing is hard. I worry alot about the content, which has been sparse in the last two years. I have many false starts on posts, some are good, some are forced, some are lame, none of them "feel right". I haven't had the "flow" for a while. I like the flow. That's that feeling where you almost don't even know that you're typing, it's just that inner voice talking and making sense and the words appear on the screen.

Panteravb.com

The name was purchased back in 1998 and reflected what I was doing WAY back then, coding vb(old school vb) and listening to Pantera. I still listen to Pantera, but haven't touched visual basic for years(thank god). I have chrisjcarter.com and have done exactly nothing with it...

I don't do php or rails which are the options I have for developing anything for chrisjcarter.com. I started a little bit with rails, and still want to pursue that, but my perfectionist attitude always gets in the way(my inner programmer tells me that if I want to do "real" rails development, it has to be on a mac or some *nix distro, which leads me down the "how the hell do I get started on that platform" and pretty much goes downhill from there).

Writing is hard. I'm a programmer not a writer. Wait a minute. What's the difference? I can write shitty sentences just as easily as I can write shitty code. Hard to read code, superfluous comments that are not in sync with the code they describe, ya shitty code is easy to write. I just said it, "shitty code is easy to write", huh, maybe I am a writer after all.

Droid Does

I watched the first iDon't commercial two weeks ago and was instantly sold on the Droid package from Verizon/Motorolla. I picked up one of these a week ago. It's awesome. It's the first phone I've had that wasn't just a phone. I am in love with touch screen technology. I totally want to develop apps not only for the Android OS but for Windows 7 as well, since it's apparently got some bomb ass touch screen technology. Touch screen apps are the way to go in the future.

Write To Done

Writing is hard. I like to look to sites that have nothing to do with programming to find motivation to write. writetodone.com is one of those sites. It's packed with ideas on how to break through your personal barriers and write great things.

Good Taste

Ira Glass is someone I was not aware of until one night of following a series of inspirational posts led me to his series on storytelling. One of the episodes describes the notion of good taste. It was awesome, strictly because it pinpointed exactly me, and where I'm at right now.

I designed this site. I don't like it, the design that is, I think it's horrible actually. I go to some sites, blogs, whatever, and immediately I have an opinion of the design of the site, whether it's good, great, shitty, etc. I know what's good. I'm excited about making great things like the design of a blog or UI for an application. However. When I actually sit down to design, what I'm creating is crap. Really bad crap. But I still have good taste.

HyperActive is my little creation for generating Castle ActiveRecord classes off of an existing db schema. Configuring it SUCKS. It's horrible to setup, I hate it. I've made a couple of attempts at a config editor and hated them all. This fits along with the same thing Ira talks about. The important thing is the advice to not give up, don't stop, keep cranking stuff out, the more work(apps) you do the better you get. This is awesome, and motivating.

My blog design is a sore spot for me. I'm extremely picky, and every design I come up with is less than appealing, and NOT what I picture in my head. Apparently I just need to keep producing. Smashing Magazine is a great inspirational site, with great tips and examples of design. They have recently posted an article highlighting the blogazine. Basically, every blog post gets it's own design, just like a magazine article. These type of posts are designed in a way that focuses the design on the content, not just plain jane templates, so they end up being easier and more fun to read.

Writing Is Hard

But I like it.

 
Author: , 0000-00-00