示例#1
0
        private async Task <ProjectId> GetOrLoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, LoadState loadedProjects, CancellationToken cancellationToken)
        {
            var projectId = loadedProjects.GetProjectId(projectFilePath);

            if (projectId == null)
            {
                projectId = await this.LoadProjectAsync(projectFilePath, loader, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);
            }

            return(projectId);
        }
示例#2
0
        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);
            }
        }
示例#3
0
        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);
                }

                if (_extensionToLanguageMap.TryGetValue(extension, out var language))
                {
                    if (_workspace.Services.SupportedLanguages.Contains(language))
                    {
                        loader = _workspace.Services.GetLanguageServices(language).GetService <IProjectFileLoader>();
                    }
                    else
                    {
                        loader = null;
                        this.ReportFailure(mode, string.Format(WorkspacesResources.Cannot_open_project_0_because_the_language_1_is_not_supported, projectFilePath, language));
                        return(false);
                    }
                }
                else
                {
                    loader = ProjectFileLoader.GetLoaderForProjectFileExtension(_workspace, extension);

                    if (loader == null)
                    {
                        this.ReportFailure(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 = _workspace.Services.GetLanguageServices(language).GetService <ICommandLineParserService>();
                    if (commandLineParser == null)
                    {
                        loader = null;
                        this.ReportFailure(mode, string.Format(WorkspacesResources.Cannot_open_project_0_because_the_language_1_is_not_supported, projectFilePath, language));
                        return(false);
                    }
                }

                return(loader != null);
            }
        }
示例#4
0
 internal bool TryGetLoaderFromProjectPath(string projectFilePath, out IProjectFileLoader loader)
 {
     return(TryGetLoaderFromProjectPath(projectFilePath, ReportMode.Ignore, out loader));
 }
