/// <summary> /// Initializes a new instance of the <see cref="Recipe"/> class, /// with the associated configuration. /// </summary> /// <param name="configuration">The configuration of the recipe.</param> public Recipe(Configuration.Recipe configuration) { if (configuration == null) { throw new ArgumentNullException("configuration"); } this.recipeConfig = configuration; }
private void CreateDynamicRecipe(Config.GuidancePackage package, out Config.Recipe recipe, out ArrayList arguments, out ArrayList actions) { // Recipe to execute and add references. recipe = new Config.Recipe(); package.Recipes = new Config.Recipe[] { recipe }; recipe.Name = "ReferenceRestoreRecipe"; recipe.Caption = Properties.Resources.ReferenceRestoreService_DialogTitle; arguments = new ArrayList(referencesToFix.Count); actions = new ArrayList(referencesToFix.Count); }
private static void SerializeToAny(Config.Recipe recipe, Wizard wizard) { StringWriter sw = new StringWriter(); new System.Xml.Serialization.XmlSerializer(typeof(Wizard)).Serialize(sw, wizard); XmlDocument doc = new XmlDocument(); doc.LoadXml(sw.ToString()); recipe.GatheringServiceData = new Configuration.RecipeGatheringServiceData(); recipe.GatheringServiceData.Any = doc.DocumentElement; }
public void SetUp() { recipeConfiguration = new Microsoft.Practices.RecipeFramework.Configuration.Recipe(); recipeConfiguration.Name = "Test"; Configuration.Action first = new Configuration.Action(); first.Name = "First"; first.Type = typeof(FirstAction).FullName; Configuration.Action second = new Configuration.Action(); second.Name = "Second"; second.Type = typeof(SecondAction).FullName; recipeConfiguration.Actions = new Configuration.RecipeActions(); recipeConfiguration.Actions.Action = new Configuration.Action[] { first, second }; container = new Microsoft.Practices.ComponentModel.ServiceContainer(); container.AddService(typeof(System.ComponentModel.Design.ITypeResolutionService), new MockResolutionService()); container.AddService(typeof(System.ComponentModel.Design.IDictionaryService), new MockDictionary()); container.AddService(typeof(Microsoft.Practices.RecipeFramework.Services.IConfigurationService), new MockConfigrationService()); container.AddService(typeof(IComponentChangeService), new MockChangeService()); container.AddService(typeof(IValueInfoService), new MockValueInfoService()); }
private void LoadReferences() { this.SuspendLayout(); this.imgImages.Images.Clear(); this.lstRecipes.Items.Clear(); GuidancePackage[] Packages = base.RecipeManagerService.GetEnabledPackages(); ArrayList images = new ArrayList(); // Add the two default images. ToolboxBitmapAttribute boundattr = new ToolboxBitmapAttribute(typeof(IBoundAssetReference)); Image boundimg = boundattr.GetImage(boundattr); this.imgImages.Images.Add(boundimg); // Add gray version this.imgImages.Images.Add(ConvertToGrayscale(boundimg)); ToolboxBitmapAttribute unboundattr = new ToolboxBitmapAttribute(typeof(IUnboundAssetReference)); Image unboundimg = unboundattr.GetImage(unboundattr); this.imgImages.Images.Add(unboundimg); // Add gray version this.imgImages.Images.Add(ConvertToGrayscale(unboundimg)); Hashtable groups = new Hashtable(); ArrayList items = new ArrayList(); // WORKAROUND: fixed for VSWhidbey "by design" bug #440390. Now we load items and groups // first, then sort everything manually, and finally add stuff to the control. // Commented lines should be documented when feature is fixed. foreach (GuidancePackage Package in Packages) { IAssetReferenceService refservice = (IAssetReferenceService) Package.GetService(typeof(IAssetReferenceService), true); IConfigurationService configService = (IConfigurationService)Package.GetService(typeof(IConfigurationService), true); foreach (IAssetReference reference in refservice.GetAll()) { ReferenceInfo info = new ReferenceInfo(reference); #region Determine image int imageindex = images.IndexOf(reference.GetType()); // Determine image to show. if (imageindex == -1) { ToolboxBitmapAttribute bmp = (ToolboxBitmapAttribute)Attribute.GetCustomAttribute( reference.GetType(), typeof(ToolboxBitmapAttribute), true); if (bmp == null) { // Use default attributes. if (reference is IBoundAssetReference) { imageindex = 0; } else { imageindex = 2; } if (!info.IsEnabled) { imageindex++; } } else { imageindex = this.imgImages.Images.Count; Image newimg = bmp.GetImage(reference); if (info.IsEnabled) { this.imgImages.Images.Add(newimg); } else { // Add gray version. this.imgImages.Images.Add(ConvertToGrayscale(newimg)); } images.Add(reference.GetType()); } } else { // Account for the 4 built-in images. imageindex = imageindex + 4; } #endregion Determine image #region Determine group // Bug: Should we fix this? // We should get custom attributes instead of a single attr. // The reference could override the category //CategoryAttribute category = (CategoryAttribute)Attribute.GetCustomAttribute( // reference.GetType(), typeof(CategoryAttribute), true); CategoryAttribute category = (CategoryAttribute)Attribute.GetCustomAttributes( reference.GetType(), typeof(CategoryAttribute), true).First(); // ListViewGroup categorygroup = this.lstRecipes.Groups[category.Category]; ListViewGroup categorygroup = (ListViewGroup)groups[category.Category]; if (categorygroup == null) { //categorygroup = this.lstRecipes.Groups.Add(category.Category, category.Category); categorygroup = new ListViewGroup(category.Category, category.Category); groups.Add(category.Category, categorygroup); } #endregion Determine group Config.Recipe recipe = null; if (reference is RecipeReference) { recipe = configService.CurrentPackage[reference.AssetName]; } string[] subitems = new string[3]; string errors = string.Empty; Exception ex = null; try { subitems[0] = reference.Caption; } catch (Exception e) { if (recipe != null) { subitems[0] = recipe.Caption; } ex = e; errors = "Caption"; } try { subitems[1] = reference.AppliesTo; } catch (Exception e) { subitems[1] = Configuration.Resources.Reference_AppliesToThrew; if (ex == null) { ex = e; } if (!string.IsNullOrEmpty(errors)) { errors += ", "; } errors += "AppliesTo"; } subitems[2] = Package.Configuration.Caption; ListViewItem item = new ListViewItem(subitems, imageindex); item.Tag = info; item.Group = categorygroup; items.Add(item); if (ex != null) { ErrorHelper.Show((IUIService)GetService(typeof(IUIService)), new RecipeExecutionException(reference.AssetName, string.Format(CultureInfo.CurrentCulture, Configuration.Resources.Reference_InvalidAttributes, errors), ex)); } } } // Sort everything. ListViewGroup[] lvgroups = new ListViewGroup[groups.Count]; groups.Values.CopyTo(lvgroups, 0); items.Sort(new ListViewItemSortComparer()); Array.Sort(lvgroups, new GroupSortComparer()); this.lstRecipes.Groups.AddRange(lvgroups); this.lstRecipes.Items.AddRange((ListViewItem[])items.ToArray(typeof(ListViewItem))); this.lstRecipes.Invalidate(); this.ResumeLayout(); if (this.lstRecipes.Items.Count > 0) { splitter.Panel1.Focus(); splitter.Panel1.Select(); ((ListViewItem)items[0]).Selected = true; this.lstRecipes.Focus(); this.lstRecipes.Select(); } else { this.txtDescription.Text = String.Empty; } }
private Hashtable CreateFixupPages(int maxfields, XmlDocument xmlfactory, ArrayList arguments, ArrayList actions, ref Page lastpage, ArrayList pages, ref ArrayList fields, int nPages) { int iPage = 1; int iArgument = 1; bool firstPage = true; Hashtable referenceDictionary = new Hashtable(); foreach (FixupReference fixup in referencesToFix) { if (firstPage || (lastpage != null) && (fields.Count == maxfields)) { #region Create new page firstPage = false; // If we have fields and a previous page, set all fields in one shot. if (lastpage != null && fields != null) { lastpage.Fields = new Field[fields.Count]; fields.CopyTo(lastpage.Fields, 0); } // Start a new page for the fields. lastpage = new Page(); pages.Add(lastpage); fields = new ArrayList(maxfields); lastpage.Title = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_PageTitle, iPage, nPages); lastpage.Help = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_PageHelp); lastpage.LinkTitle = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_PageLinkTitle, iPage, nPages); iPage++; #endregion Create new page } #region Setup recipe arguments Config.Argument argument = new Config.Argument(); argument.Name = string.Format(CultureInfo.InvariantCulture, "Argument{0}", iArgument); argument.Type = GetTargetType(fixup.ExpectedTargetKind).AssemblyQualifiedName; argument.Converter = new Config.Converter(); argument.Converter.Type = GetConverterType(fixup.ExpectedTargetKind); arguments.Add(argument); Config.Argument argumentKeyRef = new Config.Argument(); argumentKeyRef.Name = string.Format(CultureInfo.InvariantCulture, "Reference{0}", iArgument); argumentKeyRef.Type = "Microsoft.Practices.RecipeFramework.IAssetReference, Microsoft.Practices.RecipeFramework.Common"; referenceDictionary.Add(argumentKeyRef.Name, fixup.OldReference); arguments.Add(argumentKeyRef); iArgument++; #endregion Setup recipe arguments #region Create new field Field field = new Field(); if (fixup.OldReference is BoundTemplateReference) { int templateLength = fixup.OwningPackage.BasePath.Length + 11; //11 corresponds to Lenght of string "\Template\" if (templateLength > fixup.ReferencedAsset.Length) { templateLength = 0; } field.Label = string.Format("{0} ({1})", fixup.ReferencedAsset.Substring(templateLength), fixup.SavedTarget); field.Help = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_FieldTemplateHelp, fixup.ExpectedTargetKind, fixup.ReferencedAsset.Substring(templateLength)); } else // case of recipe reference { IConfigurationService configService = (IConfigurationService)fixup.OwningPackage.GetService(typeof(IConfigurationService), true); Config.Recipe recipeReference = configService.CurrentPackage[fixup.OldReference.AssetName]; field.Label = string.Format("{0} ({1})", recipeReference.Caption, fixup.SavedTarget); field.Help = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_FieldRecipeHelp, fixup.ExpectedTargetKind, recipeReference.Caption); } field.Tooltip = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_FieldTooltip, fixup.SavedTarget, fixup.ExpectedTargetKind); field.InvalidValueMessage = String.Format( CultureInfo.CurrentCulture, Properties.Resources.ReferenceRestoreService_FieldInvalid, fixup.ExpectedTargetKind); field.ValueName = argument.Name; field.Editor = new Editor(); // Again, we can't use the type directly to avoid circular references. field.Editor.Type = "Microsoft.Practices.RecipeFramework.Library.Editors.SolutionPickerEditor, Microsoft.Practices.RecipeFramework.Library"; field.PanelType = "Microsoft.Practices.RecipeFramework.VisualStudio.Services.CustomArgumentPanel, Microsoft.Practices.RecipeFramework.VisualStudio"; fields.Add(field); #endregion Create new field #region Setup an action for it Config.Action action = new Config.Action(); // Name will be ugly, but we need to ensure uniqueness too. action.Name = argument.Name; action.Type = typeof(AddFixedReferenceAction).AssemblyQualifiedName; XmlAttribute assetattr; assetattr = xmlfactory.CreateAttribute("Recipe"); assetattr.Value = fixup.ReferencedAsset; XmlAttribute packageattr = xmlfactory.CreateAttribute("TargetPackage"); packageattr.Value = fixup.OwningPackage.Configuration.Name; action.AnyAttr = new XmlAttribute[] { assetattr, packageattr }; // Sync action input with collected argument for the target. Config.Input input = new Config.Input(); input.Name = "Target"; input.RecipeArgument = argument.Name; Config.Input inputRef = new Config.Input(); inputRef.Name = "OldReference"; inputRef.RecipeArgument = argumentKeyRef.Name; action.Input = new Config.Input[] { input, inputRef }; actions.Add(action); #endregion Setup an action for it } return(referenceDictionary); }
private ExecutionResult Execute(string recipe, IAssetReference reference, IDictionary arguments) { if (recipe == null) { throw new ArgumentNullException("recipe"); } ThrowIfAlreadyExecutingRecipe(); Config.Recipe config = ThrowIfRecipeNotConfigured(recipe); ReferenceService referenceService = null; try { string appliesTo = GetReferenceAppliesToOrErrorMessage(reference); this.TraceInformation(Properties.Resources.Recipe_StartingExecution, recipe, reference != null ? String.Format(Properties.Resources.Recipe_ReferenceApplied, appliesTo) : ""); // This "parent" loader service is the one we created in OnSited that // wraps the one provided by the recipe manager itself, and adds resolution // relative to the package assembly location. ITypeResolutionService loader = GetService <ITypeResolutionService>(true); // Create the recipe from the configuration. currentRecipe = new Recipe(config); currentReference = reference; // Setup the alias resolution "gateway" loader service. // This resolves aliases defined for the recipe only. // This service will go away together with the recipe. currentRecipe.AddService(typeof(ITypeResolutionService), new AliasResolutionService( config.TypeAliasesByName, loader)); bool isPersisted; IPersistenceService persistenceService; arguments = LoadPersitedState(reference, arguments, out isPersisted, out persistenceService); // Construct the dictionary service. We don't pass the arguments at this // point as we want to go over the process of setting each in turn, so they are validated. DictionaryService dictionarysvc = new DictionaryService(null); AddService(typeof(IDictionaryService), dictionarysvc); // Expose the IComponentChangeService implementation too. AddService(typeof(IComponentChangeService), dictionarysvc); if (arguments != null && arguments.Count > 0) { bool shouldUpdateState = InitializeDictionaryService(arguments, dictionarysvc); // Persist changes if appropriate. if (isPersisted && shouldUpdateState) { persistenceService.SaveState(Configuration.Name, reference, arguments); } } // Construct the dictionary service, passing the persisted state (or null) as well // as the arguments configuration. // Site and execute the recipe. Add(currentRecipe); referenceService = new ReferenceService(); currentRecipe.AddService(typeof(IReferenceService), referenceService); bool allowsuspend = (reference != null && reference is IBoundAssetReference) || (arguments != null); EnsureInitializeMetadataForCurrentRecipe(); ExecutionResult result = currentRecipe.Execute(allowsuspend); this.TraceInformation(Properties.Resources.Recipe_ExecutionResult, result); if (result == ExecutionResult.Finish) { // If recipe is not recurrent and it's a bound reference, remove it. if (!currentRecipe.Configuration.Recurrent && reference is IBoundAssetReference) { IAssetReferenceService refsvc = GetService <IAssetReferenceService>(true); refsvc.Remove(reference); // Remove the persisted state. persistenceService.RemoveState(Configuration.Name, reference); } // Fire the AfterRecipeExecution event // note this will happen only when the recipe is finished (not suspend nor cancelled) if (AfterRecipeExecution != null) { this.AfterRecipeExecution(this, new RecipeEventArgs(config, isExecutingRecipeFromTemplate)); } } return(result); } finally { #region Cleanup if (referenceService != null) { currentRecipe.RemoveService(typeof(IReferenceService)); } // Remove recipe from container. Remove(currentRecipe); // Remove services we added. RemoveService(typeof(IDictionaryService)); RemoveService(typeof(IComponentChangeService)); // Dispose and clean current variables. if (currentRecipe != null) { currentRecipe.Dispose(); currentRecipe = null; } currentReference = null; #endregion Cleanup // Write a separator on the trace. this.TraceInformation(new string('-', 150)); } }