Exemplo n.º 1
0
        public static new FunctionContainer Import(string path, string keyProperty)
        {
            var functions = new FunctionContainer();

            var import = DictionaryXmlStore <string, FunctionDefinition> .Import(path, keyProperty);

            import.ForEach(i => functions.Add(i.Value));

            return(functions);
        }
Exemplo n.º 2
0
        private void UpdateTreeView(bool updateBreakpoints)
        {
            try
            {
                //first we try to access the list of files in the editor. If this does not work, the editor is gone. This is important when closing the editor
                var filesCount = hostObject.CurrentPowerShellTab.Files;

                //if the files count could be read we clean the treeview and the files list
                trvFunctions.Dispatcher.Invoke(new Action(() => { trvFunctions.Items.Clear(); }));
                files.Clear();

                //and get a new list from the editor
                files.AddRange(hostObject.CurrentPowerShellTab.Files.Where(file => !file.IsUntitled).Select(file => file.FullPath).ToList());
            }
            catch { }


            if (updateBreakpoints && hostObject.CurrentPowerShellTab.CanInvoke)
            {
                breakPoints.Clear();

                breakPoints.AddRange(hostObject.CurrentPowerShellTab.InvokeSynchronous("Get-PSBreakPoint").
                                     Select(pso => pso.BaseObject).Cast <LineBreakpoint>().
                                     ForEach <LineBreakpoint, BreakPoint>(b => new BreakPoint()
                {
                    ScriptFullName = b.Script,
                    LineNumber     = b.Line,
                    Enabled        = b.Enabled
                }));
            }

            foreach (var file in hostObject.CurrentPowerShellTab.Files)
            {
                var functionFound = false;
                functions.RemoveFunctionByFile(file.FullPath);

                var scriptItem = new ScriptTreeViewItem(file.FullPath);
                scriptItem.Header = file.DisplayName;
                if (file == hostObject.CurrentPowerShellTab.Files.SelectedFile)
                {
                    scriptItem.IsExpanded = true;
                }

                System.Collections.ObjectModel.Collection <PSParseError> errors = new System.Collections.ObjectModel.Collection <PSParseError>();
                var tokens = PSParser.Tokenize(file.Editor.Text, out errors).Where(t => t.Type == PSTokenType.Keyword | t.Type == PSTokenType.CommandArgument);

                foreach (var token in tokens)
                {
                    if ((token.Content.ToLower() == "function" | token.Content.ToLower() == "workflow"))
                    {
                        functionFound = true;
                        continue;
                    }

                    if (functionFound && token.Type == PSTokenType.CommandArgument)
                    {
                        FunctionDefinition function = new FunctionDefinition(file.FullPath, token.Content, token.StartLine);
                        functions.Add(function);

                        functionFound = false;
                    }
                }
                foreach (var function in (functions.GetFunctionsByFile(file.FullPath).OrderBy(f => f.Name)))
                {
                    var functionItem = new TreeViewItem();
                    functionItem.Header = function.Name;
                    functionItem.Tag    = function;

                    scriptItem.Items.Add(functionItem);
                }
                trvFunctions.Dispatcher.Invoke(new Action(() => { trvFunctions.Items.Add(scriptItem); }));
            }

            UpdateStatusBarContent(string.Format("Loaded functions: {0}", functions.Count));

            if (functionsImported)
            {
                try
                {
                    var path = fileManager.Get(functionsFileName).FullName;
                    functions.Export(path);
                }
                catch { }
            }
        }