示例#5
0
        private async Task <ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, LoadState loadedProjects, CancellationToken cancellationToken)
        {
            Debug.Assert(projectFilePath != null);
            Debug.Assert(loader != null);

            var projectId   = loadedProjects.GetOrCreateProjectId(projectFilePath);
            var projectName = Path.GetFileNameWithoutExtension(projectFilePath);

            var projectFile = await loader.LoadProjectFileAsync(projectFilePath, _properties, cancellationToken).ConfigureAwait(false);

            if (projectFile.ErrorMessage != null)
            {
                ReportFailure(ReportMode.Log, GetMsbuildFailedMessage(projectFilePath, projectFile.ErrorMessage));

                // if we failed during load there won't be any project file info, so bail early with empty project.
                loadedProjects.Add(CreateEmptyProjectInfo(projectId, projectFilePath, loader.Language));
                return(projectId);
            }

            var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

            if (projectFileInfo.ErrorMessage != null)
            {
                ReportFailure(ReportMode.Log, GetMsbuildFailedMessage(projectFilePath, projectFileInfo.ErrorMessage));
            }

            var projectDirectory = Path.GetDirectoryName(projectFilePath);
            var outputFilePath   = projectFileInfo.OutputFilePath;
            var outputDirectory  = Path.GetDirectoryName(outputFilePath);

            var version = GetProjectVersion(projectFilePath);

            // translate information from command line args
            var commandLineParser = _workspace.Services.GetLanguageServices(loader.Language).GetService <ICommandLineParserService>();
            var metadataService   = _workspace.Services.GetService <IMetadataService>();
            var analyzerService   = _workspace.Services.GetService <IAnalyzerService>();

            var commandLineArgs = commandLineParser.Parse(
                arguments: projectFileInfo.CommandLineArgs,
                baseDirectory: projectDirectory,
                isInteractive: false,
                sdkDirectory: RuntimeEnvironment.GetRuntimeDirectory());

            // we only support file paths in /r command line arguments
            var resolver           = new WorkspaceMetadataFileReferenceResolver(metadataService, new RelativePathResolver(commandLineArgs.ReferencePaths, commandLineArgs.BaseDirectory));
            var metadataReferences = commandLineArgs.ResolveMetadataReferences(resolver);

            var analyzerLoader = analyzerService.GetLoader();

            foreach (var path in commandLineArgs.AnalyzerReferences.Select(r => r.FilePath))
            {
                analyzerLoader.AddDependencyLocation(path);
            }

            var analyzerReferences = commandLineArgs.ResolveAnalyzerReferences(analyzerLoader);

            var defaultEncoding = commandLineArgs.Encoding;

            // docs & additional docs
            var docFileInfos           = projectFileInfo.Documents.ToImmutableArrayOrEmpty();
            var additionalDocFileInfos = projectFileInfo.AdditionalDocuments.ToImmutableArrayOrEmpty();

            // check for duplicate documents
            var allDocFileInfos = docFileInfos.AddRange(additionalDocFileInfos);

            CheckDocuments(allDocFileInfos, projectFilePath, projectId);

            var docs = new List <DocumentInfo>();

            foreach (var docFileInfo in docFileInfos)
            {
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out var name, out var folders);

                docs.Add(DocumentInfo.Create(
                             DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                             name,
                             folders,
                             projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                             new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                             docFileInfo.FilePath,
                             docFileInfo.IsGenerated));
            }

            var additionalDocs = new List <DocumentInfo>();

            foreach (var docFileInfo in additionalDocFileInfos)
            {
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out var name, out var folders);

                additionalDocs.Add(DocumentInfo.Create(
                                       DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                                       name,
                                       folders,
                                       SourceCodeKind.Regular,
                                       new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                                       docFileInfo.FilePath,
                                       docFileInfo.IsGenerated));
            }

            // project references
            var resolvedReferences = await ResolveProjectReferencesAsync(
                projectId, projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

            // add metadata references for project refs converted to metadata refs
            metadataReferences = metadataReferences.Concat(resolvedReferences.MetadataReferences);

            // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
            var assemblyName = commandLineArgs.CompilationName;

            if (string.IsNullOrWhiteSpace(assemblyName))
            {
                assemblyName = GetAssemblyNameFromProjectPath(projectFilePath);
            }

            // make sure that doc-comments at least get parsed.
            var parseOptions = commandLineArgs.ParseOptions;

            if (parseOptions.DocumentationMode == DocumentationMode.None)
            {
                parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Parse);
            }

            // add all the extra options that are really behavior overrides
            var compOptions = commandLineArgs.CompilationOptions
                              .WithXmlReferenceResolver(new XmlFileResolver(projectDirectory))
                              .WithSourceReferenceResolver(new SourceFileResolver(ImmutableArray <string> .Empty, projectDirectory))
                              // TODO: https://github.com/dotnet/roslyn/issues/4967
                              .WithMetadataReferenceResolver(new WorkspaceMetadataFileReferenceResolver(metadataService, new RelativePathResolver(ImmutableArray <string> .Empty, projectDirectory)))
                              .WithStrongNameProvider(new DesktopStrongNameProvider(ImmutableArray.Create(projectDirectory, outputFilePath)))
                              .WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);

            loadedProjects.Add(
                ProjectInfo.Create(
                    projectId,
                    version,
                    projectName,
                    assemblyName,
                    loader.Language,
                    projectFilePath,
                    outputFilePath,
                    compilationOptions: compOptions,
                    parseOptions: parseOptions,
                    documents: docs,
                    projectReferences: resolvedReferences.ProjectReferences,
                    metadataReferences: metadataReferences,
                    analyzerReferences: analyzerReferences,
                    additionalDocuments: additionalDocs,
                    isSubmission: false,
                    hostObjectType: null));

            return(projectId);
        }
示例#6
0
        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.Cannot_open_project_0_because_the_language_1_is_not_supported, projectFilePath, language));
                        return false;
                    }
                }
                else
                {
                    loader = ProjectFileLoader.GetLoaderForProjectFileExtension(_workspace, extension);

                    if (loader == null)
                    {
                        this.ReportFailure(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 = _workspace.Services.GetLanguageServices(language).GetService<ICommandLineParserService>();
                    if (commandLineParser == null)
                    {
                        loader = null;
                        this.ReportFailure(mode, string.Format(WorkspacesResources.Cannot_open_project_0_because_the_language_1_is_not_supported, projectFilePath, language));
                        return false;
                    }
                }

                return loader != null;
            }
        }
示例#7
0
 internal bool TryGetLoaderFromProjectPath(string projectFilePath, out IProjectFileLoader loader)
 {
     return TryGetLoaderFromProjectPath(projectFilePath, ReportMode.Ignore, out loader);
 }
示例#8
0
        private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, LoadState loadedProjects, CancellationToken cancellationToken)
        {
            Debug.Assert(projectFilePath != null);
            Debug.Assert(loader != null);

            var projectId = loadedProjects.GetOrCreateProjectId(projectFilePath);

            var projectName = Path.GetFileNameWithoutExtension(projectFilePath);

            var projectFile = await loader.LoadProjectFileAsync(projectFilePath, _properties, cancellationToken).ConfigureAwait(false);
            var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

            var projectDirectory = Path.GetDirectoryName(projectFilePath);
            var outputFilePath = projectFileInfo.OutputFilePath;
            var outputDirectory = Path.GetDirectoryName(outputFilePath);

            VersionStamp version;
            if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
            {
                version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
            }
            else
            {
                version = VersionStamp.Create();
            }

            // translate information from command line args
            var commandLineParser = _workspace.Services.GetLanguageServices(loader.Language).GetService<ICommandLineParserService>();
            var metadataService = _workspace.Services.GetService<IMetadataService>();
            var analyzerService = _workspace.Services.GetService<IAnalyzerService>();

            var commandLineArgs = commandLineParser.Parse(
                arguments: projectFileInfo.CommandLineArgs,
                baseDirectory: projectDirectory,
                isInteractive: false,
                sdkDirectory: RuntimeEnvironment.GetRuntimeDirectory());

            // we only support file paths in /r command line arguments
            var resolver = new WorkspaceMetadataFileReferenceResolver(metadataService, new RelativePathResolver(commandLineArgs.ReferencePaths, commandLineArgs.BaseDirectory));
            var metadataReferences = commandLineArgs.ResolveMetadataReferences(resolver);

            var analyzerLoader = analyzerService.GetLoader();
            foreach (var path in commandLineArgs.AnalyzerReferences.Select(r => r.FilePath))
            {
                analyzerLoader.AddDependencyLocation(path);
            }

            var analyzerReferences = commandLineArgs.ResolveAnalyzerReferences(analyzerLoader);

            var defaultEncoding = commandLineArgs.Encoding;

            // docs & additional docs
            var docFileInfos = projectFileInfo.Documents.ToImmutableArrayOrEmpty();
            var additionalDocFileInfos = projectFileInfo.AdditionalDocuments.ToImmutableArrayOrEmpty();

            // check for duplicate documents
            var allDocFileInfos = docFileInfos.AddRange(additionalDocFileInfos);
            CheckDocuments(allDocFileInfos, projectFilePath, projectId);

            var docs = new List<DocumentInfo>();
            foreach (var docFileInfo in docFileInfos)
            {
                string name;
                ImmutableArray<string> folders;
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);

                docs.Add(DocumentInfo.Create(
                    DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                    name,
                    folders,
                    projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                    new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                    docFileInfo.FilePath,
                    docFileInfo.IsGenerated));
            }

            var additionalDocs = new List<DocumentInfo>();
            foreach (var docFileInfo in additionalDocFileInfos)
            {
                string name;
                ImmutableArray<string> folders;
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);

                additionalDocs.Add(DocumentInfo.Create(
                    DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                    name,
                    folders,
                    SourceCodeKind.Regular,
                    new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                    docFileInfo.FilePath,
                    docFileInfo.IsGenerated));
            }

            // project references
            var resolvedReferences = await ResolveProjectReferencesAsync(
                projectId, projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

            // add metadata references for project refs converted to metadata refs
            metadataReferences = metadataReferences.Concat(resolvedReferences.MetadataReferences);

            // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
            var assemblyName = commandLineArgs.CompilationName;
            if (string.IsNullOrWhiteSpace(assemblyName))
            {
                assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                // if this is still unreasonable, use a fixed name.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = "assembly";
                }
            }

            // make sure that doc-comments at least get parsed.
            var parseOptions = commandLineArgs.ParseOptions;
            if (parseOptions.DocumentationMode == DocumentationMode.None)
            {
                parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Parse);
            }

            // add all the extra options that are really behavior overrides
            var compOptions = commandLineArgs.CompilationOptions
                    .WithXmlReferenceResolver(new XmlFileResolver(projectDirectory))
                    .WithSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, projectDirectory))
                    // TODO: https://github.com/dotnet/roslyn/issues/4967
                    .WithMetadataReferenceResolver(new WorkspaceMetadataFileReferenceResolver(metadataService, new RelativePathResolver(ImmutableArray<string>.Empty, projectDirectory)))
                    .WithStrongNameProvider(new DesktopStrongNameProvider(ImmutableArray.Create(projectDirectory, outputFilePath)))
                    .WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);

            loadedProjects.Add(
                ProjectInfo.Create(
                    projectId,
                    version,
                    projectName,
                    assemblyName,
                    loader.Language,
                    projectFilePath,
                    outputFilePath,
                    compilationOptions: compOptions,
                    parseOptions: parseOptions,
                    documents: docs,
                    projectReferences: resolvedReferences.ProjectReferences,
                    metadataReferences: metadataReferences,
                    analyzerReferences: analyzerReferences,
                    additionalDocuments: additionalDocs,
                    isSubmission: false,
                    hostObjectType: null));

            return projectId;
        }
