My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;
using System.Xml;
using System.Globalization;
using NAnt.Core.Tasks;
using Yahoo.Yui.Compressor;

namespace DevFuel.NAnt.Tasks
{
[TaskName("yuicompressor")]
public class YuiCompressorTask : Task
{
#region Private Fields

private DirectoryInfo m_ToDir;
private bool m_Flatten;
private FileSet m_InputFileSet = new FileSet();

#endregion

#region Public Properties

[TaskAttribute("todir", Required = true)]
public virtual DirectoryInfo ToDirectory
{
get { return m_ToDir; }
set { m_ToDir = value; }
}

[TaskAttribute("flatten")]
[BooleanValidator()]
public virtual bool Flatten
{
get { return m_Flatten; }
set { m_Flatten = value; }
}

[BuildElement("fileset", Required = true)]
public virtual FileSet InputFileSet
{
get { return m_InputFileSet; }
set { m_InputFileSet = value; }
}

#endregion

protected override void InitializeTask(XmlNode taskNode)
{
if (m_ToDir == null)
throw new BuildException(
string.Format(
CultureInfo.InvariantCulture,
"The 'todir' attribute must be set to specify the output directory of the minified JS/CSS files."),
this.Location);

if (!m_ToDir.Exists)
m_ToDir.Create();

if (m_InputFileSet == null)
throw new BuildException(
string.Format(
CultureInfo.InvariantCulture,
"The <fileset> element must be used to specify the JS/CSS files to Compress."),
Location);
}

protected override void ExecuteTask()
{
if (m_InputFileSet.BaseDirectory == null)
m_InputFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);

Log(Level.Info, "Compressing {0} JavaScript/CSS file(s) to '{1}'.", m_InputFileSet.FileNames.Count, m_ToDir.FullName);

foreach (string srcPath in m_InputFileSet.FileNames)
{
FileInfo srcFile = new FileInfo(srcPath);

if (srcFile.Exists)
{
string destPath = GetDestPath(m_InputFileSet.BaseDirectory, srcFile);

DirectoryInfo destDir = new DirectoryInfo(Path.GetDirectoryName(destPath));

if (!destDir.Exists)
destDir.Create();

Log(Level.Verbose, "Compressing '{0}' to '{1}'.", srcPath, destPath);

string fileText = File.ReadAllText(srcPath);
if (Path.GetExtension(srcPath).ToLower() == ".css")
{
File.WriteAllText(destPath, CssCompressor.Compress(fileText));
}
else if (Path.GetExtension(srcPath).ToLower() == ".js")
{
File.WriteAllText(destPath, JavaScriptCompressor.Compress(fileText, Verbose));
}
else
{
throw new BuildException(
string.Format(
CultureInfo.InvariantCulture,
"Expected a .css or .js extension. Got \"{0}\"",
Path.GetFileName(srcPath)),
Location);
}
}
else
{
throw new BuildException(
string.Format(
CultureInfo.InvariantCulture,
"Could not find file '{0}' to Compress.",
srcFile.FullName),
Location);
}
}
}

private string GetDestPath(DirectoryInfo srcBase, FileInfo srcFile)
{
string destPath = string.Empty;

if (m_Flatten)
{
destPath = Path.Combine(m_ToDir.FullName, srcFile.Name);
}
else
{
if (srcFile.FullName.IndexOf(srcBase.FullName, 0) != -1)
destPath = srcFile.FullName.Substring(srcBase.FullName.Length);
else
destPath = srcFile.Name;

if (destPath[0] == Path.DirectorySeparatorChar)
destPath = destPath.Substring(1);

destPath = Path.Combine(m_ToDir.FullName, destPath);
}

return destPath;
}
}
}
Show details Hide details

Change log

r33 by e...@devfuel.com on Jan 11, 2009   Diff
Added NAnt Tasks and Installer
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 5058 bytes, 149 lines
Hosted by Google Code