Skip to content

Commit

Permalink
Fix try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreOlivierBrillant committed Oct 17, 2024
1 parent c96f576 commit 196ba8f
Showing 1 changed file with 56 additions and 45 deletions.
101 changes: 56 additions & 45 deletions scriptsharp/ScriptSharp/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static async Task ConvertZipTo7ZAsync(string zipFilePath, string output7Z
};

using Process process = new Process();

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

if (process.ExitCode != 0)
{
throw new Exception($"7-Zip exited with code {process.ExitCode}");
Expand All @@ -53,8 +53,8 @@ public static async Task ConvertZipTo7ZAsync(string zipFilePath, string output7Z
}
LogSingleton.Get.LogAndWriteLine("Conversion de ZIP en 7z fini pour " + zipFilePath);
}


public static readonly object LogLock = new object();

public static async Task DownloadFileAsync(string url, string filePath)
Expand All @@ -80,9 +80,9 @@ public static async Task DownloadFileAsync(string url, string filePath)
totalRead += bytesRead;
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,10 +107,10 @@ public static void RunCommand(string command)
};

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))
{
writer.WriteLine("Trace de l'exeuction de la commande:");
Expand All @@ -124,7 +124,8 @@ public static void RunCommand(string command)
{
throw new Exception($"Command exited with code {process.ExitCode}");
}
}catch (Exception ex)
}
catch (Exception ex)
{
LogSingleton.Get.LogAndWriteLine($" ERREUR Une erreur est survenue en executant : {command} :: {ex.Message}");
}
Expand All @@ -144,11 +145,11 @@ public static async Task CompressFolderTo7ZAsync(string folderPath, string outpu
};

using Process process = new Process();

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

if (process.ExitCode != 0)
{
throw new Exception($"7-Zip exited with code {process.ExitCode}");
Expand All @@ -171,11 +172,11 @@ public static async Task Unzip7ZFileAsync(string sourceFile, string destinationF
};

using Process process = new Process();

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

if (process.ExitCode != 0)
{
throw new Exception($"7-Zip s'est terminé avec le code {process.ExitCode}");
Expand All @@ -196,7 +197,7 @@ public static async Task CopyFileFromNetworkShareAsync(string networkFilePath, s
{
await using FileStream sourceStream = new FileStream(networkFilePath, FileMode.Open, FileAccess.Read,
FileShare.Read, 4096, useAsync: true);

await using FileStream destinationStream = new FileStream(localFilePath, FileMode.Create, FileAccess.Write,
FileShare.None, 4096, useAsync: true);
await sourceStream.CopyToAsync(destinationStream);
Expand All @@ -205,9 +206,9 @@ public static async Task CopyFileFromNetworkShareAsync(string networkFilePath, s
{
LogSingleton.Get.LogAndWriteLine($" ERREUR Copie du fichier Une erreur est survenue: {ex.Message}");
}
LogSingleton.Get.LogAndWriteLine(" FAIT Copie du fichier "+localFilePath);
LogSingleton.Get.LogAndWriteLine(" FAIT Copie du fichier " + localFilePath);
}

public static string GetSdkPath()
{
string sdkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Android", "Sdk");
Expand All @@ -222,8 +223,12 @@ public static void CreateDesktopShortcut(string shortcutName, string targetPath)
string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string linkPath = Path.Combine(desktopFolder, $"{shortcutName}.lnk");
string commande = "$WshShell = New-Object -ComObject WScript.Shell; " +
"$Shortcut = $WshShell.CreateShortcut('"+linkPath+"'); " +
"$Shortcut.TargetPath = '"+targetPath+"'; " +
"$Shortcut = $WshShell.CreateShortcut('" +
linkPath +
"'); " +
"$Shortcut.TargetPath = '" +
targetPath +
"'; " +
"$Shortcut.Save();";
LogSingleton.Get.LogAndWriteLine("Création du raccourci sur le bureau pour " + targetPath);
RunPowerShellCommand(commande);
Expand All @@ -247,17 +252,17 @@ private static void RunPowerShellCommand(string command)
};

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

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

if (process.ExitCode != 0)
{
throw new Exception($" ERREUR PowerShell erreur avec code {process.ExitCode}");
}
}

public static Task StartIntellij()
{
// start android studio
Expand Down Expand Up @@ -306,7 +311,7 @@ public static void AddToPath(string binPath)
string currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User);
if (currentPath == null)
{
SetEnvironmentVariable("PATH", binPath);
SetEnvironmentVariable("PATH", binPath);
}
else if (!currentPath.Contains(binPath))
{
Expand All @@ -319,21 +324,21 @@ public static void AddToPath(string binPath)
private static void RemoveFromPath(string pattern)
{
string currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User);

if (currentPath == null) return;

LogSingleton.Get.LogAndWriteLine("Retrait au Path de " + pattern);

string[] currentPathArray = currentPath.Split(";");
string[] filteredCurrentPathArray = currentPathArray.Where(path => !path.Contains(pattern)).ToArray();
string updatedPath = string.Join(";", filteredCurrentPathArray);

SetEnvironmentVariable("PATH", updatedPath);
}

public static void StartKmb()
{
RunCommand(Program.PathToIntellij()+ " " + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "KickMyB-Server-main"));
RunCommand(Program.PathToIntellij() + " " + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "KickMyB-Server-main"));
}

public static void Reset()
Expand All @@ -346,17 +351,17 @@ public static void Reset()
private static void DeleteAll()
{
DeleteThis(GetSdkPath());
DeleteThis( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"AppData", "Local", "Android"));
DeleteThis( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"AppData", "Local", "Google", "AndroidStudio*"));
DeleteThis( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"AppData", "Roaming", "Google", "AndroidStudio*"));
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*"));
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*"));
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".android"));
DeleteThis(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gradle"));
}

private static void RemoveAllEnv()
{
SetEnvironmentVariable("ANDROID_HOME", null);
Expand All @@ -379,28 +384,34 @@ private static void SetEnvironmentVariable(string name, string value)
Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.Process);
}

private static void DeleteThis(string path)
{
try
DirectoryInfo parent = Directory.GetParent(path)!;
string pattern = new DirectoryInfo(path).Name;

foreach (string dir in Directory.GetDirectories(parent.FullName, pattern))
{
DirectoryInfo parent = Directory.GetParent(path)!;
string pattern = new DirectoryInfo(path).Name;

foreach (string dir in Directory.GetDirectories(parent.FullName, pattern))
try
{
Directory.Delete(dir, true);
}

foreach (string file in Directory.GetFiles(parent.FullName, pattern))
catch (Exception)
{
File.Delete(file);
LogSingleton.Get.LogAndWriteLine("Not found: " + path);
}
}
catch (Exception)

foreach (string file in Directory.GetFiles(parent.FullName, pattern))
{
LogSingleton.Get.LogAndWriteLine("Not found: " + path);
try
{
File.Delete(file);
}
catch (Exception)
{
LogSingleton.Get.LogAndWriteLine("Not found: " + path);
}
}
}
}

}

0 comments on commit 196ba8f

Please sign in to comment.