示例#9
0
        private async Task<ProjectId> GetOrLoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, LoadState loadedProjects, CancellationToken cancellationToken)
        {
            var projectId = loadedProjects.GetProjectId(projectFilePath);
            if (projectId == null)
            {
                projectId = await this.LoadProjectAsync(projectFilePath, loader, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);
            }

            return projectId;
        }
示例#10
0
        private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, List<ProjectInfo> loadedProjects, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(projectFilePath != null);
            System.Diagnostics.Debug.Assert(loader != null);

            var projectId = this.GetOrCreateProjectId(projectFilePath);

            var projectName = Path.GetFileNameWithoutExtension(projectFilePath);

            var projectFile = await loader.LoadProjectFileAsync(projectFilePath, _properties, cancellationToken).ConfigureAwait(false);
            var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

            VersionStamp version;
            if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
            {
                version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
            }
            else
            {
                version = VersionStamp.Create();
            }

            // Documents
            var docFileInfos = projectFileInfo.Documents.ToImmutableArrayOrEmpty();
            CheckDocuments(docFileInfos, projectFilePath, projectId);

            Encoding defaultEncoding = GetDefaultEncoding(projectFileInfo.CodePage);

            var docs = new List<DocumentInfo>();
            foreach (var docFileInfo in docFileInfos)
            {
                string name;
                ImmutableArray<string> folders;
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);

                docs.Add(DocumentInfo.Create(
                    DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                    name,
                    folders,
                    projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                    new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                    docFileInfo.FilePath,
                    docFileInfo.IsGenerated));
            }

            var additonalDocs = new List<DocumentInfo>();
            foreach (var docFileInfo in projectFileInfo.AdditionalDocuments)
            {
                string name;
                ImmutableArray<string> folders;
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);

                additonalDocs.Add(DocumentInfo.Create(
                    DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                    name,
                    folders,
                    SourceCodeKind.Regular,
                    new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                    docFileInfo.FilePath,
                    docFileInfo.IsGenerated));
            }

            // project references
            var resolvedReferences = await this.ResolveProjectReferencesAsync(
                projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

            var metadataReferences = projectFileInfo.MetadataReferences
                .Concat(resolvedReferences.MetadataReferences);

            var outputFilePath = projectFileInfo.OutputFilePath;
            var assemblyName = projectFileInfo.AssemblyName;

            // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
            if (string.IsNullOrWhiteSpace(assemblyName))
            {
                assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                // if this is still unreasonable, use a fixed name.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = "assembly";
                }
            }

            loadedProjects.Add(
                ProjectInfo.Create(
                    projectId,
                    version,
                    projectName,
                    assemblyName,
                    loader.Language,
                    projectFilePath,
                    outputFilePath,
                    projectFileInfo.CompilationOptions,
                    projectFileInfo.ParseOptions,
                    docs,
                    resolvedReferences.ProjectReferences,
                    metadataReferences,
                    analyzerReferences: projectFileInfo.AnalyzerReferences,
                    additionalDocuments: additonalDocs,
                    isSubmission: false,
                    hostObjectType: null));

            return projectId;
        }
