private IReadOnlyList <IFileChange> GetFileChangesInternal(IEngineEnvironmentSettings environmentSettings, IDirectory sourceDir, string targetDir, IGlobalRunSpec spec) { EngineConfig cfg = new EngineConfig(environmentSettings, EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection); IProcessor fallback = Processor.Create(cfg, spec.Operations); List <IFileChange> changes = new List <IFileChange>(); List <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors = CreateFileGlobProcessors(sourceDir.MountPoint.EnvironmentSettings, spec); foreach (IFile file in sourceDir.EnumerateFiles("*", SearchOption.AllDirectories)) { string sourceRel = file.PathRelativeTo(sourceDir); string fileName = Path.GetFileName(sourceRel); if (spec.IgnoreFileNames.Contains(fileName)) { // The placeholder file should never get copied / created / processed. It just causes the dir to get created if needed. // So this happens before all the include / exclude / copy checks. CreateTargetDir(environmentSettings, sourceRel, targetDir, spec); continue; } 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) { if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); if (environmentSettings.Host.FileSystem.FileExists(targetPath)) { changes.Add(new FileChange(targetRel, ChangeKind.Overwrite)); } else { changes.Add(new FileChange(targetRel, ChangeKind.Create)); } } break; } } } return(changes); }
private void ProcessFile(IFile sourceFile, string sourceRel, string targetDir, IGlobalRunSpec spec, IProcessor fallback, IEnumerable <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors, IReadOnlyList <IOperationProvider> locOperations) { IProcessor runner = fileGlobProcessors.FirstOrDefault(x => x.Key.IsMatch(sourceRel)).Value ?? fallback; if (runner == null) { throw new InvalidOperationException("At least one of [runner] or [fallback] cannot be null"); } if (locOperations != null) { runner = runner.CloneAndAppendOperations(locOperations); } if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); //TODO: Update context with the current file & such here bool customBufferSize = TryGetBufferSize(sourceFile, out int bufferSize); bool customFlushThreshold = TryGetFlushThreshold(sourceFile, out int flushThreshold); string fullTargetDir = Path.GetDirectoryName(targetPath); sourceFile.MountPoint.EnvironmentSettings.Host.FileSystem.CreateDirectory(fullTargetDir); try { using (Stream source = sourceFile.OpenRead()) using (Stream target = sourceFile.MountPoint.EnvironmentSettings.Host.FileSystem.CreateFile(targetPath)) { if (!customBufferSize) { runner.Run(source, target); } else { if (!customFlushThreshold) { runner.Run(source, target, bufferSize); } else { runner.Run(source, target, bufferSize, flushThreshold); } } } } catch (Exception ex) { throw new ContentGenerationException($"Error while processing file {sourceFile.FullPath}", ex); } }
private void ProcessFile(IFile sourceFile, string sourceRel, string targetDir, IGlobalRunSpec spec, IProcessor fallback, IEnumerable <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors) { IProcessor runner = fileGlobProcessors.FirstOrDefault(x => x.Key.IsMatch(sourceRel)).Value ?? fallback; if (runner == null) { throw new InvalidOperationException("At least one of [runner] or [fallback] cannot be null"); } if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); //TODO: Update context with the current file & such here bool customBufferSize = TryGetBufferSize(sourceFile, out int bufferSize); bool customFlushThreshold = TryGetFlushThreshold(sourceFile, out int flushThreshold); string fullTargetDir = Path.GetDirectoryName(targetPath); _fileSystem.CreateDirectory(fullTargetDir); try { using (Stream source = sourceFile.OpenRead()) using (Stream target = _fileSystem.CreateFile(targetPath)) { if (!customBufferSize) { runner.Run(source, target); } else { if (!customFlushThreshold) { runner.Run(source, target, bufferSize); } else { runner.Run(source, target, bufferSize, flushThreshold); } } } } catch (TemplateAuthoringException ex) { throw new TemplateAuthoringException($"Template authoring error encountered while processing file {sourceFile.FullPath}: {ex.Message}", ex.ConfigItem, ex); } catch (Exception ex) { throw new ContentGenerationException($"Error while processing file {sourceFile.FullPath}", ex); } }
private static void ProcessFile(Orchestrator self, ITemplateSourceFile sourceFile, string sourceRel, string targetDir, IGlobalRunSpec spec, IProcessor fallback, IReadOnlyDictionary <IPathMatcher, IProcessor> specializations) { IProcessor runner = specializations.FirstOrDefault(x => x.Key.IsMatch(sourceRel)).Value ?? fallback; string targetRel; if (!spec.TryGetTargetRelPath(sourceRel, out targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); //TODO: Update context with the current file & such here int bufferSize, flushThreshold; bool customBufferSize = self.TryGetBufferSize(sourceFile, out bufferSize); bool customFlushThreshold = self.TryGetFlushThreshold(sourceFile, out flushThreshold); string fullTargetDir = Path.GetDirectoryName(targetPath); Directory.CreateDirectory(fullTargetDir); try { using (Stream source = sourceFile.OpenRead()) using (FileStream target = File.Create(targetPath)) { if (!customBufferSize) { runner.Run(source, target); } else { if (!customFlushThreshold) { runner.Run(source, target, bufferSize); } else { runner.Run(source, target, bufferSize, flushThreshold); } } } } catch (Exception ex) { throw new Exception($"Error while processing file {sourceFile.FullPath}.\nCheck InnerException for details", ex); } }
private static string CreateTargetDir(IEngineEnvironmentSettings environmentSettings, string sourceRel, string targetDir, IGlobalRunSpec spec) { if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); string fullTargetDir = Path.GetDirectoryName(targetPath); environmentSettings.Host.FileSystem.CreateDirectory(fullTargetDir); return(targetPath); }
private static string CreateTargetDir(IPhysicalFileSystem fileSystem, string sourceRel, string targetDir, IGlobalRunSpec spec) { if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); string fullTargetDir = Path.GetDirectoryName(targetPath); fileSystem.CreateDirectory(fullTargetDir); return(targetPath); }
private static string CreateTargetDir(string sourceRel, string targetDir, IGlobalRunSpec spec) { string targetRel; if (!spec.TryGetTargetRelPath(sourceRel, out targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); string fullTargetDir = Path.GetDirectoryName(targetPath); Directory.CreateDirectory(fullTargetDir); return(targetPath); }
private static void RunInternal(Orchestrator self, ITemplateSourceFolder sourceDir, string targetDir, IGlobalRunSpec spec) { EngineConfig cfg = new EngineConfig(EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection); IProcessor fallback = Processor.Create(cfg, spec.Operations); Dictionary <IPathMatcher, IProcessor> specializations = spec.Special .ToDictionary( x => x.Key, x => { IReadOnlyList <IOperationProvider> operations = x.Value.GetOperations(spec.Operations); EngineConfig config = new EngineConfig(EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection); IProcessor processor = Processor.Create(config, operations); return(processor); }); foreach (ITemplateSourceFile file in sourceDir.EnumerateFiles("*", SearchOption.AllDirectories)) { string sourceRel = file.PathRelativeTo(sourceDir); 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 (!copy) { ProcessFile(self, file, sourceRel, targetDir, spec, fallback, specializations); } else { string targetRel; if (!spec.TryGetTargetRelPath(sourceRel, out targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); string fullTargetDir = Path.GetDirectoryName(targetPath); Directory.CreateDirectory(fullTargetDir); using (Stream sourceStream = file.OpenRead()) using (Stream targetStream = File.Create(targetPath)) { sourceStream.CopyTo(targetStream); } } } break; } } } }
private IReadOnlyList <IFileChange2> GetFileChangesInternal(IEngineEnvironmentSettings environmentSettings, IDirectory sourceDir, string targetRoot, string targetDir, IGlobalRunSpec spec) { List <IFileChange2> changes = new List <IFileChange2>(); string fullTargetPath = Path.Combine(targetRoot, targetDir); 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) { if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(fullTargetPath, targetRel); string targetRelativePath = targetDir.CombinePaths(targetRel).TrimStart('/'); string sourceRelativePath = file.FullPath.CombinePaths().TrimStart('/'); if (checkingDirWithPlaceholderFile) { targetPath = Path.GetDirectoryName(targetPath); if (environmentSettings.Host.FileSystem.DirectoryExists(targetPath)) { changes.Add(new FileChange(sourceRelativePath, targetRelativePath, ChangeKind.Overwrite)); } else { changes.Add(new FileChange(sourceRelativePath, targetRelativePath, ChangeKind.Create)); } } else if (environmentSettings.Host.FileSystem.FileExists(targetPath)) { changes.Add(new FileChange(sourceRelativePath, targetRelativePath, ChangeKind.Overwrite)); } else { changes.Add(new FileChange(sourceRelativePath, targetRelativePath, ChangeKind.Create)); } } break; } } } return(changes); }
private IReadOnlyList <IFileChange2> GetFileChangesInternal(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 <IFileChange2> changes = new List <IFileChange2>(); 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) { if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel)) { targetRel = sourceRel; } string targetPath = Path.Combine(targetDir, targetRel); if (checkingDirWithPlaceholderFile) { targetPath = Path.GetDirectoryName(targetPath); targetRel = Path.GetDirectoryName(targetRel); if (_fileSystem.DirectoryExists(targetPath)) { changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Overwrite)); } else { changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Create)); } } else if (_fileSystem.FileExists(targetPath)) { changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Overwrite)); } else { changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Create)); } } break; } } } return(changes); }