Пример #1
0
        public override T Clone <T>()
        {
            PowerShellEventItem item = base.Clone <PowerShellEventItem>();

            if (!K2Field.IsNullOrEmpty(_powerShellScript))
            {
                item.PowerShellScript = _powerShellScript.Clone <K2Field>();
            }

            if (_inputVariables != null)
            {
                foreach (VariableItem varItem in _inputVariables)
                {
                    item.InputVariables.Add(varItem.Clone <VariableItem>());
                }
            }

            if (_outputVariables != null)
            {
                foreach (VariableItem varItem in _outputVariables)
                {
                    item.OutputVariables.Add(varItem.Clone <VariableItem>());
                }
            }

            return(item as T);
        }
Пример #2
0
        private CodeTypeDeclaration GenerateClass(PowerShellEventItem powerShellEventItem)
        {
            AssemblyName        name = new AssemblyName(Assembly.GetExecutingAssembly().FullName);
            CodeTypeDeclaration type = new CodeTypeDeclaration("$safeitemname$")
            {
                IsPartial = true
            };

            type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(GeneratedCodeAttribute)), new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("K2")), new CodeAttributeArgument(new CodePrimitiveExpression(name.Version.ToString())) }));

            int outputMaps = 0;

            foreach (VariableItem item in powerShellEventItem.OutputVariables)
            {
                CodeTypeMember mem = GenerateOutputMapMethod(item, powerShellEventItem, outputMaps++);
                type.Members.Add(mem);
            }

            int inputMaps = 0;

            foreach (VariableItem item in powerShellEventItem.InputVariables)
            {
                CodeTypeMember mem = GenerateInputMapMethod(item, powerShellEventItem, inputMaps++);
                type.Members.Add(mem);
            }

            type.Members.Add(GeneratePerformMappingMethod(outputMaps, "OutputMap"));
            type.Members.Add(GeneratePerformMappingMethod(inputMaps, "InputMap"));

            return(type);
        }
Пример #3
0
        private CodeTypeMember GenerateInputMapMethod(VariableItem item, PowerShellEventItem powerShellEventItem, int mapNr)
        {
            CodeMemberMethod method = new CodeMemberMethod();

            CodeParameterDeclarationExpression rsParam = new CodeParameterDeclarationExpression(new CodeTypeReference("System.Management.Automation.Runspaces.Runspace"), "rs");
            CodeParameterDeclarationExpression k2Param = new CodeParameterDeclarationExpression(new CodeTypeReference("$contexttype$"), "K2");

            method.Name       = "InputMap" + mapNr.ToString();
            method.ReturnType = new CodeTypeReference(typeof(void));
            method.Parameters.Add(k2Param);
            method.Parameters.Add(rsParam);



            // Some variables we need
            CodeVariableReferenceExpression k2Field = new CodeVariableReferenceExpression("K2");


            //Declare a object variable
            CodeVariableDeclarationStatement varValueDecl = new CodeVariableDeclarationStatement(typeof(object), "variableValue");
            CodeVariableReferenceExpression  varValueRef  = new CodeVariableReferenceExpression("variableValue");

            method.Statements.Add(varValueDecl);



            //TODO: use getsimplfy function? Check why that is there?
            K2FieldPart part = Simplify(item.VariableValue);
            string      data = ((IRuntimeDataProvider)part).GetGetRuntimeData();

            // This creates a 'getvalue' method
            CodeFieldReferenceExpression resolverManagerProperty = new CodeFieldReferenceExpression(k2Field, "ResolverManager");
            CodeMethodInvokeExpression   getValue = new CodeMethodInvokeExpression(
                resolverManagerProperty,
                "GetValue",
                new CodeExpression[] { new CodePrimitiveExpression(data) }
                );

            // Assign the getValue to a variable.
            CodeAssignStatement assignCurrentValue = new CodeAssignStatement();

            assignCurrentValue.Right = getValue;
            assignCurrentValue.Left  = varValueRef;
            method.Statements.Add(assignCurrentValue);

            method.Statements.Add(new CodeSnippetStatement("  if (variableValue != null) {"));
            method.Statements.Add(new CodeSnippetStatement(string.Format("rs.SessionStateProxy.SetVariable(\"{0}\", variableValue);", item.Name)));
            method.Statements.Add(new CodeSnippetStatement("  }"));


            return(method);
        }
