/// <summary> /// Gets a MetadataReference to a project's output assembly. /// </summary> private async Task <MetadataReference> GetProjectMetadata(string projectFilePath, ImmutableArray <string> aliases, IDictionary <string, string> globalProperties, CancellationToken cancellationToken) { // use loader service to determine output file for project if possible var outputFilePath = await ProjectFileLoader.GetOutputFilePathAsync(projectFilePath, globalProperties, cancellationToken).ConfigureAwait(false); if (outputFilePath != null && File.Exists(outputFilePath)) { if (Workspace.TestHookStandaloneProjectsDoNotHoldReferences) { var documentationService = this.Services.GetService <IDocumentationProviderService>(); var docProvider = documentationService.GetDocumentationProvider(outputFilePath); var metadata = AssemblyMetadata.CreateFromImage(File.ReadAllBytes(outputFilePath)); return(metadata.GetReference( documentation: docProvider, aliases: aliases, display: outputFilePath)); } else { var metadataService = this.Services.GetService <IMetadataService>(); return(metadataService.GetReference(outputFilePath, new MetadataReferenceProperties(MetadataImageKind.Assembly, aliases))); } } return(null); }
/// <summary> /// Gets a MetadataReference to a project's output assembly. /// </summary> private async Task <MetadataReference> GetProjectMetadata(string projectFilePath, ImmutableArray <string> aliases, IDictionary <string, string> globalProperties, CancellationToken cancellationToken) { try { // use loader service to determine output file for project if possible var outputFilePath = await ProjectFileLoader.GetOutputFilePathAsync(projectFilePath, globalProperties, cancellationToken).ConfigureAwait(false); if (outputFilePath != null && File.Exists(outputFilePath)) { var docProvider = this.GetDocumentationProvider(outputFilePath); if (Workspace.TestHookStandaloneProjectsDoNotHoldReferences) { return(new MetadataImageReference( AssemblyMetadata.CreateFromImage(ImmutableArray.Create(File.ReadAllBytes(outputFilePath))), documentation: docProvider, aliases: aliases, display: outputFilePath)); } else { return(new MetadataFileReference(outputFilePath, new MetadataReferenceProperties(MetadataImageKind.Assembly, aliases), docProvider)); } } } catch (System.IO.IOException exception) { this.OnWorkspaceFailed(new ProjectDiagnostic(WorkspaceDiagnosticKind.FileAccessFailure, exception.Message, this.GetOrCreateProjectId(projectFilePath))); } return(null); }
protected ProjectFile(ProjectFileLoader loader, MSB.Evaluation.Project?loadedProject, ProjectBuildManager buildManager, DiagnosticLog log) { _loader = loader; _loadedProject = loadedProject; _buildManager = buildManager; var directory = loadedProject?.DirectoryPath ?? string.Empty; _projectDirectory = PathUtilities.EnsureTrailingSeparator(directory); Log = log; }
public bool TryGetLoaderFromProjectPath(string projectFilePath, DiagnosticReportingMode mode, out IProjectFileLoader loader) { using (_dataGuard.DisposableWait()) { var extension = Path.GetExtension(projectFilePath); if (extension.Length > 0 && extension[0] == '.') { extension = extension.Substring(1); } if (_extensionToLanguageMap.TryGetValue(extension, out var language)) { if (_workspaceServices.SupportedLanguages.Contains(language)) { loader = _workspaceServices.GetLanguageServices(language).GetService <IProjectFileLoader>(); } else { loader = null; _diagnosticReporter.Report(mode, string.Format(WorkspacesResources.Cannot_open_project_0_because_the_language_1_is_not_supported, projectFilePath, language)); return(false); } } else { loader = ProjectFileLoader.GetLoaderForProjectFileExtension(_workspaceServices, extension); if (loader == null) { _diagnosticReporter.Report(mode, string.Format(WorkspacesResources.Cannot_open_project_0_because_the_file_extension_1_is_not_associated_with_a_language, projectFilePath, Path.GetExtension(projectFilePath))); return(false); } } // since we have both C# and VB loaders in this same library, it no longer indicates whether we have full language support available. if (loader != null) { language = loader.Language; // check for command line parser existing... if not then error. var commandLineParser = _workspaceServices .GetLanguageServices(language) .GetService <ICommandLineParserService>(); if (commandLineParser == null) { loader = null; _diagnosticReporter.Report(mode, string.Format(WorkspacesResources.Cannot_open_project_0_because_the_language_1_is_not_supported, projectFilePath, language)); return(false); } } return(loader != null); } }
private bool TryGetLoaderFromProjectPath(string projectFilePath, ReportMode mode, out IProjectFileLoader loader) { using (_dataGuard.DisposableWait()) { // otherwise try to figure it out from extension var extension = Path.GetExtension(projectFilePath); if (extension.Length > 0 && extension[0] == '.') { extension = extension.Substring(1); } string language; if (_extensionToLanguageMap.TryGetValue(extension, out language)) { if (_workspace.Services.SupportedLanguages.Contains(language)) { loader = _workspace.Services.GetLanguageServices(language).GetService <IProjectFileLoader>(); } else { loader = null; this.ReportFailure(mode, string.Format(WorkspacesResources.CannotOpenProjectUnsupportedLanguage, projectFilePath, language)); return(false); } } else { loader = ProjectFileLoader.GetLoaderForProjectFileExtension(_workspace, extension); if (loader == null) { this.ReportFailure(mode, string.Format(WorkspacesResources.CannotOpenProjectUnrecognizedFileExtension, projectFilePath, Path.GetExtension(projectFilePath))); return(false); } } // since we have both C# and VB loaders in this same library, it no longer indicates whether we have full language support available. if (loader != null) { language = loader.Language; // check for command line parser existing... if not then error. var commandLineParser = _workspace.Services.GetLanguageServices(language).GetService <ICommandLineParserService>(); if (commandLineParser == null) { loader = null; this.ReportFailure(mode, string.Format(WorkspacesResources.CannotOpenProjectUnsupportedLanguage, projectFilePath, language)); return(false); } } return(loader != null); } }
protected ProjectFile(ProjectFileLoader loader, MSB.Evaluation.Project loadedProject, ProjectBuildManager buildManager, DiagnosticLog log) { _loader = loader; _loadedProject = loadedProject; _buildManager = buildManager; _projectDirectory = loadedProject != null ? PathUtilities.EnsureTrailingSeparator(loadedProject.DirectoryPath) : null; Log = log; }
private bool TryGetLoaderFromProjectPath(string projectFilePath, ReportMode mode, out IProjectFileLoader loader) { using (_dataGuard.DisposableWait()) { // check to see if we already know the loader if (!_projectPathToLoaderMap.TryGetValue(projectFilePath, out loader)) { // otherwise try to figure it out from extension var extension = Path.GetExtension(projectFilePath); if (extension.Length > 0 && extension[0] == '.') { extension = extension.Substring(1); } string language; if (_extensionToLanguageMap.TryGetValue(extension, out language)) { if (this.Services.SupportedLanguages.Contains(language)) { loader = this.Services.GetLanguageServices(language).GetService <IProjectFileLoader>(); } else { this.ReportFailure(mode, string.Format(WorkspacesResources.CannotOpenProjectUnsupportedLanguage, projectFilePath, language)); return(false); } } else { loader = ProjectFileLoader.GetLoaderForProjectFileExtension(this, extension); if (loader == null) { this.ReportFailure(mode, string.Format(WorkspacesResources.CannotOpenProjectUnrecognizedFileExtension, projectFilePath, Path.GetExtension(projectFilePath))); return(false); } } if (loader != null) { _projectPathToLoaderMap[projectFilePath] = loader; } } return(loader != null); } }
public ProjectFile(ProjectFileLoader loader, MSB.Evaluation.Project loadedProject) { _loader = loader; _loadedProject = loadedProject; }
/// <summary> /// Open a solution file and all referenced projects. /// </summary> public async Task <Solution> OpenSolutionAsync(string solutionFilePath, CancellationToken cancellationToken = default(CancellationToken)) { if (solutionFilePath == null) { throw new ArgumentNullException(nameof(solutionFilePath)); } this.ClearSolution(); var absoluteSolutionPath = this.GetAbsoluteSolutionPath(solutionFilePath, Directory.GetCurrentDirectory()); using (_dataGuard.DisposableWait(cancellationToken)) { this.SetSolutionProperties(absoluteSolutionPath); } VersionStamp version = default(VersionStamp); #if !MSBUILD12 Microsoft.Build.Construction.SolutionFile solutionFile = Microsoft.Build.Construction.SolutionFile.Parse(absoluteSolutionPath); var reportMode = this.SkipUnrecognizedProjects ? ReportMode.Log : ReportMode.Throw; var invalidProjects = new List <ProjectInSolution>(); // seed loaders from known project types using (_dataGuard.DisposableWait(cancellationToken)) { foreach (var project in solutionFile.ProjectsInOrder) { if (project.ProjectType == SolutionProjectType.SolutionFolder) { continue; } var projectAbsolutePath = TryGetAbsolutePath(project.AbsolutePath, reportMode); if (projectAbsolutePath != null) { var extension = Path.GetExtension(projectAbsolutePath); if (extension.Length > 0 && extension[0] == '.') { extension = extension.Substring(1); } var loader = ProjectFileLoader.GetLoaderForProjectFileExtension(this, extension); if (loader != null) { _projectPathToLoaderMap[projectAbsolutePath] = loader; } } else { invalidProjects.Add(project); } } } // a list to accumulate all the loaded projects var loadedProjects = new List <ProjectInfo>(); // load all the projects foreach (var project in solutionFile.ProjectsInOrder) { cancellationToken.ThrowIfCancellationRequested(); if (project.ProjectType != SolutionProjectType.SolutionFolder && !invalidProjects.Contains(project)) { var projectAbsolutePath = TryGetAbsolutePath(project.AbsolutePath, reportMode); if (projectAbsolutePath != null) { IProjectFileLoader loader; if (TryGetLoaderFromProjectPath(projectAbsolutePath, reportMode, out loader)) { // projects get added to 'loadedProjects' as side-effect // never perfer metadata when loading solution, all projects get loaded if they can. var tmp = await GetOrLoadProjectAsync(projectAbsolutePath, loader, preferMetadata : false, loadedProjects : loadedProjects, cancellationToken : cancellationToken).ConfigureAwait(false); } } } } #else SolutionFile solutionFile = null; using (var reader = new StreamReader(absoluteSolutionPath)) { version = VersionStamp.Create(File.GetLastWriteTimeUtc(absoluteSolutionPath)); var text = await reader.ReadToEndAsync().ConfigureAwait(false); solutionFile = SolutionFile.Parse(new StringReader(text)); } var solutionFolder = Path.GetDirectoryName(absoluteSolutionPath); // seed loaders from known project types using (_dataGuard.DisposableWait()) { foreach (var projectBlock in solutionFile.ProjectBlocks) { string absoluteProjectPath; if (TryGetAbsoluteProjectPath(projectBlock.ProjectPath, solutionFolder, ReportMode.Ignore, out absoluteProjectPath)) { var loader = ProjectFileLoader.GetLoaderForProjectTypeGuid(this, projectBlock.ProjectTypeGuid); if (loader != null) { _projectPathToLoaderMap[absoluteProjectPath] = loader; } } } } // a list to accumulate all the loaded projects var loadedProjects = new List <ProjectInfo>(); var reportMode = this.SkipUnrecognizedProjects ? ReportMode.Log : ReportMode.Throw; // load all the projects foreach (var projectBlock in solutionFile.ProjectBlocks) { cancellationToken.ThrowIfCancellationRequested(); string absoluteProjectPath; if (TryGetAbsoluteProjectPath(projectBlock.ProjectPath, solutionFolder, reportMode, out absoluteProjectPath)) { IProjectFileLoader loader; if (TryGetLoaderFromProjectPath(absoluteProjectPath, reportMode, out loader)) { // projects get added to 'loadedProjects' as side-effect // never perfer metadata when loading solution, all projects get loaded if they can. var tmp = await GetOrLoadProjectAsync(absoluteProjectPath, loader, preferMetadata : false, loadedProjects : loadedProjects, cancellationToken : cancellationToken).ConfigureAwait(false); } } } #endif // construct workspace from loaded project infos this.OnSolutionAdded(SolutionInfo.Create(SolutionId.CreateNewId(debugName: absoluteSolutionPath), version, absoluteSolutionPath, loadedProjects)); this.UpdateReferencesAfterAdd(); return(this.CurrentSolution); }
public ProjectFile(ProjectFileLoader loader, MSB.Evaluation.Project loadedProject, string errorMessage) { _loader = loader; _loadedProject = loadedProject; _errorMessage = errorMessage; }