private string PreProcessFile(string scriptFilePath, PreprocessorContext context) { try { var scriptContent = _fileSystem.ReadAllText(scriptFilePath); var processedScript = new StringBuilder(); var lastIndex = 0; foreach (Match match in ImportRegexp.Matches(scriptContent)) { processedScript.Append(scriptContent, lastIndex, match.Index - lastIndex); var includedFilePath = match.Groups[groupnum : 1].Value.Trim(); var subScriptFilePath = ResolveScriptPath(includedFilePath, context); var subScriptContent = PreProcessFile(subScriptFilePath, context.CreateContextFor(subScriptFilePath)); processedScript.Append(subScriptContent); lastIndex = match.Index + match.Length; } if (lastIndex < scriptContent.Length) { processedScript.Append(scriptContent, lastIndex, scriptContent.Length - lastIndex); } return(processedScript.ToString()); } catch (IOException e) { throw new ScriptCannotBeAccessedException(e.Message, e); } }
/// <summary> /// </summary> /// <param name="scriptFilePath"></param> /// <param name="includeFolders"></param> /// <returns>The preprocessed content of the script at the given path</returns> public string ProcessFile(string scriptFilePath, IReadOnlyList <string> includeFolders) { var content = PreProcessFile(MakeAbsolute(scriptFilePath), PreprocessorContext.CreateFor(scriptFilePath, includeFolders)); return(Process(content)); }
private string ResolveScriptPath(string filePath, PreprocessorContext context) { if (Path.IsPathRooted(filePath)) { return(filePath); } foreach (var includeFolder in context.IncludeFolders) { var path = Path.Combine(includeFolder, filePath + ".cs"); if (_fileSystem.FileExists(path)) { return(path); } } throw new NotImplementedException(); }
public static PreprocessorContext CreateFor(string scriptFilePath, IReadOnlyList <string> includeFolders) { var tmp = new PreprocessorContext(includeFolders); return(tmp.CreateContextFor(scriptFilePath)); }