Beispiel #1
0
        private ProjectFileInfo LoadProject(string projectFilePath)
        {
            _logger.LogInformation($"Loading project: {projectFilePath}");

            ProjectFileInfo project;
            var             diagnostics = new List <MSBuildDiagnosticsMessage>();

            try
            {
                project = ProjectFileInfo.Create(projectFilePath, _environment.TargetDirectory, GetSdksPath(projectFilePath), _loggerFactory.CreateLogger <ProjectFileInfo>(), _options, diagnostics);

                if (project == null)
                {
                    _logger.LogWarning($"Failed to load project file '{projectFilePath}'.");
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning($"Failed to load project file '{projectFilePath}'.", ex);
                _eventEmitter.Error(ex, fileName: projectFilePath);
                project = null;
            }

            _eventEmitter.MSBuildProjectDiagnostics(projectFilePath, diagnostics);

            return(project);
        }
Beispiel #2
0
        private ProjectFileInfo CreateProject(string projectFilePath)
        {
            ProjectFileInfo projectFileInfo = null;
            var             diagnostics     = new List <MSBuildDiagnosticsMessage>();

            try
            {
                projectFileInfo = ProjectFileInfo.Create(_options, _logger, _env.Path, projectFilePath, diagnostics);

                if (projectFileInfo == null)
                {
                    _logger.LogWarning($"Failed to process project file '{projectFilePath}'.");
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning($"Failed to process project file '{projectFilePath}'.", ex);
                _emitter.Emit(EventTypes.Error, new ErrorMessage()
                {
                    FileName = projectFilePath,
                    Text     = ex.ToString()
                });
            }

            _emitter.Emit(EventTypes.MsBuildProjectDiagnostics, new MSBuildProjectDiagnostics()
            {
                FileName = projectFilePath,
                Warnings = diagnostics.Where(d => d.LogLevel == "Warning"),
                Errors   = diagnostics.Where(d => d.LogLevel == "Error"),
            });

            return(projectFileInfo);
        }
Beispiel #3
0
        public async Task HelloWorldSlim_has_correct_property_values()
        {
            using (var host = CreateOmniSharpHost())
                using (var testProject = await _testAssets.GetTestProjectAsync("HelloWorldSlim"))
                {
                    var projectFilePath = Path.Combine(testProject.Directory, "HelloWorldSlim.csproj");

                    var projectFileInfo = ProjectFileInfo.Create(projectFilePath, testProject.Directory, GetSdksPath(host), this._logger);

                    Assert.NotNull(projectFileInfo);
                    Assert.Equal(projectFilePath, projectFileInfo.FilePath);
                    Assert.Equal(1, projectFileInfo.TargetFrameworks.Length);
                    Assert.Equal("netcoreapp1.0", projectFileInfo.TargetFrameworks[0]);
                    Assert.Equal("bin/Debug/netcoreapp1.0/", projectFileInfo.OutputPath.Replace('\\', '/'));
                    Assert.Equal(1, projectFileInfo.SourceFiles.Length);
                }
        }
Beispiel #4
0
        public async Task NetStandardAndNetCoreApp_has_correct_property_values()
        {
            using (var host = CreateOmniSharpHost())
                using (var testProject = await _testAssets.GetTestProjectAsync("NetStandardAndNetCoreApp"))
                {
                    var projectFilePath = Path.Combine(testProject.Directory, "NetStandardAndNetCoreApp.csproj");

                    var projectFileInfo = ProjectFileInfo.Create(projectFilePath, testProject.Directory, GetSdksPath(host), this._logger);

                    Assert.NotNull(projectFileInfo);
                    Assert.Equal(projectFilePath, projectFileInfo.FilePath);
                    Assert.Equal(2, projectFileInfo.TargetFrameworks.Length);
                    Assert.Equal("netcoreapp1.0", projectFileInfo.TargetFrameworks[0]);
                    Assert.Equal("netstandard1.5", projectFileInfo.TargetFrameworks[1]);
                    Assert.Equal(@"bin/Debug/netcoreapp1.0/", projectFileInfo.OutputPath.Replace('\\', '/'));
                    Assert.Equal(3, projectFileInfo.SourceFiles.Length); // Program.cs, AssemblyInfo.cs, AssemblyAttributes.cs
                }
        }
Beispiel #5
0
        public async Task HelloWorld_has_correct_property_values()
        {
            using (var host = CreateOmniSharpHost())
                using (var testProject = await _testAssets.GetTestProjectAsync("HelloWorld"))
                {
                    var projectFilePath = Path.Combine(testProject.Directory, "HelloWorld.csproj");

                    var projectFileInfo = ProjectFileInfo.Create(projectFilePath, testProject.Directory, GetSdksPath(host), this._logger);

                    Assert.NotNull(projectFileInfo);
                    Assert.Equal(projectFilePath, projectFileInfo.FilePath);
                    Assert.Single(projectFileInfo.TargetFrameworks);
                    Assert.Equal("netcoreapp1.0", projectFileInfo.TargetFrameworks[0]);
                    Assert.Equal("bin/Debug/netcoreapp1.0/", projectFileInfo.OutputPath.Replace('\\', '/'));
                    Assert.Equal(3, projectFileInfo.SourceFiles.Length); // Program.cs, AssemblyInfo.cs, AssemblyAttributes.cs
                    Assert.Equal(LanguageVersion.CSharp7_1, projectFileInfo.LanguageVersion);
                }
        }
Beispiel #6
0
        private ProjectFileInfo CreateProject(string projectFilePath)
        {
            ProjectFileInfo projectFileInfo = null;

            try
            {
                projectFileInfo = ProjectFileInfo.Create(_logger, _env.Path, projectFilePath);

                if (projectFileInfo == null)
                {
                    _logger.WriteWarning(string.Format("Failed to process project file '{0}'.", projectFilePath));
                }
            }
            catch (Exception ex)
            {
                _logger.WriteWarning(string.Format("Failed to process project file '{0}'.", projectFilePath), ex);
            }

            return(projectFileInfo);
        }
        public void Initalize()
        {
            var solutionFilePath = _env.SolutionFilePath;

            if (string.IsNullOrEmpty(solutionFilePath))
            {
                var solutions = Directory.GetFiles(_env.Path, "*.sln");

                switch (solutions.Length)
                {
                case 0:
                    _logger.WriteInformation(string.Format("No solution files found in '{0}'", _env.Path));
                    return;

                case 1:
                    solutionFilePath = solutions[0];
                    break;

                default:
                    _logger.WriteError("Could not determine solution file");
                    return;
                }
            }

            SolutionFile solutionFile = null;

            using (var stream = File.OpenRead(solutionFilePath))
                using (var reader = new StreamReader(stream))
                {
                    solutionFile = SolutionFile.Parse(reader);
                }

            _logger.WriteInformation(string.Format("Detecting projects in '{0}'.", solutionFilePath));

            var projectMap = new Dictionary <string, ProjectFileInfo>();

            foreach (var block in solutionFile.ProjectBlocks)
            {
                if (!_supportsProjectTypes.Contains(block.ProjectTypeGuid))
                {
                    _logger.WriteWarning("Skipped unsupported project type '{0}'", block.ProjectPath);
                    continue;
                }

                if (_projectMap.ContainsKey(block.ProjectGuid))
                {
                    continue;
                }

                var projectFilePath = Path.GetFullPath(Path.GetFullPath(Path.Combine(_env.Path, block.ProjectPath.Replace('\\', Path.DirectorySeparatorChar))));

                _logger.WriteInformation(string.Format("Loading project from '{0}'.", projectFilePath));

                ProjectFileInfo projectFileInfo = null;

                try
                {
                    projectFileInfo = ProjectFileInfo.Create(_logger, _env.Path, projectFilePath);

                    if (projectFileInfo == null)
                    {
                        _logger.WriteWarning(string.Format("Failed to process project file '{0}'.", projectFilePath));
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    _logger.WriteWarning(string.Format("Failed to process project file '{0}'.", projectFilePath), ex);
                    continue;
                }

                var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(projectFileInfo.Name),
                                                     VersionStamp.Create(),
                                                     projectFileInfo.Name,
                                                     projectFileInfo.AssemblyName,
                                                     LanguageNames.CSharp,
                                                     projectFileInfo.ProjectFilePath);

                _workspace.AddProject(projectInfo);

                projectFileInfo.WorkspaceId = projectInfo.Id;
                projectMap[projectFileInfo.ProjectFilePath] = projectFileInfo;

                _projectMap[block.ProjectGuid] = projectInfo.Id;
            }

            foreach (var projectFileInfo in projectMap.Values)
            {
                var project = _workspace.CurrentSolution.GetProject(projectFileInfo.WorkspaceId);

                var unusedDocuments = project.Documents.ToDictionary(d => d.FilePath, d => d.Id);

                foreach (var file in projectFileInfo.SourceFiles)
                {
                    if (unusedDocuments.Remove(file))
                    {
                        continue;
                    }

                    using (var stream = File.OpenRead(file))
                    {
                        var sourceText = SourceText.From(stream, encoding: Encoding.UTF8);
                        var id         = DocumentId.CreateNewId(projectFileInfo.WorkspaceId);
                        var version    = VersionStamp.Create();

                        var loader = TextLoader.From(TextAndVersion.Create(sourceText, version));

                        _workspace.AddDocument(DocumentInfo.Create(id, file, filePath: file, loader: loader));
                    }
                }

                foreach (var unused in unusedDocuments)
                {
                    _workspace.RemoveDocument(unused.Value);
                }

                var unusedProjectReferences = new HashSet <ProjectReference>(project.ProjectReferences);

                foreach (var projectReferencePath in projectFileInfo.ProjectReferences)
                {
                    ProjectFileInfo projectReferenceInfo;
                    if (projectMap.TryGetValue(projectReferencePath, out projectReferenceInfo))
                    {
                        var reference = new ProjectReference(projectReferenceInfo.WorkspaceId);

                        if (unusedProjectReferences.Remove(reference))
                        {
                            // This reference already exists
                            continue;
                        }

                        _workspace.AddProjectReference(project.Id, reference);
                    }
                    else
                    {
                        _logger.WriteWarning(string.Format("Unable to resolve project reference '{0}' for '{1}'.", projectReferencePath, projectFileInfo.ProjectFilePath));
                    }
                }

                foreach (var unused in unusedProjectReferences)
                {
                    _workspace.RemoveProjectReference(project.Id, unused);
                }

                var unusedReferences = new HashSet <MetadataReference>(project.MetadataReferences);

                foreach (var referencePath in projectFileInfo.References)
                {
                    if (!File.Exists(referencePath))
                    {
                        _logger.WriteWarning(string.Format("Unable to resolve assembly '{0}'", referencePath));
                    }
                    else
                    {
                        var metadataReference = _metadataReferenceCache.GetMetadataReference(referencePath);

                        if (unusedReferences.Remove(metadataReference))
                        {
                            continue;
                        }

                        _logger.WriteVerbose(string.Format("Adding reference '{0}' to '{1}'.", referencePath, projectFileInfo.ProjectFilePath));
                        _workspace.AddMetadataReference(project.Id, metadataReference);
                    }
                }

                foreach (var reference in unusedReferences)
                {
                    _workspace.RemoveMetadataReference(project.Id, reference);
                }
            }
        }