Skip to content

Commit

Permalink
Cleans up some groovydoc formatter code
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrooks12 committed Feb 9, 2019
1 parent fe475d1 commit 004697a
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 153 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.copperleaf.groovydoc.json

import org.apache.tools.ant.BuildException
import org.codehaus.groovy.tools.groovydoc.FileOutputTool
import java.nio.file.Path
import java.util.Properties
Expand All @@ -11,37 +10,30 @@ class GroovydocJsonFormatter(
) {

var scope = "public"
var processScripts = true

private var extensions = listOf("java", "groovy", "gv", "gvy", "gsh")

private fun getRelativeSourceFiles(): List<String> {
return dirs
.map { dir ->
dir.toFile()
.walkTopDown()
.filter { it.exists() && it.isFile }
.filter { extensions.contains(it.extension) }
.map {
val basePath = dir.toFile()
val sourceFilePath = it
val relativePath = sourceFilePath.relativeTo(basePath)
relativePath.invariantSeparatorsPath
}
.walkTopDown() // recursively find files in the source directory
.filter { it.exists() && it.isFile } // only get files, not directories
.filter { extensions.contains(it.extension) } // only use Groovy or Java files
.map { it.relativeTo(dir.toFile()).invariantSeparatorsPath } // get the file's path relative to the source directory
.toList()
}
.flatten()
.distinct()
}

@Throws(BuildException::class)
fun execute() {
val properties = Properties().apply {
setProperty("publicScope", (scope == "public").toString())
setProperty("protectedScope", (scope == "protected").toString())
setProperty("packageScope", (scope == "package").toString())
setProperty("privateScope", (scope == "private").toString())
setProperty("processScripts", processScripts.toString())
setProperty("processScripts", "true")
setProperty("includeMainForScripts", "false")
setProperty("charset", FILE_ENCODING)
setProperty("fileEncoding", FILE_ENCODING)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.copperleaf.groovydoc.json

import com.caseyjbrooks.clog.Clog
import com.copperleaf.groovydoc.json.formatter.toClassDoc
import com.copperleaf.groovydoc.json.formatter.toPackageDoc
import org.codehaus.groovy.groovydoc.GroovyClassDoc
import org.codehaus.groovy.groovydoc.GroovyPackageDoc
import org.codehaus.groovy.groovydoc.GroovyRootDoc
import org.codehaus.groovy.tools.groovydoc.ClasspathResourceManager
import org.codehaus.groovy.tools.groovydoc.GroovyDocTool
import org.codehaus.groovy.tools.groovydoc.GroovyRootDocBuilder
import org.codehaus.groovy.tools.groovydoc.OutputTool
import java.io.File
import java.util.Properties

class GroovyDocJsonTool(
Expand All @@ -20,6 +26,9 @@ class GroovyDocJsonTool(
properties
) {

// Get RootDoc model from Groovy APIs
//----------------------------------------------------------------------------------------------------------------------

private val rootDocBuilder = GroovyRootDocBuilder(this, sourcepaths, emptyList(), properties)

override fun getRootDoc(): GroovyRootDoc {
Expand All @@ -31,10 +40,58 @@ class GroovyDocJsonTool(
}

override fun renderToOutput(output: OutputTool, destdir: String) {
val writer = GroovyDocJsonWriter(output, properties)
val rootDoc = rootDocBuilder.rootDoc
writer.writePackages(rootDoc, destdir)
writer.writeClasses(rootDoc, destdir)
writePackages(output, rootDoc, destdir)
writeClasses(output, rootDoc, destdir)
}

// Write classes and packages to JSON files
//----------------------------------------------------------------------------------------------------------------------

private fun writeClasses(output: OutputTool, rootDoc: GroovyRootDoc, destdir: String) {
rootDoc
.classes()
.filter {
when {
(it.isPublic) -> true
(it.isProtected && (properties.getProperty("protectedScope")?.toBoolean() ?: false)) -> true
(it.isPackagePrivate && (properties.getProperty("packageScope")?.toBoolean() ?: false)) -> true
(it.isPrivate && (properties.getProperty("privateScope")?.toBoolean() ?: false)) -> true
else -> false
}
}
.forEach { writeClassToOutput(output, it, destdir) }
}

private fun writeClassToOutput(output: OutputTool, classDoc: GroovyClassDoc, destdir: String) {
val destFolder = destdir
val destFileName = "$destFolder/${classDoc.fullPathName}.json"
Clog.d("Generating class file $destFileName")
output.makeOutputArea(destFolder)
output.writeToOutput(
destFileName,
classDoc.toClassDoc().toJson(),
FILE_ENCODING
)
}

private fun writePackages(output: OutputTool, rootDoc: GroovyRootDoc, destdir: String) {
rootDoc
.specifiedPackages()
.filter { !File(it.name()).isAbsolute }
.forEach { writePackageToOutput(output, it, destdir) }
}

private fun writePackageToOutput(output: OutputTool, packageDoc: GroovyPackageDoc, destdir: String) {
val destFolder = "$destdir/${packageDoc.name()}"
val destFileName = "$destFolder/index.json"
output.makeOutputArea(destFolder)
Clog.d("Generating package file $destFileName")
output.writeToOutput(
destFileName,
packageDoc.toPackageDoc().toJson(),
FILE_ENCODING
)
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.copperleaf.groovydoc.json.formatter

import com.copperleaf.groovydoc.json.models.GroovydocClassDoc
import org.codehaus.groovy.groovydoc.GroovyClassDoc

fun GroovyClassDoc.toClassDoc(): GroovydocClassDoc {
return GroovydocClassDoc(
this,
this.name()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.copperleaf.groovydoc.json.formatter

import com.copperleaf.groovydoc.json.models.GroovydocPackageDoc
import org.codehaus.groovy.groovydoc.GroovyPackageDoc

fun GroovyPackageDoc.toPackageDoc(): GroovydocPackageDoc {
return GroovydocPackageDoc(
this,
this.name()
)
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package com.copperleaf.groovydoc.json

import com.caseyjbrooks.clog.Clog
import com.eden.orchid.api.cli.Cli
import com.eden.orchid.api.options.annotations.Option
import groovy.lang.GroovySystem
import java.io.File
import java.nio.file.Path

const val FILE_ENCODING = "UTF-8"

fun main(vararg args: String) {
val formatter = GroovydocFormatter()
formatter.init(args.toList().toTypedArray())
try {
formatter.runGroovydoc()
}
catch(e: Exception) {
e.printStackTrace()
}
Clog.tag("Groovy version").i("{}", GroovySystem.getVersion())

Clog.getInstance().addTagToBlacklist("FlagsParser")
val mainArgs = Cli.parseArgsInto(MainArgs(), args)

GroovydocJsonFormatter(mainArgs.srcPaths, mainArgs.outputPath).execute()
}

class MainArgs {

@Option
lateinit var src: String

@Option
lateinit var cacheDir: String

@Option
lateinit var output: String

Expand All @@ -34,10 +34,6 @@ class MainArgs {
srcDirs.map { File(it).toPath() }
}

val cachePath: Path by lazy {
File(cacheDir).toPath()
}

val outputPath: Path by lazy {
File(output).toPath()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class GroovydocInvokerImpl(
"com.copperleaf.groovydoc.json.MainKt", // Groovydoc Formatter main class
"--src", sourceDirs.map { it.toFile().absolutePath }.joinToString(separator = File.pathSeparator), // the sources to process
"--output", destinationDir.toFile().absolutePath, // where Orchid will find them later
"--cacheDir", cacheDir.toFile().absolutePath, // where Orchid will find them later
*args.toTypedArray() // allow additional arbitrary args
)

Expand Down

0 comments on commit 004697a

Please sign in to comment.