コード例 #1
0
 /// <summary>
 /// Gets the import files from the file.
 /// </summary>
 /// <param name="file">The file to analyze for imports.</param>
 /// <returns>The imports for the file.</returns>
 public static IEnumerable <string> GetImportFiles(string file)
 {
     if (FileExtension.IsCss(file) ||
         FileExtension.IsLess(file))
     {
         return(Css.CssParser.GetFileImports(file));
     }
     return(new List <string>(0));
 }
コード例 #2
0
ファイル: Bundler.cs プロジェクト: beufordy3k/Frappe
        private void ConcatBundle(BundleState bundle)
        {
            if (bundle == null)
            {
                throw new System.ArgumentNullException("bundle");
            }

            var outputFileInfo   = bundle.BundleOutputFile;
            var outputFile       = outputFileInfo.FullName;
            var includeFileInfos = bundle.Includes.Select(include => include.OutputFile).ToList();

            try
            {
                // clear the output file
                File.WriteAllText(outputFile, "");

                // concat all the output of the includes together
                foreach (var includeFileInfo in includeFileInfos)
                {
                    var file = includeFileInfo.FullName;
                    try
                    {
                        if (FileExtension.IsCss(file) ||
                            FileExtension.IsLess(file))
                        {
                            var css = Css.CssParser.GetCss(file, true, import =>
                            {
                                LogWarning("An import file could not be found. File: {0}, RelativeTo: {1}, Import: {2}, Statement: {3}", file, import.File, import.ImportFile, import.Statement);
                            });

                            // fix the relative paths
                            css = Css.CssParser.UpdateRelativePaths(css, includeFileInfo.DirectoryName, outputFileInfo.DirectoryName);

                            // append the css to the file
                            File.AppendAllText(outputFile, css);
                        }
                        else
                        {
                            File.AppendAllText(outputFile, File.ReadAllText(file));
                        }

                        OnFileBundled(outputFile, file);
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(string.Format("Failed to append include to bundle output. An error occurred trying to append the include to the bundle output.  File: {0}", file), ex);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("Failed to create bundle output. An error occurred trying to create the bundle output. OutputFile: {0}, Files: {1}", outputFile, string.Join(", ", includeFileInfos.Where(s => s != null).Select(s => string.Format("\"{0}\"", s)))), ex);
            }
        }
コード例 #3
0
        /// <summary>
        /// Get the output file for the include.
        /// </summary>
        /// <returns>The output file for the include.</returns>
        public string GetOutputFile()
        {
            if (File == null)
            {
                throw new ArgumentNullException("File");
            }
            else if (File == string.Empty)
            {
                throw new ArgumentOutOfRangeException("File", "Value cannot be empty.");
            }

            if (string.IsNullOrWhiteSpace(OutputFile))
            {
                string inputFile = File;
                if (FileExtension.IsMinified(inputFile))
                {
                    return(inputFile);
                }
                else
                {
                    if (FileExtension.IsLess(inputFile))
                    {
                        inputFile += ".css";
                    }
                    else if (FileExtension.IsJsHtml(inputFile))
                    {
                        inputFile += ".js";
                    }
                    return(IncludeFileRegex.Replace(inputFile, @"${Name}.min${TypeExt}"));
                }
            }
            else
            {
                if (Path.IsPathRooted(OutputFile))
                {
                    return(OutputFile);
                }
                else
                {
                    return(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(File)), OutputFile));
                }
            }
        }
コード例 #4
0
ファイル: Bundler.cs プロジェクト: beufordy3k/Frappe
        private void Transform(IncludeState include)
        {
            if (include == null)
            {
                throw new System.ArgumentNullException("include");
            }

            try
            {
                if (include.Transformed)
                {
                    // IF we've already transformed this file
                    // THEN bail

                    return;
                }

                LogInfo("Ensuring the include \"{0}\" is up-to-date.", include.File.FullName);

                // what are the input files
                var inputFiles = new List <FileInfo>();
                inputFiles.Add(include.File);
                inputFiles.AddRange(include.Imports.Select(s => new FileInfo(s)));

                // what is the most recent last write time utc
                var inputMostRecentLastWriteTimeUtc = inputFiles.Select(f => f.LastWriteTimeUtc)
                                                      // add the frappe lib as a last write utc source so the
                                                      // output will be updated when frappe is published to fix
                                                      // bugs and improve the output
                                                      .Concat(new[] {
                    ExecutingAssemblyFile.LastWriteTimeUtc
                })
                                                      .OrderByDescending(d => d)
                                                      .First();

                FileInfo inputFile = include.File;
                if (FileExtension.IsLess(include.File.FullName))
                {
                    // IF it's a less file
                    // THEN we need to create the css output file

                    inputFile = new FileInfo(include.File.FullName + ".css");
                    if (!inputFile.Exists || inputFile.Length == 0 || inputFile.LastWriteTimeUtc < inputMostRecentLastWriteTimeUtc)
                    {
                        // IF the input file doesn't exist
                        //   OR the file is older than any of the inputs
                        // THEN re-compile the less file

                        LogInfo("The css output \"{0}\" for less include \"{1}\" does not exist or is out-of-date.", inputFile, include.File.FullName);

                        // compile the less file
                        this.CompileLess(include.File.FullName, inputFile.FullName);

                        // refresh the input file
                        inputFile.Refresh();

                        // set the file last write time to match the inputs since it's generated based on them
                        inputFile.LastWriteTimeUtc = inputMostRecentLastWriteTimeUtc;

                        LogInfo("The css output \"{0}\" for less include \"{1}\" has been updated.", inputFile, include.File.FullName);
                    }
                }
                else if (FileExtension.IsJsHtml(include.File.FullName))
                {
                    // remove the .html from the file name so it becomes name.js
                    inputFile = new FileInfo(include.File.FullName + ".js");
                    if (!inputFile.Exists || inputFile.Length == 0 || inputFile.LastWriteTimeUtc < inputMostRecentLastWriteTimeUtc)
                    {
                        // IF the input file doesn't exist
                        //   OR the file is older than any of the inputs
                        // THEN re-compile the less file

                        LogInfo("The javascript output \"{0}\" for js.html include \"{1}\" does not exist or is out-of-date.", inputFile, include.File.FullName);

                        // compile the js.html file
                        this.CompileJsHtml(include.File.FullName, inputFile.FullName);

                        // refresh the input file
                        inputFile.Refresh();

                        // set the file last write time to match the inputs since it's generated based on them
                        inputFile.LastWriteTimeUtc = inputMostRecentLastWriteTimeUtc;

                        LogInfo("The javascript output \"{0}\" for js.html include \"{1}\" has been updated.", inputFile, include.File.FullName);
                    }
                }

                // do they exist and are they up-to-date relative to the outputs?
                var outputFile = include.OutputFile;

                // tranform the include
                if (!outputFile.Exists || outputFile.Length == 0 || outputFile.LastWriteTimeUtc < inputFile.LastWriteTimeUtc)
                {
                    // IF the output file does NOT exist
                    //   OR the output file is out-of-date
                    // THEN rebuild the output file

                    LogInfo("The minified output \"{0}\" for include \"{1}\" does not exist or is out-of-date.", outputFile, inputFile);

                    if (FileExtension.IsCss(inputFile.FullName))
                    {
                        MinifyCss(inputFile.FullName, outputFile.FullName);
                    }
                    else if (FileExtension.IsJavaScript(inputFile.FullName))
                    {
                        MinifyJavaScript(inputFile.FullName, outputFile.FullName);
                    }
                    else
                    {
                        throw new NotImplementedException(string.Format("Transformation of file has not been implemented. File: {0}", inputFile.FullName));
                    }

                    // update the output file
                    outputFile.Refresh();

                    // set the file last write time to match the input since it's generated based on it
                    outputFile.LastWriteTimeUtc = inputMostRecentLastWriteTimeUtc;

                    LogInfo("The minified output \"{0}\" for include \"{1}\" has been updated.", outputFile, inputFile);
                }

                // update the state
                include.Transformed = true;
                include.OutputFile  = outputFile;
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("An error occurred trying to transform bundle include. Include File: {0}", include.File), ex);
            }
        }