/**
         * Standard procedure to compile resources:
         * 1. Get an AppDomain correspondint to the Classloader.
         * 2. Create an assembly in the given appdomain
         * 3. Create a type for each resource, given the className (resourceName), contents
              (pReader.getBytes(resourceName)), and the AppDomain.
         * 4.  Write the compiled types to the store.
         */
        public override CompilationResult compile(
            string[] pResourceNames,
            ResourceReader pReader,
            ResourceStore pStore,
            ClassLoader pClassLoader
            )
        {
            int OFFSETCONSTANT = 8;
            Type[] types = new Type[pResourceNames.Length];
            string[] contents = new string[pResourceNames.Length];
            CodeSnippetCompileUnit[] units = new CodeSnippetCompileUnit[pResourceNames.Length];
            for (int i = 0; i < types.Length; i++)
            {
                string resourceName = pResourceNames[i].Replace('.','/')+".java";
                byte[] byteArray = pReader.getBytes(resourceName);
                string fileContents = this.StringFromBytes(byteArray);
                units[i] = new CodeSnippetCompileUnit(fileContents.Replace("cli.", ""));
                if (fileContents.Contains("public static void consequence"))
                {
                    object[] info = this.GetLinePragmaInfo(fileContents,OFFSETCONSTANT);
                    if(info != null)
                        units[i].LinePragma = new CodeLinePragma(info[0] as string, (int)info[1]);
                }
            }

            CodeDomProvider provider = GetProvider();
            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.GenerateInMemory = true;
            compilerParameters.IncludeDebugInformation = true;

            //            compilerParameters.OutputAssembly = pResourceNames[i].Substring(pResourceNames[i].LastIndexOf('.') + 1);

            int count = 0;
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {

               if (assembly.FullName.StartsWith("CompiledRules"))
                {
                    try
                    {
                        File.Delete(assembly.Location);
                    }
                    catch (System.Exception e)
                    {
                        count++;
                    }
                }
                else
                {
                    compilerParameters.ReferencedAssemblies.Add(assembly.Location);
                }

            }

            compilerParameters.OutputAssembly = "CompiledRules" + count + ".dll";

            CompilerResults results = provider.CompileAssemblyFromDom
                //(compilerParameters, contents);
                (compilerParameters, units);

            Collection problems = new ArrayList();

            DotnetPackageCompilationData pcData = (DotnetPackageCompilationData)
                ((PackageStore)pStore).getPackageCompilationData();

            MemoryStream stream = new MemoryStream(1024);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, results.CompiledAssembly);

            for (int i = 0; i < types.Length; i++)
            {
                string resourceName = pResourceNames[i];
                pcData.write(resourceName, new object[]{results.CompiledAssembly, stream.GetBuffer()});

            }
            CompilationProblem[] result = new CompilationProblem[problems.size()];
            return new CompilationResult(result);
        }
Beispiel #2
0
        /**
         * Standard procedure to compile resources:
         * 1. Get an AppDomain correspondint to the Classloader.
         * 2. Create an assembly in the given appdomain
         * 3. Create a type for each resource, given the className (resourceName), contents
         *    (pReader.getBytes(resourceName)), and the AppDomain.
         * 4.  Write the compiled types to the store.
         */
        public override CompilationResult compile(
            string[] pResourceNames,
            ResourceReader pReader,
            ResourceStore pStore,
            ClassLoader pClassLoader
            )
        {
            int OFFSETCONSTANT = 8;

            Type[]   types    = new Type[pResourceNames.Length];
            string[] contents = new string[pResourceNames.Length];
            CodeSnippetCompileUnit[] units = new CodeSnippetCompileUnit[pResourceNames.Length];
            for (int i = 0; i < types.Length; i++)
            {
                string resourceName = pResourceNames[i].Replace('.', '/') + ".java";
                byte[] byteArray    = pReader.getBytes(resourceName);
                string fileContents = this.StringFromBytes(byteArray);
                units[i] = new CodeSnippetCompileUnit(fileContents.Replace("cli.", ""));
                if (fileContents.Contains("public static void consequence"))
                {
                    object[] info = this.GetLinePragmaInfo(fileContents, OFFSETCONSTANT);
                    if (info != null)
                    {
                        units[i].LinePragma = new CodeLinePragma(info[0] as string, (int)info[1]);
                    }
                }
            }


            CodeDomProvider    provider           = GetProvider();
            CompilerParameters compilerParameters = new CompilerParameters();

            compilerParameters.GenerateInMemory        = true;
            compilerParameters.IncludeDebugInformation = true;

//            compilerParameters.OutputAssembly = pResourceNames[i].Substring(pResourceNames[i].LastIndexOf('.') + 1);

            int count = 0;

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (assembly.FullName.StartsWith("CompiledRules"))
                {
                    try
                    {
                        File.Delete(assembly.Location);
                    }
                    catch (System.Exception e)
                    {
                        count++;
                    }
                }
                else
                {
                    compilerParameters.ReferencedAssemblies.Add(assembly.Location);
                }
            }

            compilerParameters.OutputAssembly = "CompiledRules" + count + ".dll";

            CompilerResults results = provider.CompileAssemblyFromDom
                                      //(compilerParameters, contents);
                                          (compilerParameters, units);

            Collection problems = new ArrayList();

            DotnetPackageCompilationData pcData = (DotnetPackageCompilationData)
                                                  ((PackageStore)pStore).getPackageCompilationData();

            MemoryStream    stream    = new MemoryStream(1024);
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, results.CompiledAssembly);

            for (int i = 0; i < types.Length; i++)
            {
                string resourceName = pResourceNames[i];
                pcData.write(resourceName, new object[] { results.CompiledAssembly, stream.GetBuffer() });
            }
            CompilationProblem[] result = new CompilationProblem[problems.size()];
            return(new CompilationResult(result));
        }