public IPath InitPath(string rootPath, IPath path) { //Computes absolute path path = ComputeAbsolute(rootPath, path); // If path does not exist a new file or directory will be created if (path.IsDirectory) { if (!_fs.Directory.Exists(path.AbsolutePath)) { _fs.Directory.CreateDirectory(path.AbsolutePath); _log.Verbose("Created directory at {absolutePath}", path.AbsolutePath); } } else { if (!_fs.File.Exists(path.AbsolutePath)) { _fs.File.Create(path.AbsolutePath).Close(); _log.Verbose("Created file at {absolutePath}", path.AbsolutePath); } } // Every path where RecursiveInit is true will be initialized foreach (var child in path.Children.Where(child => child.RecursiveInit)) { InitPath(path.AbsolutePath, child); } return(path); }
/// <summary> /// Creates a new path. It will not be initialized /// </summary> /// <param name="relativePath">relative path of path. This will be used with a root path to compute an absolute path</param> /// <param name="parent">the parent path that will be added to the front</param> /// <param name="isDirectory"> /// if true the path will be a directory and create one. Otherwise it would be a file and create /// one /// </param> /// <param name="recursiveInit">if true the path will be initialized by its parent</param> public Path(string relativePath = "", IPath parent = null, bool isDirectory = true, bool recursiveInit = true) { RelativePath = relativePath; Parent = parent; IsDirectory = isDirectory; RecursiveInit = recursiveInit; //Add to parent Children = new List <IPath>(); parent?.Children.Add(this); }
public StreamReader FileReader(IPath path) { // Validate file if (!Validate(path)) { return(null); } // If path is file then return stream reader if (!path.IsDirectory) { return(new StreamReader(_fs.File.OpenRead(path.AbsolutePath))); } //Else path is a directory = return null _log.Warning("Path {absolutePath} is not a file. It can not be read", path.AbsolutePath); return(null); }
public bool Validate(IPath path) { // Test if absolute path is null then path is not valid if (path.AbsolutePath == null) { _log.Warning("Path {relativePath} is not valid: No absolute path was initialized", path.RelativePath); return(false); } //Test if path exists if not path is not valid if (!Exists(path)) { return(false); } _log.Verbose("Path {absolutePath} is valid", path.AbsolutePath); return(true); }
public IEnumerable <string> GetFiles(IPath path, string searchPattern = "*") { //Validate path if (!Validate(path)) { return(null); } //If path is a directory get the children paths if (path.IsDirectory) { return(_fs.Directory.GetFileSystemEntries(path.AbsolutePath, searchPattern)); } //If it's an file return null _log.Warning("Path {absolutePath} is not a directory. Can not get files", path.AbsolutePath); return(null); }
/*public IPath Path(string relativePath = "", IPath parent = null, bool isDirectory = true, * bool recursiveInit = true) * { * //Creates new path * var path = new Path(relativePath, parent, isDirectory, recursiveInit); * * //Add children to parent * parent?.Children.Add(path); * * return path; * }*/ public IPath ComputeAbsolute(string rootPath, IPath path) { // Check if absolute path is already computed and return it if (path.AbsolutePath != null) { _log.Warning("Path {absolutePath} is already computed", path.AbsolutePath); return(path); } // Combines root path with relative path and gets the full path (absolute) var absolutePath = _fs.Path.GetFullPath( _fs.Path.Combine(rootPath, path.RelativePath)); _log.Debug("Resolve path {relativePath} to {absolutePath}", path.RelativePath, absolutePath); path.AbsolutePath = absolutePath; return(path); }
public void Delete(IPath path) { // Validate path if (!Validate(path)) { return; } //Delete file or directory if (path.IsDirectory) { _log.Verbose("Deleting directory {absolutePath}", path.AbsolutePath); _fs.Directory.Delete(path.AbsolutePath); } else { _log.Verbose("Deleting file {absolutePath}", path.AbsolutePath); _fs.File.Delete(path.AbsolutePath); } }
public bool Exists(IPath path) { // True if directory of file exists return(path.IsDirectory ? _fs.Directory.Exists(path.AbsolutePath) : _fs.File.Exists(path.AbsolutePath)); }