Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Conan version in every log before running the install #156

Merged
merged 4 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions Conan.VisualStudio.Core/ConanPathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public static bool ValidateConanExecutable(string exe, out string errorMessage)
return true;
try
{
var startInfo = new ProcessStartInfo
{
FileName = exe,
Arguments = "--version",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
var conan = new ConanRunner(exe);
var startInfo = conan.Version();
var process = Process.Start(startInfo);

process.WaitForExit();
Expand Down
14 changes: 14 additions & 0 deletions Conan.VisualStudio.Core/ConanRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ private string BuildOptions(ConanBuildType build, bool update)
return options;
}

public ProcessStartInfo Version()
{
var startInfo = new ProcessStartInfo
{
FileName = _executablePath,
Arguments = "--version",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
return startInfo;
}

public ProcessStartInfo Install(ConanProject project, ConanConfiguration configuration, ConanGeneratorType generator, ConanBuildType build, bool update, Core.IErrorListService errorListService)
{
string ProcessArgument(string name, string value) => $"-s {name}={Escape(value)}";
Expand Down
3 changes: 2 additions & 1 deletion Conan.VisualStudio/Conan.VisualStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="source.extension.cs">
<DependentUpon>source.extension.vsixmanifest</DependentUpon>
</Compile>
<Compile Include="Utils.cs" />
<Compile Include="VSConanPackage.cs" />
<Compile Include="VSConan.cs">
<AutoGen>True</AutoGen>
Expand Down Expand Up @@ -306,4 +307,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
94 changes: 35 additions & 59 deletions Conan.VisualStudio/Services/ConanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,6 @@ public async Task<bool> InstallAsync(VCProject vcProject)

return await InstallDependenciesAsync(conan, project);
}
private static void AppendLinesFunc(object packedParams)
{
var paramsTuple = (Tuple<StreamWriter, StreamReader>)packedParams;
StreamWriter writer = paramsTuple.Item1;
StreamReader reader = paramsTuple.Item2;

string line;
while ((line = reader.ReadLine()) != null)
{
lock (writer)
{
Logger.Log(line);
writer.WriteLine(line);
}
}
}

private async Task<bool> InstallDependenciesAsync(ConanRunner conan, ConanProject project)
{
Expand All @@ -141,55 +125,47 @@ private async Task<bool> InstallDependenciesAsync(ConanRunner conan, ConanProjec
ConanBuildType build = _settingsService.GetConanBuild();
bool update = _settingsService.GetConanUpdate();

ProcessStartInfo process = conan.Install(project, configuration, generator, build, update, _errorListService);

string message = $"[Conan.VisualStudio] Calling process '{process.FileName}' " +
$"with arguments '{process.Arguments}'";
Logger.Log(message);
await logStream.WriteLineAsync(message);

ProcessStartInfo process = null;
try
{
using (Process exeProcess = Process.Start(process))
// Run 'conan --version' for log purposes
process = conan.Version();
int exitCode = await Utils.RunProcessAsync(process, logStream);
if (exitCode != 0)
{
string message = "Cannot get Conan version, check that the " +
"executable is pointing to a valid one";
Logger.Log(message);
await logStream.WriteLineAsync(message);
_errorListService.WriteError(message, logFilePath);
}

// Run the install
process = conan.Install(project, configuration, generator, build, update, _errorListService);
exitCode = await Utils.RunProcessAsync(process, logStream);
if (exitCode != 0)
{
string message = $"Conan has returned exit code '{exitCode}' " +
$"while processing configuration '{configuration}'. " +
$"Please check file '{logFilePath}' for details.";

Logger.Log(message);
await logStream.WriteLineAsync(message);
_errorListService.WriteError(message, logFilePath);
return false;
}
else
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;

Task outputReader = Task.Factory.StartNew(AppendLinesFunc,
Tuple.Create(logStream, exeProcess.StandardOutput),
token, TaskCreationOptions.None, TaskScheduler.Default);
Task errorReader = Task.Factory.StartNew(AppendLinesFunc,
Tuple.Create(logStream, exeProcess.StandardError),
token, TaskCreationOptions.None, TaskScheduler.Default);

int exitCode = await exeProcess.WaitForExitAsync();

Task.WaitAll(outputReader, errorReader);

if (exitCode != 0)
{
message = $"Conan has returned exit code '{exitCode}' " +
$"while processing configuration '{configuration}'. " +
$"Please check file '{logFilePath}' for details.";

Logger.Log(message);
await logStream.WriteLineAsync(message);
_errorListService.WriteError(message, logFilePath);
return false;
}
else
{
message = $"[Conan.VisualStudio] Conan has succsessfully " +
$"installed configuration '{configuration}'";
Logger.Log(message);
await logStream.WriteLineAsync(message);
_errorListService.WriteMessage(message);
}
string message = $"[Conan.VisualStudio] Conan has succsessfully " +
$"installed configuration '{configuration}'";
Logger.Log(message);
await logStream.WriteLineAsync(message);
_errorListService.WriteMessage(message);
}
}
catch(System.ComponentModel.Win32Exception e)
catch (System.ComponentModel.Win32Exception e)
{
message = $"[Conan.VisualStudio] Unhandled error running '{process.FileName}'" +
string message = $"[Conan.VisualStudio] Unhandled error running '{process.FileName}'" +
$": {e.Message}. Check log file '{logFilePath}' for details";
Logger.Log(message);
await logStream.WriteLineAsync(message);
Expand Down
56 changes: 56 additions & 0 deletions Conan.VisualStudio/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
SSE4 marked this conversation as resolved.
Show resolved Hide resolved
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

namespace Conan.VisualStudio
{
public static class Utils
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may we use something more specific instead of just utils?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest a name, my plan is to gather common utilities like this run, maybe som print-to-log-and-output,... in a single place to make them more reusable.

{
private static void AppendLinesFunc(object packedParams)
{
var paramsTuple = (Tuple<StreamWriter, StreamReader>)packedParams;
StreamWriter writer = paramsTuple.Item1;
StreamReader reader = paramsTuple.Item2;

string line;
while ((line = reader.ReadLine()) != null)
{
lock (writer)
{
Logger.Log(line);
writer.WriteLine(line);
}
}
}

public static async Task<int> RunProcessAsync(ProcessStartInfo process, StreamWriter logStream)
{
string message = $"[Conan.VisualStudio] Calling process '{process.FileName}' " +
$"with arguments '{process.Arguments}'";
Logger.Log(message);
await logStream.WriteLineAsync(message);

using (Process exeProcess = Process.Start(process))
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;

Task outputReader = Task.Factory.StartNew(AppendLinesFunc,
Tuple.Create(logStream, exeProcess.StandardOutput),
token, TaskCreationOptions.None, TaskScheduler.Default);
Task errorReader = Task.Factory.StartNew(AppendLinesFunc,
Tuple.Create(logStream, exeProcess.StandardError),
token, TaskCreationOptions.None, TaskScheduler.Default);

int exitCode = await exeProcess.WaitForExitAsync();

Task.WaitAll(outputReader, errorReader);

return exitCode;
}
}
}
}