示例#11
0
        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;
            }
        }
        private async Task <ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, LoadState loadedProjects, CancellationToken cancellationToken)
        {
            Debug.Assert(projectFilePath != null);
            Debug.Assert(loader != null);

            var projectId = loadedProjects.GetOrCreateProjectId(projectFilePath);

            var projectName = Path.GetFileNameWithoutExtension(projectFilePath);

            var projectFile = await loader.LoadProjectFileAsync(projectFilePath, _properties, cancellationToken).ConfigureAwait(false);

            var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

            var projectDirectory = Path.GetDirectoryName(projectFilePath);
            var outputFilePath   = projectFileInfo.OutputFilePath;
            var outputDirectory  = Path.GetDirectoryName(outputFilePath);

            VersionStamp version;

            if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
            {
                version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
            }
            else
            {
                version = VersionStamp.Create();
            }

            // translate information from command line args
            var commandLineParser = _workspace.Services.GetLanguageServices(loader.Language).GetService <ICommandLineParserService>();
            var metadataService   = _workspace.Services.GetService <IMetadataService>();
            var analyzerService   = _workspace.Services.GetService <IAnalyzerService>();

            var commandLineArgs = commandLineParser.Parse(
                arguments: projectFileInfo.CommandLineArgs,
                baseDirectory: projectDirectory,
                isInteractive: false,
                sdkDirectory: RuntimeEnvironment.GetRuntimeDirectory());

            var resolver           = new RelativePathReferenceResolver(commandLineArgs.ReferencePaths, commandLineArgs.BaseDirectory);
            var metadataReferences = commandLineArgs.ResolveMetadataReferences(new AssemblyReferenceResolver(resolver, metadataService.GetProvider()));

            var analyzerLoader = analyzerService.GetLoader();

            foreach (var path in commandLineArgs.AnalyzerReferences.Select(r => r.FilePath))
            {
                analyzerLoader.AddDependencyLocation(path);
            }

            var analyzerReferences = commandLineArgs.ResolveAnalyzerReferences(analyzerLoader);

            var defaultEncoding = commandLineArgs.Encoding;

            // docs & additional docs
            var docFileInfos           = projectFileInfo.Documents.ToImmutableArrayOrEmpty();
            var additionalDocFileInfos = projectFileInfo.AdditionalDocuments.ToImmutableArrayOrEmpty();

            // check for duplicate documents
            var allDocFileInfos = docFileInfos.AddRange(additionalDocFileInfos);

            CheckDocuments(allDocFileInfos, projectFilePath, projectId);

            var docs = new List <DocumentInfo>();

            foreach (var docFileInfo in docFileInfos)
            {
                string name;
                ImmutableArray <string> folders;
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);

                docs.Add(DocumentInfo.Create(
                             DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                             name,
                             folders,
                             projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                             new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                             docFileInfo.FilePath,
                             docFileInfo.IsGenerated));
            }

            var additionalDocs = new List <DocumentInfo>();

            foreach (var docFileInfo in additionalDocFileInfos)
            {
                string name;
                ImmutableArray <string> folders;
                GetDocumentNameAndFolders(docFileInfo.LogicalPath, out name, out folders);

                additionalDocs.Add(DocumentInfo.Create(
                                       DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                                       name,
                                       folders,
                                       SourceCodeKind.Regular,
                                       new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                                       docFileInfo.FilePath,
                                       docFileInfo.IsGenerated));
            }

            // project references
            var resolvedReferences = await this.ResolveProjectReferencesAsync(
                projectId, projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

            // add metadata references for project refs converted to metadata refs
            metadataReferences = metadataReferences.Concat(resolvedReferences.MetadataReferences);

            // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
            var assemblyName = commandLineArgs.CompilationName;

            if (string.IsNullOrWhiteSpace(assemblyName))
            {
                assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                // if this is still unreasonable, use a fixed name.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = "assembly";
                }
            }

            // make sure that doc-comments at least get parsed.
            var parseOptions = commandLineArgs.ParseOptions;

            if (parseOptions.DocumentationMode == DocumentationMode.None)
            {
                parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Parse);
            }

            // add all the extra options that are really behavior overrides
            var compOptions = commandLineArgs.CompilationOptions
                              .WithXmlReferenceResolver(new XmlFileResolver(projectDirectory))
                              .WithSourceReferenceResolver(new SourceFileResolver(ImmutableArray <string> .Empty, projectDirectory))
                              .WithMetadataReferenceResolver(
                new AssemblyReferenceResolver(
                    new RelativePathReferenceResolver(ImmutableArray <string> .Empty, projectDirectory),
                    MetadataFileReferenceProvider.Default))
                              .WithStrongNameProvider(new DesktopStrongNameProvider(ImmutableArray.Create(projectDirectory, outputFilePath)))
                              .WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);

            loadedProjects.Add(
                ProjectInfo.Create(
                    projectId,
                    version,
                    projectName,
                    assemblyName,
                    loader.Language,
                    projectFilePath,
                    outputFilePath,
                    compilationOptions: compOptions,
                    parseOptions: parseOptions,
                    documents: docs,
                    projectReferences: resolvedReferences.ProjectReferences,
                    metadataReferences: metadataReferences,
                    analyzerReferences: analyzerReferences,
                    additionalDocuments: additionalDocs,
                    isSubmission: false,
                    hostObjectType: null));

            return(projectId);
        }
