예제 #1
0
        public Task <PackageRestoreResult> RestorePackagesAsync()
        {
            var tcs = new TaskCompletionSource <PackageRestoreResult>();

            string projectPath         = null;
            string nugetHome           = null;
            string nugetFilePath       = null;
            string currentLockFileHash = null;

            try
            {
                projectPath         = Path.Combine(_functionDirectory, DotNetConstants.ProjectFileName);
                nugetHome           = GetNugetPackagesPath();
                nugetFilePath       = ResolveNuGetPath();
                currentLockFileHash = GetCurrentLockFileHash(_functionDirectory);

                // Copy the file to a temporary location, which is where we'll be performing our restore from:
                string tempRestoreLocation = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                string restoreProjectPath  = Path.Combine(tempRestoreLocation, Path.GetFileName(projectPath));
                Directory.CreateDirectory(tempRestoreLocation);
                File.Copy(projectPath, restoreProjectPath);

                var startInfo = new ProcessStartInfo
                {
                    FileName = nugetFilePath,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    ErrorDialog            = false,
                    WorkingDirectory       = _functionDirectory,
                    Arguments = string.Format(CultureInfo.InvariantCulture, "restore \"{0}\" --packages \"{1}\"", restoreProjectPath, nugetHome)
                };

                startInfo.Environment.Add(EnvironmentSettingNames.DotnetSkipFirstTimeExperience, "true");

                var process = new Process {
                    StartInfo = startInfo
                };
                process.ErrorDataReceived  += ProcessDataReceived;
                process.OutputDataReceived += ProcessDataReceived;
                process.EnableRaisingEvents = true;

                process.Exited += (s, e) =>
                {
                    string lockFileLocation = Path.Combine(tempRestoreLocation, "obj", DotNetConstants.ProjectLockFileName);
                    if (process.ExitCode == 0 && File.Exists(lockFileLocation))
                    {
                        File.Copy(lockFileLocation, Path.Combine(_functionDirectory, DotNetConstants.ProjectLockFileName), true);
                    }

                    string newLockFileHash = GetCurrentLockFileHash(_functionDirectory);
                    var    result          = new PackageRestoreResult
                    {
                        IsInitialInstall  = string.IsNullOrEmpty(currentLockFileHash),
                        ReferencesChanged = !string.Equals(currentLockFileHash, newLockFileHash),
                    };

                    tcs.SetResult(result);
                    process.Close();
                };

                _logger.PackageManagerStartingPackagesRestore();

                process.Start();

                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
            }
            catch (Exception exc)
            {
                _logger.PackageManagerRestoreFailed(exc, _functionDirectory, projectPath, nugetHome, nugetFilePath, currentLockFileHash);

                tcs.SetException(exc);
            }

            return(tcs.Task);
        }
        public Task <PackageRestoreResult> RestorePackagesAsync()
        {
            var tcs = new TaskCompletionSource <PackageRestoreResult>();

            string projectPath         = null;
            string nugetHome           = null;
            string nugetFilePath       = null;
            string currentLockFileHash = null;

            try
            {
                projectPath         = Path.Combine(_functionDirectory, DotNetConstants.ProjectFileName);
                nugetHome           = GetNugetPackagesPath();
                nugetFilePath       = ResolveNuGetPath();
                currentLockFileHash = GetCurrentLockFileHash(_functionDirectory);

                var startInfo = new ProcessStartInfo
                {
                    FileName = nugetFilePath,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    ErrorDialog            = false,
                    WorkingDirectory       = _functionDirectory,
                    Arguments = string.Format(CultureInfo.InvariantCulture, "restore \"{0}\" -PackagesDirectory \"{1}\"", projectPath, nugetHome)
                };

                var process = new Process {
                    StartInfo = startInfo
                };
                process.ErrorDataReceived  += ProcessDataReceived;
                process.OutputDataReceived += ProcessDataReceived;
                process.EnableRaisingEvents = true;

                process.Exited += (s, e) =>
                {
                    string newLockFileHash = GetCurrentLockFileHash(_functionDirectory);
                    var    result          = new PackageRestoreResult
                    {
                        IsInitialInstall  = string.IsNullOrEmpty(currentLockFileHash),
                        ReferencesChanged = !string.Equals(currentLockFileHash, newLockFileHash),
                    };

                    tcs.SetResult(result);
                    process.Close();
                };

                string message = "Starting NuGet restore";
                _traceWriter.Info(message);
                _logger?.LogInformation(message);

                process.Start();

                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
            }
            catch (Exception exc)
            {
                string message = $@"NuGet restore failed with message: '{exc.Message}'
Function directory: {_functionDirectory}
Project path: {projectPath}
Packages path: {nugetHome}
Nuget client path: {nugetFilePath}
Lock file hash: {currentLockFileHash}";

                _traceWriter.Error(message);
                _logger?.LogError(message);

                tcs.SetException(exc);
            }

            return(tcs.Task);
        }