Skip to content

Commit

Permalink
smartsprites: SMARTSPRITES-37: support for processing individual CSS …
Browse files Browse the repository at this point in the history
…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
stanislawosinski committed Aug 7, 2009
1 parent 9b7a6d3 commit b258a5c
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 89 deletions.
1 change: 0 additions & 1 deletion .classpath
@@ -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"/>
Expand Down
11 changes: 0 additions & 11 deletions .project
Expand Up @@ -5,24 +5,13 @@
<projects>
</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>
31 changes: 30 additions & 1 deletion build-autonomous.xml
Expand Up @@ -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>

1 change: 0 additions & 1 deletion doc/website/index.html
Expand Up @@ -755,7 +755,6 @@ <h1>Version history</h1>
<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>
Expand Down
12 changes: 7 additions & 5 deletions etc/findbugs/findbugs2html.xsl
Expand Up @@ -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>
Expand Down
Expand Up @@ -97,13 +97,20 @@ 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
Expand Down
15 changes: 12 additions & 3 deletions src-test/org/carrot2/labs/smartsprites/SpriteBuilderTest.java
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
46 changes: 36 additions & 10 deletions src/org/carrot2/labs/smartsprites/SmartSpritesParameters.java
Expand Up @@ -106,7 +106,7 @@ public final class SmartSpritesParameters
/** 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 */
Expand Down Expand Up @@ -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()))
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
15 changes: 3 additions & 12 deletions src/org/carrot2/labs/smartsprites/SpriteBuilder.java
Expand Up @@ -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 []
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Expand Up @@ -52,6 +52,7 @@ Collection<SpriteImageOccurrence> collectSpriteImageOccurrences(String cssFile)
{
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;
Expand Down Expand Up @@ -97,6 +98,8 @@ Collection<SpriteReferenceOccurrence> collectSpriteReferenceOccurrences(String c
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;

Expand Down
2 changes: 1 addition & 1 deletion src/org/carrot2/labs/smartsprites/SpriteImageBuilder.java
Expand Up @@ -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());
Expand Down

0 comments on commit b258a5c

Please sign in to comment.