Пример #4
0
        private CodeTypeMember GenerateOutputMapMethod(VariableItem item, PowerShellEventItem powerShellEventItem, int mapNr)
        {
            CodeMemberMethod method = new CodeMemberMethod();

            CodeParameterDeclarationExpression rsParam = new CodeParameterDeclarationExpression(new CodeTypeReference("System.Management.Automation.Runspaces.Runspace"), "rs");
            CodeParameterDeclarationExpression k2Param = new CodeParameterDeclarationExpression(new CodeTypeReference("$contexttype$"), "K2");

            method.Name       = "OutputMap" + mapNr.ToString();
            method.ReturnType = new CodeTypeReference(typeof(void));
            method.Parameters.Add(k2Param);
            method.Parameters.Add(rsParam);



            method.Statements.Add(new CodeSnippetStatement(string.Format("object psVariable = rs.SessionStateProxy.GetVariable(\"{0}\");", item.Name)));
            method.Statements.Add(new CodeSnippetStatement("if (psVariable != null) {"));

            //TODO: use getsimplfy function? Check why that is there?
            K2FieldPart part = item.VariableValue.Parts[0];
            string      data = ((IRuntimeDataProvider)part).GetGetRuntimeData();


            // Some variables we need
            CodeVariableReferenceExpression k2Field    = new CodeVariableReferenceExpression("K2");
            CodeVariableReferenceExpression psVariable = new CodeVariableReferenceExpression("psVariable");


            // new object variable
            CodeVariableDeclarationStatement destVariableDeclaration = new CodeVariableDeclarationStatement(typeof(object), "destinationValue");

            method.Statements.Add(destVariableDeclaration);

            CodeVariableReferenceExpression destVariable = new CodeVariableReferenceExpression("destinationValue");


            // This creates a 'getvalue' method
            CodeFieldReferenceExpression resolverManagerProperty = new CodeFieldReferenceExpression(k2Field, "ResolverManager");
            CodeMethodInvokeExpression   getValue = new CodeMethodInvokeExpression(
                resolverManagerProperty,
                "GetValue",
                new CodeExpression[] { new CodePrimitiveExpression(data) }
                );


            // Assign the getValue to a variable.
            CodeAssignStatement assignCurrentValue = new CodeAssignStatement();

            assignCurrentValue.Right = getValue;
            assignCurrentValue.Left  = destVariable;
            method.Statements.Add(assignCurrentValue);

            //TODO: convert to statements
            method.Statements.Add(new CodeSnippetStatement("  if (psVariable.GetType() != destinationValue.GetType()) {"));
            method.Statements.Add(new CodeSnippetStatement("    K2.ProcessInstance.Logger.LogDebugMessage(\"PowerShellWizard\", \"Source and destination type of PowerShell Variable " + item.Name + " do not match. Doing conversion.\");"));


            // Do the system.Convert.ChangeType method.
            CodeTypeReferenceExpression systemConvert    = new CodeTypeReferenceExpression("System.Convert");
            CodeMethodInvokeExpression  changeTypeInvoke = new CodeMethodInvokeExpression(
                systemConvert,
                "ChangeType",
                new CodeExpression[] {
                psVariable,
                new CodeMethodInvokeExpression(destVariable, "GetType")
            });

            CodeAssignStatement convertAssign = new CodeAssignStatement();

            convertAssign.Left  = psVariable;
            convertAssign.Right = changeTypeInvoke;
            method.Statements.Add(convertAssign);


            //TODO: convert to statements
            method.Statements.Add(new CodeSnippetStatement("    if (psVariable == null) {"));
            method.Statements.Add(new CodeSnippetStatement("      throw new System.Exception(\"Conversion of variable '" + item.Name + "' failed. Please make sure the types are the same.\");"));
            method.Statements.Add(new CodeSnippetStatement("    }"));



            method.Statements.Add(new CodeSnippetStatement("  }"));



            // Create a setValue on the ResolverManager
            CodeMethodInvokeExpression setValue = new CodeMethodInvokeExpression(
                resolverManagerProperty,
                "SetValue",
                new CodeExpression[] {
                new CodePrimitiveExpression(data),
                psVariable
            });

            method.Statements.Add(setValue);

            // Commit the resolverManager
            CodeMethodInvokeExpression commit = new CodeMethodInvokeExpression(
                resolverManagerProperty,
                "Commit"
                );

            method.Statements.Add(commit);

            method.Statements.Add(new CodeSnippetStatement("}"));

            return(method);
        }