diff --git a/.gitignore b/.gitignore index 4cc3624..aa91bcd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* *.iml *.iws -build/* \ No newline at end of file +build/* +.gradle/* diff --git a/README.md b/README.md index 7baec41..3ca3f09 100644 --- a/README.md +++ b/README.md @@ -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" } } @@ -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 @@ -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 @@ -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. diff --git a/build.gradle b/build.gradle index 52eaad7..406fba2 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 2438e09..358b171 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1bf8e24..a17a534 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/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 diff --git a/src/main/groovy/com/stehno/gradle/web/PreviewServer.groovy b/src/main/groovy/com/stehno/gradle/web/PreviewServer.groovy index 2ce3a84..42b2e79 100644 --- a/src/main/groovy/com/stehno/gradle/web/PreviewServer.groovy +++ b/src/main/groovy/com/stehno/gradle/web/PreviewServer.groovy @@ -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 @@ -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() } } @@ -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() { diff --git a/src/main/groovy/com/stehno/gradle/web/StartPreviewTask.groovy b/src/main/groovy/com/stehno/gradle/web/StartPreviewTask.groovy index f93f913..8d705f5 100644 --- a/src/main/groovy/com/stehno/gradle/web/StartPreviewTask.groovy +++ b/src/main/groovy/com/stehno/gradle/web/StartPreviewTask.groovy @@ -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 diff --git a/src/main/groovy/com/stehno/gradle/web/WebPreviewExtension.groovy b/src/main/groovy/com/stehno/gradle/web/WebPreviewExtension.groovy index 4d3f87e..ac2e085 100644 --- a/src/main/groovy/com/stehno/gradle/web/WebPreviewExtension.groovy +++ b/src/main/groovy/com/stehno/gradle/web/WebPreviewExtension.groovy @@ -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 } diff --git a/src/site/index.html b/src/site/index.html index 0865663..4b97e92 100644 --- a/src/site/index.html +++ b/src/site/index.html @@ -60,6 +60,7 @@

Configure It

port = 0 copyUrl = true resourceDir = file('build/web') + contextPath = '/' }

where:

@@ -74,6 +75,9 @@

Configure It

  • copyUrl - 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.
  • +
  • contextPath - optional root context path to be used as a prefix for all content. If not specified the server root "/" + will be used. +
  • Use It

    diff --git a/src/test/groovy/com/stehno/gradle/web/PreviewTaskSpec.groovy b/src/test/groovy/com/stehno/gradle/web/PreviewTaskSpec.groovy index ccac582..a6a8cbd 100644 --- a/src/test/groovy/com/stehno/gradle/web/PreviewTaskSpec.groovy +++ b/src/test/groovy/com/stehno/gradle/web/PreviewTaskSpec.groovy @@ -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() { '''