Пример #1
0
        void ThrowIfValueTypeArgumentIsOptionalNotNullable()
        {
            if (Configuration.Arguments == null || Configuration.Arguments.Length == 0)
            {
                return;
            }
            IValueInfoService infoSvc            = GetService <IValueInfoService>(true);
            ArrayList         valueTypeNullables = new ArrayList(Configuration.Arguments.Length);

            foreach (Config.Argument arg in Configuration.Arguments)
            {
                // ValueTypes must be either required or Nullable<T>.
                ValueInfo info = infoSvc.GetInfo(arg.Name);
                if (info.Type.IsValueType && !arg.Required &&
                    (!info.Type.IsGenericType ||
                     !(info.Type.GetGenericTypeDefinition() == typeof(Nullable <>))))
                {
                    valueTypeNullables.Add(arg.Name);
                }
            }

            if (valueTypeNullables.Count != 0)
            {
                throw new RecipeExecutionException(this.Configuration.Name, String.Format(
                                                       System.Globalization.CultureInfo.CurrentCulture,
                                                       Properties.Resources.Recipe_InvalidOptionalValueType,
                                                       String.Join(", ", (string[])valueTypeNullables.ToArray(typeof(string)))));
            }
        }
Пример #2
0
        void ThrowIfRequiredArgumentsAreNull(IDictionaryService arguments)
        {
            bool requiredAreNull = false;

            if (Configuration.Arguments == null)
            {
                return;
            }

            IValueInfoService infoSvc        = GetService <IValueInfoService>(true);
            ArrayList         missingReqArgs = new ArrayList(Configuration.Arguments.Length);

            foreach (Config.Argument arg in Configuration.Arguments)
            {
                bool isNull = arguments.GetValue(arg.Name) == null;
                if (arg.Required && isNull)
                {
                    missingReqArgs.Add(arg.Name);
                }
                requiredAreNull = requiredAreNull || (arg.Required && isNull);
            }

            if (requiredAreNull)
            {
                throw new RecipeExecutionException(this.Configuration.Name, String.Format(
                                                       System.Globalization.CultureInfo.CurrentCulture,
                                                       Properties.Resources.Recipe_MissingRequiredArguments,
                                                       String.Join(", ", (string[])missingReqArgs.ToArray(typeof(string)))));
            }
        }
        private string Render(string templateCode, string templateFile)
        {
            EnvDTE.DTE vs       = this.GetService <EnvDTE.DTE>(true);
            string     basePath = this.GetBasePath();

            Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngine engine = new Microsoft.VisualStudio.TextTemplating.Engine();
            IValueInfoService service = (IValueInfoService)this.GetService(typeof(IValueInfoService));
            Dictionary <string, PropertyData> arguments = new Dictionary <string, PropertyData>();

            foreach (string str2 in base.additionalArguments.Keys)
            {
                Type type = null;
                try
                {
                    type = service.GetInfo(str2).Type;
                }
                catch (ArgumentException)
                {
                    if (base.additionalArguments[str2] != null)
                    {
                        type = base.additionalArguments[str2].GetType();
                    }
                    else
                    {
                        continue;
                    }
                }
                PropertyData data = new PropertyData(base.additionalArguments[str2], type);
                arguments.Add(str2, data);
            }
            TemplateHost host = new TemplateHost(basePath, arguments);

            host.TemplateFile = templateFile;
            Helpers.LogMessage(vs, this, templateFile);

            string str3 = engine.ProcessTemplate(templateCode, host);

            if (host.Errors.HasErrors)
            {
                string errors = "";
                foreach (CompilerError error in host.Errors)
                {
                    Helpers.LogMessage(vs, this, error.ErrorText);
                    errors += error.ErrorText + Environment.NewLine;
                }
                throw new TemplateException(host.Errors);
            }
            if (host.Errors.HasWarnings)
            {
                StringBuilder builder = new StringBuilder();
                foreach (CompilerError error in host.Errors)
                {
                    builder.AppendLine(error.ErrorText);
                }
                //Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "CompilationWarnings", new object[] { templateFile, builder.ToString() }));
            }
            return(str3);
        }
