Skip to content

Commit

Permalink
Ensure built before conversion
Browse files Browse the repository at this point in the history
Relates to #592
  • Loading branch information
GrahamTheCoder committed Jul 25, 2020
1 parent e6dcd32 commit 976fe55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Vsix/CodeConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public CodeConversion(IAsyncServiceProvider serviceProvider,
public async Task ConvertProjectsAsync<TLanguageConversion>(IReadOnlyCollection<Project> selectedProjects, CancellationToken cancellationToken) where TLanguageConversion : ILanguageConversion, new()
{
try {
await EnsureBuiltAsync();
await _joinableTaskFactory.RunAsync(async () => {
var convertedFiles = ConvertProjectUnhandled<TLanguageConversion>(selectedProjects, cancellationToken);
await WriteConvertedFilesAndShowSummaryAsync(convertedFiles);
Expand All @@ -67,6 +68,7 @@ await _joinableTaskFactory.RunAsync(async () => {
public async Task ConvertDocumentAsync<TLanguageConversion>(string documentFilePath, Span selected, CancellationToken cancellationToken) where TLanguageConversion : ILanguageConversion, new()
{
try {
await EnsureBuiltAsync();
var conversionResult = await _joinableTaskFactory.RunAsync(async () => {
var result = await ConvertDocumentUnhandledAsync<TLanguageConversion>(documentFilePath, selected, cancellationToken);
await WriteConvertedFilesAndShowSummaryAsync(new[] { result }.ToAsyncEnumerable());
Expand All @@ -85,6 +87,15 @@ await _joinableTaskFactory.RunAsync(async () => {
}
}

/// <remarks>
/// https://github.com/icsharpcode/CodeConverter/issues/592
/// https://github.com/dotnet/roslyn/issues/6615
/// </remarks>
private async Task EnsureBuiltAsync()
{
await VisualStudioInteraction.EnsureBuiltAsync(m => _outputWindow.WriteToOutputWindowAsync(m));
}

private static async Task SetClipboardTextOnUiThreadAsync(string conversionResultConvertedCode)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
Expand Down
12 changes: 12 additions & 0 deletions Vsix/VisualStudioInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ public static async Task<bool> ShowMessageBoxAsync(IAsyncServiceProvider service
return userAnswer == MessageBoxResult.OK;
}

public static async Task EnsureBuiltAsync(Func<string, Task> writeMessageAsync)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(CancelAllToken);
var build = Dte.Solution.SolutionBuild;
if (build.BuildState == vsBuildState.vsBuildStateInProgress) {
throw new InvalidOperationException("Build in progress, please wait for it to complete before conversion.");
}
await writeMessageAsync("Building solution prior to conversion for maximum accuracy...");
build.Build(true);
await TaskScheduler.Default;
}

public static async Task WriteStatusBarTextAsync(IAsyncServiceProvider serviceProvider, string text)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(CancelAllToken);
Expand Down

0 comments on commit 976fe55

Please # to comment.