示例#13
0
        private async Task <ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, List <ProjectInfo> loadedProjects, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(projectFilePath != null);
            System.Diagnostics.Debug.Assert(loader != null);

            var projectId = this.GetOrCreateProjectId(projectFilePath);

            var name = Path.GetFileNameWithoutExtension(projectFilePath);

            var projectFile = await loader.LoadProjectFileAsync(projectFilePath, _properties, cancellationToken).ConfigureAwait(false);

            var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

            VersionStamp version;

            if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
            {
                version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
            }
            else
            {
                version = VersionStamp.Create();
            }

            // Documents
            var docFileInfos = projectFileInfo.Documents.ToImmutableArrayOrEmpty();

            CheckDocuments(docFileInfos, projectFilePath, projectId);

            Encoding defaultEncoding = GetDefaultEncoding(projectFileInfo.CodePage);

            var docs = new List <DocumentInfo>();

            foreach (var docFileInfo in docFileInfos)
            {
                docs.Add(DocumentInfo.Create(
                             DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                             Path.GetFileName(docFileInfo.LogicalPath),
                             GetDocumentFolders(docFileInfo.LogicalPath),
                             projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                             new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                             docFileInfo.FilePath,
                             docFileInfo.IsGenerated));
            }

            var additonalDocs = new List <DocumentInfo>();

            foreach (var docFileInfo in projectFileInfo.AdditionalDocuments)
            {
                additonalDocs.Add(DocumentInfo.Create(
                                      DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                                      Path.GetFileName(docFileInfo.LogicalPath),
                                      GetDocumentFolders(docFileInfo.LogicalPath),
                                      SourceCodeKind.Regular,
                                      new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                                      docFileInfo.FilePath,
                                      docFileInfo.IsGenerated));
            }

            // project references
            var resolvedReferences = await this.ResolveProjectReferencesAsync(
                projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

            var metadataReferences = projectFileInfo.MetadataReferences
                                     .Concat(resolvedReferences.MetadataReferences);

            var outputFilePath = projectFileInfo.OutputFilePath;
            var assemblyName   = projectFileInfo.AssemblyName;

            // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
            if (string.IsNullOrWhiteSpace(assemblyName))
            {
                assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                // if this is still unreasonable, use a fixed name.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = "assembly";
                }
            }

            loadedProjects.Add(
                ProjectInfo.Create(
                    projectId,
                    version,
                    name,
                    assemblyName,
                    loader.Language,
                    projectFilePath,
                    outputFilePath,
                    projectFileInfo.CompilationOptions,
                    projectFileInfo.ParseOptions,
                    docs,
                    resolvedReferences.ProjectReferences,
                    metadataReferences,
                    analyzerReferences: projectFileInfo.AnalyzerReferences,
                    additionalDocuments: additonalDocs,
                    isSubmission: false,
                    hostObjectType: null));

            return(projectId);
        }
