|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Globalization; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using EnvDTE; |
| 9 | +using Microsoft.VisualStudio.Shell.Interop; |
| 10 | +using Microsoft.VisualStudio.TemplateWizard; |
| 11 | + |
| 12 | +namespace LigerShark.Templates |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Custom wizard used by the Angular2 item templates to produce proper names |
| 16 | + /// </summary> |
| 17 | + public class Angular2RenameWizard : Component, IWizard |
| 18 | + { |
| 19 | + public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) |
| 20 | + { |
| 21 | + try |
| 22 | + { |
| 23 | + var safeItemName = replacementsDictionary["$safeitemname$"]; |
| 24 | + var properName = safeItemName; |
| 25 | + |
| 26 | + if (safeItemName.Contains(".")) |
| 27 | + { |
| 28 | + var indexOfPeriod = safeItemName.IndexOf(".", StringComparison.CurrentCulture); |
| 29 | + properName = safeItemName.Remove(indexOfPeriod); |
| 30 | + } |
| 31 | + |
| 32 | + properName = FromCamelCase(properName); |
| 33 | + |
| 34 | + replacementsDictionary.Add("$properName$", properName); |
| 35 | + } |
| 36 | + catch (Exception ex) |
| 37 | + { |
| 38 | + LogError(ex.ToString()); |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + public void ProjectFinishedGenerating(Project project) |
| 44 | + { |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + public void ProjectItemFinishedGenerating(ProjectItem projectItem) |
| 49 | + { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public bool ShouldAddProjectItem(string filePath) |
| 54 | + { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + public void BeforeOpeningFile(ProjectItem projectItem) |
| 59 | + { |
| 60 | + } |
| 61 | + |
| 62 | + public void RunFinished() |
| 63 | + { |
| 64 | + } |
| 65 | + |
| 66 | + private string FromCamelCase(string value) |
| 67 | + { |
| 68 | + if (string.IsNullOrEmpty(value)) |
| 69 | + { |
| 70 | + return value; |
| 71 | + } |
| 72 | + if (char.IsUpper(value[0])) |
| 73 | + { |
| 74 | + return value; |
| 75 | + } |
| 76 | + |
| 77 | + var chArray = value.ToCharArray(); |
| 78 | + |
| 79 | + if (value.Length < 3) |
| 80 | + { |
| 81 | + chArray[0] = char.ToUpper(chArray[0], CultureInfo.InvariantCulture); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + |
| 86 | + if (char.IsUpper(chArray[2])) |
| 87 | + { |
| 88 | + for (var i = 0; i < 2; i++) |
| 89 | + { |
| 90 | + chArray[i] = char.ToUpper(chArray[i], CultureInfo.InvariantCulture); |
| 91 | + } |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + chArray[0] = char.ToUpper(chArray[0], CultureInfo.InvariantCulture); |
| 96 | + } |
| 97 | + } |
| 98 | + return new string(chArray); |
| 99 | + } |
| 100 | + |
| 101 | + private void LogError(string message) |
| 102 | + { |
| 103 | + try |
| 104 | + { |
| 105 | + IVsActivityLog _log = GetService(typeof(SVsActivityLog)) as IVsActivityLog; |
| 106 | + |
| 107 | + _log.LogEntry( |
| 108 | + (UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, |
| 109 | + this.ToString(), |
| 110 | + string.Format(CultureInfo.CurrentCulture, "{0}", message)); |
| 111 | + } |
| 112 | + catch (Exception) |
| 113 | + { |
| 114 | + // there was likely an error getting the activity service, ignore it so it won't throw |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments