From 2178e68896fdc6911f89eefd9f3ba2435fc87057 Mon Sep 17 00:00:00 2001 From: Daniel Spilker Date: Sun, 8 Jan 2017 01:15:58 +0100 Subject: [PATCH] show more available method signatures in embedded API viewer --- docs/Home.md | 2 ++ .../plugin/EmbeddedApiDocGenerator.groovy | 32 ++++++++++++++++++- .../src/test/resources/expected-dsl.json | 4 +++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/docs/Home.md b/docs/Home.md index fdc8a279b..3f64a0ce0 100644 --- a/docs/Home.md +++ b/docs/Home.md @@ -29,6 +29,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins * 1.57 (unreleased) * Allow `DashboardPortletContext` to be extended ([#981](https://github.com/jenkinsci/job-dsl-plugin/pull/981)) + * Show more available method signatures in embedded API viewer + ([#982](https://github.com/jenkinsci/job-dsl-plugin/pull/982)) * 1.56 (January 06 2016) * Fixed support for [Config File Provider Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Config+File+Provider+Plugin) diff --git a/job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/EmbeddedApiDocGenerator.groovy b/job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/EmbeddedApiDocGenerator.groovy index 0657c3268..207eb847b 100644 --- a/job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/EmbeddedApiDocGenerator.groovy +++ b/job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/EmbeddedApiDocGenerator.groovy @@ -91,13 +91,43 @@ class EmbeddedApiDocGenerator { } knownMethods.addAll(extensions*.name) + methods.each { JSONObject method -> + if (!hasOptionalClosureSignature(method)) { + knownMethods.remove(method.getString('name')) + } + } + Map symbols = findDescribableModels(extensibleContextClass, knownMethods) symbols.sort().each { String symbol, DescribableModel model -> - methods << generateMethod(symbol, model) + JSONObject method = methods.find { it.getString('name') == symbol } as JSONObject + if (method) { + method.getJSONArray('signatures').add(generateSignature(model)) + } else { + methods << generateMethod(symbol, model) + } } } } + private static boolean hasOptionalClosureSignature(JSONObject method) { + method.getJSONArray('signatures').any { JSONObject signature -> + isOptionalClosureSignature(signature) + } + } + + private static boolean isOptionalClosureSignature(JSONObject signature) { + if (!signature.has('parameters')) { + return true + } + JSONArray parameters = signature.getJSONArray('parameters') + if (parameters.size() > 1) { + return false + } else if (parameters.empty) { + return true + } + parameters[0].getString('type') == 'Closure' + } + /** * Generates an extension method for an {@link ContextExtensionPoint} of a built-in context. */ diff --git a/job-dsl-plugin/src/test/resources/expected-dsl.json b/job-dsl-plugin/src/test/resources/expected-dsl.json index d96ca1876..d59ff8356 100644 --- a/job-dsl-plugin/src/test/resources/expected-dsl.json +++ b/job-dsl-plugin/src/test/resources/expected-dsl.json @@ -121,6 +121,10 @@ } ], "extension": true + }, + { + "parameters": [], + "generated": true } ] },