Search in sources :

Example 6 with LESSSkinFileResourceReference

use of org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference in project xwiki-platform by xwiki.

the class CachedLESSCompiler method compute.

@Override
public String compute(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity, boolean useLESS, String skin) throws LESSCompilerException {
    StringWriter source = new StringWriter();
    try {
        semaphore.acquire();
        if (lessResourceReference instanceof LESSSkinFileResourceReference || includeSkinStyle) {
            if (includeSkinStyle) {
                // Add the import line to the LESS resource.
                // We import this file to be able to use variables and mix-ins defined in it.
                // But we don't want it in the output.
                source.write(String.format("@import (reference) \"%s\";%s", MAIN_SKIN_STYLE_FILENAME, System.lineSeparator()));
            }
            // Get the content of the LESS resource
            source.write(lessResourceReference.getContent(skin));
        }
        // Parse the LESS content with Velocity
        String lessCode = source.toString();
        if (useVelocity) {
            lessCode = executeVelocity(lessCode, skin);
        }
        // Compile the LESS code
        if (useLESS) {
            return less4JCompiler.compile(lessCode, skin, lessConfiguration.isGenerateInlineSourceMaps());
        }
        // Otherwise return the raw LESS code
        return lessCode;
    } catch (Less4jException | InterruptedException e) {
        throw new LESSCompilerException(String.format("Failed to compile the resource [%s] with LESS.", lessResourceReference), e);
    } finally {
        semaphore.release();
    }
}
Also used : StringWriter(java.io.StringWriter) Less4jException(com.github.sommeri.less4j.Less4jException) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)

Example 7 with LESSSkinFileResourceReference

use of org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference in project xwiki-platform by xwiki.

the class DefaultLESSResourcesCacheTest method clearFromLESSResource.

@Test
public void clearFromLESSResource() throws Exception {
    // Mocks
    LESSSkinFileResourceReference file1 = createLESSSkinFileResourceReference("file1");
    LESSSkinFileResourceReference file2 = createLESSSkinFileResourceReference("file2");
    when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin1")), eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k1");
    when(cacheKeyFactory.getCacheKey(eq(file2), eq(new FSSkinReference("skin1")), eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k3");
    when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin2")), eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k4");
    // Add the first one twice
    mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"), new NamedColorThemeReference("colorTheme1"), "css1");
    mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"), new NamedColorThemeReference("colorTheme1"), "css1");
    // Others
    mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file2"), new FSSkinReference("skin1"), new NamedColorThemeReference("colorTheme1"), "css");
    mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin2"), new NamedColorThemeReference("colorTheme1"), "css3");
    // Test
    mocker.getComponentUnderTest().clearFromLESSResource(createLESSSkinFileResourceReference("file1"));
    // Verify
    verify(cache, times(1)).remove("k1");
    verify(cache).remove("k4");
    verify(cache, never()).remove("k3");
}
Also used : NamedColorThemeReference(org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference) FSSkinReference(org.xwiki.lesscss.internal.skin.FSSkinReference) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference) Test(org.junit.Test)

Example 8 with LESSSkinFileResourceReference

use of org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference in project xwiki-platform by xwiki.

the class DefaultLESSResourcesCacheTest method get.

@Test
public void get() throws Exception {
    // Mock
    when(cache.get("12_lessResource_4_skin_10_colorTheme")).thenReturn("Expected output");
    // Test
    String result = mocker.getComponentUnderTest().get(new LESSSkinFileResourceReference("lessResource", null, null), new FSSkinReference("skin"), new NamedColorThemeReference("colorTheme"));
    // Verify
    assertEquals("Expected output", result);
}
Also used : NamedColorThemeReference(org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference) FSSkinReference(org.xwiki.lesscss.internal.skin.FSSkinReference) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference) Test(org.junit.Test)

Example 9 with LESSSkinFileResourceReference

use of org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference in project xwiki-platform by xwiki.

the class CachedLESSColorThemeConverterTest method compute.

@Test
public void compute() throws Exception {
    StringWriter lessSource = new StringWriter();
    IOUtils.copy(getClass().getResourceAsStream("/styleWithColorTheme.less"), lessSource);
    // To have a better test, we use Less4j to generate the CSS that contains the color theme mapping
    LessCompiler less4jCompiler = new DefaultLessCompiler();
    LessCompiler.Configuration options = new LessCompiler.Configuration();
    options.setCompressing(true);
    LessCompiler.CompilationResult lessResult = less4jCompiler.compile(lessSource.toString(), options);
    when(lessCompiler.compile(any(LESSResourceReference.class), eq(false), eq(false), eq("skin"), eq(false))).thenReturn(lessResult.getCss());
    // So now we can test the converter on the less4j output
    Map<String, String> results = mocker.getComponentUnderTest().compute(new LESSSkinFileResourceReference("file", null, null), false, false, true, "skin");
    assertEquals("#E8E8E8", results.get("borderColor"));
    assertEquals("#3e444c", results.get("highlightColor"));
}
Also used : StringWriter(java.io.StringWriter) DefaultLessCompiler(com.github.sommeri.less4j.core.DefaultLessCompiler) LessCompiler(com.github.sommeri.less4j.LessCompiler) DefaultLessCompiler(com.github.sommeri.less4j.core.DefaultLessCompiler) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference) Test(org.junit.Test)

Example 10 with LESSSkinFileResourceReference

use of org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference in project xwiki-platform by xwiki.

the class DefaultLESSColorThemeConverterTest method setUp.

@Before
public void setUp() throws Exception {
    cache = mocker.getInstance(ColorThemeCache.class);
    currentColorThemeGetter = mocker.getInstance(CurrentColorThemeGetter.class);
    skinReferenceFactory = mocker.getInstance(SkinReferenceFactory.class);
    colorThemeReferenceFactory = mocker.getInstance(ColorThemeReferenceFactory.class);
    lessResourceReferenceFactory = mocker.getInstance(LESSResourceReferenceFactory.class);
    xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    xcontext = mock(XWikiContext.class);
    when(xcontextProvider.get()).thenReturn(xcontext);
    xwiki = mock(XWiki.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    when(xwiki.getSkin(xcontext)).thenReturn("skin");
    when(currentColorThemeGetter.getCurrentColorTheme(true, "default")).thenReturn("colorTheme");
    when(skinReferenceFactory.createReference("skin")).thenReturn(new FSSkinReference("skin"));
    when(colorThemeReferenceFactory.createReference("colorTheme")).thenReturn(new NamedColorThemeReference("colorTheme"));
    when(cache.getMutex(eq(new LESSSkinFileResourceReference("file", null, null)), eq(new FSSkinReference("skin")), eq(new NamedColorThemeReference("colorTheme")))).thenReturn("mutex");
}
Also used : CurrentColorThemeGetter(org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter) LESSResourceReferenceFactory(org.xwiki.lesscss.resources.LESSResourceReferenceFactory) ColorThemeReferenceFactory(org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory) NamedColorThemeReference(org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) FSSkinReference(org.xwiki.lesscss.internal.skin.FSSkinReference) ColorThemeCache(org.xwiki.lesscss.internal.cache.ColorThemeCache) SkinReferenceFactory(org.xwiki.lesscss.internal.skin.SkinReferenceFactory) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference) Before(org.junit.Before)

Aggregations

LESSSkinFileResourceReference (org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)10 NamedColorThemeReference (org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference)8 FSSkinReference (org.xwiki.lesscss.internal.skin.FSSkinReference)8 Test (org.junit.Test)7 StringWriter (java.io.StringWriter)2 Before (org.junit.Before)2 Less4jException (com.github.sommeri.less4j.Less4jException)1 LessCompiler (com.github.sommeri.less4j.LessCompiler)1 DefaultLessCompiler (com.github.sommeri.less4j.core.DefaultLessCompiler)1 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 Cache (org.xwiki.cache.Cache)1 CacheFactory (org.xwiki.cache.CacheFactory)1 CacheManager (org.xwiki.cache.CacheManager)1 CacheConfiguration (org.xwiki.cache.config.CacheConfiguration)1 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)1 ColorThemeCache (org.xwiki.lesscss.internal.cache.ColorThemeCache)1 ColorTheme (org.xwiki.lesscss.internal.colortheme.ColorTheme)1 ColorThemeReferenceFactory (org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory)1 CurrentColorThemeGetter (org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter)1