Esempio n. 1
0
        protected void BuildStyleFile(int fileID, CancelDelegate cancelled)
        {
            File file = EngineInstance.Files.FromID(fileID);

            // Will return null if the file isn't used by this builder
            Path outputFile = Styles_OutputFile(file.FileName);

            if (outputFile == null)
            {
                return;
            }

            if (file.Deleted)
            {
                if (System.IO.File.Exists(outputFile))
                {
                    System.IO.File.Delete(outputFile);

                    lock (accessLock)
                    { buildState.FoldersToCheckForDeletion.Add(outputFile.ParentFolder); }
                }
            }

            else             // file new or changed
            {
                // Creates all subdirectories needed.  Does nothing if it already exists.
                System.IO.Directory.CreateDirectory(outputFile.ParentFolder);

                string extension = outputFile.Extension.ToLower();

                if (extension == "js" || extension == "json")
                {
                    ResourceProcessors.JavaScript jsProcessor = new ResourceProcessors.JavaScript();
                    string output = jsProcessor.Process(System.IO.File.ReadAllText(file.FileName), EngineInstance.Config.ShrinkFiles);
                    System.IO.File.WriteAllText(outputFile, output);
                }
                else if (extension == "css")
                {
                    ResourceProcessors.CSS cssProcessor = new ResourceProcessors.CSS();
                    string output = cssProcessor.Process(System.IO.File.ReadAllText(file.FileName), EngineInstance.Config.ShrinkFiles);
                    System.IO.File.WriteAllText(outputFile, output);
                }
                else
                {
                    System.IO.File.Copy(file.FileName, outputFile, true);
                }
            }
        }
Esempio n. 2
0
        // Group: Files.IStyleChangeWatcher Functions
        // __________________________________________________________________________


        public Files.Manager.ReleaseClaimedFileReason OnAddOrChangeFile(Path originalStyleFile)
        {
            Path outputStyleFile = Styles_OutputFile(originalStyleFile);

            if (outputStyleFile == null)
            {
                return(Files.Manager.ReleaseClaimedFileReason.SuccessfullyProcessed);
            }
            else
            {
                // Creates all subdirectories needed.  Does nothing if it already exists.
                System.IO.Directory.CreateDirectory(outputStyleFile.ParentFolder);

                try
                {
                    string extension = outputStyleFile.Extension.ToLower();

                    if (extension == "js" || extension == "json")
                    {
                        ResourceProcessors.JavaScript jsProcessor = new ResourceProcessors.JavaScript();
                        string output = jsProcessor.Process(System.IO.File.ReadAllText(originalStyleFile), EngineInstance.Config.ShrinkFiles);
                        System.IO.File.WriteAllText(outputStyleFile, output);
                    }
                    else if (extension == "css")
                    {
                        ResourceProcessors.CSS cssProcessor = new ResourceProcessors.CSS();
                        string output = cssProcessor.Process(System.IO.File.ReadAllText(originalStyleFile), EngineInstance.Config.ShrinkFiles);
                        System.IO.File.WriteAllText(outputStyleFile, output);
                    }
                    else
                    {
                        System.IO.File.Copy(originalStyleFile, outputStyleFile, true);
                    }
                }
                catch (System.IO.FileNotFoundException)
                { return(Files.Manager.ReleaseClaimedFileReason.FileDoesntExist); }
                catch (System.IO.DirectoryNotFoundException)
                { return(Files.Manager.ReleaseClaimedFileReason.FileDoesntExist); }
                catch (System.UnauthorizedAccessException)
                { return(Files.Manager.ReleaseClaimedFileReason.CantAccessFile); }
                catch (System.IO.IOException)
                { return(Files.Manager.ReleaseClaimedFileReason.CantAccessFile); }

                return(Files.Manager.ReleaseClaimedFileReason.SuccessfullyProcessed);
            }
        }
