Skip to content

Instantly share code, notes, and snippets.

@lot224
Created December 19, 2012 16:28
Show Gist options
  • Save lot224/4338012 to your computer and use it in GitHub Desktop.
Save lot224/4338012 to your computer and use it in GitHub Desktop.
Microsoft Build Task to Concatenate/Compress/Obfuscate javascript files.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Debug" xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
<UsingTask TaskName="Concatenate" AssemblyFile="location to the dll that contains the concatenate class" />
<ItemGroup>
<FirstGroup Include="dir\javascript1.js" />
<FirstGroup Include="dir\javascript2.js" />
<FirstGroup Include="dir\javascript3.js" />
<SecondGroup Include="dir\javascript4.js" />
<SecondGroup Include="dir\javascript5.js" />
<SecondGroup Include="dir\javascript6.js" />
</ItemGroup>
<Target Name="Debug">
<Concatenate files="@(FirstGroup)" output="out\firstgroup.js" />
<Concatenate files="@(SecondGroup)" output="out\secondgroup.js" />
</Target>
<Target Name="Release">
<Concatenate files="@(FirstGroup)" output="out\firstgroup.js" IncludeHeaders="false" YUICompress="true" YUIObfuscateJavascript="true" />
<Concatenate files="@(SecondGroup)" output="out\secondgroup.js" IncludeHeaders="false" YUICompress="true" YUIObfuscateJavascript="true" />
</Target>
</Project>
// Uses a port of the Yahoo YUI Compressor
// http://yuicompressor.codeplex.com/
using System;
using System.IO;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using Yahoo.Yui.Compressor;
using System.Resources;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Globalization;
public class Concatenate : Task {
public bool YUICompress { get; set; }
public bool YUIVerboseLogging { get; set; }
public bool YUIObfuscateJavascript { get; set; }
public bool YUIPreserveAllSemicolons { get; set; }
public bool YUIDisableOptimizations { get; set; }
public bool IncludeHeaders { get; set; }
[Required]
public string output { get; set; }
[Required]
public ITaskItem[] Files { get; set; }
public Concatenate() {
Files = new ITaskItem[0];
}
public override bool Execute() {
FileInfo _fileOut = new FileInfo(output);
string baseDir = new DirectoryInfo(".").FullName + "\\";
if (Files.Length > 0) {
string _header = "// {0} ";
_header += Environment.NewLine;
_header += "//==================================";
_header += Environment.NewLine;
_header += "{1}";
_header += Environment.NewLine;
_header += Environment.NewLine;
if (!IncludeHeaders)
_header = "{1}" + Environment.NewLine;
StringBuilder sb = new StringBuilder();
Log.LogMessage("Building ({0})", _fileOut.FullName.Replace(baseDir, ""));
foreach (ITaskItem item in Files) {
FileInfo FileIn = new FileInfo(item.ItemSpec);
string data = string.Empty;
try {
data = File.ReadAllText(FileIn.FullName);
} catch {
Log.LogMessage(" => {0} CAN NOT READ, skipping", FileIn.FullName.Replace(baseDir, ""));
}
if (data.Length > 0) {
Log.LogMessage(" => {0}", FileIn.FullName.Replace(baseDir, ""));
sb.Append(string.Format(_header, FileIn.FullName, data));
} else {
Log.LogWarning(" => {0} FILE IS EMPTY, skipping.", FileIn.FullName.Replace(baseDir, ""));
}
string nResult = sb.ToString();
if (YUICompress == true) {
nResult = JavaScriptCompressor.Compress(
nResult,
YUIVerboseLogging,
YUIObfuscateJavascript,
YUIPreserveAllSemicolons,
YUIDisableOptimizations,
-1
);
}
try {
File.WriteAllText(_fileOut.FullName, nResult);
} catch {
Log.LogWarning("Unable to write ({0}), path may not exist or file may be locked.", _fileOut.FullName);
return false;
}
}
Log.LogMessage("{0} files processed.", Files.Length);
} else {
Log.LogWarning(
"No file found to concatenate, Ignoring Request. \"{0}\" not created.",
_fileOut.Name
);
}
Log.LogMessage(Environment.NewLine);
return true;
}
}
@lot224
Copy link
Author

lot224 commented Dec 19, 2012

This can be used for any text files that you wish to concatenate, not just js files. If you have a collection of css files that you would like to concatenate, exclude the YUICompress/YUIObfuscateJavascript attributes and the task will do the rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment