/// <summary> /// flavor a single file /// </summary> /// <param name="input">input file to flavor</param> /// <param name="output">where the file is mirrored to, flavored</param> /// <param name="activeFlavors">a list of all active flavors</param> /// <param name="logSession">logging session for this file</param> public FlavorResult FlavorFile(FileInfo input, FileInfo output, List <string> activeFlavors, Log.AsyncLogSession logSession) { //check filter first bool shouldFlavor = FlavorFilter.MatchesFilter(input); //check if file should be included if (!IncludeFilter.ShouldInclude(input, output, shouldFlavor)) { logSession?.v("Skipping file: does not match includeFilter!"); return(FlavorResult.Skipped); } //check if file should be flavored or just copied if (shouldFlavor) { //process into a temp file FileInfo temp = new FileInfo(Path.GetTempFileName()); using (StreamReader inputStream = input.OpenText()) using (StreamWriter outputStream = temp.CreateText()) { seasoner.FlavorStream(inputStream, outputStream, activeFlavors, logSession); } //check if old output and temp file are the same if (output.Exists && FileComparator.IsSameFile(output, temp)) { logSession.v("old processed file matches the new one, not replacing"); temp.Delete(); return(FlavorResult.FlavoredSkipped); } else { //not equal, move temp to output string delTemp = temp.FullName; temp.MoveTo(output.FullName, true); IncludeFilter.PostProcessed(input, output); //delete old temp file if (File.Exists(delTemp)) { File.Delete(delTemp); } return(FlavorResult.FlavoredReplace); } } else { //check if file in output is the same as in input if (output.Exists && FileComparator.IsSameFile(input, output)) { logSession.v("old copied file matches file in input, skipping"); return(FlavorResult.Skipped); } //copy file from input to output input.CopyTo(output.FullName, true); IncludeFilter.PostProcessed(input, output); return(FlavorResult.Copied); } }
/// <summary> /// splits the expression list into flavor/content pairs /// </summary> /// <param name="expression">the expression to split into pairs</param> /// <returns>the flavor/content pairs</returns> Dictionary <string /*flavor name*/, string /*flavor content*/> GetFlavorsFromExpression(string expression, Log.AsyncLogSession logSession) { //init dictionary to hold results Dictionary <string, string> flavorDict = new Dictionary <string, string>(); //match all expressions MatchCollection expressionMatches = Regex.Matches(expression, FLAVOR_EXPRESSION_PATTERN); logSession?.d($"Processing expressions for {expression}, found {expressionMatches.Count} matches."); if (expressionMatches.Count <= 0) { return(null); } //enumerate all expressionss foreach (Match expressionMatch in expressionMatches) { //skip if no succcess or CG1 or CG2 are not valid if (!expressionMatch.Success || string.IsNullOrWhiteSpace(expressionMatch.Groups[1].Value) || expressionMatch.Groups[2].Value == null) { continue; } //get flavor name and flavor content string flavorName = expressionMatch.Groups[1].Value; string flavorContent = expressionMatch.Groups[2].Value; //do some processing with the strings flavorName = flavorName.ToLower().Trim(); flavorContent = flavorContent.Trim(); //skip if dict already contains this flavor name if (flavorDict.ContainsKey(flavorName)) { logSession?.w($"Duplicate flavor {flavorName} in expression {expression}!"); continue; } //add to dict logSession?.v($"add {flavorName} - {flavorContent} to dict"); flavorDict.Add(flavorName, flavorContent); } //return null if dict is empty logSession?.v($"flavorDict.Count is {flavorDict.Count}"); if (flavorDict.Count <= 0) { return(null); } //return filled dict return(flavorDict); }
/// <summary> /// flavors a stream of text /// </summary> /// <param name="input">the input stream (eg. source file)</param> /// <param name="output">the output file (eg. output file)</param> /// <param name="activeFlavors">a list of currently active flavors</param> /// <param name="logSession">log session for logging</param> public void FlavorStream(StreamReader input, StreamWriter output, List <string> activeFlavors, Log.AsyncLogSession logSession) { //log processing the file logSession?.v($"Start processing stream..."); //process line by line string ln; int lnCount = 0; while ((ln = input.ReadLine()) != null) { //set logging tag logSession?.PushTag($"{logSession?.GetTag()}:{lnCount++}"); //flavor string and write to output ln = FlavorString(ln, activeFlavors, logSession); output.WriteLine(ln); //pop back tag logSession?.PopTag(); } }