示例#14
0
 public bool TryGetLoaderFromProjectPath(string projectFilePath, out IProjectFileLoader loader)
 {
     return(TryGetLoaderFromProjectPath(projectFilePath, DiagnosticReportingMode.Ignore, out loader));
 }
示例#15
0
        private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, List<ProjectInfo> loadedProjects, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(projectFilePath != null);
            System.Diagnostics.Debug.Assert(loader != null);

            var projectId = this.GetOrCreateProjectId(projectFilePath);

            var name = Path.GetFileNameWithoutExtension(projectFilePath);

            try
            {
                var projectFile = await loader.LoadProjectFileAsync(projectFilePath, this.properties, cancellationToken).ConfigureAwait(false);
                var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

                VersionStamp version;
                if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
                {
                    version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
                }
                else
                {
                    version = VersionStamp.Create();
                }

                // Documents
                var docFileInfos = projectFileInfo.Documents.ToImmutableListOrEmpty();
                CheckDocuments(docFileInfos, projectFilePath, projectId);

                var docs = new List<DocumentInfo>();
                foreach (var docFileInfo in docFileInfos)
                {
                    docs.Add(DocumentInfo.Create(
                        DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                        Path.GetFileName(docFileInfo.LogicalPath),
                        GetDocumentFolders(docFileInfo.LogicalPath),
                        projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                        new FileTextLoader(docFileInfo.FilePath),
                        docFileInfo.FilePath,
                        docFileInfo.IsGenerated));
                }

                // project references
                var resolvedReferences = await this.ResolveProjectReferencesAsync(
                    projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

                // metadata references
                var metadataReferences = projectFileInfo.MetadataReferences
                    .Select(mi => new MetadataFileReference(mi.Path, mi.Properties, this.GetDocumentationProvider(mi.Path)))
                    .Concat(resolvedReferences.MetadataReferences);

                var outputFilePath = projectFileInfo.OutputFilePath;
                var assemblyName = projectFileInfo.AssemblyName;

                // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                    // if this is still unreasonable, use a fixed name.
                    if (string.IsNullOrWhiteSpace(assemblyName))
                    {
                        assemblyName = "assembly";
                    }
                }

                loadedProjects.Add(
                    ProjectInfo.Create(
                        projectId,
                        version,
                        name,
                        assemblyName,
                        loader.Language,
                        projectFilePath,
                        outputFilePath,
                        projectFileInfo.CompilationOptions,
                        projectFileInfo.ParseOptions,
                        docs.ToImmutableListOrEmpty(),
                        resolvedReferences.ProjectReferences.ToImmutableListOrEmpty(),
                        metadataReferences.ToImmutableListOrEmpty(),
                        analyzerReferences: projectFileInfo.AnalyzerReferences,
                        isSubmission: false,
                        hostObjectType: null));

                return projectId;
            }
            catch (System.IO.IOException exception)
            {
                this.OnWorkspaceFailed(new ProjectDiagnostic(WorkspaceDiagnosticKind.FileAccessFailure, exception.Message, projectId));
                loadedProjects.Add(ProjectInfo.Create(projectId, VersionStamp.Default, name, name, loader.Language, filePath: projectFilePath));
                return projectId;
            }
        }
