Saturday 5 November 2011

Checkout Source code and Build - using perl script

The below perl script code is to chk-out the source code from the SVN and build the project (IDE MS Visual Studio) .
This script is to make sure that the source code on SVN  is, in, good shape (no compilation error).
If more info is needed or if you see any errors, feel free to ping me.

This is what the code does.
1. Initialize the variable for SVN path and local machine path
2. Open the log file
3. Check-out the source code from SVN - chkOutSourceCodeFromSVN
4. Build the source code - buildProject
5. Close the Log file

# Code start
use strict;
use warnings;
use POSIX;


my $LogFilePath = "D:\\BuildLog.txt"; # Log file path
my $SVNServerPath = "http://svn"; #SVN path
my $LocalSourceCodePath = "D:\\Source"; #Local source code path

# SVN path of the branches
my $SVNProject1 = $SVNServerPath."/branches/Project1";
my $SVNProject2  = $SVNServerPath."/branches/Project2";

# Local path on machine
my $LocalProject1Path = $LocalSourceCodePath."\\Project1";
my $LocalProject2Path  = $LocalSourceCodePath."\\Project2";

# Project 1 .sln, used for building projects.
my $Proj1SLNFile = $LocalProject1Path."\\Prj1.sln";

# Project 2 .sln file name
my $Proj2SLNFile  = $LocalProject2Path ."\\Prj2.sln";

#Log the result
open LOG, ">>".$LogFilePath;
  
#Chk out the source code from the SVN server
if( &chkOutSourceCodeFromSVN( $SVNProject1, $LocalProject1Path ) == 0 )
{
   #Release mode Encodjiro project.
  if( &buildProject($Proj1SLNFile) == 0)
   {
     print LOG = "Build Successful";
   }
   else
   {
      print LOG = "Build FAILED";
   } #End of building the project

} #End of SVN Chk out condition

#================================================
#Function name - chkOutSourceCodeFromSVN
#Checkout all the branches to local path
#Arg: [0] SVN path.
#     [1] Local machine path.
#================================================
sub chkOutSourceCodeFromSVN
{
   # SVN command to checkout the source code, 0-svn path 1-localpath
   my $ChkOutFromSVN = "svn checkout ".$_[0]." ".$_[1];
   my $CmdLineOutput= system( $ChkOutFromSVN ); #! Execute the command.
   if ( $CmdLineOutput == 0 )
   {
print LOG "\nCheckout successfully Return code = $CmdLineOutput\n";
   }
   else
   {
print LOG "\n\nFailed to checkout from SVN Return code =  $CmdLineOutput\n";
   }
   return $CmdLineOutput;
}#End chkOutSourceCodeFromSVN

#================================================
#Build the project solution - build
#Arg: [0] Project Name.
#================================================
sub buildProject{
    my $ProjectName = $_[0];
    my $BuildCmd;

    # Execute the command
    $BuildCmd = "devenv $ProjectName /build \"Release\"";
    
   # Execute the command
    my $cmdOutPut = system($BuildCmd);

    if( $cmdOutPut == 0 )
    {
        print LOG "\nProject Build successfully = $ProjectName";
    }
    else
    {
    print "\nFailed to build = $ProjectName\n";
    }

   return $cmdOutPut; #Successfull is zero
}#End of buildProject


#close the log file
close LOG;


The above code is posted to help other who are willing to automate the source code building process, be causes while using the scripts on SVN.