Ruby Rake Files And Directories manipulation tasks

by Indra 10. November 2011 05:54

These are file and directory manipulation tasks that I use during continuous integration process at MAP Teams(a really cool .NET shop in Jakarta, Indonesia).

List of tasks:
  1. Change Inheritance of some classes before compiling the project(solution)
  2. Copy all files named "copy of app.config" to "app.config" or web.config before compiling solution.
  3. Change version number in assemblyInfo file before committing source code back to repository.
  4. Deleting and creating temp directory hierarchy.
  5. Will be added later.

1. Change Inheritance of some classes before compiling the project(solution)

task :Change_Class_Inheritance do

   classFiles =  File.join(LOC_WORKING_DIRECTORY,"another\\folders/hierarchy","../foldersA/folderB\\MapTeamsPolls/Admin*.aspx.cs")
	# glob with the pattern for  files with " Admin*.aspx.cs" in the directory location
	fileCollection = Dir[classFiles] 
	fileCollection.each do|sourceFile|
		#read file content to string variable
		text = File.read( sourceFile)
		
		#change the line matching the pattern
		text = text.gsub(/(^\s*public partial class .*:\s*)(.*)/, '\1UmbracoEnsuredPage')		
		
		#write the modified content back to file
		File.open(sourceFile, 'w') { |f| f.write(text) }
			
	end
End
    
(Back to index.)

2. Copy all files named "copy of app.config" to "app.config" or web.config before compiling solution.

def CommonUtils.rename_config_files(sourcePath)
	
    #replace backslash to forward slash; glob doesn't work with backslash!!!
    sourceFolder = sourcePath.gsub(/\\/, '/')
    pattern1 = sourceFolder+'/**/copy of *.config'   
    pattern2 = sourceFolder+'/**/*copy.config'
	
    #search recursive directory below solution folder; search for "copy of " files ; remove the "copy of "
    Dir[pattern1, pattern2].each { |configFile| 		
	    FileUtils.cp(configFile, configFile.gsub(/copy of | - Copy/i,'') , :verbose=>true )
    }
end
    
(Back to index.)

3. Change version number in assemblyInfo file before committing source code back to repository

### sourceFolder = project folder containing assemblyInfo; multiple folder
### "1.2.4.5"
def CommonUtils.update_assemblyInfo(sourceFolder, version)

	sourceFolder = sourceFolder.gsub(/\\/, '/') #replace backslash ke forward slash; glob tidak bekerja kalau pakai backslash!!!
	
	pattern1 = sourceFolder+"/**/Properties/AssemblyInfo.cs"
	
	fileCollection = Dir[pattern1]
	
	raise "No Assembly info found in (#{sourceFolder}) ; please check folder" if fileCollection.count == 0
	
	fileCollection.each { |configFile| 	
		text = File.read(configFile)
		newContent = text.gsub!(/Assembly(|File)Version\(\".*\"\)/, "Assembly\\1Version(\"#{version}\")")
	 
		File.open(configFile, 'w') { |f| f.write(newContent) }
	}
end
    
(Back to index.)

4. Deleting and creating temp directory hierarchy.

#target can be folders hierarchy (d:\\folderA/folderB\\folderC/folderD)
def RecreateEmptyFolder(target)
    FileUtils.rm_rf(target, :verbose=>true) if (Dir[target] != [] and Dir[target] != nil )
		
    #make sure no other process is holding the folder
    #sometimes will fail as if rm_rf still not finished while mkdir_p is working
    sleep 3 
    FileUtils.mkdir_p target , :verbose=>true
end
    
(Back to index.)

Tags: ,

Continuous Integration | Snippets | Ruby