Пример #4
0
        /// <summary>
        /// <see cref="ISupportInitialize.BeginInit"/>
        /// </summary>
        public void EndInit()
        {
            IValueInfoService metaDataService = (IValueInfoService)
                                                GetService(typeof(IValueInfoService));

            this.Title = metaDataService.ComponentName;
            foreach (Configuration.Page step in wizardConfig.Pages)
            {
                CreateWizardPage(step);
            }
            this.CheckValueArguments();
            this.initializing = false;
        }
Пример #5
0
 private void SetFirstArgumentPanelFocus()
 {
     this.Wizard.Focus();
     this.Focus();
     if (this.Configuration != null && this.Configuration.Fields.Length >= 1)
     {
         IValueInfoService metaDataService =
             (IValueInfoService)GetService(typeof(IValueInfoService));
         ValueInfo argument = metaDataService.GetInfo(this.Configuration.Fields[0].ValueName);
         if (argument != null)
         {
             ArgumentPanel firstArgumentPanel =
                 (ArgumentPanel)Arguments[argument];
             firstArgumentPanel.Focus();
         }
     }
 }
Пример #6
0
        protected override void BuildRecipeArguments()
        {
            IValueInfoService metaDataService =
                (IValueInfoService)GetService(typeof(IValueInfoService));

            foreach (Configuration.Field field in this.Configuration.Fields)
            {
                ValueInfo argument = metaDataService.GetInfo(field.ValueName);
                if (argument == null)
                {
                    throw new WizardFrameworkException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  Properties.Resources.WizardGatheringService_MissingArgumentMetaData,
                                  field.ValueName));
                }
                Arguments.Add(argument, null);
            }
        }
Пример #7
0
        /// <summary>
        /// <see cref="ISupportInitialize.BeginInit"/>
        /// </summary>
        public override void EndInit()
        {
            base.EndInit();
            //If we keep initializing then do nothing
            if (IsInitializing)
            {
                return;
            }
            //OK: The base class is now initialized
            argumentLabel.Text   = this.FieldConfig.Label;
            argumentLabel.Cursor = this.Cursor;
            IValueInfoService metaDataService =
                (IValueInfoService)GetService(typeof(IValueInfoService));

            valueEditor.ValueRequired = metaDataService.GetInfo(this.FieldConfig.ValueName).IsRequired;
            valueEditor.ValueType     = this.ValueType;
            valueEditor.ToolTip       = this.FieldConfig.Tooltip;
            valueEditor.ReadOnly      = this.FieldConfig.ReadOnly;
            ((ISupportInitialize)(this.valueEditor)).EndInit();
        }
Пример #8
0
 /// <summary>
 /// Ends the construction of the component
 /// </summary>
 public virtual void EndInit()
 {
     if (Parent == null)
     {
         return;
     }
     if (Parent.Site != null)
     {
         this.Name = this.fieldConfig.ValueName;
         if (this.fieldConfig.InvalidValueMessage != null)
         {
             this.invalidValueMessage = this.fieldConfig.InvalidValueMessage;
         }
         IServiceProvider serviceProvider = (IServiceProvider)Parent.Site.GetService(typeof(IServiceProvider));
         this.Site = new ComponentModel.Site(serviceProvider, this, this.Name);
         ServiceHelper.CheckDependencies(this);
         IValueInfoService metaDataService =
             (IValueInfoService)GetService(typeof(IValueInfoService));
         valueType    = metaDataService.GetInfo(this.FieldConfig.ValueName).Type;
         initializing = false;
     }
 }
Пример #9
0
        /// <summary>
        /// Builds the list of recipe arguments collected by this wizard, based on the defined setter properties.
        /// <seealso cref="WizardPage.BuildRecipeArguments"/>
        /// </summary>
        protected override void BuildRecipeArguments()
        {
            IValueInfoService metaDataService =
                (IValueInfoService)GetService(typeof(IValueInfoService));

            foreach (PropertyInfo property in this.GetType().GetProperties())
            {
                if (Attribute.GetCustomAttribute(property, typeof(RecipeArgumentAttribute), true) != null)
                {
                    ValueInfo argument = metaDataService.GetInfo(property.Name);
                    if (argument == null)
                    {
                        throw new WizardFrameworkException(
                                  String.Format(
                                      CultureInfo.CurrentCulture,
                                      Properties.Resources.WizardGatheringService_MissingArgumentMetaData,
                                      property.Name));
                    }
                    Arguments.Add(argument, property);
                }
            }
        }
Пример #10
0
        private ArgumentPanel CreateEditingPanel(Configuration.Field field)
        {
            IValueInfoService metaDataService =
                (IValueInfoService)GetService(typeof(IValueInfoService));
            ValueInfo argument = metaDataService.GetInfo(field.ValueName);

            if (argument == null)
            {
                throw new ArgumentNullException("Field");
            }
            ArgumentPanel          argumentPanel = null;
            ITypeResolutionService loaderService =
                (ITypeResolutionService)GetService(typeof(ITypeResolutionService));
            Type argType = argument.Type;

            if (argType == null)
            {
                throw new TypeLoadException(
                          String.Format(
                              CultureInfo.CurrentCulture, Properties.Resources.WizardGatheringService_CannotLoadTypeStepField,
                              argument.Name,
                              this.Configuration.Title));
            }
            if (argType == typeof(bool) ||               // The type is a simple boolean value, so let's use ArgumentPanelBool
                argType == typeof(Nullable <bool>))
            {
                argumentPanel = new ArgumentPanelBool();
            }
            else
            {
                if (!string.IsNullOrEmpty(field.PanelType))
                {
                    Type paneltype = loaderService.GetType(field.PanelType);
                    argumentPanel = (ArgumentPanelTypeEditor)Activator.CreateInstance(paneltype);
                }
                else
                {
                    argumentPanel = new ArgumentPanelTypeEditor();
                }
                //First try to use any UITypeEditor defined for our type
                Type editorType = field.Editor == null ? null : loaderService.GetType(field.Editor.Type);
                if (editorType != null)
                {
                    UITypeEditor editorInstance = (UITypeEditor)Activator.CreateInstance(editorType);
                    if (editorInstance is IAttributesConfigurable)
                    {
                        ImmutableKeyStringDictionary values = new ImmutableKeyStringDictionary();
                        if (field.Editor.AnyAttr != null)
                        {
                            foreach (XmlAttribute xattr in field.Editor.AnyAttr)
                            {
                                values.Add(xattr.Name, xattr.Value);
                            }
                        }
                        ((IAttributesConfigurable)editorInstance).Configure(values);
                    }
                    ((ArgumentPanelTypeEditor)argumentPanel).EditorInstance = editorInstance;
                }
                ((ArgumentPanelTypeEditor)argumentPanel).ConverterInstance = argument.Converter;
            }
            argumentPanel.FieldConfig = field;
            this.Arguments[argument]  = argumentPanel;
            return(argumentPanel);
        }
Пример #11
0
        IDictionary LoadProviders(ITypeResolutionService resolution)
        {
            if (Configuration.Arguments == null)
            {
                return(new System.Collections.Specialized.HybridDictionary(0));
            }

            try
            {
                // At most we will have a provider for each argument.
                IDictionary providers = new System.Collections.Specialized.HybridDictionary(Configuration.Arguments.Length);
                monitoredArguments = new System.Collections.Specialized.HybridDictionary(Configuration.Arguments.Length);
                bool hasmonitors            = false;
                IValueInfoService mdservice = GetService <IValueInfoService>(true);

                // Setup monitoring for dependent argument changes.
                foreach (Config.Argument argument in Configuration.Arguments)
                {
                    if (argument.ValueProvider != null)
                    {
                        IValueProvider provider = GetInstance <IValueProvider>(resolution, argument.ValueProvider.Type);

                        // Initialize the provider by passing the configuration for the argument it provides values to.
                        provider.Initialize(mdservice.GetInfo(argument.Name));
                        if (provider is IAttributesConfigurable)
                        {
                            Configure((IAttributesConfigurable)provider, argument.ValueProvider.AnyAttr);
                        }
                        providers.Add(argument.Name, provider);
                        // Site the provider so it can access all services.
                        if (provider is IComponent)
                        {
                            Add((IComponent)provider);
                        }

                        // Add to the argument-indexed list of providers monitoring arguments.
                        if (argument.ValueProvider.MonitorArgument != null)
                        {
                            hasmonitors = true;
                            foreach (Config.MonitorArgument monitored in argument.ValueProvider.MonitorArgument)
                            {
                                // Throw if the value provider is monitoring the same argument it's attached to.
                                if (monitored.Name == argument.Name)
                                {
                                    throw new System.Configuration.ConfigurationException(String.Format(
                                                                                              System.Globalization.CultureInfo.CurrentCulture,
                                                                                              Properties.Resources.Recipe_ArgumentCantMonitorItself,
                                                                                              argument.ValueProvider.Type,
                                                                                              argument.Name));
                                }

                                ArrayList monitoringproviders = (ArrayList)monitoredArguments[monitored.Name];
                                if (monitoringproviders == null)
                                {
                                    monitoringproviders = new ArrayList();
                                    monitoredArguments[monitored.Name] = monitoringproviders;
                                }
                                monitoringproviders.Add(provider);
                            }
                        }
                    }
                }

                if (providers.Count != 0 && hasmonitors)
                {
                    // Attach to change event if monitoring arguments.
                    IComponentChangeService changes = GetService <IComponentChangeService>(true);
                    changes.ComponentChanged += new ComponentChangedEventHandler(OnArgumentChanged);
                }

                return(providers);
            }
            catch (Exception ex)
            {
                throw new ValueProviderException(this.Configuration.Name,
                                                 Properties.Resources.Recipe_ValueProviderLoadFailed, ex);
            }
        }
Пример #12
0
        /// <summary>
        /// Invokes the T4 rendering engine
        /// </summary>
        /// <param name="templateCode">The T4 template to render</param>
        /// <param name="templateFile">The file containing the template code</param>
        /// <returns>The rendered result</returns>
        protected string Render(string templateCode, string templateFile)
        {
            // Get the package root folder.
            string basePath = GetBasePath();

            // Create the Engine
            ITextTemplatingEngine engine = new Engine();

            // Build arguments.
            IValueInfoService mdService            = (IValueInfoService)GetService(typeof(IValueInfoService));
            Dictionary <string, PropertyData> args = new Dictionary <string, PropertyData>();

            foreach (string key in additionalArguments.Keys)
            {
                Type type = null;
                try
                {
                    type = mdService.GetInfo(key).Type;
                }
                catch (ArgumentException)
                {
                    // Type is not defined in recipe, so take it from the argument value itself.
                    // This is useful for values passed-in as configuration attributes.
                    if (additionalArguments[key] != null)
                    {
                        type = additionalArguments[key].GetType();
                    }
                    else
                    {
                        // Can't determine type, so we can't emit a property.
                        continue;
                    }
                }

                PropertyData propertyData = new PropertyData(additionalArguments[key], type);
                args.Add(key, propertyData);
            }

            // Create the Host. References will be resolved relative to the guidance
            // package installation folder.
            TemplateHost host = new TemplateHost(basePath, args);

            host.TemplateFile = templateFile;

            // Set the output
            string content = engine.ProcessTemplate(templateCode, host);

            // Looking for errors
            if (host.Errors.HasErrors)
            {
                throw new TemplateException(host.Errors);
            }
            else if (host.Errors.HasWarnings)
            {
                StringBuilder warnings = new StringBuilder();
                foreach (CompilerError warning in host.Errors)
                {
                    warnings.AppendLine(warning.ToString());
                }

                Trace.WriteLine(String.Format(
                                    CultureInfo.CurrentCulture,
                                    Properties.Resources.T4Action_CompilationWarnings,
                                    templateFile,
                                    warnings.ToString()));
            }
            return(content);
        }
Пример #13
0
        /// <summary>
        /// See <see cref="IDictionaryService.SetValue"/>.
        /// </summary>
        /// <exception cref="InvalidOperationException">The service is not sited.</exception>
        /// <exception cref="ArgumentException">Value is invalid.</exception>
        public void SetValue(object key, object value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (this.Site == null)
            {
                throw new InvalidOperationException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.General_ComponentMustBeSited, this));
            }
            IValueInfoService mdservice = (IValueInfoService)
                                          ComponentModel.ServiceHelper.GetService(this, typeof(IValueInfoService));
            // Will throw if not present.
            ValueInfo mdata = mdservice.GetInfo(key.ToString());

            context.SetData(mdata.Name, mdata.Type);
            if (mdata.Converter != null && value != null)
            {
                bool needsconversion = !ReflectionHelper.IsAssignableTo(mdata.Type, value);
                // We're restrictive and don't allow assignment of COM objects to
                // arguments that are not interfaces.
                if (needsconversion && value.GetType().IsCOMObject&& !mdata.Type.IsInterface)
                {
                    throw new ArgumentException(String.Format(
                                                    CultureInfo.CurrentCulture,
                                                    Properties.Resources.IDictionaryService_InvalidType,
                                                    value, key, mdata.Type));
                }
                if (needsconversion)
                {
                    // Perform conversion.
                    if (!mdata.Converter.CanConvertFrom(context, value.GetType()))
                    {
                        throw new ArgumentException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.IDictionaryService_InvalidType,
                                                        value, key, mdata.Type));
                    }
                    // Verify if value is valid and can be converted according to converter.
                    if (!mdata.Converter.IsValid(context, value))
                    {
                        throw new ArgumentException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.IDictionaryService_InvalidValue, key));
                    }
                    // Even if the value says that "IsValid" it could be failing performing the conversion
                    try
                    {
                        value = mdata.Converter.ConvertFrom(context, CultureInfo.CurrentCulture, value);
                    }
                    catch (FormatException)
                    {
                        throw new ArgumentException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.IDictionaryService_InvalidValue, key));
                    }
                    catch (Exception ex)
                    {
                        if (ex.InnerException != null &&
                            (ex.InnerException is FormatException || ex.InnerException is ArgumentException))
                        {
                            throw new ArgumentException(String.Format(
                                                            CultureInfo.CurrentCulture,
                                                            Properties.Resources.IDictionaryService_InvalidValue, key),
                                                        ex.InnerException);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    // Check again for validity.
                    bool invalidconversion = !ReflectionHelper.IsAssignableTo(mdata.Type, value);
                    // We're restrictive and don't allow assignment of COM objects to
                    // arguments that are not interfaces.
                    if (invalidconversion && value.GetType().IsCOMObject&& !mdata.Type.IsInterface)
                    {
                        throw new ArgumentException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.IDictionaryService_InvalidType,
                                                        value, key, mdata.Type));
                    }
                    if (invalidconversion)
                    {
                        throw new ArgumentException(String.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Properties.Resources.IDictionaryService_InvalidConvertedValue, key));
                    }
                }
            }

            if (ComponentChanging != null)
            {
                ComponentChanging(this, new ComponentChangingEventArgs(
                                      mdata, null));
            }
            State[key] = value;
            if (ComponentChanged != null)
            {
                ComponentChanged(this, new ComponentChangedEventArgs(
                                     mdata, null, State[key], value));
            }
        }