Request: Output directory

Jan 14, 2009 at 7:17 AM
I like the intitiative of this project! Having a build task that compresses all my javascript and css makes life so much easier. I do have a request about the output though. I personally do not want to concatenate all my js-files into one, what I'd like is to be able to enter an input directory like say "/js_src/*.js" and then output directory "/js/". The build task would then build the directory structure in the output directory and output files with the same name. What do you think?
Coordinator
Jan 14, 2009 at 12:16 PM
Edited Jan 14, 2009 at 12:18 PM
Hi ZNS - glad u like it :)

it's very simple to do, right now, with the current release.

The trick is that you can add multiple compressor tasks to your Build. each task will compress one file and output that one file. Therefore, no combining is required. If i can figure out how to make a new wiki page in codeplex, i'll show a complete, verbose example.

but off the top of my head .. how does this look?? (it's untested btw .. i'm just writing out loud) ..

Each task just deals with one file, and OVERWRITES the source with the destination.

    <!-- Compress JavaScript file #1. -->
    <CompressorTask
        CssFiles=""
        CssOutputFile=""
        CssCompressionType="YuiStockCompression"
        JavaScriptFiles="$(TempBuildDir)JavaScript\JavaScriptSample1.js"
        ObfuscateJavaScript="FoSho"
        PreserveAllSemicolons="Yeah"
        DisableOptimizations="Nope"
        EncodingType="Default"
        JavaScriptOutputFile="$(TempBuildDir)JavaScript\JavaScriptSample1.js"
        LoggingType="ALittleBit" />
  </Target>

   <!-- and now the second file ... -->
    <CompressorTask
        CssFiles=""
        CssOutputFile=""
        CssCompressionType="YuiStockCompression"
        JavaScriptFiles="$(TempBuildDir)JavaScript\JavaScriptSample2.js"
        ObfuscateJavaScript="FoSho"
        PreserveAllSemicolons="Yeah"
        DisableOptimizations="Nope"
        EncodingType="Default"
        JavaScriptOutputFile="$(TempBuildDir)JavaScript\JavaScriptSample2.js"
        LoggingType="ALittleBit" />
  </Target>
Feb 6, 2009 at 1:59 PM
Hi,
This project looked ideal, but I have the same problem as above.
This solution is potentially a bit of a pain if you have quite a few js files.
I had a look at the source code, and it looked relatively straightforward to add an extra parameter for this in CompressorTask.cs (good architecture ;-) ).
Is there any chance you could add this into the next stable release?
(I realise you may not want to use my buggy code as a base)

Thanks,

stew

@@ -23,7 +23,7 @@
         private bool _disableOptimizations;
         private int _lineBreakPosition;
         private Encoding _encoding;
-        private bool _compressOriginalFiles;
+
         #endregion
 
         #region Properties
@@ -41,7 +41,7 @@
         public string DeleteJavaScriptFiles { get; set; }
         public string JavaScriptOutputFile { get; set; }
         public string LoggingType { get; set; }
-        public string CompressOriginalFiles { get; set; }
+        
         #endregion
 
         #region Methods
@@ -205,15 +205,6 @@
                     break;
             }
 
-            // Optional Property.           
-            if (!string.IsNullOrEmpty(this.CompressOriginalFiles))
-            {
-              this._compressOriginalFiles = CompressorTask.ParseSillyTrueFalseValue(this.CompressOriginalFiles.ToUpperInvariant());
-            }
-            else
-            {
-              this._compressOriginalFiles = false;
-            }
             #endregion
         }
 
@@ -354,13 +345,6 @@
                             }
                             finalContent.Append(compressedContent);
                         }
-
-                        if (this._compressOriginalFiles)
-                        {
-                          this.LogMessage(string.Format(CultureInfo.InvariantCulture, "    ** Writing Files: {0} ",
-                        file));
-                          File.WriteAllText(file, compressedContent);
-                        }
                     }
                     catch (Exception exception)
                     {
Feb 11, 2009 at 5:25 PM
You don't need any change in the compressor task.

You can easily achieve this using the right MSBuild syntax to batch the task.

Try this:

  <Target Name="JavaScriptCssMinify">
    <!-- Create the list of files: you can create an explicit semi-colon delimited list or use wildcards as below -->
    <CreateItem Include="$(SolutionDir)**\*.js">
      <Output TaskParameter="Include" ItemName="JavaScriptFiles" />
    </CreateItem>
    <CreateItem Include="$(SolutionDir)**\*.css">
      <Output TaskParameter="Include" ItemName="CssFiles" />
    </CreateItem>
    
    <!-- Execute CompressorTask for each file in the list -->
    <CompressorTask
       Condition="'%(JavaScriptFiles.Identity)' != ''"
       JavaScriptFiles="%(JavaScriptFiles.Identity)"
       ObfuscateJavaScript="FoSho"
       PreserveAllSemicolons="Yeah"
       DisableOptimizations="Nope"
       EncodingType="Default"
       DeleteJavaScriptFiles="false"
       JavaScriptOutputFile="%(JavaScriptFiles.Identity)"
       LoggingType="ALittleBit" />
    <CompressorTask
       Condition="'%(CssFiles.Identity)' != ''"
       CssFiles="%(CssFiles.Identity)"
       DeleteCssFiles="false"
       CssOutputFile="%(CssFiles.Identity)"
       CssCompressionType="YuiStockCompression"
       EncodingType="Default"
       LoggingType="ALittleBit" />
  </Target>

This sample will replace the compressed files in-place. Play around with it to get what you need.
Basically the % operator helps to batch tasks and its the right MSBuild approach.

Refer:
http://msdn.microsoft.com/en-us/library/s2y3e43x.aspx
http://msdn.microsoft.com/en-us/library/ms171473.aspx
http://www.sedodream.com/PermaLink,guid,5f1e0445-ce3d-4052-ba80-42fd19512d42.aspx
Coordinator
Feb 11, 2009 at 9:30 PM
Awesome post nameetpai!!! i'll add this to the front page when i get some time :)
Feb 24, 2009 at 8:06 PM
This thread saved my day.  purekrome and nameetpai, thanks for the info!
Feb 26, 2009 at 9:19 AM
Great contribution, nameetpai.

And, How could be a single file some of many excluded from the compression batch? Things like .vsdoc, for instance.

Thanks for your attention.
Coordinator
Feb 26, 2009 at 10:09 PM
Hi Belitre,

are you asking for:

"All files in a directory BUT exclude some which you manually define?"
Mar 10, 2009 at 12:14 PM
Yes. I apologize i've cross sent the same question within other discussion post.


Sep 17, 2009 at 5:51 AM

Many thanks, nameetpai - that is exactly what we need, and that code snippet definitely needs to go on the front page, otherwise you'll continue to get questions asking about it!