} // MainForm /// <summary> /// Called when the form is loaded for the first time. /// First, it finds the available templates. /// Then it loads the macro definitions and fills the macro list view. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void mainForm_Load(object sender, System.EventArgs e) { // Fill the templates combo box with the available templates: these are taken from the names of the folders in the Template folder. DirectoryInfo templateFolder = new DirectoryInfo("Template"); foreach (DirectoryInfo dir in templateFolder.GetDirectories()) { comboBoxTemplates.Items.Add(dir.Name); } if (comboBoxTemplates.Items.Count == 0) { return; } // Select the first template and construct the absolute spec of the meta data file in its folder. TemplateName = templateFolder.GetDirectories()[0].Name; comboBoxTemplates.SelectedIndex = 0; string templateMetaDataFileSpec = Path.Combine(templateFolder.GetDirectories()[0].FullName, "Template.xml"); try { // Read the template meta data from the xml file. Template = TemplateMetaData.ReadFromXml(templateMetaDataFileSpec); } catch (Exception ex) { MessageBox.Show(ex.Message); System.Windows.Forms.Application.Exit(); return; } } // mainForm_Load
} // menuFileExit_Click /// <summary> /// Called whe the user selects an item in the templates combo box. The template meta data is read and the macro list /// view is filled with the macro names and values. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBoxTemplates_SelectedIndexChanged(object sender, EventArgs e) { // Empty the macros list view. listViewMacros.Items.Clear(); // Get the name of the selected template. DirectoryInfo templateFolder = new DirectoryInfo("Template"); TemplateName = templateFolder.GetDirectories()[comboBoxTemplates.SelectedIndex].Name; try { // Read the template meta data of the selected template. string templateMetaDataFileSpec = Path.Combine(templateFolder.GetDirectories()[comboBoxTemplates.SelectedIndex].FullName, "Template.xml"); Template = TemplateMetaData.ReadFromXml(templateMetaDataFileSpec); } catch (Exception ex) { MessageBox.Show(ex.Message); System.Windows.Forms.Application.Exit(); return; } // Display the template description. labelTemplateDescription.Text = Template.Description; // Fill the macros list view with the macros of the selected template. fillMacrosListView(); } // listViewMacros_SelectedIndexChanged
static void Main(string[] args) { try { // Read the meta data of the template given by the first argument. string templatePath = Path.Combine("Template", args[0]); TemplateMetaData template = TemplateMetaData.ReadFromXml(Path.Combine(templatePath, "Template.xml")); // Read the macro values from the file given by the second argument. List <MacroData> macroDataList = MacroData.ReadFromXml(args[1]); // Fill the value fields in the macro definitions with the macro values. foreach (MacroData macroData in macroDataList) { foreach (MacroDefinition macroDefinition in template.MacroDefinitions) { if (macroDefinition.Type == MacroType.UserString && macroDefinition.Name == macroData.Name) { macroDefinition.Value = macroData.Value; } } } // Create the target in the folder given by the third argument. string templateContentPath = Path.Combine(templatePath, "Content"); TargetCreation.TargetCreator.CreateTarget(templateContentPath, template.MacroDefinitions, args[2]); Console.WriteLine("Target successfully created!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } // Main
private static string GenerateTemplateMetaDataCode(CompiledTemplateData compiledTemplateData) { StringBuilder builder = new StringBuilder(2048); LightList <CompiledTemplate> compiledTemplates = compiledTemplateData.compiledTemplates; builder.AppendLine($"{s_Indent12}{nameof(TemplateMetaData)}[] templateData = new {nameof(TemplateMetaData)}[{compiledTemplates.size}];"); builder.AppendLine($"{s_Indent12}{nameof(TemplateMetaData)} template;"); builder.AppendLine($"{s_Indent12}{nameof(StyleSheetReference)}[] styleSheetRefs;"); for (int i = 0; i < compiledTemplates.size; i++) { TemplateMetaData meta = compiledTemplates[i].templateMetaData; if (meta.styleReferences != null && meta.styleReferences.Length > 0) { builder.Append(s_Indent12); builder.Append("styleSheetRefs = new StyleSheetReference["); builder.Append(meta.styleReferences.Length); builder.AppendLine("];"); for (int j = 0; j < meta.styleReferences.Length; j++) { StyleSheetReference sheetReference = meta.styleReferences[j]; builder.Append(s_Indent12); builder.Append("styleSheetRefs["); builder.Append(j); builder.Append("] = new StyleSheetReference("); builder.Append(sheetReference.alias == null ? "null" : "\"" + sheetReference.alias + "\""); builder.Append(", sheetMap[@\""); builder.Append(sheetReference.styleSheet.path); builder.AppendLine("\"]);"); } builder.AppendLine($"{s_Indent12}template = new {nameof(TemplateMetaData)}({compiledTemplates[i].templateId}, @\"{compiledTemplates[i].filePath}\", styleMap, styleSheetRefs);"); } else { builder.AppendLine($"{s_Indent12}template = new {nameof(TemplateMetaData)}({compiledTemplates[i].templateId}, @\"{compiledTemplates[i].filePath}\", styleMap, null);"); } builder.AppendLine($"{s_Indent12}template.BuildSearchMap();"); builder.AppendLine($"{s_Indent12}templateData[{i}] = template;"); } builder.AppendLine($"{s_Indent12}return templateData;"); return(builder.ToString()); }
public void Flatten(TemplateMetaData metaData, LightList <UIStyleGroupContainer> containers) { if (data == null) { return; } switch (type) { case DataType.String: { UIStyleGroupContainer style = metaData.ResolveStyleByName((string)data); if (style != null) { containers.Add(style); } break; } case DataType.CharArray: { UIStyleGroupContainer style = metaData.ResolveStyleByName((char[])data); if (style != null) { containers.Add(style); } break; } case DataType.StringList: { IList <string> list = (IList <string>)data; for (int i = 0; i < list.Count; i++) { UIStyleGroupContainer style = metaData.ResolveStyleByName(list[i]); if (style != null) { containers.Add(style); } } break; } case DataType.StyleRef: { containers.Add((UIStyleGroupContainer)data); break; } case DataType.StyleRefList: { IList <UIStyleGroupContainer> list = (IList <UIStyleGroupContainer>)data; for (int i = 0; i < list.Count; i++) { if (list[i] == null) { continue; } containers.Add(list[i]); } break; } } }
private CTCFile CompileCTO(out string tempCTO) { string tempCTC = null; string tempCPP = null; string tempCTCEXE = null; try { var templates = new List <IVsTemplate>(); foreach (var vsTemplate in this.Templates) { var templateMetadata = new TemplateMetaData( Path.Combine(this.OutputPath, vsTemplate.ItemSpec), new CommandID(new Guid(this.Configuration.Guid), this.Templates.ToList().IndexOf(vsTemplate) + 1), this.Configuration.Name, RegistryHelper.GetCurrentVsRegistryKey(false)); templates.Add(templateMetadata); } CTCFile ctcFile = new CTCBuilder(this.Configuration, this.ComponentPath, null, templates).Create(); tempCTC = Path.GetTempFileName(); using (TextWriter textWriter = new StreamWriter(tempCTC)) { new CTCGenerator(ctcFile, textWriter).Generate(); } tempCPP = Path.GetTempFileName(); File.Delete(tempCPP); tempCPP += ".bat"; using (TextWriter textWriter = new StreamWriter(tempCPP)) { textWriter.WriteLine("@echo off"); textWriter.WriteLine("echo #line 1 %4"); textWriter.WriteLine("type %4"); } tempCTO = Path.GetTempFileName(); tempCTCEXE = Path.GetTempFileName(); File.Delete(tempCTCEXE); tempCTCEXE += ".exe"; using (Stream ctcExeStream = Assembly.GetExecutingAssembly().GetManifestResourceStream( ReflectionHelper.GetAssemblyName(Assembly.GetExecutingAssembly()) + ".CTC.ctc.exe")) { using (FileStream ctcExeFile = new FileStream(tempCTCEXE, FileMode.OpenOrCreate)) { const int bufferSize = 0x1000; // 1Kb Buffer byte[] buffer = new byte[bufferSize]; int readed = bufferSize; while (readed == bufferSize) { readed = ctcExeStream.Read(buffer, 0, bufferSize); ctcExeFile.Write(buffer, 0, readed); } } } Process ctcProcess = new Process(); StringBuilder arguments = new StringBuilder(); arguments.Append(" \""); arguments.Append(tempCTC); arguments.Append("\" \""); arguments.Append(tempCTO); arguments.Append("\" -s- \"-C"); arguments.Append(tempCPP.ToString()); arguments.Append("\""); ctcProcess.StartInfo.UseShellExecute = false; ctcProcess.StartInfo.RedirectStandardError = true; ctcProcess.StartInfo.RedirectStandardOutput = true; ctcProcess.StartInfo.Arguments = arguments.ToString(); ctcProcess.StartInfo.FileName = tempCTCEXE; ctcProcess.StartInfo.WorkingDirectory = this.ComponentPath; if (ctcProcess.Start()) { ctcProcess.WaitForExit(); if (ctcProcess.ExitCode == 0 && File.Exists(tempCTO)) { return(ctcFile); } Debug.Fail("Cannot excute ctc.exe", String.Format(CultureInfo.InvariantCulture, "Invalid exit code {0}, arguments = {1}, stderror= {2}", ctcProcess.ExitCode, arguments.ToString(), ctcProcess.StandardError.ReadToEnd())); } else { Debug.Assert(true, "CTC.exe cannot be started"); } return(null); } finally { try { if (tempCTC != null && File.Exists(tempCTC)) { #if !DEBUG File.Delete(tempCTC); #endif } if (tempCPP != null && File.Exists(tempCPP)) { File.Delete(tempCPP); } if (tempCTCEXE != null && File.Exists(tempCTCEXE)) { File.Delete(tempCTCEXE); } } catch { } } }
/// <summary> /// Executes the task /// </summary> /// <returns></returns> public override bool Execute() { var configuration = Microsoft.Practices.RecipeFramework.GuidancePackage.ReadConfiguration(Path.GetFileName(this.ConfigurationFile.ItemSpec)); var cacheFiles = new List <string>(); var addItemsProjectFactories = new List <Guid>(); List <ITaskItem> templateList = this.Templates.ToList(); foreach (var vsTemplate in this.Templates) { var templateMetadata = new TemplateMetaData( Path.Combine(this.OutputPath, vsTemplate.ItemSpec), new CommandID(new Guid(configuration.Guid), templateList.IndexOf(vsTemplate) + 1), configuration.Name, RegistryHelper.GetCurrentVsRegistryKey(false)); templateMetadata.Register(true); if (templateMetadata.Kind == Common.TemplateKind.ProjectItem && !addItemsProjectFactories.Contains(templateMetadata.ProjectFactory)) { addItemsProjectFactories.Add(templateMetadata.ProjectFactory); } cacheFiles.AddRange(Directory.EnumerateFiles(templateMetadata.CacheBaseBath)); } var registryTemplate = new GuidancePackageRegistryTemplate(); registryTemplate.GuidancePackage = configuration; registryTemplate.SatelliteDllFile = Path.GetFileName(this.SatelliteDllFile); registryTemplate.VsTemplates = this.Templates; registryTemplate.AddItemsProjectFactories = addItemsProjectFactories; registryTemplate.OutputPath = Path.GetFullPath(this.OutputPath); var content = registryTemplate.TransformText(); this.CacheFiles = cacheFiles.Distinct().Select(file => { var item = new TaskItem(file); item.SetMetadata("VSIXSubPath", Path.GetDirectoryName(file).Remove(0, this.OutputPath.Length)); // Fix the output path in the .vsz file if (".vsz".Equals(Path.GetExtension(item.ItemSpec), StringComparison.InvariantCultureIgnoreCase)) { var vsz = File.ReadAllText(file).Replace("Param=\"" + this.OutputPath, "Param=\""); File.Delete(file); File.WriteAllText(file, vsz); } return(item); } ).ToArray(); if (File.Exists(this.OutputFile)) { File.Delete(this.OutputFile); } File.WriteAllText(this.OutputFile, content); return(true); }