File-Find-ProjectCycleMigration

 view release on metacpan or  search on metacpan

lib/File/Find/ProjectCycleMigration.pm  view on Meta::CPAN

    my $backupRoot  = $backupFolderPath . '/' . $1;

    my $refchangedFiles       = [];
    my $totalFileChangedCount = 0;

    sub fileFilterQ ($) {
        my $fileName = $_[0];
        next if ( $fileName =~ /(svn|\.svn)/ig );

        if ( -f $fileName ) {
			#print "processing files:  $fileName\n";
            return 1;
        }
    }

    # go through each file, accumulate a hash.
    sub processFile {
        my $currentFile     = $File::Find::name;
        my $currentDir      = $File::Find::dir;
        my $previousDir     = $`;
        my $lastDir         = $1;
        my $currentFileName = $_;
        if ( not fileFilterQ($currentFile) ) {
            # fileFilterQ It returns true in case of file, false in case of  dir, not a text file.
            return 1;
        }
        # open file. Read in the whole file.
        if( not( open FILE, "<$currentFile" ) ) {
            die("Error opening file: $!");
        }
        my $wholeFileString;
        { local $/ = undef; $wholeFileString = <FILE>; };
        if ( not( close(FILE) ) ) { die("Error closing file: $!"); }

        # do the replacement.
        my $replaceCount = 0;
        foreach my $key1 ( keys %findReplaceH ) {
            my $pattern = ( $useRegexQ ? $key1 : quotemeta($key1) );
            $replaceCount = $replaceCount + ( $wholeFileString =~ s/$pattern/$findReplaceH{$key1}/g );
        }
        if ( $replaceCount > 0 ) {    # replacement has happened
            push( @$refchangedFiles, $currentFile );
            $totalFileChangedCount++;

            # do backup make a directory in the backup path, make a backup copy.
            my $pathAdd = $currentDir;
            $pathAdd =~ s[$folderPath][];
            mkpath( "$backupRoot/$pathAdd", 0, 0777 );
            copy( $currentFile, "$backupRoot/$pathAdd/$currentFileName" ) or die "error: file copying file failed on $currentFile\n$!";

            # write to the original and  get the file mode.
            my ( $mode, $uid, $gid ) = ( stat($currentFile) )[ 2, 4, 5 ];

            # write out a new file.
            if ( not( open OUTFILE, ">$currentFile" ) ) { die("Error opening file: $!"); }
            print OUTFILE $wholeFileString;
            if ( not( close(OUTFILE) ) ) { die("Error closing file: $!"); }

            # set the file mode.
            chmod( $mode, $currentFile );
            chown( $uid, $gid, $currentFile );

            open( FILE, ">>$logFile" ) || die "cann't open the $!\n";
            print FILE "---------------------------------------------\n";
            print FILE "$replaceCount replacements made at\n";
            print FILE "$currentFile\n";
            close FILE;
        }
    }
    find( \&processFile, $folderPath );
    open( FILE, ">>$logFile" ) || die "cann't open the $!\n";
    print FILE "--------------------------------------------\n";
    print FILE "Total changed files -> $totalFileChangedCount\n";

    if ( scalar @$refchangedFiles > 0 ) {
        print FILE "\nFollowing files are changed:\n";
        print FILE Dumper($refchangedFiles);
    }
    close FILE;
}
sub Usage {
        print<<'EOF';
        Usage:  
            #!/usr/bin/perl
            use File::Find::ProjectCycleMigration;
            my $config = {year=>2011, srcdir=>'/home/uid/foo/project2011'};
            FindReplace($config);
            
        File::Find::ProjectCycleMigration is to convert a project from one cycle to next. The Script scans
        the code in provided path <srcdir> and auto generates a list of possible replacements
        required for moving the code to the next cycle.
          Once you run the script from command line it shows you the list of possible
        replacements in your specified folder and prompts you to confirm or selectively
        remove some of the auto generated list of replacements. enter a name of a
        replacement key to remove it from the list of replacements or type yes or no to
        continue or abort.

        --year=<year>    replace <year> with 4-digits of current cycle year.
                       For example if you are moving the code base from 2011 to 2012 cycle
                       replace <year> with 2011.
                       Required
	    --srcdir=<srcdir> replace <srcdir> with the absolute path of the directory where the replacement
                       should be made. Also script automatically backs up the current code in 'Back-up Directory' 
		               and for logs it creates a 'Log Directory' at one level above the 'Current Directory'.
                       Required
EOF
}
sub separator{
    my ($length, $symbol) = @_;
        print "\t";foreach(1..$length){print $symbol}; print "\n";
}
sub textFormat{
    my ($limit, $text) = @_;
    my $ln = length($text);
    my $sp=0;
    while($ln>0){print "\t",  (substr($text,$sp, $limit)) . "\n"; $sp += $limit;$ln -=$limit;}
}    

1;

__END__



( run in 1.511 second using v1.01-cache-2.11-cpan-71847e10f99 )