Skip to content

Commit

Permalink
Implement support for WebJars and LESS assets pipeline
Browse files Browse the repository at this point in the history
Signed-off-by: Ashar Fuadi <[email protected]>
  • Loading branch information
fushar committed Sep 19, 2020
1 parent b0b48a5 commit 554c8b4
Show file tree
Hide file tree
Showing 36 changed files with 1,271 additions and 43 deletions.
14 changes: 14 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ gradlePlugin {
implementationClass = "org.gradle.playframework.plugins.PlayRoutesPlugin"
}

register("play-less-plugin") {
id = "org.gradle.playframework-less"
displayName = "Play LESS Plugin"
description = "Plugin for compiling LESS stylesheets in a Play application."
implementationClass = "org.gradle.playframework.plugins.PlayLessPlugin"
}

register("play-webjars-plugin") {
id = "org.gradle.playframework-webjars"
displayName = "Play WebJars Plugin"
description = "Plugin for extracting WebJars in a Play application."
implementationClass = "org.gradle.playframework.plugins.PlayWebJarsPlugin"
}

register("play-application-plugin") {
id = "org.gradle.playframework-application"
displayName = "Play Application Plugin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class UserGuidePlugin : Plugin<Project> {
val htmlUserGuideFile = file("$outputDir/html5/index.html")
var text = htmlUserGuideFile.readText()
text = text.replace(Regex("id 'org.gradle.playframework' version '.+'"), "id 'org.gradle.playframework' version '${project.version}'")
text = text.replace(Regex("id 'org.gradle.playframework-less' version '.+'"), "id 'org.gradle.playframework-less' version '${project.version}'")
text = text.replace(Regex("id 'org.gradle.playframework-webjars' version '.+'"), "id 'org.gradle.playframework-webjars' version '${project.version}'")
htmlUserGuideFile.writeText(text)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/docs/asciidoc/13-tasks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ _Depends on_: `stageMainDist`
+
Stages the Play distribution.

`createPlayAssetsJar` — {uri-gradle-dsl-reference}/org.gradle.api.Task.html[Task]::
+
Bundles asset files into a jar file.

==== Running and testing tasks

The plugin also provides tasks for running, testing and packaging your Play application.
Expand Down
13 changes: 12 additions & 1 deletion src/docs/asciidoc/14-source-sets.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,43 @@ One type of element that describes the application are the source sets that defi
.Default Play source sets
[%header%autowidth,compact]
|===
| Source Set | Type | Directory | Filters
| Source Set | Type | Directory | Filters | Plugin extension

| java
| {uri-gradle-dsl-reference}/org.gradle.api.tasks.SourceSet.html[SourceSet]
| app
| \**/*.java
| (built-in)

| scala
| {uri-gradle-dsl-reference}/org.gradle.api.tasks.SourceSet.html[SourceSet]
| app
| \**/*.scala
| (built-in)

| routes
| link:{uri-plugin-api}/org/gradle/playframework/sourcesets/RoutesSourceSet.html[RoutesSourceSet]
| conf
| routes, *.routes
| (built-in)
| twirl
| link:{uri-plugin-api}/org/gradle/playframework/sourcesets/TwirlSourceSet.html[TwirlSourceSet]
| app
| \**/*.scala.*
| (built-in)

| javaScript
| link:{uri-plugin-api}/org/gradle/playframework/sourcesets/JavaScriptSourceSet.html[JavaScriptSourceSet]
| app/assets
| \**/*.js
| (built-in)

| less
| link:{uri-plugin-api}/org/gradle/playframework/sourcesets/LessSourceSet.html[LessSourceSet]
| app/assets
| \**/*.less
| org.gradle.playframework-less
|===

These <<adding-source-directories,source sets can be configured>>.
4 changes: 3 additions & 1 deletion src/docs/asciidoc/20-usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ include::23-package-distribution.adoc[]

include::24-multi-project.adoc[]

include::25-ide.adoc[]
include::25-ide.adoc[]

include::26-asset-pipelines.adoc[]
67 changes: 67 additions & 0 deletions src/docs/asciidoc/26-asset-pipelines.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
=== Adding asset pipelines

The Play plugin's `createPlayAssetsJar` task (on which `runPlay` and `dist` tasks depend) bundles all assets into a single jar file. This jar file is included in the distribution package to be served by the Play application.

The following asset pipelines are supported.

==== Public assets

Files in the `public/` directory will be included in the assets JAR without any processing.

==== JavaScript minification

Files in the `javaScript` source set (the default one is `app/assets` with `\**/*.js` filter), will be minified using Google Closure compiler and then included in the assets jar.

==== WebJars

WebJar libraries will be extracted and then included in the assets jar, under the `lib/` subdirectory.

To apply this pipeline, apply the `org.gradle.playframework-webjars` plugin as well:

[source,groovy]
.build.gradle
----
plugins {
id 'org.gradle.playframework' version '0.9'
id 'org.gradle.playframework-webjars' version '0.9'
}
----

To add a WebJar library, add it to the dependency list using `webJar` configuration, e.g.:

[source,groovy]
.build.gradle
----
dependencies {
webJar 'org.webjars:requirejs:2.3.6'
}
----

==== LESS compilation

Files in the `less` source set (the default one is `app/assets` with `\**/*.less` filter) will be compiled into CSS and then included in the assets jar.

To apply this pipeline, apply the `org.gradle.playframework-less` plugin as well:

[source,groovy]
.build.gradle
----
plugins {
id 'org.gradle.playframework' version '0.9'
id 'org.gradle.playframework-less' version '0.9'
}
----

The following `\@import`s in LESS files are supported:

- Partial LESS files (ones with start with underscores, such ass `_common.less`).
- WebJar extracted files (using the WebJar pipeline described above), with `lib/` subdirectory prefix.

Example:

[source,less]
.main.css
----
@import "./_common.less";
@import "lib/css-reset/reset.css";
----
1 change: 0 additions & 1 deletion src/docs/asciidoc/40-migrating-software-model.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ The following features are not available in this plugin:
* The custom configurations `playTest` and `playRun` do not exist anymore. Use the standard configurations `implementation`, `testImplementation` and `runtime` of the Java/Scala plugin instead.
* The extension does not allow for configuring a target platform. You will need to configure Play, Scala and Java version individually.
* {uri-gradle-userguide}//play_plugin.html#sec:adding_extra_source_sets[Adding new source sets] of a specific type is a built-in feature of the software model. This functionality is currently not available.
* The concept of an "asset" does not exist and therefore cannot be used to {uri-gradle-userguide}/play_plugin.html#sec:injecting_a_custom_asset_pipeline[configure a custom asset pipeline].
* Source sets cannot be added by type. You will need to add additional source directories to the existing source sets provided by the plugin.
* The CoffeeScript plugin is not available anymore.
5 changes: 5 additions & 0 deletions src/docs/samples/source-sets/groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'org.gradle.playframework' version '0.9'
id 'org.gradle.playframework-less' version '0.9'
}

repositories {
Expand Down Expand Up @@ -30,6 +31,10 @@ sourceSets {
srcDir 'additional/javascript'
exclude '**/old_*.js'
}
less {
srcDir 'additional/less'
exclude '**/old_*.less'
}
}
}
// end::add-source-directories[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import org.gradle.playframework.application.PlayApplicationPluginIntegrationTest
import org.gradle.playframework.fixtures.app.AdvancedPlayApp
import org.gradle.playframework.fixtures.app.PlayApp

