public string UpdateFunctionJS(DTO.FunctionDefinitions funcDef) { //For næsten alt i denne metode tjek Create string functionName = funcDef.Name; string functionString = funcDef.FunctionData; string path = Directory.GetCurrentDirectory() + @"\DllerJS\" + functionName + @"\"; if (File.Exists(path + functionName + ".json")) { Directory.CreateDirectory(path + functionName + @"BackUps\" + functionName + "BackUp" + (Directory.GetDirectories(path + functionName + @"BackUps").Count() + 1)); string pathForBackUp = path + functionName + @"BackUps\" + functionName + "BackUp" + (Directory.GetDirectories(path + functionName + @"BackUps").Count()) + @"\" + functionName + "BackUp" + (Directory.GetDirectories(path + functionName + @"BackUps").Count()); string res = "ERROR WHILE UPDATING FUNCTION " + functionName; try { File.Move(path + functionName + ".json", pathForBackUp + ".json"); File.WriteAllText(Directory.GetCurrentDirectory() + @"\DllerJS\" + functionName + @"\" + functionName + ".json", JsonConvert.SerializeObject(funcDef)); res = "FUNCTION '" + functionName + "' WAS UPDATED!"; } catch (Exception ex) { Console.WriteLine(ex); } return(res); } else { return("FUNCTION '" + functionName + "' DOES NOT EXIST!"); } }
/// <summary> /// Reads the funktions code and returns it to the client /// </summary> /// <param name="functionName">String med navnet på funktionen</param> /// <returns>Sender tilbage filens kode eller om at filen ikke findes </returns> public string ReadFunction(string functionName, Language currentLanguage) { string path; string basePath = currentLanguage == Language.Sharp ? Directory.GetCurrentDirectory() + @"\Dller\" : Directory.GetCurrentDirectory() + @"\DllerJS\"; if (!functionName.Contains("BackUp")) { path = basePath + functionName + @"\"; } else { var folderName = functionName.Split("BackUp")[0]; path = basePath + folderName + @"\" + folderName + @"BackUps\" + functionName + @"\"; } if (File.Exists(path + functionName + ".json")) { DTO.FunctionDefinitions func = JsonConvert.DeserializeObject <DTO.FunctionDefinitions>(File.ReadAllText(path + functionName + ".json")); return(func.FunctionData); } else { return("FUNCTION '" + functionName + "' DOES NOT EXIST!"); } }
/// <summary> /// Updates a functon /// </summary> /// <param name="jsonobj">DTO.FunctionDefinitons, dataen til den JSON fil der skal laves</param> /// <returns>Svare tilbage om det lykkedes at opdatere eller ej</returns> public string UpdateFunction(DTO.FunctionDefinitions jsonobj) { //For næsten alt i denne metode tjek Create string functionName = jsonobj.Name; string functionString = jsonobj.FunctionData; string path = Directory.GetCurrentDirectory() + @"\Dller\" + functionName + @"\"; if (File.Exists(path + functionName + ".dll")) { Directory.CreateDirectory(path + functionName + @"BackUps\" + functionName + "BackUp" + (Directory.GetDirectories(path + functionName + @"BackUps").Count() + 1)); string pathForBackUp = path + functionName + @"BackUps\" + functionName + "BackUp" + (Directory.GetDirectories(path + functionName + @"BackUps").Count()) + @"\" + functionName + "BackUp" + (Directory.GetDirectories(path + functionName + @"BackUps").Count()); string fileName = functionName + "Holder.dll"; var pathToEmit = Path.Combine(path, fileName); SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(functionString); var compilation = CSharpCompilation.Create(fileName, new SyntaxTree[] { syntaxTree }, defaultReferences, options); string res = "ERROR WHILE UPDATING FUNCTION " + functionName; try { var result = compilation.Emit(pathToEmit); if (result.Success) { res = functionName + " was updated!"; //De nu gamle funktions filer bliver rykket og "holder" filen bliver om døbt til at blive den nye "hoved fil" File.Move(path + functionName + ".dll", pathForBackUp + ".dll"); File.Move(path + functionName + ".json", pathForBackUp + ".json"); File.Move(path + functionName + "Holder.dll", path + functionName + ".dll"); File.WriteAllText(Directory.GetCurrentDirectory() + @"\Dller\" + functionName + @"\" + functionName + ".json", JsonConvert.SerializeObject(jsonobj)); } else { Directory.Delete(pathForBackUp); File.Delete(path + functionName + "Holder.dll"); IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (Diagnostic diagnostic in failures) { Console.Error.WriteLine("\t{0}: {1}", diagnostic.Id, diagnostic.GetMessage()); } } } catch (Exception ex) { Console.WriteLine(ex); } return(res); } else { return("FUNCTION '" + functionName + "' DOES NOT EXIST!"); } }
// ****************************************JavaScript************************************************ public string CreateFunctionJS(DTO.FunctionDefinitions funcDef) { //Her laves der 2 strings some indeholder navnet og koden til den funktion der skal oprettes string functionName = funcDef.Name; string functionString = funcDef.FunctionData; //Her laver vi en string med vores pat for nem brug senere string path = Directory.GetCurrentDirectory() + @"\DllerJS\" + functionName + @"\"; //Tjekker om mappen findes if (!Directory.Exists(path)) { //Hvis mappen ikke findes laver vi den + dens backup mappe Directory.CreateDirectory(Directory.GetCurrentDirectory() + @"\DllerJS\" + functionName); Directory.CreateDirectory(path + functionName + "BackUps"); //result string bliver lavet med error fejlen så den er klar til at blive sendt hvis der er fejl string res = "ERROR WHILE CREATING FUNCTION '" + functionName + "'"; try { //JSON fil den funktionen bliver lavet File.WriteAllText(path + functionName + ".json", JsonConvert.SerializeObject(funcDef)); //Result string bliver fyldt ud med sucess resultat res = "FUNCTION '" + functionName + "' WAS CREATED"; } catch (Exception ex) { //Hvis der opstår en fejl vi ikke kender til Console.WriteLine(ex); } //Retunere at der var et problem mens at filen blev lavet return(res); } else { //Sender tilbage at funktionen allerede eksistere return("FUNCTION '" + functionName + "' ALREADY EXSISTS!"); } }
/// <summary> /// Creates DLL files and JSON files for functions /// </summary> /// <param name="jsonObj">DTO.FunctionDefinitons, dataen til den JSON fil der skal laves</param> /// <returns>Svare tilbage om filen blev lavet eller ej</returns> public string CreateFunction(DTO.FunctionDefinitions jsonObj) { //Her laves der 2 strings some indeholder navnet og koden til den funktion der skal oprettes string functionName = jsonObj.Name; string functionString = jsonObj.FunctionData; //Her laver vi en string med vores pat for nem brug senere string path = Directory.GetCurrentDirectory() + @"\Dller\" + functionName + @"\"; //Tjekker om mappen findes if (!Directory.Exists(path)) { //Hvis mappen ikke findes laver vi den + dens backup mappe Directory.CreateDirectory(Directory.GetCurrentDirectory() + @"\Dller\" + functionName); Directory.CreateDirectory(path + functionName + "BackUps"); //her lavet vi en string som har navnet på den DLL fil der skal laves string fileName = functionName + ".dll"; var pathToEmit = Path.Combine(path, fileName); //Her der tjekker vi om syntaxen er korrekt SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(functionString); //Her bliver koden compilet var compilation = CSharpCompilation.Create(fileName, new SyntaxTree[] { syntaxTree }, defaultReferences, options); //result string bliver lavet med error fejlen så den er klar til at blive sendt hvis der er fejl string res = "ERROR WHILE CREATING FUNCTION '" + functionName + "'"; try { //Får fat i den resulteret fil var result = compilation.Emit(pathToEmit); //tjekker om hvis DLL filen blev lavet uden problemer if (result.Success) { //JSON fil den funktionen bliver lavet File.WriteAllText(path + functionName + ".json", JsonConvert.SerializeObject(jsonObj)); //Result string bliver fyldt ud med sucess resultat res = "FUNCTION '" + functionName + "' WAS CREATED"; } else { //Sletter mappen der er lavet hvis der er en error DeleteFunction(functionName, Language.Sharp); //Får sat error messagesne ind IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (Diagnostic diagnostic in failures) { //sender error messagesne Console.Error.WriteLine("\t{0}: {1}", diagnostic.Id, diagnostic.GetMessage()); } } } catch (Exception ex) { //Hvis der opstår en fejl vi ikke kender til Console.WriteLine(ex); } //Retunere at der var et problem mens at filen blev lavet return(res); } else { //Sender tilbage at funktionen allerede eksistere return("FUNCTION '" + functionName + "' ALREADY EXSISTS!"); } }