示例#16
0
        private async Task <ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, List <ProjectInfo> loadedProjects, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(projectFilePath != null);
            System.Diagnostics.Debug.Assert(loader != null);

            var projectId = this.GetOrCreateProjectId(projectFilePath);

            var name = Path.GetFileNameWithoutExtension(projectFilePath);

            try
            {
                var projectFile = await loader.LoadProjectFileAsync(projectFilePath, this.properties, cancellationToken).ConfigureAwait(false);

                var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

                VersionStamp version;
                if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
                {
                    version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
                }
                else
                {
                    version = VersionStamp.Create();
                }

                // Documents
                var docFileInfos = projectFileInfo.Documents.ToImmutableArrayOrEmpty();
                CheckDocuments(docFileInfos, projectFilePath, projectId);

                // TODO: is htere a way how to specify encoding to msbuild? csc.exe has /codepage command line option.
                // For now use auto-detection. (bug 941489).
                Encoding defaultEncoding = null;

                var docs = new List <DocumentInfo>();
                foreach (var docFileInfo in docFileInfos)
                {
                    docs.Add(DocumentInfo.Create(
                                 DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                                 Path.GetFileName(docFileInfo.LogicalPath),
                                 GetDocumentFolders(docFileInfo.LogicalPath),
                                 projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                                 new FileTextLoader(docFileInfo.FilePath, defaultEncoding),
                                 docFileInfo.FilePath,
                                 defaultEncoding,
                                 docFileInfo.IsGenerated));
                }

                // project references
                var resolvedReferences = await this.ResolveProjectReferencesAsync(
                    projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

                // metadata references
                var metadataReferences = projectFileInfo.MetadataReferences
                                         .Select(mi => new MetadataFileReference(mi.Path, mi.Properties, this.GetDocumentationProvider(mi.Path)))
                                         .Concat(resolvedReferences.MetadataReferences);

                var outputFilePath = projectFileInfo.OutputFilePath;
                var assemblyName   = projectFileInfo.AssemblyName;

                // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                    // if this is still unreasonable, use a fixed name.
                    if (string.IsNullOrWhiteSpace(assemblyName))
                    {
                        assemblyName = "assembly";
                    }
                }

                loadedProjects.Add(
                    ProjectInfo.Create(
                        projectId,
                        version,
                        name,
                        assemblyName,
                        loader.Language,
                        projectFilePath,
                        outputFilePath,
                        projectFileInfo.CompilationOptions,
                        projectFileInfo.ParseOptions,
                        docs,
                        resolvedReferences.ProjectReferences,
                        metadataReferences,
                        analyzerReferences: projectFileInfo.AnalyzerReferences,
                        isSubmission: false,
                        hostObjectType: null));

                return(projectId);
            }
            catch (System.IO.IOException exception)
            {
                this.OnWorkspaceFailed(new ProjectDiagnostic(WorkspaceDiagnosticKind.FileAccessFailure, exception.Message, projectId));
                loadedProjects.Add(ProjectInfo.Create(projectId, VersionStamp.Default, name, name, loader.Language, filePath: projectFilePath));
                return(projectId);
            }
        }