public Stream CreateFile(string path) { if (!IsPathInCone(path, out string processedPath)) { return(_basis.CreateFile(path)); } path = processedPath; string rel = path.Substring(_root.FullPath.Length).Trim('/', '\\'); string[] parts = rel.Split('/', '\\'); FileSystemDirectory currentDir = _root; for (int i = 0; i < parts.Length - 1; ++i) { FileSystemDirectory dir; if (!currentDir.Directories.TryGetValue(parts[i], out dir)) { dir = new FileSystemDirectory(parts[i], Path.Combine(currentDir.FullPath, parts[i])); currentDir.Directories[parts[i]] = dir; } currentDir = dir; } FileSystemFile targetFile; if (!currentDir.Files.TryGetValue(parts[parts.Length - 1], out targetFile)) { targetFile = new FileSystemFile(parts[parts.Length - 1], Path.Combine(currentDir.FullPath, parts[parts.Length - 1])); currentDir.Files[parts[parts.Length - 1]] = targetFile; } return(targetFile.OpenWrite()); }
internal static void WriteObject(this IPhysicalFileSystem fileSystem, string path, object obj) { using (var fileStream = fileSystem.CreateFile(path)) using (var textWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8)) using (var jsonWriter = new JsonTextWriter(textWriter)) { var serializer = new JsonSerializer(); serializer.Serialize(jsonWriter, obj); } }
private void RunInternal(IDirectory sourceDir, string targetDir, IGlobalRunSpec spec) { EngineConfig cfg = new EngineConfig(_logger, EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection); IProcessor fallback = Processor.Create(cfg, spec.Operations); List <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors = CreateFileGlobProcessors(_logger, spec); foreach (IFile file in sourceDir.EnumerateFiles("*", SearchOption.AllDirectories)) { string sourceRel = file.PathRelativeTo(sourceDir); string fileName = Path.GetFileName(sourceRel); bool checkingDirWithPlaceholderFile = false; if (spec.IgnoreFileNames.Contains(fileName)) { // The placeholder file should never get copied / created / processed. It just causes the dir to get created if needed. // The change checking / reporting is different, setting this variable tracks it. checkingDirWithPlaceholderFile = true; } foreach (IPathMatcher include in spec.Include) { if (include.IsMatch(sourceRel)) { bool excluded = false; foreach (IPathMatcher exclude in spec.Exclude) { if (exclude.IsMatch(sourceRel)) { excluded = true; break; } } if (!excluded) { bool copy = false; foreach (IPathMatcher copyOnly in spec.CopyOnly) { if (copyOnly.IsMatch(sourceRel)) { copy = true; break; } } if (checkingDirWithPlaceholderFile) { CreateTargetDir(_fileSystem, sourceRel, targetDir, spec); } else if (!copy) { ProcessFile(file, sourceRel, targetDir, spec, fallback, fileGlobProcessors); } else { string targetPath = CreateTargetDir(_fileSystem, sourceRel, targetDir, spec); using (Stream sourceStream = file.OpenRead()) using (Stream targetStream = _fileSystem.CreateFile(targetPath)) { sourceStream.CopyTo(targetStream); } } } break; } } } }