Esempio n. 3
0
        // Group: Builder Functions
        // __________________________________________________________________________


        /* Function: BuildMainStyleFiles
         * Builds main.css and main.js.
         */
        protected void BuildMainStyleFiles(CancelDelegate cancelDelegate)
        {
            // Creates all subdirectories needed.  Does nothing if it already exists.
            System.IO.Directory.CreateDirectory(Styles_OutputFolder());


            // main.css

            // There's nothing to condense so just write it directly to a file.
            using (System.IO.StreamWriter mainCSSFile = System.IO.File.CreateText(Styles_OutputFolder() + "/main.css"))
            {
                foreach (HTMLStyle style in styles)
                {
                    if (style.IsCSSOnly)
                    {
                        Path outputPath         = Styles_OutputFile(style.CSSFile);
                        Path relativeOutputPath = outputPath.MakeRelativeTo(Styles_OutputFolder());
                        mainCSSFile.Write("@import URL(\"" + relativeOutputPath.ToURL() + "\");");
                    }
                    else if (style.Links != null)
                    {
                        foreach (HTMLStyleFileLink link in style.Links)
                        {
                            // We don't care about filters for CSS files.
                            if (link.File.Extension.ToLower() == "css")
                            {
                                Path outputPath         = Styles_OutputFile(style.Folder + '/' + link.File);
                                Path relativeOutputPath = outputPath.MakeRelativeTo(Styles_OutputFolder());
                                mainCSSFile.Write("@import URL(\"" + relativeOutputPath.ToURL() + "\");");
                            }
                        }
                    }
                }
            }


            // main.js

            StringBuilder[] jsLinks   = new StringBuilder[AllPageTypes.Length];
            StringBuilder[] jsOnLoads = new StringBuilder[AllPageTypes.Length];

            foreach (HTMLStyle style in styles)
            {
                if (style.Links != null)
                {
                    foreach (var link in style.Links)
                    {
                        string extension = link.File.Extension.ToLower();

                        if (extension == "js" || extension == "json")
                        {
                            if (jsLinks[(int)link.Type] == null)
                            {
                                jsLinks[(int)link.Type] = new StringBuilder();
                            }
                            else
                            {
                                jsLinks[(int)link.Type].Append(", ");
                            }

                            Path outputPath         = Styles_OutputFile(style.Folder + "/" + link.File);
                            Path relativeOutputPath = outputPath.MakeRelativeTo(Styles_OutputFolder());
                            jsLinks[(int)link.Type].Append("\"" + relativeOutputPath.ToURL() + "\"");
                        }
                    }
                }

                if (style.OnLoad != null)
                {
                    foreach (var onLoadStatement in style.OnLoad)
                    {
                        StringBuilder onLoadStatementsForType = jsOnLoads[(int)onLoadStatement.Type];

                        if (onLoadStatementsForType == null)
                        {
                            onLoadStatementsForType = new StringBuilder();
                            jsOnLoads[(int)onLoadStatement.Type] = onLoadStatementsForType;
                        }

                        onLoadStatementsForType.Append("      ");
                        onLoadStatementsForType.Append(onLoadStatement.Statement);

                        if (onLoadStatementsForType[onLoadStatementsForType.Length - 1] != ';')
                        {
                            onLoadStatementsForType.Append(';');
                        }

                        onLoadStatementsForType.Append('\n');
                    }
                }
            }

            StringBuilder jsOutput = new System.Text.StringBuilder(
                "\"use strict\";\n" +
                "\n" +
                "var NDLoader = new function ()\n" +
                "   {\n");

            for (int i = 0; i < AllPageTypes.Length; i++)
            {
                jsOutput.Append("   this.JSLinks_" + AllPageTypeNames[i] + " = [ ");

                if (jsLinks[i] != null)
                {
                    jsOutput.Append(jsLinks[i].ToString());
                }

                jsOutput.Append(" ];\n");
            }


            jsOutput.Append(
                "\n" +
                "   this.LoadJS = function (pageType, relativePrefix)\n" +
                "      {\n" +
                "      this.LoadJSArray(this.JSLinks_All, relativePrefix);\n" +
                "      this.LoadJSArray(this['JSLinks_' + pageType], relativePrefix);\n" +
                "      };\n" +
                "\n" +
                "   this.LoadJSArray = function (links, relativePrefix)\n" +
                "      {\n" +

                // WebKit, and I'm guessing KHTML just to be safe, doesn't import scripts included the other way in time
                // for their functions to be accessible to body.OnLoad().

                "      if (navigator.userAgent.indexOf('KHTML') != -1)\n" +
                "         {\n" +
                "         for (var i = 0; i < links.length; i++)\n" +
                "            {\n" +
                "            document.write('<script type=\"text/javascript\" src=\"' + relativePrefix + links[i] + '\"></script>');\n" +
                "            }\n" +
                "         }\n" +

                // The proper way.

                "      else\n" +
                "         {\n" +
                "         var head = document.getElementsByTagName('head')[0];\n" +
                "         \n" +
                "         for (var i = 0; i < links.length; i++)\n" +
                "            {\n" +
                "            var script = document.createElement('script');\n" +
                "            script.src = relativePrefix + links[i];\n" +
                "            script.type = 'text/javascript';\n" +
                "            \n" +
                "            head.appendChild(script);\n" +
                "            }\n" +
                "         }\n" +
                "      };\n" +
                "\n" +
                "   this.OnLoad = function (pageType)\n" +
                "      {\n" +
                "      this.OnLoad_All();\n" +
                "      this['OnLoad_' + pageType]();\n" +
                "      };\n");


            for (int i = 0; i < AllPageTypes.Length; i++)
            {
                jsOutput.Append(
                    "\n" +
                    "   this.OnLoad_" + AllPageTypeNames[i] + " = function ()\n" +
                    "      {\n");

                if (jsOnLoads[i] != null)
                {
                    jsOutput.Append(jsOnLoads[i].ToString());
                }

                jsOutput.Append(
                    "      };\n");
            }

            jsOutput.Append(
                "   };\n");

            string jsOutputString = jsOutput.ToString();

            ResourceProcessors.JavaScript jsProcessor = new ResourceProcessors.JavaScript();
            jsOutputString = jsProcessor.Process(jsOutputString, EngineInstance.Config.ShrinkFiles);

            System.IO.File.WriteAllText(Styles_OutputFolder() + "/main.js", jsOutputString);
        }