Cake common file and path operations
In building automation tasks, many operations are dealing with files, such files are packaged, file compression, file archiving, file transfer, directory cleanup, etc. This section describes some cake in common file operation method
Cake relative path
Automated done classmates may know, when using a relative path, relative path in the test script often and formal environment is not the same, the path of the host environment, often resulting in obviously good test script eventually the emergence of various routing problem. But If the path to absolute path they often result in written scripts are not universal, each move to a new project will need to modify the script file, modify the script to script often means that the risk of error. and Cake relative path mechanism is precisely to solve the problem relative path depending upon the host environment changes, that is to say as long as the directory or file relative relationship is fixed, the relative path will not be a problem.
Relative paths are in Cake in 以build.ps1文件为参照而形成的相对路径
, so long as the file and directory structure unchanged, cake in the relative path will have no problems.
Turn absolute path relative path
There may be some operations may not support relative paths, this time should be converted into an absolute path to a relative path, Cake Lane provides a MakeAbsolute
method of receiving a DirectoryPath or FilePath parameter to convert the relative path to the directory / file absolute path
The following example of the current path (the path build.ps1) converted to absolute path
var path=MakeAbsolute(new DirectoryPath("./"));
Note that if you want the absolute and relative path of the file path, you need to pass a new Filepath ( "filename") objects.
File Operations
Rename the file
Rename the file as plain and by renaming method is to place a file copy, get rid of the name, can be achieved by CopyFile method in the Cake
CopyFile("./bin/debug/buildDemo.pdb","./bin/debug/demo.pdb");
The above example buildDemo.pdb rename demo.pdb
Copy the folder
Copy the folder can be CopyDirectory
specified to copy the folder and can be copied to a directory.
Copy the folder did not rule out the option, but a complete copy to a folder to another place
Copy the files to a folder
Can CopyFiles()
copy the method file to a folder that receives a IEnumerable type options, contains the file path, we can GetFiles()
get all the files methods, and then filter out specific types through the where, the last copy specific files to the specified folder
The method of copying files and folders are beginning to copy, if you want to move a file or folder, the method is generally based on
Move
the beginning
File compression / decompression file
The file may be compressed by methods Zip, and may specify a set FilePath IEnumerable type, specify only a certain compressed file
Correspondingly, the decompression method
Unzip
Delete files / folders
There DeleteDirectory(s)
and DeleteFile(s)
methods
Empty Folder
CleanDirectory(s)
method
Whether the directory / file exists
DirectoryExists/FileExists
method
In practice often need to detect folder exists, if it does not exist is created, then you can use the
EnsureDirectoryExists
method
Calculating relative path
Sometimes we get to the location of two files or directories, then how you want to get the relative position between them, can be DirectoryPath
an object in the GetRelativePath
calculation, should be noted that the two paths are calculated relative path must be an absolute path , (if it is a relative path, we can easily be converted to absolute paths through MakeAbsolute method)