public void Process(SitecoreResourceManagerArgs args) { if (args.TargetControllerPath.IsNullOrEmpty() || args.ControllerAction.IsNullOrEmpty()) { Log.Debug("Unable to add to existing controller due to the lack of _TARGETCONTROLLERPATH_ and/or _CONTROLLERACTION_"); return; } if (!args.TargetControllerPath.EndsWith(".cs")) { Log.Debug("Unable to add to existing controller due to _TARGETCONTROLLERPATH_ not ending in .cs"); return; } string text = System.IO.File.ReadAllText(args.TargetControllerPath); int index = text.LastIndexOf('}'); if (index > -1) { index--; } index = text.LastIndexOf('}', index); string action = ReplaceAllTokens.ReplaceTokens(args.ActionFormat ?? ActionFormat, args, "Controller Code"); text = text.Insert(index - 1, action); Log.Debug($"Adding controller action to controller at {args.TargetControllerPath}"); System.IO.File.WriteAllText(args.TargetControllerPath, text); args.EventLog.Add($"Added code to the existing controller {args.TargetControllerPath}"); }
public void Process(SitecoreResourceManagerArgs args) { if (Path.GetFileName(args.Wrapper.TemplateZip.ToLower()) != _applicableTemplateZip.ToLower()) { return; } var fileName = ReplaceAllTokens.ReplaceTokens(_fileName, args); var template = ReplaceAllTokens.ReplaceTokens(_template, args); var path = Directory.EnumerateFiles(args.OverlayTarget, $"*{fileName}*", SearchOption.AllDirectories).FirstOrDefault(x => (Path.GetFileName(x)?.ToLower() ?? string.Empty) == fileName.ToLower()); if (path == null) { return; } var content = File.ReadAllText(path); int index = content.LastIndexOf('}'); if (index > -1) { index--; } index = content.LastIndexOf('}', index); content = content.Insert(index, template); File.WriteAllText(path, content); }
public void Process(SitecoreResourceManagerArgs args) { if (Path.GetFileName(args.Wrapper.TemplateZip.ToLower()) != _applicableTemplateZip.ToLower()) { return; } var fileName = ReplaceAllTokens.ReplaceTokens(_fileName, args); var template = ReplaceAllTokens.ReplaceTokens(_template, args); var methodName = ReplaceAllTokens.ReplaceTokens(_methodName, args); var path = Directory.EnumerateFiles(args.OverlayTarget, $"*{fileName}*", SearchOption.AllDirectories).FirstOrDefault(x => (Path.GetFileName(x)?.ToLower() ?? string.Empty) == fileName.ToLower()); if (path == null) { return; } var content = File.ReadAllText(path); int index = content.IndexOf(methodName, StringComparison.Ordinal); if (index == -1) { return; } int brace = content.IndexOf("{", index, StringComparison.Ordinal); int semi = content.IndexOf(";", index, StringComparison.Ordinal); while (semi < brace) { index = content.IndexOf(methodName, index, StringComparison.Ordinal); if (index == -1) { return; } brace = content.IndexOf("{", index, StringComparison.Ordinal); semi = content.IndexOf(";", index, StringComparison.Ordinal); } if (_insertAtEnd) { int tracker = 0; for (; brace < content.Length; brace++) { if (content[brace] == '{') { tracker++; } else if (content[brace] == '}') { tracker--; } if (tracker == 0) { break; } } if (tracker != 0) { return; } brace--; } content = content.Insert(brace, template); File.WriteAllText(path, content); }