Permalink
Browse files

smartsprites: SMARTSPRITES-37: support for processing individual CSS …

…files in the Ant task, logging of reading/writing CSS files, improved unit test coverage

git-svn-id: https://carrot2.svn.sourceforge.net/svnroot/carrot2/labs/smartsprites@3637 7ff1d41c-760d-0410-a7ff-a3a56f310b35
  • Loading branch information...
1 parent 9b7a6d3 commit b258a5cd071ef10ffe369403ddf78aed43efc3ca @stanislawosinski stanislawosinski committed Aug 7, 2009
View
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="var" path="CLOVER_RUNTIME"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src-test"/>
<classpathentry exported="true" kind="lib" path="lib/google-collections.jar"/>
View
@@ -6,23 +6,12 @@
</projects>
<buildSpec>
<buildCommand>
- <name>com.cenqua.clover.core.prejavabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
- <buildCommand>
- <name>com.cenqua.clover.core.postjavabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
- <nature>com.cenqua.clover.core.clovernature</nature>
</natures>
</projectDescription>
View
@@ -18,15 +18,44 @@ Please provide SmartSprite parameters in a 'smartsprites.properties' file.
</classpath>
</taskdef>
+ <!--
+ The simplest way to invoke SmartSprites is to provide a directory
+ in the rootdir parameter. SmartSprites will attempt to process all
+ files with 'css' extension from that directory.
+ -->
<smartsprites rootdir="${root.dir.path}"
documentrootdir="${document.root.dir.path}"
outputdir="${output.dir.path}"
cssfileencoding="${css.file.encoding}"
cssfilesuffix="${css.file.suffix}"
- csspropertyindent="${css.property.indent}"
loglevel="${log.level}"
spritepngdepth="${sprite.png.depth}"
spritepngie6="${sprite.png.ie6}" />
+
+ <!--
+ Alternatively, instead of rootdir, you can provide individual CSS
+ files for processing using one or more nested <fileset> elements.
+ This type of invocation gives more fine-grained control over which
+ CSS files are processed.
+
+ For a description of how processing of individual CSS files relates
+ to rootdir and outputdir, please see the documentation on:
+
+ http://smartsprites.osinski.name/
+ -->
+ <!--
+ <smartsprites documentrootdir="${document.root.dir.path}"
+ outputdir="${output.dir.path}"
+ cssfileencoding="${css.file.encoding}"
+ cssfilesuffix="${css.file.suffix}"
+ loglevel="${log.level}"
+ spritepngdepth="${sprite.png.depth}"
+ spritepngie6="${sprite.png.ie6}">
+ <fileset dir="${root.dir.path}">
+ <include name="**/*.css" />
+ </fileset>
+ </smartsprites>
+ -->
</target>
</project>
@@ -755,7 +755,6 @@
<a href="#css-file-encoding">CSS file encoding parameter</a> added
(<a href="http://issues.carrot2.org/browse/SMARTSPRITES-33">SMARTSPRITES-33</a>)
</li>
-
</ul>
<p>
@@ -211,11 +211,13 @@
</xsl:call-template>
</xsl:for-each>
- <h1>Bug Explanations</h1>
- <xsl:apply-templates select="/BugCollection/BugPattern">
- <xsl:sort select="@abbrev"/>
- <xsl:sort select="ShortDescription"/>
- </xsl:apply-templates>
+ <xsl:if test="/BugCollection/BugPattern">
+ <h1>Bug Explanations</h1>
+ <xsl:apply-templates select="/BugCollection/BugPattern">
+ <xsl:sort select="@abbrev"/>
+ <xsl:sort select="ShortDescription"/>
+ </xsl:apply-templates>
+ </xsl:if>
</body>
</html>
@@ -98,12 +98,19 @@ public void testValidateDocumentRootDirIsNotADirectory()
@Test
public void testValidateNoOutputDirAndEmptyCssFileSuffix()
{
+ checkInvalid(new SmartSpritesParameters(null, Lists.newArrayList("css/file.css"),
+ null, null, null, "", null, false, null), new Message(MessageLevel.ERROR,
+ MessageType.CSS_FILE_SUFFIX_IS_REQUIRED_IF_NO_OUTPUT_DIR));
+ }
+
+ @Test
+ public void testValidateRootDirAndCssFilesWithoutOutputDir()
+ {
checkInvalid(
- new SmartSpritesParameters(null, Lists.newArrayList("css/file.css"), null,
- null, null, "", null, false, null),
+ parameters(existingRootDirPath, Lists.newArrayList("css/file.css"), null),
new Message(
MessageLevel.ERROR,
- MessageType.CSS_FILE_SUFFIX_IS_REQUIRED_IF_NO_OUTPUT_DIR));
+ MessageType.ROOT_DIR_AND_CSS_FILES_CANNOT_BE_BOTH_SPECIFIED_UNLESS_WITH_OUTPUT_DIR));
}
@Test
@@ -87,13 +87,13 @@ public void testSimpleHorizontalSpriteImportant() throws FileNotFoundException,
IOException
{
final File testDir = testDir("simple-horizontal-sprite-important");
- buildSprites(testDir);
+ buildSprites(testDir, true);
assertThat(messages).doesNotHaveMessagesOfLevel(MessageLevel.WARN);
assertThat(processedCss()).hasSameContentAs(expectedCss());
assertThat(new File(testDir, "img/sprite.png")).exists();
org.fest.assertions.Assertions.assertThat(sprite(testDir)).hasSize(
- new Dimension(17 + 15 + 48, 47));
+ new Dimension(17 + 15 + 48 + 20, 47));
}
@Test
@@ -663,7 +663,16 @@ private File processedCss(File sourceCss)
private void buildSprites(File dir) throws IOException
{
- buildSprites(new SmartSpritesParameters(dir.getPath()));
+ buildSprites(dir, false);
+ }
+
+ private void buildSprites(File dir, boolean ie6) throws IOException
+ {
+ buildSprites(new SmartSpritesParameters(dir.getPath(), null, null, null,
+ SmartSpritesParameters.DEFAULT_LOGGING_LEVEL,
+ SmartSpritesParameters.DEFAULT_CSS_FILE_SUFFIX,
+ SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH, ie6,
+ SmartSpritesParameters.DEFAULT_CSS_FILE_ENCODING));
}
private void buildSprites(List<String> cssFiles) throws IOException
@@ -106,7 +106,7 @@
/** By default, we use full color only when necessary */
public static final PngDepth DEFAULT_SPRITE_PNG_DEPTH = PngDepth.AUTO;
- /** By default, we'll generate separate sprites for IE6 if needed */
+ /** By default, we don't generate separate sprites for IE6 */
public static final boolean DEFAULT_SPRITE_PNG_IE6 = false;
/** By default, we'll assume CSS files are UTF-8 encoded */
@@ -167,13 +167,23 @@ public boolean validate(MessageLog log)
{
boolean valid = true;
- if (StringUtils.isBlank(rootDir) && !hasCssFiles())
+ // Either root dir or css files are required
+ if (!hasRootDir() && !hasCssFiles())
{
log.error(MessageType.EITHER_ROOT_DIR_OR_CSS_FILES_IS_REQIRED);
return false;
}
- if (StringUtils.isNotBlank(this.rootDir))
+ // If there is no output dir, we can't have both root dir or css files
+ if (!hasOutputDir() && hasRootDir() && hasCssFiles())
+ {
+ log
+ .error(MessageType.ROOT_DIR_AND_CSS_FILES_CANNOT_BE_BOTH_SPECIFIED_UNLESS_WITH_OUTPUT_DIR);
+ return false;
+ }
+
+ // Check root dir if provided
+ if (hasRootDir())
{
final File rootDir = FileUtils.getCanonicalOrAbsoluteFile(this.rootDir);
if ((!rootDir.exists() || !rootDir.isDirectory()))
@@ -184,9 +194,11 @@ public boolean validate(MessageLog log)
}
}
- if (StringUtils.isNotBlank(this.outputDir))
+ // Check output dir if provided
+ if (hasOutputDir())
{
- if (StringUtils.isBlank(this.rootDir))
+ // For output dir, we need root dir
+ if (!hasRootDir())
{
log.error(MessageType.ROOT_DIR_IS_REQIRED_FOR_OUTPUT_DIR);
return false;
@@ -200,14 +212,13 @@ public boolean validate(MessageLog log)
}
}
- if (StringUtils.isBlank(this.outputDir) && StringUtils.isBlank(cssFileSuffix))
+ if (!hasOutputDir() && StringUtils.isBlank(cssFileSuffix))
{
- log
- .error(MessageType.CSS_FILE_SUFFIX_IS_REQUIRED_IF_NO_OUTPUT_DIR);
+ log.error(MessageType.CSS_FILE_SUFFIX_IS_REQUIRED_IF_NO_OUTPUT_DIR);
valid = false;
}
- if (StringUtils.isNotBlank(documentRootDir))
+ if (hasDocumentRootDir())
{
final File documentRootDir = FileUtils
.getCanonicalOrAbsoluteFile(this.documentRootDir);
@@ -243,12 +254,17 @@ private String getCssFileSuffix(String suffix)
return suffix;
}
}
-
+
public String getRootDir()
{
return rootDir;
}
+ public boolean hasRootDir()
+ {
+ return StringUtils.isNotBlank(rootDir);
+ }
+
public List<String> getCssFiles()
{
return cssFiles;
@@ -264,11 +280,21 @@ public String getOutputDir()
return outputDir;
}
+ public boolean hasOutputDir()
+ {
+ return StringUtils.isNotBlank(outputDir);
+ }
+
public String getDocumentRootDir()
{
return documentRootDir;
}
+ public boolean hasDocumentRootDir()
+ {
+ return StringUtils.isNotBlank(documentRootDir);
+ }
+
public MessageLevel getLogLevel()
{
return logLevel;
@@ -106,17 +106,6 @@ public void buildSprites() throws FileNotFoundException, IOException
{
// Take all css files from the root dir
filePaths = Lists.newArrayList();
- final File outputDir = parameters.getOutputDir() != null ? new File(
- parameters.getOutputDir()) : null;
- if (parameters.getOutputDir() != null && !outputDir.exists())
- {
- if (!outputDir.mkdirs())
- {
- messageLog.warning(Message.MessageType.CANNOT_CREATE_DIRECTORIES,
- outputDir.getPath());
- }
- }
-
final Collection<File> files = org.apache.commons.io.FileUtils.listFiles(
new File(parameters.getRootDir()), new String []
{
@@ -246,8 +235,10 @@ private void createProcessedCss(String originalCssFile,
final String processedCssFile = getProcessedCssFile(originalCssFile);
final BufferedReader originalCssReader = new BufferedReader(resourceHandler
.getResourceAsReader(originalCssFile));
+ messageLog.info(MessageType.READING_CSS, originalCssFile);
final BufferedWriter processedCssWriter = new BufferedWriter(resourceHandler
.getResourceAsWriter(processedCssFile));
+ messageLog.info(MessageType.WRITING_CSS, processedCssFile);
String originalCssLine;
int originalCssLineNumber = -1;
@@ -357,7 +348,7 @@ String getProcessedCssFile(String originalCssFile)
processedCssFile = originalCssFile + parameters.getCssFileSuffix();
}
- if (parameters.getOutputDir() != null)
+ if (parameters.hasOutputDir())
{
return FileUtils.changeRoot(processedCssFile, parameters.getRootDir(),
parameters.getOutputDir());
@@ -52,6 +52,7 @@
{
final Collection<SpriteImageOccurrence> occurrences = Lists.newArrayList();
final BufferedReader reader = new BufferedReader(resourceHandler.getResourceAsReader(cssFile));
+ messageLog.info(MessageType.READING_SPRITE_IMAGE_DIRECTIVES, cssFile);
int lineNumber = -1;
String line;
@@ -97,6 +98,8 @@
final Collection<SpriteReferenceOccurrence> directives = Lists.newArrayList();
final BufferedReader reader = new BufferedReader(resourceHandler.getResourceAsReader(cssFile));
+ messageLog.info(MessageType.READING_SPRITE_REFERENCE_DIRECTIVES, cssFile);
+
int lineNumber = -1;
String line;
@@ -214,7 +214,7 @@ String getImageFile(String cssFile, String imagePath)
final String path = resourceHandler.getResourcePath(cssFile, imagePath);
// Just handle the root directory changing
- if (!imagePath.startsWith("/") && parameters.getOutputDir() != null)
+ if (!imagePath.startsWith("/") && parameters.hasOutputDir())
{
return FileUtils.changeRoot(path, parameters.getRootDir(), parameters
.getOutputDir());
Oops, something went wrong.

0 comments on commit b258a5c

Please sign in to comment.