Skip to content

Commit

Permalink
added the ability to use import in sass files (#1048)
Browse files Browse the repository at this point in the history
* added the ability to use import in sass files

* fixed one test

* improved the file detection a bit so that css files etc are no longer
handled
  • Loading branch information
sterlp authored and alexo committed Jun 27, 2017
1 parent 741584c commit 481e1a7
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 3 deletions.
Binary file added .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2017. All rights reserved.
*/
package ro.isdc.wro.model.resource.locator;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.isdc.wro.model.resource.locator.support.LocatorProvider;

/**
* Adjusted loader class for saas resources "scss" files. As so imports can now the achieved using
* the following configuration in the <b>wro.properties</b>:
*
* <pre>
* managerFactoryClassName=ro.isdc.wro.manager.factory.ConfigurableWroManagerFactory
* preProcessors=lessCssImport
* postProcessors=rubySassCss
* uriLocators=webjar,saasUri,uri,classpath
* </pre>
*
* @author Paul Sterl
* @created Created on Jun 22 2017
*/
public class SaasUriLocator implements UriLocator {
private static final Logger LOG = LoggerFactory.getLogger(SaasUriLocator.class);
/**
* Alias used to register this locator with {@link LocatorProvider}.
*/
public static final String ALIAS = "saasUri";

/**
* {@inheritDoc}
*/
public boolean accept(final String url) {
if (url == null) return false;
final String extension = FilenameUtils.getExtension(url);
// scss file have either no extension or scss
// maybe check for the "_"?
if ("".equals(extension) || "scss".equals(extension)) {
boolean result = getScssFile(url) != null;
if (!result) {
LOG.debug("Possible scss file not found {}", url);
}
return result;
} else {
return false;
}
}

File getScssFile(String url) {
if (url == null) return null;
File result;
// remove any "file:" at start
if (url.startsWith("file:")) url = url.replace("file:", "");

result = new File(url);
if (result.isFile()) return result;
result = new File(url + ".scss");
if (result.isFile()) return result;

// if we don't have a scss file end the ending isn't a / it is most likely an import
if (!url.endsWith(".scss") && !url.endsWith("/")) {
final int lastSlash = url.lastIndexOf('/') + 1;
String cleanUrl = url.substring(0, lastSlash);
cleanUrl = cleanUrl + "_" + url.substring(lastSlash, url.length()) + ".scss";
result = new File(cleanUrl);
} else {
result = new File(url);
}

if (result.isFile()) return result;
else return null;
}

/**
* {@inheritDoc}
*/
public InputStream locate(final String uri) throws IOException {
Validate.notNull(uri, "URI cannot be NULL!");
LOG.debug("loading scss file: {}", uri);
return new FileInputStream(getScssFile(uri));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import ro.isdc.wro.model.resource.locator.ClasspathUriLocator;
import ro.isdc.wro.model.resource.locator.SaasUriLocator;
import ro.isdc.wro.model.resource.locator.ServletContextUriLocator;
import ro.isdc.wro.model.resource.locator.ServletContextUriLocator.LocatorStrategy;
import ro.isdc.wro.model.resource.locator.UriLocator;
Expand Down Expand Up @@ -33,6 +34,7 @@ public Map<String, UriLocator> provideLocators() {
map.put(ServletContextUriLocator.ALIAS_SERVLET_CONTEXT_ONLY,
new ServletContextUriLocator().setLocatorStrategy(LocatorStrategy.SERVLET_CONTEXT_ONLY));
map.put(UrlUriLocator.ALIAS, new UrlUriLocator());
map.put(SaasUriLocator.ALIAS, new SaasUriLocator());
return map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ro.isdc.wro.model.resource.locator;

import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;

import org.junit.Test;

public class TestSaasUriLocator {

@Test
public void testSaasImport() throws Exception {
final String base = new File("./src/test/resources/sass/").getAbsolutePath() + "/";
SaasUriLocator saasUriLocator = new SaasUriLocator();

assertTrue(saasUriLocator.accept(base + "_sassimport"));
assertTrue(saasUriLocator.accept(base + "style.scss"));
assertFalse(saasUriLocator.accept(base + "style.css"));

assertNotNull(saasUriLocator.locate(base + "_sassimport"));
assertNotNull(saasUriLocator.locate(base + "style"));

assertNotNull(saasUriLocator.locate("file:" + base + "style"));
assertEquals(new File(base + "style.scss"), saasUriLocator.getScssFile("file:/" + base + "style"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void shouldHaveEmptyConfiguredStrategiesByDefault() {

@Test
public void shouldHaveNonEmptyListOfAvailableStrategies() {
assertEquals(7, victim.getAvailableStrategies().size());
assertEquals(8, victim.getAvailableStrategies().size());
}

@Test(expected = WroRuntimeException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setUp() {

@Test
public void shouldHaveSeveralDefaultLocators() {
assertEquals(7, victim.getUriLocators().size());
assertEquals(8, victim.getUriLocators().size());
}

@Test
Expand Down
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void setUp() {

@Test
public void shouldHaveSeveralDefaultLocators() {
assertEquals(8, victim.getUriLocators().size());
assertEquals(9, victim.getUriLocators().size());
}

@Test
Expand Down

0 comments on commit 481e1a7

Please sign in to comment.