Skip to content

Commit

Permalink
v0.3.0 (#8)
Browse files Browse the repository at this point in the history
Added support for defining a context path prefix.
  • Loading branch information
cjstehno committed Apr 28, 2017
1 parent 6a8811d commit b602396
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
.idea/*
*.iml
*.iws
build/*
build/*
.gradle/*
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -21,7 +21,7 @@ You can apply the plugin using the `buildscript` block:
}
}
dependencies {
classpath "gradle.plugin.com.stehno.gradle:webpreview:0.2.0"
classpath "gradle.plugin.com.stehno.gradle:webpreview:0.3.0"
}
}

Expand All @@ -30,7 +30,7 @@ You can apply the plugin using the `buildscript` block:
Or the newer `plugins` block:

plugins {
id "com.stehno.gradle.webpreview" version "0.2.0"
id "com.stehno.gradle.webpreview" version "0.3.0"
}

## Usage
Expand All @@ -43,6 +43,7 @@ To configure the Web Preview plugin, the `webPreview` extension is provided:
port = 0
copyUrl = true
resourceDir = file('build/web')
contextPath = '/'
}

The `port` property is the web server port where content is to be served. The default is `0`, which will choose a random available port. This port
Expand All @@ -54,6 +55,9 @@ will fail to start without it.
The `copyUrl` property determines whether or not the server URL is copied to the local clipboard on startup. This is handy when running locally
with the random port setting.

The `contextPath` is an optional property used to define the root context path to be used as a prefix for all content. If not specified the server
root "/" will be used.

### Tasks

* `startPreview` - used to start the preview server with the configuration provided in the `webPreview` extension.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -9,7 +9,7 @@ plugins {
id 'com.stehno.gradle.site' version '0.0.3'
}

version = '0.2.0'
version = '0.3.0'
group = 'com.stehno.gradle'

sourceCompatibility = 8
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Wed Mar 15 12:55:27 CDT 2017
#Fri Apr 28 10:01:12 CDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
26 changes: 20 additions & 6 deletions src/main/groovy/com/stehno/gradle/web/PreviewServer.groovy
Expand Up @@ -17,6 +17,8 @@ package com.stehno.gradle.web

import groovy.transform.TypeChecked
import io.undertow.Undertow
import io.undertow.server.HttpHandler
import io.undertow.server.handlers.PathHandler
import io.undertow.server.handlers.resource.FileResourceManager
import io.undertow.server.handlers.resource.ResourceHandler

Expand All @@ -27,20 +29,28 @@ import io.undertow.server.handlers.resource.ResourceHandler
class PreviewServer {

private Undertow server
private String path

/**
* Starts the server instance on the specified port and resource directory, if it is not already running.
*
* @param port the server port
* @param directory the resource directory to be served
* @param contextPath the optional root context path
*/
void start(final int port, final File directory) {
void start(final int port, final File directory, final String contextPath = null) {
if (!server) {
server = Undertow.builder()
.addHttpListener(port, '0.0.0.0')
.setHandler(new ResourceHandler(new FileResourceManager(directory, 1)))
.build()
this.path = contextPath

HttpHandler handler = new ResourceHandler(new FileResourceManager(directory, 1))

if (contextPath) {
PathHandler pathHandler = new PathHandler()
pathHandler.addPrefixPath(contextPath, handler)
handler = pathHandler
}

server = Undertow.builder().addHttpListener(port, '0.0.0.0').setHandler(handler).build()
server.start()
}
}
Expand All @@ -50,7 +60,11 @@ class PreviewServer {
}

String getUrl() {
server ? "http://localhost:$port" : null
server ? "http://localhost:$port$contextPath" : null
}

String getContextPath() {
return path ? (path.startsWith('/') ? path : "/$path") : ''
}

void stop() {
Expand Down
Expand Up @@ -34,7 +34,7 @@ class StartPreviewTask extends DefaultTask {
assert extension.resourceDir, 'No resourceDir configuration was provided.'

PreviewServer server = PreviewServer.instance
server.start(extension.port, extension.resourceDir)
server.start(extension.port, extension.resourceDir, extension.contextPath)

logger.lifecycle 'Started preview server ({}) for {}', server.url, extension.resourceDir

Expand Down
Expand Up @@ -35,4 +35,9 @@ class WebPreviewExtension {
* Whether or not the server URL is copied to the local clipboard on startup.
*/
boolean copyUrl = true

/**
* Specifies the optional root context path.
*/
String contextPath
}
4 changes: 4 additions & 0 deletions src/site/index.html
Expand Up @@ -60,6 +60,7 @@ <h2>Configure It</h2>
port = 0
copyUrl = true
resourceDir = file('build/web')
contextPath = '/'
}</pre>

<p>where:</p>
Expand All @@ -74,6 +75,9 @@ <h2>Configure It</h2>
<li><code>copyUrl</code> - determines whether or not the server URL is copied to the local clipboard on startup. This is handy when
running locally with the random port setting.
</li>
<li><code>contextPath</code> - optional root context path to be used as a prefix for all content. If not specified the server root "/"
will be used.
</li>
</ul>

<h2>Use It</h2>
Expand Down
40 changes: 40 additions & 0 deletions src/test/groovy/com/stehno/gradle/web/PreviewTaskSpec.groovy
Expand Up @@ -84,6 +84,46 @@ class PreviewTaskSpec extends Specification implements UsesGradleBuild {
PreviewServer.instance.stop()
}

def 'preview server with context path'(){
setup:
Random random = new Random()

def portRange = 10101..20202
int serverPort = portRange[random.nextInt(portRange.size())]

buildFile(extension: """
webPreview {
port = $serverPort
resourceDir = file('src/site')
copyUrl = false
contextPath = 'foo'
}
""")

String content = 'This is some really cool web content!'

File siteDir = projectRoot.newFolder('src', 'site')
new File(siteDir, 'index.html').text = content

when:
BuildResult result = gradleRunner(['startPreview']).build()

then:
totalSuccess result

and:
"http://localhost:$serverPort/foo/index.html".toURL().text == content

when:
"http://localhost:$serverPort/index.html".toURL().text != content

then:
thrown(FileNotFoundException)

cleanup:
PreviewServer.instance.stop()
}

@Override
String getBuildTemplate() {
'''
Expand Down

0 comments on commit b602396

Please sign in to comment.