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();
}
}
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");
}
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);
}
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"));
}
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");
}
Aggregations