private static void AddProjectOption( Command command, IDirectoryAccessor directoryAccessor, string projectFileExtension) { var projectOptionArgument = new Argument <FileInfo>( (SymbolResult result, out FileInfo projectFile) => { var projectPath = new RelativeFilePath(result.Tokens.Select(t => t.Value).Single()); if (directoryAccessor.FileExists(projectPath)) { projectFile = directoryAccessor.GetFullyQualifiedFilePath(projectPath); return(true); } result.ErrorMessage = $"Project not found: {projectPath.Value}"; projectFile = null; return(false); }) { Name = "project", Arity = ArgumentArity.ExactlyOne }; projectOptionArgument.SetDefaultValue(() => { var rootDirectory = directoryAccessor.GetFullyQualifiedPath(new RelativeDirectoryPath(".")); var projectFiles = directoryAccessor.GetAllFilesRecursively() .Where(file => directoryAccessor.GetFullyQualifiedPath(file.Directory).FullName == rootDirectory.FullName && file.Extension == projectFileExtension) .ToArray(); if (projectFiles.Length == 1) { return(directoryAccessor.GetFullyQualifiedPath(projectFiles.Single())); } return(null); }); var projectOption = new Option("--project") { Argument = projectOptionArgument }; command.Add(projectOption); }
public static bool IsSubDirectoryOf(this IDirectoryAccessor potentialChild, IDirectoryAccessor directory) { var child = potentialChild.GetFullyQualifiedPath(_here).FullName; var parent = directory.GetFullyQualifiedPath(_here).FullName; return(IsBaseOf(parent, child, selfIsChild: false)); }
public static bool IsChildOf(this FileSystemInfo file, IDirectoryAccessor directory) { var parent = directory.GetFullyQualifiedPath(_here).FullName; var child = Path.GetDirectoryName(file.FullName); child = child.EndsWith('/') || child.EndsWith('\\') ? child : child + "/"; return(IsBaseOf(parent, child, selfIsChild: true)); }
private static void AddProjectOption( Command csharp, IDirectoryAccessor directoryAccessor) { var projectOptionArgument = new Argument <FileInfo>(result => { var projectPath = new RelativeFilePath(result.Tokens.Select(t => t.Value).Single()); if (directoryAccessor.FileExists(projectPath)) { return(ArgumentResult.Success(directoryAccessor.GetFullyQualifiedPath(projectPath))); } return(ArgumentResult.Failure($"Project not found: {projectPath.Value}")); }) { Name = "project", Arity = ArgumentArity.ExactlyOne }; projectOptionArgument.SetDefaultValue(() => { var rootDirectory = directoryAccessor.GetFullyQualifiedPath(new RelativeDirectoryPath(".")); var projectFiles = directoryAccessor.GetAllFilesRecursively() .Where(file => { return(directoryAccessor.GetFullyQualifiedPath(file.Directory).FullName == rootDirectory.FullName && file.Extension == ".csproj"); }) .ToArray(); if (projectFiles.Length == 1) { return(directoryAccessor.GetFullyQualifiedPath(projectFiles.Single())); } return(null); }); var projectOption = new Option("--project", argument: projectOptionArgument); csharp.Add(projectOption); }
public static Buffer GetBufferAsync( this AnnotatedCodeBlock block, IDirectoryAccessor directoryAccessor, MarkdownFile markdownFile) { if (block.Annotations is LocalCodeBlockAnnotations localOptions) { var absolutePath = directoryAccessor.GetFullyQualifiedPath(localOptions.SourceFile).FullName; var bufferId = new BufferId(absolutePath, block.Annotations.Region); return(new Buffer(bufferId, block.SourceCode)); } return(null); }
private static void VerifyAnnotationReferences( AnnotatedCodeBlock annotatedCodeBlock, IDirectoryAccessor markdownFileDir, IConsole console, MarkdownProcessingContext context) { Console.ResetColor(); console.Out.WriteLine(" Checking Markdown..."); var diagnostics = annotatedCodeBlock.Diagnostics.ToArray(); var hasDiagnostics = diagnostics.Any(); if (hasDiagnostics) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Green; } if (annotatedCodeBlock.Annotations is LocalCodeBlockAnnotations annotations) { var file = annotations?.SourceFile ?? annotations?.DestinationFile; var fullyQualifiedPath = file != null ? markdownFileDir.GetFullyQualifiedPath(file).FullName : "UNKNOWN"; var project = annotatedCodeBlock.ProjectOrPackageName() ?? "UNKNOWN"; var symbol = hasDiagnostics ? "X" : "✓"; var error = $" {symbol} Line {annotatedCodeBlock.Line + 1}:\t{fullyQualifiedPath} (in project {project})"; if (hasDiagnostics) { context.Errors.Add(error); } console.Out.WriteLine(error); } foreach (var diagnostic in diagnostics) { console.Out.WriteLine($"\t\t{diagnostic}"); } }
private static string WriteTargetFile(string content, RelativeFilePath relativePath, IDirectoryAccessor targetDirectoryAccessor, PublishOptions publishOptions, WriteOutput writeOutput) { var fullyQualifiedPath = targetDirectoryAccessor.GetFullyQualifiedPath(relativePath); targetDirectoryAccessor.EnsureDirectoryExists(relativePath); var targetPath = fullyQualifiedPath.FullName; if (publishOptions.Format == PublishFormat.HTML) { targetPath = Path.ChangeExtension(targetPath, ".html"); } writeOutput(targetPath, content); return(targetPath); }
public static DirectoryInfo GetFullyQualifiedDirectoryPath(this IDirectoryAccessor directoryAccessor, RelativeDirectoryPath relativePath) => (DirectoryInfo)directoryAccessor.GetFullyQualifiedPath(relativePath);
public static FileInfo GetFullyQualifiedFilePath(this IDirectoryAccessor directoryAccessor, RelativeFilePath relativePath) => (FileInfo)directoryAccessor.GetFullyQualifiedPath(relativePath);
public static DirectoryInfo GetFullyQualifiedRoot(this IDirectoryAccessor directoryAccessor) => (DirectoryInfo)directoryAccessor.GetFullyQualifiedPath(new RelativeDirectoryPath("."));
internal async Task <(Dictionary <string, Buffer[]> buffers, Dictionary <string, File[]> files)> GetIncludes(IDirectoryAccessor directoryAccessor) { var buffersToIncludeBySession = new Dictionary <string, Buffer[]>(StringComparer.InvariantCultureIgnoreCase); var contentBuildersByBufferBySession = new Dictionary <string, Dictionary <BufferId, StringBuilder> >(StringComparer.InvariantCultureIgnoreCase); var filesToIncludeBySession = new Dictionary <string, File[]>(StringComparer.InvariantCultureIgnoreCase); var contentBuildersByFileBySession = new Dictionary <string, Dictionary <string, StringBuilder> >(StringComparer.InvariantCultureIgnoreCase); var blocks = await GetNonEditableAnnotatedCodeBlocks(); foreach (var block in blocks) { var sessionId = string.IsNullOrWhiteSpace(block.Annotations.Session) ? "global" : block.Annotations.Session; var filePath = (block.Annotations as LocalCodeBlockAnnotations)?.SourceFile ?? block.Annotations.DestinationFile ?? new RelativeFilePath($"./generated_include_file_{sessionId}.cs"); var absolutePath = directoryAccessor.GetFullyQualifiedPath(filePath).FullName; if (string.IsNullOrWhiteSpace(block.Annotations.Region)) { if (!contentBuildersByFileBySession.TryGetValue(sessionId, out var sessionFileBuffers)) { sessionFileBuffers = new Dictionary <string, StringBuilder>(StringComparer.InvariantCultureIgnoreCase); contentBuildersByFileBySession[sessionId] = sessionFileBuffers; } if (!sessionFileBuffers.TryGetValue(absolutePath, out var fileBuffer)) { fileBuffer = new StringBuilder(); sessionFileBuffers[absolutePath] = fileBuffer; } fileBuffer.AppendLine(block.SourceCode); } else { var bufferId = new BufferId(absolutePath, block.Annotations.Region); if (!contentBuildersByBufferBySession.TryGetValue(sessionId, out var sessionFileBuffers)) { sessionFileBuffers = new Dictionary <BufferId, StringBuilder>(); contentBuildersByBufferBySession[sessionId] = sessionFileBuffers; } if (!sessionFileBuffers.TryGetValue(bufferId, out var bufferContentBuilder)) { bufferContentBuilder = new StringBuilder(); sessionFileBuffers[bufferId] = bufferContentBuilder; } bufferContentBuilder.AppendLine(block.SourceCode); } } foreach (var(sessionId, contentBuildersByBuffer) in contentBuildersByBufferBySession) { buffersToIncludeBySession[sessionId] = contentBuildersByBuffer .Select(contentBuilder => new Buffer( contentBuilder.Key, contentBuilder.Value.ToString()) ).ToArray(); } foreach (var(sessionId, contentBuildersByFile) in contentBuildersByFileBySession) { filesToIncludeBySession[sessionId] = contentBuildersByFile .Select(fileBuffer => new File( fileBuffer.Key, fileBuffer.Value.ToString() ) ).ToArray(); } return(buffersToIncludeBySession, filesToIncludeBySession); }