Ejemplo n.º 1
0
        public void Handle(string wrapperFile, params string[] args)
        {
            var wPath     = Path.Combine(WorkContext.WorkingPath, wrapperFile + ".xaml");
            var wapperTxt = File.ReadAllText(wPath);
            var target    = WorkContext.Activity;

            var counter = 1;

            foreach (var arg in args)
            {
                wapperTxt = wapperTxt.Replace("$" + counter + "$", XMLExetension.Escape(arg));
                counter++;
            }

            WrapperDoc = XDocument.Parse(wapperTxt);

            var wrapper = WrapperDoc.XElement("Activity").XElement("Sequence");

            WorkContext.WrapTarget(wrapper);

            // Namespaces
            var existingNs = WorkContext.Doc.GetNamespaces();
            var templateNs = WrapperDoc.GetNamespaces();

            foreach (var ns in templateNs)
            {
                if (!existingNs.ContainsKey(ns.Key))
                {
                    WorkContext.Doc.XElement("Activity").SetAttributeValue(ns.Value.Name, ns.Value.Value);
                    Console.WriteLine("Add attribute:" + ns.Value.Value);
                }
            }

            MergeNamespaces();
        }
Ejemplo n.º 2
0
        public void Handle(string newWorkflowName)
        {
            // 1. New doc of the workflow copy from origin
            // 2. Only keep the ele inside the main sequence
            // 3. Keep all arguments
            // 4*(0). Get all accessible varibles in original doc and convert them to arguments
            // 5. Invoke new workflow from origin workflow
            var n         = WorkContext.Activity.Parent;
            var variables = new List <XElement>();

            while (n != null)
            {
                if (n.Name.LocalName == "Sequence" && n.XElement("Sequence.Variables") != null)
                {
                    foreach (var ele in n.XElement("Sequence.Variables").Elements())
                    {
                        var vname = ele.XAttribute("Name").Value;
                        if (variables.FirstOrDefault(v => v.XAttribute("Name").Value == vname) == null)
                        {
                            variables.Add(ele);
                        }
                    }
                }
                n = n.Parent;
            }

            // Create new workflow
            var p = Path.Combine(WorkContext.WorkingPath, newWorkflowName + ".xaml");

            //if (File.Exists(p)) throw new Exception("Workflow file existed:" + p);
            File.Copy(WorkContext.FileName, p, true);
            XDocument newDoc = null;
            XElement  root;

            using (var stream = File.OpenRead(p))
            {
                newDoc = XDocument.Load(stream);
                root   = newDoc.XElement("Activity");
                root.XElement("Sequence").RemoveAll();

                // Add arguments for new workflow
                if (root.XElement("Members", XMLExetension.ns_x) == null)
                {
                    var e = new XElement(XName.Get("Members", XMLExetension.ns_x));
                    root.AddFirst(e);
                }
                var nMembers = root.XElement("Members", XMLExetension.ns_x);
                foreach (var v in variables)
                {
                    var pt = XMLExetension.ParseElementFromTemplate("Property", v.Attribute("Name").Value, v.XAttribute("TypeArguments", XMLExetension.ns_x).Value);
                    nMembers.Add(pt);
                }
                var eleWorkflow = XMLExetension.ParseElementFromTemplate("InvokeWorkflowFile", newWorkflowName);
                foreach (var m in nMembers.Elements())
                {
                    var args = eleWorkflow.XElement("InvokeWorkflowFile.Arguments", XMLExetension.ns_ui);
                    var type = m.Attribute("Type").Value;
                    var name = m.Attribute("Name").Value;
                    type = type.Substring(type.IndexOf('(') + 1, type.LastIndexOf(')') - type.IndexOf('(') - 1);

                    var direction = "InOut";
                    args.Add(XMLExetension.ParseElement(string.Format("<{2}Argument x:TypeArguments=\"{0}\" x:Key=\"{1}\" >[{1}]</{2}Argument>", type, name, direction)));
                }

                // Change location of target activity
                WorkContext.Activity.SetAttributeValue("UIPathPreprocessor", "TRUE");
                root.XElement("Sequence").Add(WorkContext.Activity);
                var newEle = newDoc.Root.XPathSelectElement("//*[@UIPathPreprocessor]");
                newEle.Attribute("UIPathPreprocessor").Remove();
                Globals.AddToSaveXMLFile(p, newDoc);

                WorkContext.Activity.ReplaceWith(eleWorkflow);
                WorkContext.Activity = newEle;
                WorkContext.Doc      = newDoc;
            }
        }