Example #1
0
        } // end operator-

        /// <summary>
        ///    Combines two PsContexts. If 'right' is non-empty, creates a new PsContext
        ///    object (rather than mutating an existing one). "Conflicts" are won by
        ///    'right'.
        /// </summary>
        public static PsContext operator+(PsContext left, PsContext right)
        {
            if ((null == right) || (0 == right.TotalCount))
            {
                return(left);
            }

            if (null == left)
            {
                return(right);
            }

            PsContext newCtx = new PsContext(left);

            foreach (var key in right.Funcs.Keys)
            {
                newCtx.Funcs[key] = right.Funcs[key];
            }

            foreach (var key in right.Vars.Keys)
            {
                newCtx.Vars[key] = right.Vars[key];
            }
            return(newCtx);
        } // end operator+
Example #2
0
        /// <summary>
        ///    Diffs two PsContexts. This makes it easy to see what the differences are,
        ///    and potentially cuts down on what must be stored. If 'right' is non-empty,
        ///    this creates a new PsContext object (unless 'left' is null, in which case
        ///    it returns null).
        /// </summary>
        public static PsContext operator-(PsContext left, PsContext right)
        {
            if ((null == right) || (0 == right.TotalCount))
            {
                return(left);
            }

            if (null == left)
            {
                return(null);
            }

            PsContext newCtx = new PsContext();

            foreach (var key in left.Funcs.Keys)
            {
                if (!right.Funcs.ContainsKey(key) ||
                    (right.Funcs[key].Ast.Extent.Text != left.Funcs[key].Ast.Extent.Text))
                {
                    newCtx.Funcs.Add(key, left.Funcs[key]);
                }
            }

            foreach (var key in left.Vars.Keys)
            {
                if (!right.Vars.ContainsKey(key))
                {
                    newCtx.Vars.Add(key, left.Vars[key]);
                }
            }
            return(newCtx);
        } // end operator-
Example #3
0
        public PsContext(PsContext other)
        {
            if (null == other)
            {
                throw new ArgumentNullException("other");
            }

            Funcs = new Dictionary <string, ScriptBlock>(other.Funcs);
            Vars  = new Dictionary <string, PSVariable>(other.Vars);
        }
Example #4
0
        public DbgValueScriptConverter(string typeName, ScriptBlock script, bool captureContext)
        {
            if (String.IsNullOrEmpty(typeName))
            {
                throw new ArgumentException("You must supply a type name.", "typeName");
            }

            if (null == script)
            {
                throw new ArgumentNullException("script");
            }

            TypeName = typeName;
            Script   = script;
            if (captureContext)
            {
                Context = DbgProvider.CapturePsContext();
            }
            else
            {
                Context = new PsContext();
            }
        } // end constructor
        public PsContextHelper(ScriptBlock scriptBlock,
                               PsContext withContext,
                               bool saveContext)
        {
            if (null == scriptBlock)
            {
                throw new ArgumentNullException("scriptBlock");
            }

            m_withContext = withContext;

            if (!saveContext)
            {
                m_scriptBlock = scriptBlock;
                return;
            }

            if (null == m_withContext)
            {
                m_withContext = new PsContext();
            }

            m_savedContext = new PSVariable(c_SavedContextVarName, null);
            m_withContext.Vars[m_savedContext.Name] = m_savedContext;

            m_ctxMarker = DbgProvider.SetContextStartMarker();

            // Hacking the scriptBlock and smuggling the context out like this is
            // pretty ugly. I don't know a better way, though--I'm open to
            // suggestions.
            m_scriptBlock = ScriptBlock.Create(Util.Sprintf(@"{0}
. ""{1}\\GetPsContextFunc.ps1""
${2} = Get-PsContext",
                                                            scriptBlock.ToString(),
                                                            DbgProvider.PsScriptRoot,
                                                            c_SavedContextVarName));
        } // end constructor