diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2010-01-18 20:10:18 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2010-01-18 20:10:18 -0800 |
commit | 47ba4e9c49be9479b7c3d30613cab982ecda042d (patch) | |
tree | 07a2a747a70e13d80aedd41294a0a3e94f60b192 /src/DotNetOpenAuth.BuildTasks/JsPack.cs | |
parent | 1f79fcb88004d86372b7392f51b914af826f1b9d (diff) | |
parent | cdb850c6cf90381e6db75365272b7d65ef5fe359 (diff) | |
download | DotNetOpenAuth-47ba4e9c49be9479b7c3d30613cab982ecda042d.zip DotNetOpenAuth-47ba4e9c49be9479b7c3d30613cab982ecda042d.tar.gz DotNetOpenAuth-47ba4e9c49be9479b7c3d30613cab982ecda042d.tar.bz2 |
Merge branch 'master' into simpleauth
Conflicts:
src/DotNetOpenAuth.vsmdi
src/DotNetOpenAuth/DotNetOpenAuth.csproj
Diffstat (limited to 'src/DotNetOpenAuth.BuildTasks/JsPack.cs')
-rw-r--r-- | src/DotNetOpenAuth.BuildTasks/JsPack.cs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.BuildTasks/JsPack.cs b/src/DotNetOpenAuth.BuildTasks/JsPack.cs new file mode 100644 index 0000000..fa8c7f0 --- /dev/null +++ b/src/DotNetOpenAuth.BuildTasks/JsPack.cs @@ -0,0 +1,75 @@ +//----------------------------------------------------------------------- +// <copyright file="JsPack.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.BuildTasks { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using Microsoft.Build.Framework; + using Microsoft.Build.Utilities; + + /// <summary> + /// Compresses .js files. + /// </summary> + public class JsPack : Task { + /// <summary> + /// The Javascript packer to use. + /// </summary> + private Dean.Edwards.ECMAScriptPacker packer = new Dean.Edwards.ECMAScriptPacker(); + + /// <summary> + /// Gets or sets the set of javascript files to compress. + /// </summary> + /// <value>The inputs.</value> + [Required] + public ITaskItem[] Inputs { get; set; } + + /// <summary> + /// Gets or sets the paths where the packed javascript files should be saved. + /// </summary> + /// <value>The outputs.</value> + [Required] + public ITaskItem[] Outputs { get; set; } + + /// <summary> + /// Executes this instance. + /// </summary> + /// <returns>A value indicating whether the packing was successful.</returns> + public override bool Execute() { + if (this.Inputs.Length != this.Outputs.Length) { + Log.LogError("{0} inputs and {1} outputs given.", this.Inputs.Length, this.Outputs.Length); + return false; + } + + for (int i = 0; i < this.Inputs.Length; i++) { + if (!File.Exists(this.Outputs[i].ItemSpec) || File.GetLastWriteTime(this.Outputs[i].ItemSpec) < File.GetLastWriteTime(this.Inputs[i].ItemSpec)) { + Log.LogMessage(MessageImportance.Normal, TaskStrings.PackingJsFile, this.Inputs[i].ItemSpec, this.Outputs[i].ItemSpec); + string input = File.ReadAllText(this.Inputs[i].ItemSpec); + string output = this.packer.Pack(input); + if (!Directory.Exists(Path.GetDirectoryName(this.Outputs[i].ItemSpec))) { + Directory.CreateDirectory(Path.GetDirectoryName(this.Outputs[i].ItemSpec)); + } + + // Minification removes all comments, including copyright notices + // that must remain. So if there's metadata on this item with + // a copyright notice on it, stick it on the front of the file. + string copyright = this.Inputs[i].GetMetadata("Copyright"); + if (!string.IsNullOrEmpty(copyright)) { + output = "/*" + copyright + "*/" + output; + } + + File.WriteAllText(this.Outputs[i].ItemSpec, output, Encoding.UTF8); + } else { + Log.LogMessage(MessageImportance.Low, TaskStrings.SkipPackingJsFile, this.Inputs[i].ItemSpec); + } + } + + return true; + } + } +} |