dpeter99

Untitled

Jul 3rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace IceBreaker.Model
  9. {
  10.     public class GitWrapper
  11.     {
  12.         static string FileName = @"C:\Program Files\Git" + @"\bin\git.exe";
  13.  
  14.         public delegate void Return(bool succes, string error);
  15.  
  16.         public class ReturnData
  17.         {
  18.             public string output;
  19.         }
  20.  
  21.         public static Task<bool> ExecuteProcess(string dir, string command, Return returnDelegate)
  22.         {
  23.             ReturnData returnData = new ReturnData { output = ""};
  24.  
  25.             ProcessStartInfo gitInfo = new ProcessStartInfo()
  26.             {
  27.                 CreateNoWindow = true,
  28.                 RedirectStandardError = true,
  29.                 RedirectStandardOutput = true,
  30.                 FileName = FileName,
  31.                 UseShellExecute = false,
  32.                 Arguments = command, // such as "fetch orign"
  33.                 WorkingDirectory = dir
  34.             };
  35.  
  36.             Process gitProcess = new Process
  37.             {
  38.                 StartInfo = gitInfo
  39.             };
  40.             gitProcess.ErrorDataReceived += (s,e)=>GitProcess_ErrorDataReceived(s,e,returnData);
  41.             gitProcess.OutputDataReceived += (s, e) => GitProcess_OutputDataReceived(s, e, returnData);
  42.             gitProcess.Exited += GitProcess_Exited;
  43.  
  44.             App.Current.Dispatcher.Invoke(() =>
  45.             {
  46.                 IceConsole.OpenConsole();
  47.             });
  48.  
  49.  
  50.             //THIS IS THE PROBLEM PART
  51.             Task<bool> task = new Task<bool>(async() => { return await RunCommandTask(gitProcess, returnDelegate, returnData); });
  52.  
  53.  
  54.             return task;
  55.         }
  56.  
  57.         static async Task<bool> RunCommandTask(Process process, Return returnDelegate, ReturnData returnData)
  58.         {
  59.             var tcs = new TaskCompletionSource<bool>();
  60.  
  61.             process.Start();
  62.             process.BeginErrorReadLine();
  63.             process.BeginOutputReadLine();
  64.             process.WaitForExit();
  65.  
  66.             //Console.WriteLine("fineshed");
  67.             bool succes = false;
  68.             if (process.ExitCode == 0)
  69.             {
  70.                 succes = true;
  71.             }
  72.             returnDelegate(succes, returnData.output);
  73.  
  74.             return tcs.Task.Result;
  75.  
  76.             return succes;
  77.         }
  78.  
  79.  
  80.         private static void GitProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e,ReturnData returnData)
  81.         {
  82.             if (e.Data != null)
  83.             {
  84.                 IceConsole.WriteLine("[Git] "+e.Data);
  85.                 returnData.output += e.Data + "\n";
  86.             }
  87.  
  88.             //Console.WriteLine(e.Data);
  89.         }
  90.  
  91.         private static void GitProcess_OutputDataReceived(object sender, DataReceivedEventArgs e, ReturnData returnData)
  92.         {
  93.             if (e.Data != null)
  94.             {
  95.                 IceConsole.WriteLine("[Git] "+e.Data);
  96.                 returnData.output += e.Data + "\n";
  97.             }
  98.             //Console.WriteLine(e.Data);
  99.         }
  100.  
  101.         private static void GitProcess_Exited(object sender, EventArgs e)
  102.         {
  103.             //IceConsole.WriteLine(e.Data);
  104.             Console.WriteLine("fineshed");
  105.         }
  106.  
  107.  
  108.         public static Task Clone(string dir, string url, Return returnDelegate)
  109.         {
  110.             return ExecuteProcess(dir, "clone " + url, (e, d) => DefaultEndHandler(e,d,returnDelegate));
  111.         }
  112.  
  113.         public static Task Help()
  114.         {
  115.             return ExecuteProcess("", "help", (e, d) => { });
  116.         }
  117.  
  118.         public static async Task Status(string dir, Return returnDelegate)
  119.         {
  120.             await ExecuteProcess(dir, "status", (e, d) => DefaultEndHandler(e, d, returnDelegate));
  121.         }
  122.  
  123.         public static Task GetCurrentCommit(string dir, Return returnDelegate)
  124.         {
  125.             return ExecuteProcess(dir, "rev-parse HEAD", (e, d) => DefaultEndHandler(e, d, returnDelegate));
  126.         }
  127.  
  128.         public static Task GetCurrentCommitShort(string dir, Return returnDelegate)
  129.         {
  130.             return ExecuteProcess(dir, "rev-parse --short HEAD", (e, d) => DefaultEndHandler(e, d, returnDelegate));
  131.         }
  132.  
  133.         public static Task GetCurrentCommitText(string dir, Return returnDelegate)
  134.         {
  135.             return ExecuteProcess(dir, "log -1 --pretty=%B", (e, d) => DefaultEndHandler(e, d, returnDelegate));
  136.         }
  137.  
  138.         public static Task GetCurrentBranchName(string dir, Return returnDelegate)
  139.         {
  140.             return ExecuteProcess(dir, "rev-parse --abbrev-ref HEAD", (e, d) => DefaultEndHandler(e, d, returnDelegate));
  141.         }
  142.  
  143.         public static Task GetChangedFiles(string dir, Return returnDelegate)
  144.         {
  145.             return ExecuteProcess(dir, "ls-files --other --exclude-standard", (e, d) => DefaultEndHandler(e, d, returnDelegate));
  146.         }
  147.  
  148.         public static Task ResetFile(string dir,string file, Return returnDelegate)
  149.         {
  150.             return ExecuteProcess(dir, "checkout -- " + file, (e, d) => DefaultEndHandler(e, d, returnDelegate));
  151.         }
  152.  
  153.         public static async Task<bool> CheckFileTracking(string dir, string file, Return returnDelegate)
  154.         {
  155.             return await ExecuteProcess(dir, "ls-files --error-unmatch " + file, (e, d) => DefaultEndHandler(e, d, returnDelegate));
  156.         }
  157.  
  158.         public static void DefaultEndHandler(bool succes, string error, Return returnHandler)
  159.         {
  160.             returnHandler?.Invoke(succes, error);
  161.  
  162.             if (succes)
  163.             {
  164.                 if (Properties.Settings.Default.CloseConsole)
  165.                 {
  166.                     App.Current.Dispatcher.Invoke(() =>
  167.                     {
  168.                         IceConsole.CloseConsole();
  169.                     });
  170.                 }
  171.             }
  172.  
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment