/// <summary>
        /// EndProcessing method.
        /// </summary>
        protected override void EndProcessing()
        {
            var group = new TemplateGroupString(Definition)
            {
                Listener = new ErrorListener(this)
            };

            var groupInfo = new TemplateGroupInfo(group);

            group.RegisterModelAdaptor(typeof(PSObject), new PSObjectAdaptor());
            group.RegisterModelAdaptor(typeof(Type), new TypeAdapter());
            group.RegisterRenderer(typeof(DateTime), new DateRenderer());
            group.RegisterRenderer(typeof(DateTimeOffset), new DateRenderer());
            group.Listener = ErrorManager.DefaultErrorListener;

            WriteObject(groupInfo);
        }
Esempio n. 2
0
        /// <summary>
        /// ProcessRecord method.
        /// </summary>
        protected override void ProcessRecord()
        {
            TemplateGroupInfo group = null;

            if (Group != null)
            {
                group = Group;
                group.Bind(this);
                var templateNames     = group.Templates.Select(t => t.Name);
                var safeTemplateNames = templateNames as string[] ?? templateNames.ToArray();

                var name = Name ?? safeTemplateNames.FirstOrDefault();

                if (!safeTemplateNames.Contains(name))
                {
                    var nameString = string.Join(
                        CultureInfo.CurrentCulture.TextInfo.ListSeparator,
                        safeTemplateNames);

                    var msg = string.Format(
                        CultureInfo.CurrentCulture,
                        Strings.TemplateNotFound,
                        name,
                        nameString);

                    var error = new ErrorRecord(
                        new InvalidOperationException(msg),
                        nameof(Strings.TemplateNotFound),
                        ErrorCategory.ObjectNotFound,
                        name);

                    ThrowTerminatingError(error);
                }

                _currentTemplate = group.Templates
                                   .FirstOrDefault(t => t.Name == name);
            }

            if (Definition != null)
            {
                group            = TemplateGroupInfo.CreateFromTemplateDefinition(this, Definition);
                _currentTemplate = group.Templates.FirstOrDefault();
            }

            Debug.Assert(_currentTemplate != null, "_currentTemplate != null");
            if (Parameters != null)
            {
                if (Parameters.BaseObject is IDictionary asDictionary)
                {
                    foreach (DictionaryEntry keyValuePair in asDictionary)
                    {
                        AddTemplateArgument(keyValuePair.Key as string, keyValuePair.Value);
                    }
                }
                else
                {
                    ProcessObjectAsArguments();
                }
            }

            var result = _currentTemplate.Instance.Render(CultureInfo.CurrentCulture);

            if (result != null)
            {
                WriteObject(result);
            }

            _currentTemplate.ResetInstance();

            group.Unbind();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TemplateInfo"/> class.
 /// </summary>
 /// <param name="templateInstance">The internal <see cref="Template"/> object.</param>
 /// <param name="groupInfo">The group the template belongs to.</param>
 internal TemplateInfo(Template templateInstance, TemplateGroupInfo groupInfo)
 {
     Instance = templateInstance;
     Group    = groupInfo;
 }