Skip to content

Commit

Permalink
Implement modules for asciidoctor.js (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysb33r committed Apr 21, 2019
1 parent 832f397 commit 2d40613
Show file tree
Hide file tree
Showing 19 changed files with 480 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.gradle.base

import groovy.transform.CompileDynamic
Expand All @@ -22,7 +37,9 @@ import org.ysb33r.grolifant.api.FileUtils

import java.nio.file.Path

import static org.asciidoctor.gradle.base.AsciidoctorUtils.*
import static org.asciidoctor.gradle.base.AsciidoctorUtils.UNDERSCORE_LED_FILES
import static org.asciidoctor.gradle.base.AsciidoctorUtils.executeDelegatingClosure
import static org.asciidoctor.gradle.base.AsciidoctorUtils.getSourceFileTree
import static org.gradle.api.tasks.PathSensitivity.RELATIVE

/** Abstract base task for Asciidoctor that can be shared between AsciidoctorJ and Asciidoctor.js.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.gradle.base

import groovy.transform.CompileStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ public interface AsciidoctorModuleDefinition {
* The latter usually implies that the specific component is not needed.
*/
String getVersion();

/** Whether the component has been allocated a version.
*
* @return {@code true} if the component has been defined
*/
boolean isDefined();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.gradle.base.internal

import groovy.transform.CompileStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.asciidoctor.gradle.base

import org.asciidoctor.gradle.testfixtures.AsciidoctorjTestVersions
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package org.asciidoctor.gradle.js.base

import groovy.transform.CompileStatic
import org.asciidoctor.gradle.base.AbstractImplementationEngineExtension
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration

import static org.ysb33r.grolifant.api.ClosureUtils.configureItem
import static org.ysb33r.grolifant.api.StringUtils.stringize

/**
Expand All @@ -35,7 +37,7 @@ abstract class AbstractAsciidoctorJSExtension extends AbstractImplementationEngi
public final static String DEFAULT_DOCBOOK_VERSION = '2.0.0'

private Object version = DEFAULT_ASCIIDOCTORJS_VERSION
private Optional<Object> docbookVersion
private final AsciidoctorJSModules modules

/** Version of AsciidoctorJS that should be used.
*
Expand All @@ -56,39 +58,33 @@ abstract class AbstractAsciidoctorJSExtension extends AbstractImplementationEngi
this.version = v
}

/** Version of the Docbook converter that should be used.
/** Additional AsciidoctorJ modules to be configured.
*
* @return Version of extension DSL or {@code null} if extensions will not be used.
* @return Module definition object.
*/
String getDocbookVersion() {
if (task) {
if (this.docbookVersion != null && this.docbookVersion.present) {
stringize(this.docbookVersion.get())
} else {
extFromProject.docbookVersion
}
} else {
this.docbookVersion?.present ? stringize(this.docbookVersion.get()) : null
}
AsciidoctorJSModules getModules() {
this.modules
}

/** Set a new Docbook converter version to use.
/** Configure modules via a closure.
*
* Implies {@link #useDocbook}, but sets a custom version rather than a default.
*
* @param v Groovy DSL version.
* @param cfg Configurating closure
*/
void setDocbookVersion(Object v) {
this.docbookVersion = Optional.of(v)
@SuppressWarnings('ConfusingMethodName')
void modules(@DelegatesTo(AsciidoctorJSModules) Closure cfg) {
configureItem(this.modules, cfg)
}

/** Enables Docbook conversion with whichever docbook version is the default.
/** Configure modules via an {@code Action}.
*
* @param cfg Configurating {@code Action}
*/
void useDocbook() {
setDocbookVersion(DEFAULT_DOCBOOK_VERSION)
@SuppressWarnings('ConfusingMethodName')
void modules(Action<AsciidoctorJSModules> cfg) {
cfg.execute(this.modules)
}


/** A configuration of packages related to asciidoctor.js
*
* @return Configuration of resolvable packages.
Expand All @@ -101,6 +97,7 @@ abstract class AbstractAsciidoctorJSExtension extends AbstractImplementationEngi
*/
protected AbstractAsciidoctorJSExtension(Project project) {
super(project)
this.modules = createModulesConfiguration()
}

/** Attach extension to a task.
Expand All @@ -109,9 +106,33 @@ abstract class AbstractAsciidoctorJSExtension extends AbstractImplementationEngi
*/
protected AbstractAsciidoctorJSExtension(Task task, final String name) {
super(task, name)
this.modules = createModulesConfiguration()
}

/** Get the Docbook version after resolving all of the relevant extensions.
*
* @return Docbook version to use. Can be {@code null} to indicate that Docbook
* is not required.
*/
protected String getFinalDocbookVersion() {
if (task) {
this.modules.docbook.version ?: extFromProject.modules.docbook.version
} else {
extFromProject.modules.docbook.version
}
}

/** Creates a new modules block.
*
* @return Modules block that can be attached to this extension.
*/
@SuppressWarnings('FactoryMethodName')
abstract protected AsciidoctorJSModules createModulesConfiguration()

private AbstractAsciidoctorJSExtension getExtFromProject() {
task ? (AbstractAsciidoctorJSExtension) projectExtension : this
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.gradle.js.base

import groovy.transform.CompileStatic
import org.asciidoctor.gradle.base.AsciidoctorModuleDefinition

/** Define versions for standard AsciidoctorJS modules.
*
* @author Schalk W. Cronjé
*
* @since 3.0
*/
@SuppressWarnings(['ConfusingMethodName', 'ClassName'])
@CompileStatic
interface AsciidoctorJSModules {

/** Configure docbook via closure.
*
* @param cfg Configurating closure
*/
void docbook(@DelegatesTo(AsciidoctorModuleDefinition) Closure cfg)

/** The Docbook module
*
* @return Acess to the Docbook module. Never {@code null}.
*/
AsciidoctorModuleDefinition getDocbook()

/** For the module that are configured in both module sets,
* compare to see if the versions are the same
*
* @param other Other module set to compare.
*
* @return {@code true} if modules of the same kind were found, but with different versions.
*/
boolean isSetVersionsDifferentTo(AsciidoctorJSModules other)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.gradle.js.base.internal

import org.asciidoctor.gradle.base.AsciidoctorModuleDefinition
import org.gradle.api.Action

import static org.ysb33r.grolifant.api.StringUtils.stringize

/** A single configurable asciidoctor.js module.
*
* @author Schalk W. Cronjé
* @since 3.0.0
*/
class AsciidoctorJSModule implements AsciidoctorModuleDefinition {

private Optional<Object> version = Optional.empty()
private final Object defaultVersion
private final Action<Object> setAction

static AsciidoctorJSModule of(final Object defaultVersion) {
new AsciidoctorJSModule(defaultVersion)
}

static AsciidoctorJSModule of(final Object defaultVersion, Closure setAction) {
new AsciidoctorJSModule(defaultVersion, setAction as Action<Object>)
}

AsciidoctorJSModule(final Object defaultVersion, Action<Object> setAction = null) {
this.defaultVersion = defaultVersion
this.setAction = setAction
}

@Override
void use() {
setVersion(this.defaultVersion)
}

@Override
void setVersion(Object o) {
this.version = Optional.of(o)
if (setAction) {
setAction.execute(o)
}
}

@Override
@SuppressWarnings('ConfusingMethodName')
void version(Object o) {
setVersion(o)
}

@Override
String getVersion() {
this.version.present ? stringize(this.version.get()) : null
}

/** Whether the component has been allocated a version.
*
* @return {@code true} if the component has been defined
*/
@Override
boolean isDefined() {
version.present
}
}
Loading

0 comments on commit 2d40613

Please sign in to comment.