Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreOlivierBrillant committed Oct 16, 2024
1 parent 166dae9 commit 95d5f72
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
1 change: 0 additions & 1 deletion scriptsharp/ScriptSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private static async Task Main()
{
//clear the log file
LogSingleton.Get.LogAndWriteLine("Bienvenue dans l'installeur pour les cours de mobile");
Environment.Exit(0);
LogSingleton.Get.LogAndWriteLine("ATTENTION DE BIEN ATTENDRE LA FIN DE L'INSTALLATION AVANT D'OUVRIR UN PROJET");
LogSingleton.Get.LogAndWriteLine("Une fois un projet ouvert, surtout choisir Automatically si on vous propose de configurer Defender");
LogSingleton.Get.LogAndWriteLine("Un fichier de log de l'installation est dispo sur le bureau, dossier log ");
Expand Down
113 changes: 56 additions & 57 deletions scriptsharp/ScriptSharp/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace ScriptSharp;

public class Utils
public static class Utils
{
public static async Task ConvertZipTo7ZAsync(string zipFilePath, string output7ZFilePath)
{
Expand All @@ -32,14 +32,15 @@ public static async Task ConvertZipTo7ZAsync(string zipFilePath, string output7Z
CreateNoWindow = true
};

using (Process process = new Process { StartInfo = processStartInfo })
using Process process = new Process();

process.StartInfo = processStartInfo;
process.Start();
await process.WaitForExitAsync();

if (process.ExitCode != 0)
{
process.Start();
await process.WaitForExitAsync();
if (process.ExitCode != 0)
{
throw new Exception($"7-Zip exited with code {process.ExitCode}");
}
throw new Exception($"7-Zip exited with code {process.ExitCode}");
}
}
finally
Expand Down Expand Up @@ -67,25 +68,23 @@ public static async Task DownloadFileAsync(string url, string filePath)
long totalBytes = response.Content.Headers.ContentLength ?? -1;
long totalRead = 0L;
byte[] buffer = new byte[8192];
int bytesRead;
double lastReportedProgress = 0;
using (Stream contentStream = await response.Content.ReadAsStreamAsync(),
fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192,
true))
await using (Stream contentStream = await response.Content.ReadAsStreamAsync(),
fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192,
true))
{
while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
int bytesRead;
while ((bytesRead = await contentStream.ReadAsync(buffer)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead));
totalRead += bytesRead;
if (totalBytes != -1)
{
double progress = (double)totalRead / totalBytes * 100;
if (progress - lastReportedProgress >= 10)
{
LogSingleton.Get.LogAndWriteLine($"Téléchargement de {url} : {progress:F1}%");
lastReportedProgress = progress;
}
}
if (totalBytes == -1) continue;
double progress = (double)totalRead / totalBytes * 100;

if (!(progress - lastReportedProgress >= 10)) continue;

LogSingleton.Get.LogAndWriteLine($"Téléchargement de {url} : {progress:F1}%");
lastReportedProgress = progress;
}
}
}
Expand All @@ -107,23 +106,23 @@ public static void RunCommand(string command)
CreateNoWindow = true
};

using (Process process = Process.Start(processStartInfo))
using Process process = Process.Start(processStartInfo);

string currentTime = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string outputFilePath = Path.Combine(Config.LogPath, $"Commande-{currentTime}.txt");

using (StreamWriter writer = new StreamWriter(outputFilePath))
{
string currentTime = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string outputFilePath = Path.Combine(Config.LogPath, $"Commande-{currentTime}.txt");
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
writer.WriteLine("Trace de l'exeuction de la commande:");
writer.WriteLine(command);
writer.WriteLine(process.StandardOutput.ReadToEnd());
writer.WriteLine(process.StandardError.ReadToEnd());
writer.Close();
}
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"Command exited with code {process.ExitCode}");
}
writer.WriteLine("Trace de l'exeuction de la commande:");
writer.WriteLine(command);
writer.WriteLine(process.StandardOutput.ReadToEnd());
writer.WriteLine(process.StandardError.ReadToEnd());
writer.Close();
}
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"Command exited with code {process.ExitCode}");
}
}catch (Exception ex)
{
Expand All @@ -144,14 +143,15 @@ public static async Task CompressFolderTo7ZAsync(string folderPath, string outpu
CreateNoWindow = true
};

using (Process process = new Process { StartInfo = processStartInfo })
using Process process = new Process();

process.StartInfo = processStartInfo;
process.Start();
await process.WaitForExitAsync();

if (process.ExitCode != 0)
{
process.Start();
await process.WaitForExitAsync();
if (process.ExitCode != 0)
{
throw new Exception($"7-Zip exited with code {process.ExitCode}");
}
throw new Exception($"7-Zip exited with code {process.ExitCode}");
}
}

Expand All @@ -160,7 +160,7 @@ public static async Task Unzip7ZFileAsync(string sourceFile, string destinationF
LogSingleton.Get.LogAndWriteLine("Dézippage avec 7z commencé pour " + sourceFile + " dans " + destinationFolder);
try
{
string sevenZipPath = @"C:\Program Files\7-Zip\7z.exe";
const string sevenZipPath = @"C:\Program Files\7-Zip\7z.exe";
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = sevenZipPath,
Expand All @@ -170,14 +170,15 @@ public static async Task Unzip7ZFileAsync(string sourceFile, string destinationF
CreateNoWindow = true
};

using (Process process = new Process { StartInfo = processStartInfo })
using Process process = new Process();

process.StartInfo = processStartInfo;
process.Start();
await process.WaitForExitAsync();

if (process.ExitCode != 0)
{
process.Start();
await process.WaitForExitAsync();
if (process.ExitCode != 0)
{
throw new Exception($"7-Zip s'est terminé avec le code {process.ExitCode}");
}
throw new Exception($"7-Zip s'est terminé avec le code {process.ExitCode}");
}
}
catch (Exception ex)
Expand Down Expand Up @@ -232,8 +233,6 @@ public static void CreateDesktopShortcut(string shortcutName, string targetPath)
{
LogSingleton.Get.LogAndWriteLine(" ERREUR Raccourci pour " + targetPath);
}


}

private static void RunPowerShellCommand(string command)
Expand All @@ -249,7 +248,7 @@ private static void RunPowerShellCommand(string command)

using Process process = Process.Start(processStartInfo)!;

process.OutputDataReceived += (sender, e) => LogSingleton.Get.LogAndWriteLine(e.Data);
process.OutputDataReceived += (_, e) => LogSingleton.Get.LogAndWriteLine(e.Data);
process.BeginOutputReadLine();
process.WaitForExit();

Expand Down

0 comments on commit 95d5f72

Please sign in to comment.