private IEnumerable <IDocument> Execute(IDocument input, string path, IExecutionContext context) { if (path != null) { path = System.IO.Path.Combine(context.InputFolder, PathHelper.NormalizePath(path)); path = System.IO.Path.Combine(System.IO.Path.GetFullPath(System.IO.Path.GetDirectoryName(path)), System.IO.Path.GetFileName(path)); string fileRoot = System.IO.Path.GetDirectoryName(path); if (fileRoot != null && Directory.Exists(fileRoot)) { return(Directory.EnumerateFiles(fileRoot, System.IO.Path.GetFileName(path), _searchOption) .AsParallel() .Where(x => (_predicate == null || _predicate(x)) && (_extensions == null || _extensions.Contains(System.IO.Path.GetExtension(x)))) .Select(file => { Trace.Verbose("Read file {0}", file); string relativePath = PathHelper.GetRelativePath(context.InputFolder, file); return context.GetDocument(input, file, SafeIOHelper.OpenRead(file), new MetadataItems { { Keys.SourceFileRoot, fileRoot }, { Keys.SourceFileBase, System.IO.Path.GetFileNameWithoutExtension(file) }, { Keys.SourceFileExt, System.IO.Path.GetExtension(file) }, { Keys.SourceFileName, System.IO.Path.GetFileName(file) }, { Keys.SourceFileDir, System.IO.Path.GetDirectoryName(file) }, { Keys.SourceFilePath, file }, { Keys.SourceFilePathBase, PathHelper.RemoveExtension(file) }, { Keys.RelativeFilePath, relativePath }, { Keys.RelativeFilePathBase, PathHelper.RemoveExtension(relativePath) }, { Keys.RelativeFileDir, System.IO.Path.GetDirectoryName(relativePath) } }); })); } } return(Array.Empty <IDocument>()); }
private IEnumerable <IDocument> Execute(IDocument input, string path, IExecutionContext context) { if (path != null) { bool isPathUnderInputFolder = false; if (!System.IO.Path.IsPathRooted(path)) { path = PathHelper.CombineToFullPath(context.InputFolder, path); isPathUnderInputFolder = path.StartsWith(context.InputFolder); } string fileRoot = System.IO.Path.GetDirectoryName(path); if (fileRoot != null && Directory.Exists(fileRoot)) { return(Directory.EnumerateFiles(fileRoot, System.IO.Path.GetFileName(path), _searchOption) .AsParallel() .Where(x => (_predicate == null || _predicate(x)) && (_withoutExtensions == null || !_withoutExtensions.Contains(System.IO.Path.GetExtension(x)))) .Select(file => { string destination = null; if (_destinationPath == null) { if (file != null) { string relativePath = isPathUnderInputFolder ? PathHelper.GetRelativePath(context.InputFolder, System.IO.Path.GetDirectoryName(file)) : ""; destination = System.IO.Path.Combine(context.OutputFolder, relativePath, System.IO.Path.GetFileName(file)); } } else { destination = _destinationPath(file); } if (!string.IsNullOrWhiteSpace(destination)) { string destinationDirectory = System.IO.Path.GetDirectoryName(destination); if (destinationDirectory != null && !Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } SafeIOHelper.Copy(file, destination, true); Trace.Verbose("Copied file {0} to {1}", file, destination); return context.GetDocument(input, new MetadataItems { { Keys.SourceFilePath, file }, { Keys.DestinationFilePath, destination } }); } return null; }) .Where(x => x != null)); } } return(Array.Empty <IDocument>()); }