Ejemplo n.º 1
0
        public bool CompileClasses(out CompilerErrorCollection compilerErrorCollection, bool copyCompiledCode, params string[] definitions)
        {
            if (this.CompilerLanguage == "Html")
            {
                compilerErrorCollection = new CompilerErrorCollection();
                return(true);
            }

            this.assembly = null;

            Debug.StartTimer("Page:" + this.ID + ":Compile()");

            List <string> classCode = new List <string>();

            foreach (PageClass pageClass in LatestReversion.Classes)
            {
                classCode.Add(pageClass.Data);
            }

            if (this.CompilerLanguage == null || this.CompilerLanguage == string.Empty)
            {
                this.CompilerLanguage = "CSharp";
            }

            dynamic languageHandler = null;

            try
            {
                languageHandler = ModuleSession.Get(HttpContext.Current).GetLanguageHandler(this.CompilerLanguage);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Could not fetch language handler for {0}, Exception: {1}", this.ID, e);
                compilerErrorCollection = new CompilerErrorCollection();
                return(false);
            }

            if (languageHandler != null)
            {
                if (copyCompiledCode)
                {
                    this.CompiledCode = languageHandler.CompileClasses(classCode.ToArray(), this.References.ToArray(), out compilerErrorCollection);
                }
                else
                {
                    languageHandler.CompileClasses(classCode.ToArray(), this.References.ToArray(), out compilerErrorCollection);
                }

                return(true);
            }
            else
            {
                string[] references = this.References.ToArray();

                for (int i = 0; i < references.Length; i++)
                {
                    if (references[i].ToLower().EndsWith(".bmf"))
                    {
                        string moduleID = references[i].Substring(0, references[i].Length - 4);
                        File.WriteAllBytes(HttpContext.Current.Server.MapPath("./Data/Temp/") + moduleID + ".bmf.dll", ModuleManager.Session.GetModuleByID(moduleID).CompiledCode);
                        references[i] += ".dll";
                    }
                }

                CodeDomProvider    codeDomProvider = CodeDomProvider.CreateProvider(this.CompilerLanguage);
                CompilerParameters codeParameters  = new CompilerParameters();

                codeParameters.IncludeDebugInformation = true;
                codeParameters.CompilerOptions         = string.Format("{0}/lib:{1},{2},{3} /define:{4}", this.CompilerLanguage != "JScript" ? "/debug:pdbonly " : "", HttpContext.Current.Server.MapPath("./bin/"), HttpContext.Current.Server.MapPath("./bin/Libraries/"), HttpContext.Current.Server.MapPath("./Data/Temp/"), definitions.Length > 0 ? "WEBCORE;WEBCORE_2014;" + string.Join(";", definitions) : "WEBCORE;WEBCORE_2014");
                codeParameters.GenerateInMemory        = false;
                codeParameters.WarningLevel            = 4;
                codeParameters.ReferencedAssemblies.AddRange(References.ToArray());

                CompilerErrorCollection tmpCompilerErrorCollection = new CompilerErrorCollection();

                CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromSource(codeParameters, classCode.ToArray());

                compilerErrorCollection = compilerResults.Errors;
                Debug.StopTimer("Page:" + this.ID + ":Compile()");

                if (tmpCompilerErrorCollection.Count > 0)
                {
                    compilerErrorCollection.AddRange(tmpCompilerErrorCollection);
                }

                if (compilerErrorCollection.HasErrors)
                {
                    return(false);
                }

                FileInfo AssemblyFileInfo = new FileInfo(compilerResults.PathToAssembly);

                if (copyCompiledCode)
                {
                    this.CompiledCode = File.ReadAllBytes(AssemblyFileInfo.FullName);
                    if (File.Exists(AssemblyFileInfo.FullName.Replace(".dll", ".pdb")))
                    {
                        this.CompiledCodeSymbols = File.ReadAllBytes(AssemblyFileInfo.FullName.Replace(".dll", ".pdb"));
                    }
                }

                return(true);
            }
        }
Ejemplo n.º 2
0
        public string GetTemplateAndParse(string templateKey, dynamic page)
        {
            string templateData = GetTemplate(templateKey);

            templateData = ThemeManager.Session.ParseTemplate(templateData, page.CorePage, page, ModuleSession.Get(HttpContext.Current));
            return(templateData);
        }