Allow "Silent" option for build task

Apr 18, 2011 at 9:43 PM

I have some js files that already have a .min.js file that were downloaded, for example, jQuery already has a min.js file. I also have the minified version associated with source control, and when it is checked in, the file is read-only.

When I build, I don't want to see error messages that it cannot write the file because it's read-only.

A quick fix, add a bool Silent property to AjaxMinBuildTask, then change out the calls to Log.LogError to use a private method

For example:

Log.LogError("The Microsoft Ajax Minifier does not have permission to write to {0}",
new object[] { path });

Would become:

this.LogError("The Microsoft Ajax Minifier does not have permission to write to {0}",
new object[] { path });

And LogError would just be a method that did this:

    private void LogError(string message, params object[] messageArgs)
        {
            if (!this.Silent)
            {
                Log.LogError(message, messageArgs);
            }
        }

Coordinator
May 4, 2011 at 6:43 PM

Alternatively, what if the build task was a little smarter to begin with? If the file it's being asked to minify already has a .MIN.js (or .MIN.css) extension and it's read-only, the task should probably just skip it altogether (and issue an informational log message that it was skipped). Right?

Maybe we could even take the .MIN.js check out of it altogether: if the destination file already exists and is read-only, skip it and issue an informational message that the file was skipped. What do you think?

Coordinator
May 4, 2011 at 7:57 PM

Another alternative: you should set up your build task to exclude JS files that match the pattern "*.min.js" automatically. In your .csproj file, check your <JS> element; it should have an Exclude property:

<JS Include="**\*.js" Exclude="**\*.min.js;Scripts\*.js" />

The entire block should look something like:

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
  <Target Name="AfterBuild">
    <ItemGroup>
      <JS Include="**\*.js" Exclude="**\*.min.js;Scripts\*.js" />
    </ItemGroup>
    <ItemGroup>
      <CSS Include="**\*.css" Exclude="**\*.min.css" />
    </ItemGroup>
    <AjaxMin WarningLevel="5" TreatWarningsAsErrors="false" 
             JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js" 
             CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />
  </Target>
If you set it up that way, then your pre-minified files won't even be considered and therefore won't cause any error messages.