import static org.gradle.playframework.plugins.PlayTwirlPlugin.TWIRL_COMPILE_TASK_NAME

class PlayBinaryAdvancedAppIntegrationTest extends PlayApplicationPluginIntegrationTest {

private static final TWIRL_COMPILE_TASK_PATH = ":$TWIRL_COMPILE_TASK_NAME".toString()

@Override
PlayApp getPlayApp() {
new AdvancedPlayApp(playVersion)
Expand All @@ -32,12 +28,10 @@ class PlayBinaryAdvancedAppIntegrationTest extends PlayApplicationPluginIntegrat

jar("build/libs/${playApp.name}-assets.jar").containsDescendants(
"public/javascripts/sample.js",
"public/javascripts/sample.min.js"
"public/javascripts/sample.min.js",
"public/stylesheets/main.css",
"public/stylesheets/extra.css",
"public/lib/css-reset/reset.css",
)
}

@Override
String[] getBuildTasks() {
return super.getBuildTasks() + TWIRL_COMPILE_TASK_PATH
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.gradle.playframework.plugins

import org.gradle.playframework.AbstractIntegrationTest

import static org.gradle.playframework.fixtures.file.FileFixtures.findFile
import static org.gradle.playframework.fixtures.Repositories.playRepositories
import static org.gradle.playframework.plugins.PlayLessPlugin.LESS_COMPILE_TASK_NAME

class PlayLessPluginIntegrationTest extends AbstractIntegrationTest {

def setup() {
buildFile << """
plugins {
id 'org.gradle.playframework'
id 'org.gradle.playframework-less'
}
${playRepositories()}
"""
}

def "can compile LESS files"() {
given:
File lessDir = temporaryFolder.newFolder('app', 'assets', 'stylesheets')
new File(lessDir, 'main.less') << lessSource()

when:
build(LESS_COMPILE_TASK_NAME)

then:
File outputDir = file('build/src/play/less')
outputDir.isDirectory()

File[] cssFiles = new File(outputDir, 'stylesheets').listFiles()
cssFiles.length == 1
findFile(cssFiles, 'main.css')
}

def "can add source directories to default source set"() {
given:
File lessDir = temporaryFolder.newFolder('app', 'assets', 'stylesheets')
new File(lessDir, 'main.less') << lessSource()

File extraLessDir = temporaryFolder.newFolder('extra', 'less', 'stylesheets')
new File(extraLessDir, 'extra.less') << lessSource()

buildFile << """
sourceSets {
main {
less {
srcDir 'extra/less'
}
}
}
"""

when:
build(LESS_COMPILE_TASK_NAME)

then:
File outputDir = file('build/src/play/less')
outputDir.isDirectory()

File[] cssFiles = new File(outputDir, 'stylesheets').listFiles()
cssFiles.length == 2
findFile(cssFiles, 'main.css')
findFile(cssFiles, 'extra.css')
}

static String lessSource() {
"""
.some-class {
float: left;
}
"""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.gradle.playframework.plugins

import org.gradle.playframework.AbstractIntegrationTest

import static org.gradle.playframework.fixtures.Repositories.playRepositories
import static org.gradle.playframework.fixtures.file.FileFixtures.findFile
import static org.gradle.playframework.plugins.PlayLessPlugin.LESS_COMPILE_TASK_NAME

class PlayLessWithWebJarsPluginIntegrationTest extends AbstractIntegrationTest {

def setup() {
buildFile << """
plugins {
id 'org.gradle.playframework'
id 'org.gradle.playframework-less'
id 'org.gradle.playframework-webjars'
}
${playRepositories()}
dependencies {
webJar 'org.webjars.bower:css-reset:2.5.1'
}
"""
}

def "can compile LESS files which import files in WebJars"() {
given:
File lessDir = temporaryFolder.newFolder('app', 'assets', 'stylesheets')
new File(lessDir, 'main.less') << lessSource()

when:
build(LESS_COMPILE_TASK_NAME)

then:
File outputDir = file('build/src/play/less')
outputDir.isDirectory()

File[] cssFiles = new File(outputDir, 'stylesheets').listFiles()
cssFiles.length == 1
findFile(cssFiles, 'main.css')
}

static String lessSource() {
"""
@import (inline) "lib/css-reset/reset.css";
.some-class {
float: left;
}
"""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.gradle.playframework.plugins

import org.gradle.playframework.AbstractIntegrationTest

import static org.gradle.playframework.fixtures.Repositories.playRepositories
import static org.gradle.playframework.fixtures.file.FileFixtures.findFile
import static org.gradle.playframework.plugins.PlayWebJarsPlugin.WEBJARS_EXTRACT_TASK_NAME

class PlayWebJarsPluginIntegrationTest extends AbstractIntegrationTest {

def setup() {
buildFile << """
plugins {
id 'org.gradle.playframework'
id 'org.gradle.playframework-webjars'
}
${playRepositories()}
dependencies {
webJar 'org.webjars:requirejs:2.3.6'
webJar 'org.webjars.npm:inherits:2.0.4'
}
"""
}

def "can extract WebJars"() {
when:
build(WEBJARS_EXTRACT_TASK_NAME)

then:
File outputDir = file('build/src/play/webJars/lib')
outputDir.isDirectory()

File[] libs = outputDir.listFiles()
libs.length == 2
findFile(libs, 'requirejs')
findFile(libs, 'inherits')
}
}
Loading

0 comments on commit 554c8b4

Please sign in to comment.