public static bool ValidateAllPluins() { var plugins = Directory.EnumerateFiles("Plugins/"); var jsonFiles = plugins.Where(a => a.Contains(".json")); bool result = false; foreach (var json in jsonFiles) { var content = File.ReadAllText(Path.GetFullPath(json)); var config = JsonConvert.DeserializeObject <PluginConfig>(content); if (config == null) { throw new Exception($"Fichier configuration Module {json} invalide"); } var jsonIsValide = ModuleErp.PluginIsValide(config.Name, config.User, config.Key); if (jsonIsValide == false) { throw new Exception($"Le module {json} n'est pas valide"); } result = result && jsonIsValide; } return(result); }
public static bool InstallPlugin(string fullPath, string filenameWithExtention, InstallMode mode) { switch (mode) { case InstallMode.Zip: try { ZipArchive archive = ZipFile.OpenRead(Path.GetFullPath(fullPath)); string tempPath = $"temp/plugin_{DateTime.Now.Ticks}"; Directory.CreateDirectory(tempPath); archive.ExtractToDirectory(Path.GetFullPath(tempPath), true); var allfiles = Directory.EnumerateFiles(Path.GetFullPath($"{tempPath}/Plugins")).ToList(); var json = allfiles.FirstOrDefault(a => a.Contains(".json")); //string json = $"{tempPath}/{filenameWithExtention}.json"; if (!File.Exists(json)) { DataHelpers.ShowMessage(_("Invalide package!")); return(false); } var jsonPlugin = File.ReadAllText(json); PluginConfig configC = JsonConvert.DeserializeObject <PluginConfig>(jsonPlugin); if (configC == null) { DataHelpers.ShowMessage(_("Invalide package!")); return(false); } PluginPackage pluginPackage = new PluginPackage(); pluginPackage.Libelle = configC?.Name; pluginPackage.PluginVersion = configC?.Version; pluginPackage.PluginDate = configC?.Release; pluginPackage.PluginLien = configC?.Link; archive.ExtractToDirectory(Environment.CurrentDirectory, true); if (Directory.Exists($"{tempPath}/Plugins")) { var filesPlugins = Directory.EnumerateFiles($"{tempPath}/Plugins"); foreach (var item in filesPlugins) { pluginPackage.AllPluginFiles.Add(new PluginFile(item.Replace(tempPath, ""))); } } if (Directory.Exists($"{tempPath}/templates")) { var filesTemplates = Directory.EnumerateFiles($"{tempPath}/templates"); foreach (var item in filesTemplates) { pluginPackage.AllPluginFiles.Add(new PluginFile(item.Replace(tempPath, ""))); } } if (Directory.Exists($"{tempPath}/Languages")) { var filesLanguages = Directory.EnumerateFiles($"{tempPath}/Languages"); foreach (var item in filesLanguages) { pluginPackage.AllPluginFiles.Add(new PluginFile(item.Replace(tempPath, ""))); } } if (Directory.Exists($"{tempPath}/Reports")) { var filesReports = Directory.EnumerateFiles($"{tempPath}/Reports"); foreach (var item in filesReports) { pluginPackage.AllPluginFiles.Add(new PluginFile(item.Replace(tempPath, ""))); } } pluginPackage.Save(); DataHelpers.ShowMessage(_("Plugin installé, redémarrer l'application")); } catch (Exception s) { DataHelpers.ShowMessageError(s); return(false); } break; case InstallMode.Path: try { if (!string.IsNullOrWhiteSpace(fullPath)) { var file = Path.GetFullPath(fullPath); var safe = Path.GetFileNameWithoutExtension(file); var json = file.Replace(".dll", ".json"); // Path.Combine(location, $"/{safe}.json"); if (File.Exists(json)) { var jsonContent = File.ReadAllText(json); PluginConfig config = JsonConvert.DeserializeObject <PluginConfig>(jsonContent); if (ModuleErp.PluginIsValide(config.Name, config.User, config.Key)) { // Check Dependencies var deps = config.Dependencies; File.Copy(file, $"Plugins/{safe}.dll"); File.Copy(Path.GetFullPath(json), $"Plugins/{safe}.json"); DataHelpers.ShowMessage("Fermer l'application pour que le systéme recharge cette extention"); return(true); } else { DataHelpers.ShowMessage("Key config invalide"); return(false); } } else { DataHelpers.ShowMessage($"Le fichier de configuration {safe}.json est introuvable"); return(false); } } } catch (Exception s) { DataHelpers.ShowMessage(s.Message); } break; default: break; } return(false); }