private void LaunchBrowser(IBrowserLauncher browserLauncher, IDirectoryAccessor directoryAccessor, Uri uri) { if (StartupOptions.Uri != null && !StartupOptions.Uri.IsAbsoluteUri) { uri = new Uri(uri, StartupOptions.Uri); } else if (StartupOptions.Uri == null) { var readmeFile = FindReadmeFileAtRoot(); if (readmeFile != null) { uri = new Uri(uri, readmeFile.ToString()); } } browserLauncher.LaunchBrowser(uri); RelativeFilePath FindReadmeFileAtRoot() { var files = directoryAccessor .GetAllFilesRecursively() .Where(f => StringComparer.InvariantCultureIgnoreCase.Compare(f.FileName, "readme.md") == 0 && IsRoot(f.Directory)) .ToList(); return(files.FirstOrDefault()); } bool IsRoot(RelativeDirectoryPath path) { var isRoot = path == null || path == RelativeDirectoryPath.Root; return(isRoot); } }
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); }
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); }
private void LaunchBrowser(IBrowserLauncher browserLauncher, IDirectoryAccessor directoryAccessor) { var processName = Process.GetCurrentProcess().ProcessName; var uri = processName == "dotnet" || processName == "dotnet.exe" ? new Uri("http://localhost:4242") : new Uri("http://localhost:5000"); if (StartupOptions.Uri != null && !StartupOptions.Uri.IsAbsoluteUri) { uri = new Uri(uri, StartupOptions.Uri); } else if (StartupOptions.Uri == null) { var readmeFile = FindReadmeFileAtRoot(); if (readmeFile != null) { uri = new Uri(uri, readmeFile.ToString()); } } browserLauncher.LaunchBrowser(uri); RelativeFilePath FindReadmeFileAtRoot() { var files = directoryAccessor.GetAllFilesRecursively().Where(f => (StringComparer.InvariantCultureIgnoreCase.Compare(f.FileName, "readme.md") == 0) && IsRoot(f.Directory)).ToList(); return(files.FirstOrDefault()); } bool IsRoot(RelativeDirectoryPath path) { var isRoot = path == null || path == RelativeDirectoryPath.Root; return(isRoot); } }