OWASP O2 Platform Blog

Consuming extension method from external file

Once we have a LAMDA method that we want to consume in a generic way (i.e. from other scripts), the best place to put it is in a external *.cs file (which can be passed as a reference to any O2 Script)

For example, this is how to do this for the script developed here: https://o2platform.wordpress.com/2011/02/20/o2-script-create-xsd-and-assembly-from-xml-file/

step 1: Create a stand alone CSharp file (in this case called _Extra_O2_Scripts.cs and located in C:\O2\_New_O2_Scripts\TeamMentor\_Extra_O2_Scripts.cs)

// This file is part of the OWASP O2 Platform (<a href="http://www.owasp.org/index.php/OWASP_O2_Platform">http://www.owasp.org/index.php/OWASP_O2_Platform</a>) and is released under the Apache 2.0 License (<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>)
using System;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.External.SharpDevelop.ExtensionMethods;
using NUnit.Framework;
//O2Ref:nunit.framework.dll

namespace O2.Extra_O2_Scripts
{
    public static class XmlToAssembly_ExtensionMethods
    {   
     public static string createAssemblyFromXml(this string xmlFile, string targetFolder, string xsdAndDll_Name )
     {
   var xsd = targetFolder.pathCombine(xsdAndDll_Name + ".xsd");
   xmlFile.xmlCreateXSD().saveAs(xsd);
   Assert.That(xsd.fileExists(), "xsd was not created");
   "[createAssemblyFromXml] XSD Created: {0}".debug(xsd);
   var cs = xsd.xsdCreateCSharpFile()
      .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
   
   Assert.That(cs.fileExists(), "cs was not created");  
   "[createAssemblyFromXml] CSharp file Created: {0}".debug(cs);
   
   var dll = targetFolder.pathCombine(xsdAndDll_Name + ".dll");
   var tempDll = cs.compile(dll);
   
   Assert.That(dll.fileExists(), "dll_MainFile was not created");
   "[createAssemblyFromXml] dll file Created: {0}".debug(dll);
   
   var assembly = dll.assembly();
   Assert.That(assembly.notNull(), "assembly was null");  
   return dll;        
  }
    }
}

step 2: Consume it from an O2 Script:

var workFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";

var mainFile = rootFolder.pathCombine("OWASP.xml");

return mainFile.createAssemblyFromXml(workFolder, "tm_Main");

return "ok";
 
//using O2.Extra_O2_Scripts
//O2File:C:\O2\_New_O2_Scripts\TeamMentor\_Extra_O2_Scripts.cs

February 20, 2011 - Posted by | O2 Internals

1 Comment »

  1. […] creating a Lamba method and putting it on a CSharp file the usual next step is to create an API that can wrap the functionality that we […]

    Pingback by Creating an API to be consumed by an O2 Script « O2Platform.com for Developers | February 20, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: