Esempio n. 1
0
        /// <summary>
        /// Restore all files in a specified package.
        /// </summary>
        /// <param name="package">The package file.</param>
        /// <param name="pm">Where to log progress/errors.</param>
        private void RestoreNugetPackage(string package, IProgressMonitor pm)
        {
            pm.NugetInstall(package);

            /* Use nuget.exe to install a package.
             * Note that there is a clutch of NuGet assemblies which could be used to
             * invoke this directly, which would arguably be nicer. However they are
             * really unwieldy and this solution works for now.
             */

            string exe, args;

            if (Util.Win32.IsWindows())
            {
                exe  = nugetExe;
                args = string.Format("install -OutputDirectory {0} {1}", PackageDirectory, package);
            }
            else
            {
                exe  = "mono";
                args = string.Format("{0} install -OutputDirectory {1} {2}", nugetExe, PackageDirectory, package);
            }

            var pi = new ProcessStartInfo(exe, args)
            {
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                UseShellExecute        = false
            };

            try
            {
                using var p = Process.Start(pi);

                if (p is null)
                {
                    pm.FailedNugetCommand(pi.FileName, pi.Arguments, "Couldn't start process.");
                    return;
                }

                var output = p.StandardOutput.ReadToEnd();
                var error  = p.StandardError.ReadToEnd();

                p.WaitForExit();
                if (p.ExitCode != 0)
                {
                    pm.FailedNugetCommand(pi.FileName, pi.Arguments, output + error);
                }
            }
            catch (Exception ex)
                when(ex is System.ComponentModel.Win32Exception || ex is FileNotFoundException)
                {
                    pm.FailedNugetCommand(pi.FileName, pi.Arguments, ex.Message);
                }
        }