Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ocalles committed Aug 22, 2023
1 parent 31be419 commit dd529dd
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,6 @@ Microsoft.DotNet.Interactive.Utility
public System.Threading.Tasks.Task<CommandLineResult> Clean(System.Nullable<System.TimeSpan> timeout = null)
public System.Threading.Tasks.Task<CommandLineResult> Execute(System.String args, System.Nullable<System.TimeSpan> timeout = null)
public System.Threading.Tasks.Task<CommandLineResult> New(System.String templateName, System.String args = null)
public System.Threading.Tasks.Task<CommandLineResult> NewGlobalJson()
public System.Threading.Tasks.Task<CommandLineResult> Pack(System.String args = null, System.Nullable<System.TimeSpan> timeout = null)
public System.Threading.Tasks.Task<CommandLineResult> Publish(System.String args = null, System.Nullable<System.TimeSpan> timeout = null)
public System.Diagnostics.Process StartProcess(System.String args, System.Action<System.String> output = null, System.Action<System.String> error = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ protected SyntaxTree CreateInstrumentationEmitterSyntaxTree()
}
}

protected BuildDataResults DesignTimeBuildResult { get; set; }
private protected BuildDataResults DesignTimeBuildResult { get; set; }

protected virtual bool ShouldDoFullBuild()
{
Expand All @@ -560,7 +560,7 @@ protected virtual bool ShouldDoDesignTimeBuild()
|| DesignTimeBuildResult.Succeeded == false;
}

