public static string createPackagedDirectoryForMethod(MethodInfo targetMethod, string loadDllsFrom)
        {
            var standAloneExe = createMainPointingToMethodInfo(targetMethod);

            if (File.Exists(standAloneExe))
            {
                var createdDirectory = CecilAssemblyDependencies.populateDirectoryWithAllDependenciesOfAssembly(standAloneExe, loadDllsFrom);
                if (Directory.Exists(createdDirectory))
                {
                    var exeInCreatedDirectory = Path.Combine(createdDirectory, Path.GetFileName(standAloneExe));
                    if (File.Exists(exeInCreatedDirectory))
                    {
                        var newDirectoryName = PublicDI.config.getTempFolderInTempDirectory(targetMethod.Name);
                        Files.copyFilesFromDirectoryToDirectory(createdDirectory, newDirectoryName);
                        Files.deleteFolder(createdDirectory);

                        var exeInFinalDirectory = Path.Combine(newDirectoryName, Path.GetFileName(standAloneExe));
                        var pdbFile             = IL.createPdb(exeInFinalDirectory, false);
                        PublicDI.log.debug("created temp exe for method execution: {0}", exeInFinalDirectory);
                        return(exeInFinalDirectory);
                    }
                }
            }
            PublicDI.log.error("in createPackagedDirectoryForMethod: could not create exe for method");
            return("");
        }
        public void createAppDomainAndLoadDll_MainTestCase()
        {
            string fullPathToDllToProcess = DI.config.ExecutingAssembly;  //Path.Combine(hardCodedO2DevelopmentLib, o2DllToProcess + ".exe");
            //AppDomainUtils.findDllInCurrentAppDomain(o2DllToProcess);
            Assert.That(File.Exists(fullPathToDllToProcess),
                        "fullPathToDllToProcess doesn't exist:" + fullPathToDllToProcess);

            // get test AppDomain in temp folder
            var o2AppDomainFactory = new O2AppDomainFactory();

            // load dll from it (this will fail until all dependencies are resolved
            Assert.That(o2AppDomainFactory.load(o2DllToProcess, fullPathToDllToProcess, true),
                        "Dll failed to load into AppDomain");

            // get assemblyDependencies 
            Dictionary<string, string> assemblyDependencies =
                new CecilAssemblyDependencies(fullPathToDllToProcess).calculateDependencies();
            DI.log.debug("There are {0} assembly dependencies to load", assemblyDependencies.Count);
            // load them and abort if we were not able to load all)
            Assert.That(o2AppDomainFactory.load(assemblyDependencies).Count == 0,
                        "There were assemblyDependencies that were not loaded");

            // double check that all is OK by dinamcally invoking some methods on the new AppDomain                           
            Assert.That(null != o2AppDomainFactory.invokeMethod("nameOfCurrentDomainStatic O2Proxy O2_Kernel", new object[0]),
                        "Could not invoke methods inside O2_CoreLib");
            o2AppDomainFactory.invokeMethod("logInfo O2Proxy O2_Kernel",
                                            new object[] {"Hello from createAppDomainInTempFolder UnitTest"});

            AppDomainUtils.closeAppDomain(o2AppDomainFactory.appDomain, onTestCompletionDeleteTempFilesCreated);
        }
 public void showDependenciesForFile(string fileToProcess)
 {
     clearDependenciesOnViewControls();
     if (File.Exists(fileToProcess))
     {
         var cecilDependencies =  new CecilAssemblyDependencies(fileToProcess);
         var dependencies = cecilDependencies.calculateDependencies();                
         updateDependenciesFlatList(dependencies);
     }            
 }
 public static bool findInAssembly_Dependency(String sAssemblyToLoad, String dependencyToFind, bool recursiveSearch)
 {
     try
     {
         var dependencies = CecilAssemblyDependencies.getDictionaryOfDependenciesForAssembly_WithNoRecursiveSearch(sAssemblyToLoad);
     }
     catch (Exception ex)
     {
         DI.log.error("in findInAssembly_CustomAttribute: ", ex.Message);
     }
     return(false);
 }
 public static void copyAssemblyDependenciesToAssemblyDirectory(string pathToAssemblyToAnalyze, List<string> pathsToFindReferencedDlls)
 {
     var assemblyDependencies =
         new CecilAssemblyDependencies(pathToAssemblyToAnalyze, pathsToFindReferencedDlls).calculateDependencies();
     var targetDirectory = Path.GetDirectoryName(pathToAssemblyToAnalyze);
     foreach (var assemblyToCopy in assemblyDependencies.Values)
         if (File.Exists(assemblyToCopy))
         {
             var targetFile = Path.Combine(targetDirectory, Path.GetFileName(assemblyToCopy));
             if (false == File.Exists(targetFile))
             {
                 File.Copy(assemblyToCopy, targetFile);
                 var pdbFile = assemblyToCopy.Replace(Path.GetExtension(assemblyToCopy), ".pdb");
                 if (File.Exists(pdbFile))
                     Files.Copy(pdbFile, targetDirectory);
             }
         }
 }
        public void createAppDomainAndAllDependencies()
        {
            string fullPathToDllToProcess = Path.Combine(hardCodedO2DevelopmentLib, o2DllToProcess + ".exe");
            // AppDomainUtils.findDllInCurrentAppDomain(o2DllToProcess);
            DI.log.debug("For the dll: {0}", o2DllToProcess);

            Dictionary<string, string> assemblyDependencies =
                new CecilAssemblyDependencies(fullPathToDllToProcess).calculateDependencies();
            DI.log.debug("There are {0} assembly dependencies to load", assemblyDependencies.Count);
            var o2AppDomainFactory = new O2AppDomainFactory();

            Assert.That(o2AppDomainFactory.load(assemblyDependencies).Count == 0,
                        "There were assemblyDependencies that were not loaded");


            DI.log.d("List of loaded Assemblies");
            foreach (string assembly in o2AppDomainFactory.getAssemblies(true))
                DI.log.d("  -  " + assembly);

            AppDomainUtils.closeAppDomain(o2AppDomainFactory.appDomain, onTestCompletionDeleteTempFilesCreated);
        }
        public static void copyAssemblyDependenciesToAssemblyDirectory(string pathToAssemblyToAnalyze, List <string> pathsToFindReferencedDlls)
        {
            var assemblyDependencies =
                new CecilAssemblyDependencies(pathToAssemblyToAnalyze, pathsToFindReferencedDlls).calculateDependencies();
            var targetDirectory = Path.GetDirectoryName(pathToAssemblyToAnalyze);

            foreach (var assemblyToCopy in assemblyDependencies.Values)
            {
                if (File.Exists(assemblyToCopy))
                {
                    var targetFile = Path.Combine(targetDirectory, Path.GetFileName(assemblyToCopy));
                    if (false == File.Exists(targetFile))
                    {
                        File.Copy(assemblyToCopy, targetFile);
                        var pdbFile = assemblyToCopy.Replace(Path.GetExtension(assemblyToCopy), ".pdb");
                        if (File.Exists(pdbFile))
                        {
                            Files.copy(pdbFile, targetDirectory);
                        }
                    }
                }
            }
        }
        public static O2AppDomainFactory getO2AppDomainFactoryOnTempDirWithAllDependenciesResolved(
            string fullPathToDllToProcess)
        {
            var o2AppDomainFactory = new O2AppDomainFactory();

            if (false ==
                o2AppDomainFactory.load(Path.GetFileNameWithoutExtension(fullPathToDllToProcess), fullPathToDllToProcess,
                                        true))
            {
                return(null);
            }
            Dictionary <string, string> assemblyDependencies =
                new CecilAssemblyDependencies(fullPathToDllToProcess).calculateDependencies();

            if (o2AppDomainFactory.load(assemblyDependencies).Count == 0)
            {
                return(o2AppDomainFactory);
            }
            PublicDI.log.error(
                "in getO2AppDomainFactoryOnTempDirWithAllDependenciesResolved, there were assemblyDependencies that were not loaded, for: " +
                fullPathToDllToProcess);
            return(null);
        }
 public static List<string> getListOfDependenciesForAssemblies(List<String> assemblies, List<String> directoriesWithSourceAssemblies)
 {
     var calculatedAssembliesDictionary = new CecilAssemblyDependencies(assemblies, directoriesWithSourceAssemblies).calculateDependencies();
     return new List<string>(calculatedAssembliesDictionary.Values);
 }
    public static O2AppDomainFactory getO2AppDomainFactoryOnTempDirWithAllDependenciesResolved(
        string fullPathToDllToProcess)
    {
        var o2AppDomainFactory = new O2AppDomainFactory();
        if (false ==
            o2AppDomainFactory.load(Path.GetFileNameWithoutExtension(fullPathToDllToProcess), fullPathToDllToProcess,
                                    true))
            return null;
        Dictionary<string, string> assemblyDependencies =
            new CecilAssemblyDependencies(fullPathToDllToProcess).calculateDependencies();
 
        if (o2AppDomainFactory.load(assemblyDependencies).Count == 0)
            return o2AppDomainFactory;
        PublicDI.log.error(
            "in getO2AppDomainFactoryOnTempDirWithAllDependenciesResolved, there were assemblyDependencies that were not loaded, for: " +
            fullPathToDllToProcess);
        return null;
    }
        public static List <string> getListOfDependenciesForAssemblies(List <String> assemblies, List <String> directoriesWithSourceAssemblies)
        {
            var calculatedAssembliesDictionary = new CecilAssemblyDependencies(assemblies, directoriesWithSourceAssemblies).calculateDependencies();

            return(new List <string>(calculatedAssembliesDictionary.Values));
        }
        public void findAllDependenciesForDll()
        {
            string fullPathToDllToProcess = DI.config.ExecutingAssembly; //Path.Combine(hardCodedO2DevelopmentLib, o2DllToProcess + ".exe");
            // AppDomainUtils.findDllInCurrentAppDomain(o2DllToProcess);
            var cecilAssemblyDependencies = new CecilAssemblyDependencies(fullPathToDllToProcess);
            Dictionary<string, string> assemblyDependencies = cecilAssemblyDependencies.calculateDependencies();
            Assert.That(assemblyDependencies != null && assemblyDependencies.Count > 0,
                        " problem calculating assemblyDependencies");

            DI.log.debug("There are {0} assembly dependencies to resolve", assemblyDependencies.Count);
            foreach (string assemblyToResolve in assemblyDependencies.Keys)
                DI.log.info("{0} : {1}", assemblyDependencies[assemblyToResolve], assemblyToResolve);
        }
        public void loadCSharpScriptsGuiOnSeparateAppDomainUsingWCF_ManualWay()
        {                        
            // create Wcf Host
            var wcfHostName = "loadCSharpScriptsGui";
            var wcfHost = O2WcfUtils.createWcfHostAndStartIt(wcfHostName);                        
            IO2WcfKernelMessage o2WcfClientProxy = wcfHost.getClientProxy();
            Assert.That(o2WcfClientProxy.allOK(), "o2WcfProxy was not OK");

            // create separateAppDomain
            var testAppDomain = "AppDomainWithCSharpScripts";
            var targetAssemblies = new List<string> { typeof(StartCSharpScriptGui).Assembly.Location };
            var directoryWithSourceAssemblies = DI.config.hardCodedO2LocalBuildDir;
            var dependentAssemblies = new CecilAssemblyDependencies(targetAssemblies, new List<string> { directoryWithSourceAssemblies }).calculateDependencies();
            o2WcfClientProxy.createAppDomainWithDlls(testAppDomain, new List<string>(dependentAssemblies.Values));

            // will just open an empty O2 Gui
            //o2WcfClientProxy.invokeOnAppDomainObject(testAppDomain, "O2AscxGUI", "launch",null);

            // open the CSharpScripts O2 Module by simulating the Main execution invocation
            o2WcfClientProxy.invokeOnAppDomainObject(testAppDomain, typeof (StartCSharpScriptGui).FullName, "Main", new object[0]);

            o2WcfClientProxy.invokeOnAppDomainObject(testAppDomain, "O2AscxGUI", "logInfo", new[] {"Hello from Unit test"}); 
         //   Processes.Sleep(2000);
            o2WcfClientProxy.invokeOnAppDomainObject(testAppDomain, "O2AscxGUI", "close", null);            
          //  o2WcfClientProxy.invokeOnAppDomainObject(testAppDomain);
            //O2WcfUtils.
            wcfHost.stopHost();
        }
        public void test_openCirDataControlOnSeparateAppDomainViaWcf()
        {
            // First we need to figure out which DLL has the type we need
            var targetAssemblies = new List<string> { typeof (ascx_CirAnalysis).Assembly.Location , typeof(O2AscxGUI).Assembly.Location};
            //Assert.That(File.Exists(targetAssembly), "could not find assembly: " + targetAssembly);

            var dependentAssemblies = new CecilAssemblyDependencies(targetAssemblies, new List<string> { DI.config.hardCodedO2LocalBuildDir }).calculateDependencies();
            Assert.That(dependentAssemblies.Count > 0,"There we no dependencies calculated");
            DI.log.info("There are {0} dependent assemblies", dependentAssemblies.Count);
            // StringsAndLists.showListContents(dependentAssemblies);  // uncoment to see list of dlls
            var wcfHost = O2WcfUtils.createWcfHostForThisO2KernelProcess();
            IO2WcfKernelMessage o2WcfProxy = O2WcfUtils.getClientProxyForThisO2KernelProcess();

            runTestsViaWcfProxy(o2WcfProxy);
            // now that all tests are completed close host
            wcfHost.stopHost();            
            
        }
        public void runTestsViaWcfProxy(IO2WcfKernelMessage o2WcfProxy)
        {
            // first thing to do is to create a new AppDomain that has all the required dlls for the two main types we want to load ascx_CirAnalysis and O2AscxGUI
            var testAppDomain = "AppDomainWithCirData";
            var targetAssemblies = new List<string> { typeof(ascx_CirAnalysis).Assembly.Location, typeof(O2AscxGUI).Assembly.Location };
            var dependentAssemblies = new CecilAssemblyDependencies(targetAssemblies, new List<string> { DI.config.hardCodedO2LocalBuildDir }).calculateDependencies();
            o2WcfProxy.createAppDomainWithDlls(testAppDomain, new List<string>(dependentAssemblies.Values));
            
            // remote O2 Kernel is all set, now let's open the ascx_CirAnalyis control
            var ascxControlName = "Cir Analysis";
            o2WcfProxy.invokeOnAppDomainObject(testAppDomain, typeof(O2AscxGUI).FullName, "openAscxAsForm", new object[] { typeof(ascx_CirAnalysis).FullName, ascxControlName });

            // make sure it is there
            Assert.That((bool)o2WcfProxy.invokeOnAppDomainObject(testAppDomain, typeof(O2AscxGUI).FullName, "isAscxLoaded", new object[] { ascxControlName }), "isAscxLoaded should be true here");
            Assert.That(false == (bool)o2WcfProxy.invokeOnAppDomainObject(testAppDomain, typeof(O2AscxGUI).FullName, "isAscxLoaded", new object[] { "dummyControlName" }), "isAscxLoaded should be false here");

            // now load some CirData into it
            o2WcfProxy.O2Messages("executeOnAscxSync", new object[] { ascxControlName, "loadO2CirDataFile", new [] { DI.config.ExecutingAssembly } });

            // and retrive the data as string lists
            var cirClasses = o2WcfProxy.getStringListFromAscxControl(ascxControlName, "getClasses", new string[0]);
            var cirFunctions = o2WcfProxy.getStringListFromAscxControl(ascxControlName, "getFunctions", new string[0]);

            Assert.That(cirClasses.Count > 0 && cirFunctions.Count >0, "There were no classes or functions returned in cirClasses");
            DI.log.info("There were {0} classes returned", cirClasses.Count);                        
            DI.log.info("There were {0} functions returned", cirFunctions.Count);                        
                        
            // close the control and its parant form
            o2WcfProxy.invokeOnAppDomainObject(testAppDomain, typeof(O2AscxGUI).FullName, "closeAscxParent", new object[] { ascxControlName });            
        }