Skip to content

Commit

Permalink
Use AdHocWorkspace instead of MSBuildWorkspace (dotnet#1963).
Browse files Browse the repository at this point in the history
Remove support for project.json (we only support MSBuild-style projects now).
  • Loading branch information
tintoy committed Nov 26, 2017
1 parent 2e97d67 commit 6864ff2
Show file tree
Hide file tree
Showing 9 changed files with 591 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ namespace Microsoft.DocAsCode.Metadata.ManagedReference
using System.Threading.Tasks;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.DotNet.ProjectModel.Workspaces;

using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.DataContracts.Common;
using Microsoft.DocAsCode.DataContracts.ManagedReference;
using Microsoft.DocAsCode.Exceptions;
using Microsoft.DocAsCode.Metadata.ManagedReference.MSBuildWorkspaces;

public sealed class ExtractMetadataWorker : IDisposable
{
Expand All @@ -33,7 +32,7 @@ public sealed class ExtractMetadataWorker : IDisposable
private readonly Dictionary<string, string> _msbuildProperties;

//Lacks UT for shared workspace
private readonly Lazy<MSBuildWorkspace> _workspace;
private AdhocWorkspace _workspace;

internal const string IndexFileName = ".manifest";

Expand Down Expand Up @@ -68,16 +67,6 @@ public ExtractMetadataWorker(ExtractMetadataInputModel input)
{
_msbuildProperties["Configuration"] = "Release";
}

_workspace = new Lazy<MSBuildWorkspace>(() =>
{
var workspace = MSBuildWorkspace.Create(_msbuildProperties);
workspace.WorkspaceFailed += (s, e) =>
{
Logger.LogWarning($"Workspace failed with: {e.Diagnostic}");
};
return workspace;
});
}

public async Task ExtractMetadataAsync()
Expand Down Expand Up @@ -206,7 +195,7 @@ private async Task SaveAllMembersFromCacheAsync()
foreach (var path in solutions)
{
documentCache.AddDocument(path, path);
var solution = await GetSolutionAsync(path);
var solution = GetSolution(path);
if (solution != null)
{
foreach (var project in solution.Projects)
Expand Down Expand Up @@ -235,15 +224,6 @@ private async Task SaveAllMembersFromCacheAsync()
}
}

if (_files.TryGetValue(FileType.ProjectJsonProject, out var pjp))
{
await pjp.Select(s => s.NormalizedPath).ForEachInParallelAsync(path =>
{
projectCache.GetOrAdd(path, s => GetProjectJsonProject(s));
return Task.CompletedTask;
}, 60);
}

foreach (var item in projectCache)
{
var path = item.Key;
Expand Down Expand Up @@ -900,14 +880,15 @@ private static Dictionary<string, ReferenceItem> MergeYamlProjectReferences(List
return result;
}

private async Task<Solution> GetSolutionAsync(string path)
private Solution GetSolution(string path)
{
try
{
Logger.LogVerbose($"Loading solution {path}", file: path);
var solution = await _workspace.Value.OpenSolutionAsync(path);
_workspace.Value.CloseSolution();
return solution;

_workspace = MSBuildWorkspace.FromSolutionFile(path, _msbuildProperties);

return _workspace.CurrentSolution;
}
catch (Exception e)
{
Expand All @@ -923,15 +904,10 @@ private Project GetProject(ConcurrentDictionary<string, Project> cache, string p
try
{
Logger.LogVerbose($"Loading project {s}", file: s);
var project = _workspace.Value.CurrentSolution.Projects.FirstOrDefault(
p => FilePathComparer.OSPlatformSensitiveRelativePathComparer.Equals(p.FilePath, s));

if (project != null)
{
return project;
}

return _workspace.Value.OpenProjectAsync(s).Result;

return _workspace.CurrentSolution.Projects.FirstOrDefault(
project => project.FilePath == path
);
}
catch (AggregateException e)
{
Expand All @@ -946,21 +922,6 @@ private Project GetProject(ConcurrentDictionary<string, Project> cache, string p
});
}

private Project GetProjectJsonProject(string path)
{
try
{
Logger.LogVerbose($"Loading project {path}", file: path);
var workspace = new ProjectJsonWorkspace(path);
return workspace.CurrentSolution.Projects.FirstOrDefault(p => p.FilePath == Path.GetFullPath(path));
}
catch (Exception e)
{
Logger.Log(LogLevel.Warning, $"Error opening project {path}: {e.Message}. Ignored.");
return null;
}
}

/// <summary>
/// use DFS to get topological sorted items
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ private static FileType GetFileType(string filePath)
{
var extension = Path.GetExtension(filePath);
var fileName = Path.GetFileName(filePath);
if (fileName.Equals("project.json", StringComparison.OrdinalIgnoreCase))
{
return FileType.ProjectJsonProject;
}

switch (extension.ToLowerInvariant())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ namespace Microsoft.DocAsCode.Metadata.ManagedReference

internal static class FileInformationExtension
{
public static bool IsSupportedProject(this FileInformation file)
{
return file.Type == FileType.Project || file.Type == FileType.ProjectJsonProject;
}
public static bool IsSupportedProject(this FileInformation file) => file.Type == FileType.Project;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal enum FileType
NotSupported,
Solution,
Project,
ProjectJsonProject,
VBSourceCode,
CSSourceCode,
Assembly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ namespace Microsoft.DocAsCode.Metadata.ManagedReference
using System.Linq;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;

using Microsoft.DocAsCode.Common;

internal class IncrementalCheck
{
private static readonly Lazy<MSBuildWorkspace> Workspace = new Lazy<MSBuildWorkspace>(() => MSBuildWorkspace.Create());

private VersionStamp _versionToBeCompared;

private ConcurrentDictionary<string, VersionStamp> _metadataVersionCache;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.Metadata.ManagedReference.MSBuildWorkspaces
{
using System;
using System.Collections.Concurrent;
using System.Diagnostics;

/// <summary>
/// Information about the .NET Core runtime.
/// </summary>
public class DotNetRuntimeInfo
{
/// <summary>
/// A cache of .NET runtime information by target directory.
/// </summary>
static readonly ConcurrentDictionary<string, DotNetRuntimeInfo> _cache = new ConcurrentDictionary<string, DotNetRuntimeInfo>();

/// <summary>
/// The .NET Core version.
/// </summary>
public string Version { get; set; }

/// <summary>
/// The .NET Core base directory.
/// </summary>
public string BaseDirectory { get; set; }

/// <summary>
/// The current runtime identifier (RID).
/// </summary>
public string RID { get; set; }

/// <summary>
/// Get information about the current .NET Core runtime.
/// </summary>
/// <param name="baseDirectory">
/// An optional base directory where dotnet.exe should be run (this may affect the version it reports due to global.json).
/// </param>
/// <returns>
/// A <see cref="DotNetRuntimeInfo"/> containing the runtime information.
/// </returns>
public static DotNetRuntimeInfo GetCurrent(string baseDirectory = null)
{
return _cache.GetOrAdd(baseDirectory, _ =>
{
DotNetRuntimeInfo runtimeInfo = new DotNetRuntimeInfo();

Process dotnetInfoProcess = Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
WorkingDirectory = baseDirectory,
Arguments = "--info",
UseShellExecute = false,
RedirectStandardOutput = true
});
using (dotnetInfoProcess)
{
dotnetInfoProcess.WaitForExit();

string currentSection = null;
string currentLine;
while ((currentLine = dotnetInfoProcess.StandardOutput.ReadLine()) != null)
{
if (String.IsNullOrWhiteSpace(currentLine))
continue;

if (!currentLine.StartsWith(" "))
{
currentSection = currentLine;

continue;
}

string[] property = currentLine.Split(new char[] { ':' }, count: 2);
if (property.Length != 2)
continue;

property[0] = property[0].Trim();
property[1] = property[1].Trim();

switch (currentSection)
{
case "Product Information:":
{
switch (property[0])
{
case "Version":
{
runtimeInfo.Version = property[1];

break;
}
}

break;
}
case "Runtime Environment:":
{
switch (property[0])
{
case "Base Path":
{
runtimeInfo.BaseDirectory = property[1];

break;
}
case "RID":
{
runtimeInfo.RID = property[1];

break;
}
}

break;
}
}
}
}

return runtimeInfo;
});
}

/// <summary>
/// Clear the cache of .NET runtime information.
/// </summary>
public static void ClearCache()
{
_cache.Clear();
}
}
}
Loading

0 comments on commit 6864ff2

Please sign in to comment.