protected async Task<BuildDataResults> DesignTimeBuild()
private protected async Task<BuildDataResults> DesignTimeBuild()
{
using (var operation = _log.OnEnterAndConfirmOnExit())
{
Expand All @@ -571,7 +571,7 @@ protected async Task<BuildDataResults> DesignTimeBuild()
using (await FileLock.TryCreateAsync(Directory))
{
await BuildCacheFileUtilities.BuildAndCreateCacheFileAsync(csProj.FullName);
result = GetResultsFromProjectFile(csProj.FullName);
result = ResultsFromCacheFileUsingProjectFilePath(csProj.FullName);
var languageVersion = csProj.SuggestedLanguageVersion();
// TODO: analyzer.SetGlobalProperty("langVersion", languageVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public virtual async Task InitializeAsync(
{
var dotnet = new Dotnet(directory);

await dotnet.NewGlobalJson();
await dotnet.New("globaljson");

var result = await dotnet
.New(Template,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private async Task<BuildDataResults> TryLoadAnalyzerResultsAsync(FileInfo binLog
BuildDataResults results = null;
await binLog.DoWhenFileAvailable(() =>
{
results = GetResultsFromProjectFile(binLog.FullName);
results = ResultsFromCacheFileUsingProjectFilePath(binLog.FullName);
});
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

namespace Microsoft.DotNet.Interactive.CSharpProject.RoslynWorkspaceUtilities;

public class BuildDataResults
internal class BuildDataResults
{
public BuildProjectData BuildProjectData { get; set; }
internal BuildProjectData BuildProjectData { get; init; }

public CodeAnalysis.Workspace Workspace { get; set; }
internal CodeAnalysis.Workspace Workspace { get; init; }

public bool Succeeded { get; set; }
internal bool Succeeded { get; init; }

public string CacheFilePath { get; set; }
internal string CacheFilePath { get; init; }

public CSharpParseOptions CSharpParseOptions { get; set; }
internal CSharpParseOptions CSharpParseOptions { get; init; }

// TODO: Set this value
public string ProjectFilePath { get; set; }
internal string ProjectFilePath { get; init; }

internal Dictionary<string, BuildDataProjectItem[]> Items { get; set; } = new Dictionary<string, BuildDataProjectItem[]>();
internal Dictionary<string, BuildDataProjectItem[]> Items { get; } = new Dictionary<string, BuildDataProjectItem[]>();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using Markdig.Syntax;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
Expand Down Expand Up @@ -52,11 +53,11 @@ internal static BuildDataResults GetResultsFromCacheFile(string cacheFilePath)
}
}

internal static BuildDataResults GetResultsFromProjectFile(string csprojFilePath)
internal static BuildDataResults ResultsFromCacheFileUsingProjectFilePath(string csprojFilePath)
{
if (!File.Exists(csprojFilePath))
{
return null;
throw new ArgumentException($"project file does not exist : {csprojFilePath}");
}

var cacheFilePath = csprojFilePath + cacheFilenameSuffix;
Expand All @@ -70,11 +71,11 @@ internal static void PopulateBuildProjectData(string fileContent, out BuildProje

string[] lines = fileContent.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);

List<string> referencesList = new List<string>();
List<string> projectReferencesList = new List<string>();
List<string> analyzerReferencesList = new List<string>();
List<string> preprocessorSymbolsList = new List<string>();
List<string> sourceFilesList = new List<string>();
var referencesList = new List<string>();
var projectReferencesList = new List<string>();
var analyzerReferencesList = new List<string>();
var preprocessorSymbolsList = new List<string>();
var sourceFilesList = new List<string>();

foreach (string line in lines)
{
Expand Down Expand Up @@ -205,42 +206,27 @@ private static void WorkspaceChangedHandler(object sender, WorkspaceChangeEventA
private static ProjectInfo GetProjectInfo(BuildProjectData buildProjectData, CodeAnalysis.Workspace workspace, ProjectId projectId)
{
string projectName = Path.GetFileNameWithoutExtension(buildProjectData.ProjectFilePath);
if (!TryGetSupportedLanguageName(buildProjectData.ProjectFilePath, out string languageName))
{
return null;
}

return ProjectInfo.Create(
projectId,
VersionStamp.Create(),
projectName,
projectName,
languageName,
LanguageNames.CSharp,
filePath: buildProjectData.ProjectFilePath,
outputFilePath: buildProjectData.TargetPath,
documents: GetDocuments(buildProjectData, projectId),
projectReferences: GetExistingProjectReferences(buildProjectData, workspace),
metadataReferences: GetMetadataReferences(buildProjectData),
analyzerReferences: GetAnalyzerReferences(buildProjectData, workspace),
parseOptions: CreateParseOptions(buildProjectData, languageName),
compilationOptions: CreateCompilationOptions(buildProjectData, languageName));
}

private static bool TryGetSupportedLanguageName(string projectPath, out string languageName)
{
switch (Path.GetExtension(projectPath))
{
case ".csproj":
languageName = LanguageNames.CSharp;
return true;
default:
languageName = null;
return false;
}
parseOptions: CreateParseOptions(buildProjectData),
compilationOptions: CreateCompilationOptions(buildProjectData));
}

private static IReadOnlyList<DocumentInfo> GetDocuments(BuildProjectData buildProjectData, ProjectId projectId) =>
buildProjectData
.SourceFiles?
buildProjectData.SourceFiles is null ?
Array.Empty<DocumentInfo>() :
buildProjectData.SourceFiles
.Where(File.Exists)
.Select(x => DocumentInfo.Create(
DocumentId.CreateNewId(projectId),
Expand All @@ -265,21 +251,14 @@ private static IReadOnlyList<AnalyzerReference> GetAnalyzerReferences(BuildProje
{
IAnalyzerAssemblyLoader loader = workspace.Services.GetRequiredService<IAnalyzerService>().GetLoader();

return buildProjectData
.AnalyzerReferences?
.Where(File.Exists)
.Select(x => new AnalyzerFileReference(x, loader)).ToList();
return buildProjectData.AnalyzerReferences is null ?
Array.Empty<AnalyzerReference>() :
buildProjectData.AnalyzerReferences?
.Where(File.Exists)
.Select(x => new AnalyzerFileReference(x, loader)).ToList();
}

private static ParseOptions CreateParseOptions(BuildProjectData buildProjectData, string languageName)
{
if (languageName == LanguageNames.CSharp)
{
return CreateCSharpParseOptions(buildProjectData);
}

return null;
}
private static ParseOptions CreateParseOptions(BuildProjectData buildProjectData) => CreateCSharpParseOptions(buildProjectData);

private static CSharpParseOptions CreateCSharpParseOptions(BuildProjectData buildProjectData)
{
Expand All @@ -297,7 +276,7 @@ private static CSharpParseOptions CreateCSharpParseOptions(BuildProjectData buil
return parseOptions;
}

private static CompilationOptions CreateCompilationOptions(BuildProjectData buildProjectData, string languageName)
private static CompilationOptions CreateCompilationOptions(BuildProjectData buildProjectData)
{
string outputType = buildProjectData.OutputType;
OutputKind? kind = null;
Expand All @@ -317,16 +296,7 @@ private static CompilationOptions CreateCompilationOptions(BuildProjectData buil
break;
}

if (kind.HasValue)
{
if (languageName == LanguageNames.CSharp)
{
CompilationOptions CreateCSharpCompilationOptions() => new CSharpCompilationOptions(kind.Value);

return CreateCSharpCompilationOptions();
}
}

return null;
CompilationOptions CreateCSharpCompilationOptions() => new CSharpCompilationOptions(kind.Value);
return CreateCSharpCompilationOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Microsoft.DotNet.Interactive.CSharpProject.Servers.Roslyn;

public static class PackageExtensions
internal static class PackageExtensions
{
public static async Task<Compilation> Compile(
this Package package,
Expand Down
5 changes: 0 additions & 5 deletions src/Microsoft.DotNet.Interactive/Utility/Dotnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public async Task<CommandLineResult> New(string templateName, string args = null
return await Execute($@"new ""{templateName}"" {args}");
}

public Task<CommandLineResult> NewGlobalJson()
{
return Execute($"new globaljson");
}

public async Task<AddPackageResult> AddPackage(string packageId, string version = null)
{
var versionArg = string.IsNullOrWhiteSpace(version)
Expand Down

0 comments on commit dd529dd

Please sign in to comment.