55using System . IO ;
66using System . Text ;
77using System . Diagnostics ;
8+ using System . Threading . Tasks ;
89using WGetNET . HelperClasses ;
910
1011namespace WGetNET
@@ -15,7 +16,7 @@ namespace WGetNET
1516 /// </summary>
1617 internal class ProcessManager
1718 {
18- private readonly ProcessStartInfo _winGetStartInfo ;
19+ private readonly ProcessStartInfo _winGetStartInfoTemplate ;
1920
2021 /// <summary>
2122 /// Initializes a new instance of the <see cref="WGetNET.ProcessManager"/> class.
@@ -25,7 +26,7 @@ internal class ProcessManager
2526 /// </param>
2627 public ProcessManager ( string processName )
2728 {
28- _winGetStartInfo = new ProcessStartInfo ( )
29+ _winGetStartInfoTemplate = new ProcessStartInfo ( )
2930 {
3031 CreateNoWindow = true ,
3132 FileName = processName ,
@@ -46,10 +47,45 @@ public ProcessManager(string processName)
4647 /// </returns>
4748 public ProcessResult ExecuteWingetProcess ( string cmd )
4849 {
49- //Set Arguments
50- _winGetStartInfo . Arguments = cmd ;
50+ return RunProcess ( GetStartInfo ( cmd ) ) ;
51+ }
5152
52- return RunProcess ( ) ;
53+ /// <summary>
54+ /// Asynchronous executes a winget process with the given command and returns the result.
55+ /// </summary>
56+ /// <param name="cmd">
57+ /// A <see cref="System.String"/> representing the command that winget should be executed with.
58+ /// </param>
59+ /// <returns>
60+ /// A <see cref="WGetNET.ProcessResult"/> object,
61+ /// containing the output an exit id of the process.
62+ /// </returns>
63+ public async Task < ProcessResult > ExecuteWingetProcessAsync ( string cmd )
64+ {
65+ return await RunProcessAsync ( GetStartInfo ( cmd ) ) ;
66+ }
67+
68+ /// <summary>
69+ /// Gets the start info for a process.
70+ /// </summary>
71+ /// <param name="cmd">
72+ /// String containig the arguments for the action.
73+ /// </param>
74+ /// <returns>
75+ /// A <see cref="System.Diagnostics.ProcessStartInfo"/> object, for the process.
76+ /// </returns>
77+ private ProcessStartInfo GetStartInfo ( string cmd )
78+ {
79+ return new ProcessStartInfo ( )
80+ {
81+ CreateNoWindow = _winGetStartInfoTemplate . CreateNoWindow ,
82+ FileName = _winGetStartInfoTemplate . FileName ,
83+ RedirectStandardOutput = _winGetStartInfoTemplate . RedirectStandardOutput ,
84+ StandardOutputEncoding = _winGetStartInfoTemplate . StandardOutputEncoding ,
85+ UseShellExecute = _winGetStartInfoTemplate . UseShellExecute ,
86+ WindowStyle = _winGetStartInfoTemplate . WindowStyle ,
87+ Arguments = cmd
88+ } ;
5389 }
5490
5591 /// <summary>
@@ -59,12 +95,12 @@ public ProcessResult ExecuteWingetProcess(string cmd)
5995 /// A <see cref="WGetNET.ProcessResult"/> object,
6096 /// containing the output an exit id of the process.
6197 /// </returns>
62- private ProcessResult RunProcess ( )
98+ private ProcessResult RunProcess ( ProcessStartInfo processStartInfo )
6399 {
64100 ProcessResult result = new ProcessResult ( ) ;
65101
66102 //Create and run process
67- using ( Process proc = new Process { StartInfo = _winGetStartInfo } )
103+ using ( Process proc = new Process { StartInfo = processStartInfo } )
68104 {
69105 proc . Start ( ) ;
70106
@@ -78,6 +114,32 @@ private ProcessResult RunProcess()
78114 return result ;
79115 }
80116
117+ /// <summary>
118+ /// Asynchronous runs a process with the current start informations.
119+ /// </summary>
120+ /// <returns>
121+ /// A <see cref="WGetNET.ProcessResult"/> object,
122+ /// containing the output an exit id of the process.
123+ /// </returns>
124+ private async Task < ProcessResult > RunProcessAsync ( ProcessStartInfo processStartInfo )
125+ {
126+ ProcessResult result = new ProcessResult ( ) ;
127+
128+ //Create and run process
129+ using ( Process proc = new Process { StartInfo = processStartInfo } )
130+ {
131+ proc . Start ( ) ;
132+
133+ result . Output = await ReadSreamOutputAsync ( proc . StandardOutput ) ;
134+
135+ //Wait till end and get exit code
136+ proc . WaitForExit ( ) ;
137+ result . ExitCode = proc . ExitCode ;
138+ }
139+
140+ return result ;
141+ }
142+
81143 /// <summary>
82144 /// Reads the data from the process output to a string array.
83145 /// </summary>
@@ -106,5 +168,34 @@ private string[] ReadSreamOutput(StreamReader output)
106168
107169 return outputArray ;
108170 }
171+
172+ /// <summary>
173+ /// Asynchronous reads the data from the process output to a string array.
174+ /// </summary>
175+ /// <param name="output">
176+ /// The <see cref="System.IO.StreamReader"/> with the process output.
177+ /// </param>
178+ /// <returns>
179+ /// A <see cref="System.String"/> array
180+ /// containing the process output stream content by lines.
181+ /// </returns>
182+ private async Task < string [ ] > ReadSreamOutputAsync ( StreamReader output )
183+ {
184+ string [ ] outputArray = new string [ 0 ] ;
185+
186+ //Read output to list
187+ while ( ! output . EndOfStream )
188+ {
189+ string ? outputLine = await output . ReadLineAsync ( ) ;
190+ if ( outputLine is null )
191+ {
192+ continue ;
193+ }
194+
195+ outputArray = ArrayManager . Add ( outputArray , outputLine ) ;
196+ }
197+
198+ return outputArray ;
199+ }
109200 }
110201}
0 commit comments