-
Notifications
You must be signed in to change notification settings - Fork 1
File System
Working with files is a common task for a typical build script. That's why Rosalia has special set of helper classes to make work with File system more comfortable.
Two classes, IFile
and IDirectory
forms the core or Rosalia's FS library. They intended to incapsulate information for a file or a directory respectively.
The starting point is getting an instance of IFile
or IDirectory
that represents your File System. Both IFile
and IDirectory
could be instantiated by implicit conversion from string representing the path:
IDirectory src = "C:/Projects/MyProject/Src";
IFile solutionFile = "C:/Projects/MyProject/Src/MyProject.sln";
When you work with Rosalia's Workflow you already have an instance of IDirectory
that points to the work directory:
protected override void RegisterTasks()
{
/*...*/
IDirectory workDirectory = this.WorkDirectory;
// work with work directory
/*...*/
}
Once you have an instance of IDirectory
, you can access child directories with /
operator:
IDirectory src = WorkDirectory/"Src";
The same applies to files -- you can access a file from parent directory using /
operator:
IDirectory src = WorkDirectory/"Src";
IFile solutionFile = WorkDirectory/"Src"/"MyProject.sln";
/
operator is a handy equivalent of Path.Combine
and it's preferred to use this operator instead of hard-coding a separator character ('/' or ''):
IFile solutionFile = WorkDirectory/"Src"/"MyProject.sln"; // Good
IFile solutionFile = WorkDirectory/"Src/MyProject.sln"; // Bad
Once you've got an instance of IFile
or IDirectory
, you can use it to achieve your actual File-System-related tasks.
Both IFile
and IDirectory
classes have a set of methods for typical FS operations. Please see IFile members and IDirectory members for details.
While IFile
and IDirectory
classes form the basis to work with FS, there is one more important class called FileList. This class holds not only the list of files but the base directory that could be used to build relative path for recursive operations. See FileList for details.
Writes string to file.
IFile myfile = "C:/Data/myfile.txt";
myfile.WriteStringToFile("Hello World!");
Reads string content from file.
IFile myfile = "C:/Data/myfile.txt";
string content = myfile.ReadAllText(); // "Hello World"
Returns FileList that contains all files within the directory (including subdirectories). This method is used when you want to do recursive operations with files (copy a directory, zip it all pack to NuGet package etc). See FileList for details.
Returns an IEnumerable<IDirectory>
that contains the directory itself and all it's parents up to the tree. It might be useful if you are searching for a particular parent directory.
IDirectory dir = "C:/A/B/C";
IDirectory[] parents = dir.GetParentsChain().ToArray(); // ["C:/A/B/C", "C:/A/B", "C:/A", "C:/"]
Returns a closest directory (from GetParentsChain()
list) that matches provided condition.
IDirectory dir = "C:/Projects/MyProject/Src/Web/bin/Debug";
IDirectory src = dir.Closest(d => d.Name == "Src"); // "C:/Projects/MyProject/Src"
Writing Tasks
- Creating a Workflow
- Defining Tasks
- Share State accross Tasks
- Mastering Dependencies
- Using result transformers
- Using Subflows to organize tasks
- Tasks Preconditions
- Recovering failure results
- Declaring dynamic tasks
- Creating Custom Tasks
Running Tasks
API
Tasklib