Пример #1
0
        public static void Compress(TextReader reader, TextWriter writer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            JavaScriptCompressor compressor = new JavaScriptCompressor(reader, writer);

            compressor.Compress();
        }
        public static void Compress(TextReader reader, TextWriter writer)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            if (writer == null)
                throw new ArgumentNullException("writer");

            JavaScriptCompressor compressor = new JavaScriptCompressor(reader, writer);
            compressor.Compress();
        }
Пример #3
0
        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>

        public override bool Execute()
        {
            //
            // Nothing to work on? Bail out early.
            //

            if (files == null || files.Length == 0)
            {
                compressedFiles = new ITaskItem[0];
                return(true);
            }

            //
            // Compress all files...da main loop!
            //

            ITaskItem[]      inputs     = Files;
            List <ITaskItem> outputList = new List <ITaskItem>(inputs.Length);

            foreach (ITaskItem input in inputs)
            {
                string path = input.ItemSpec;

                if (path.Length == 0)
                {
                    continue;
                }

                try
                {
                    //
                    // Get the original file size to compare later.
                    // If the file is empty then skip it.
                    //

                    FileInfo info = new FileInfo(path);

                    if (info.Length == 0)
                    {
                        continue;
                    }

                    //
                    // Determine if a specific encoding should be assumed.
                    //

                    Encoding encoding     = null;
                    string   encodingName = Encoding ?? string.Empty;

                    if (encodingName.Length > 0)
                    {
                        encoding = System.Text.Encoding.GetEncoding(encodingName);
                    }

                    //
                    // Read the source and compress to memory. This assumes
                    // that we won't be dealing with huge JavaScript source
                    // files so doing the work in memory won't hurt too much.
                    //

                    Log.LogMessage(string.Format(Resources.JSCompressCompressing, path));

                    string source;

                    using (StreamReader reader = encoding != null ?
                                                 new StreamReader(path, encoding) : new StreamReader(path))
                    {
                        StringWriter writer = new StringWriter();
                        JavaScriptCompressor.Compress(reader, writer);
                        source = writer.GetStringBuilder().ToString();

                        if (encoding == null)
                        {
                            encoding = reader.CurrentEncoding;
                        }
                    }

                    //
                    // Write out the result!
                    //

                    Log.LogMessage(MessageImportance.Low,
                                   string.Format(Resources.JSCompressWriting, path, encoding.EncodingName));

                    File.WriteAllText(path, source, encoding);

                    outputList.Add(input);

                    //
                    // Compare new file size and log the compression ratio.
                    //

                    FileInfo newInfo = new FileInfo(path);
                    double   ratio   = 1 - (newInfo.Length / (double)info.Length);

                    Log.LogMessage(MessageImportance.Low, string.Format(
                                       Resources.JSCompressCompressed,
                                       ratio.ToString("P"),
                                       info.Length.ToString("N0"),
                                       newInfo.Length.ToString("N0")));
                }
                catch (Exception e)
                {
                    if (e is IOException || e is JavaScriptCompressor.Exception)
                    {
                        Log.LogError("Error compressing " + path + ". " + e.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            compressedFiles = outputList.ToArray();

            return(!Log.HasLoggedErrors);
        }