Beispiel #1
0
        static async Task <LaunchResult> InnerLaunch(ResolvedTool tool, string tempFile, string targetFile)
        {
            var arguments = tool.Arguments(tempFile, targetFile);
            var command   = tool.BuildCommand(tempFile, targetFile);

            if (ProcessCleanup.TryGetProcessInfo(command, out var processCommand))
            {
                if (tool.AutoRefresh)
                {
                    await DiffEngineTray.AddMove(tempFile, targetFile, tool.ExePath, arguments, tool.IsMdi !, processCommand.Process, processCommand.StartTime);

                    return(LaunchResult.AlreadyRunningAndSupportsRefresh);
                }

                if (!tool.IsMdi)
                {
                    ProcessCleanup.Kill(command);
                }
            }

            var instanceCount = Interlocked.Increment(ref launchedInstances);

            if (instanceCount > maxInstancesToLaunch)
            {
                await DiffEngineTray.AddMove(tempFile, targetFile, tool.ExePath, arguments, tool.IsMdi !, null, null);

                return(LaunchResult.TooManyRunningDiffTools);
            }

            try
            {
                var startInfo = new ProcessStartInfo(tool.ExePath, arguments)
                {
                    UseShellExecute = true
                };

                using var process = Process.Start(startInfo);
                if (process == null)
                {
                    var message = $@"Failed to launch diff tool.
{tool.ExePath} {arguments}";
                    throw new Exception(message);
                }

                await DiffEngineTray.AddMove(tempFile, targetFile, tool.ExePath, arguments, !tool.IsMdi, process.Id, process.StartTime);

                return(LaunchResult.StartedNewInstance);
            }
            catch (Exception exception)
            {
                var message = $@"Failed to launch diff tool.
{tool.ExePath} {arguments}";
                throw new Exception(message, exception);
            }
        }
Beispiel #2
0
        public static Task <LaunchResult> LaunchAsync(ResolvedTool tool, string tempFile, string targetFile)
        {
            GuardFiles(tempFile, targetFile);
            Guard.AgainstNull(tool, nameof(tool));

            return(InnerLaunchAsync(
                       (out ResolvedTool? resolvedTool) =>
            {
                resolvedTool = tool;
                return true;
            },
                       tempFile,
                       targetFile));
        }
Beispiel #3
0
        static bool TryCreate(ResolvedTool tool, string targetFile)
        {
            var targetExists = File.Exists(targetFile);

            if (tool.RequiresTarget && !targetExists)
            {
                if (!AllFiles.TryCreateFile(targetFile, useEmptyStringForTextFiles: true))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #4
0
        public static Task <LaunchResult> Launch(ResolvedTool tool, string tempFile, string targetFile)
        {
            GuardFiles(tempFile, targetFile);
            Guard.AgainstNull(tool, nameof(tool));
            if (Disabled)
            {
                return(Task.FromResult(LaunchResult.Disabled));
            }

            var targetExists = File.Exists(targetFile);

            if (tool.RequiresTarget && !targetExists)
            {
                if (!AllFiles.TryCreateFile(targetFile, useEmptyStringForTextFiles: true))
                {
                    return(Task.FromResult(LaunchResult.NoEmptyFileForExtension));
                }
            }

            return(InnerLaunch(tool, tempFile, targetFile));
        }