/// <summary> /// Prepares LPHP-instructions for processing /// </summary> private static void PrepareInstructions() { foreach (string instruction in instructionSessionBuffer.ToArray()) { // [SET] Local variable if (Regex.IsMatch(instruction, @"^set\s*?\S*?\s*?=\s*?[\S\s]*?$")) { string varName = Regex.Replace(instruction, @"^set\s*|\s*?=[\S\s]*?$", ""); object varValue = ValueParser(Regex.Replace(instruction, @"^set[\S\s]*?=\s*", "")); if (!localVariables.ContainsKey(varName)) { localVariables.Add(varName, varValue); } else { LPHPDebugger.PrintWarning("Warning in " + currentCompileFile + ":"); LPHPDebugger.PrintWarning("Variable \"" + varName + "\" gets assigned more than once!"); throw new ApplicationException(); } } // [GLOB] Global variable else if (Regex.IsMatch(instruction, @"^glob\s*?\S*?\s*?=\s*?[\S\s]*?$")) { string varName = Regex.Replace(instruction, @"^glob\s*|\s*?=[\S\s]*?$", ""); object varValue = ValueParser(Regex.Replace(instruction, @"^glob[\S\s]*?=\s*", "")); if (!globalVariables.ContainsKey(varName)) { globalVariables.Add(varName, varValue); } else { LPHPDebugger.PrintWarning("Warning in " + currentCompileFile + ":"); LPHPDebugger.PrintWarning("Global variable \"" + varName + "\" gets assigned more than once!"); throw new ApplicationException(); } } // Ignored instructions else if ( Regex.IsMatch(instruction, @"^Layout\s*?=\s*?\""[\S\s]*?\""$") || Regex.IsMatch(instruction, @"^NoCompile\s*?=\s*?(true|false)$") ) { // Ignore. } // Invalid instruction else { LPHPDebugger.PrintError("*** Error in \"" + currentCompileFile + "\" ***"); LPHPDebugger.PrintError($"Unknown instruction: \"{instruction}\""); throw new ApplicationException(); } // Remove entry from List instructionSessionBuffer.Remove(instruction); } }
/// <summary> /// Constantly checks the given directory for changes and re-compiles as soon as a change is detected. /// </summary> /// <param name="pWatchFolder">Folder to watch</param> private static int Run(bool pRunInfinite = true) { try { if (!string.IsNullOrEmpty(ProjectRoot)) { if (pRunInfinite) { lphpFiles = new Dictionary <string, string>(); } do { foreach (string filePath in Directory.EnumerateFiles(ProjectRoot, "*.*", SearchOption.AllDirectories)) { try { if (Path.GetExtension(filePath) == ".lphp") { using (var md5 = MD5.Create()) { try { using (var stream = File.OpenRead(filePath)) { byte[] md5Bytes = md5.ComputeHash(stream); string md5Hash = Encoding.UTF8.GetString(md5Bytes, 0, md5Bytes.Length); if (!lphpFiles.ContainsKey(md5Hash)) { foreach (KeyValuePair <string, string> entry in lphpFiles.ToArray()) { if (entry.Value == filePath) { lphpFiles[entry.Key] = null; } } foreach (var item in lphpFiles.Where(kvp => kvp.Value == null).ToList()) { lphpFiles.Remove(item.Key); } lphpFiles.Add(md5Hash, filePath); LPHPDebugger.PrintMessage($"\r\nChange detected in {filePath}..."); LPHPCompiler.Run(lphpFiles); LPHPDebugger.PrintSuccess($"Compiled successfully!"); } } } catch (IOException) { LPHPDebugger.PrintWarning("Can't keep up! Compilation-Cycle skipped."); } } } } catch { LPHPDebugger.PrintWarning("Compilation aborted. Please fix all errors shown above and try again."); if (!pRunInfinite) { return(-2); } } } Thread.Sleep(100); }while (pRunInfinite); } else { LPHPDebugger.PrintError("*** LPHP Watchdog Error ***"); LPHPDebugger.PrintError("Please provide a path to the target folder and try again."); if (!pRunInfinite) { return(-1); } } } catch { LPHPDebugger.PrintError("*** Error reading the directory ***"); LPHPDebugger.PrintError("Please make sure the given directory exists."); if (!pRunInfinite) { return(-1); } } return(0); }