/** * Short-hand for the top-level invocation. */ static String step2Groovy(Step s) throws UnsupportedOperationException { return object2Groovy(new StringBuilder(), s, false).toString(); }
/** * Writes out a snippet that instantiates {@link UninstantiatedDescribable} * * @param nested * true if this object is written as a nested expression (in which case we always produce parenthesis for readability */ private static StringBuilder ud2groovy(StringBuilder b, UninstantiatedDescribable ud, boolean blockArgument, boolean nested) { if (!canUseSymbol(ud)) { // if there's no symbol, we need to write this as [$class:...] return map2groovy(b, ud.toShallowMap()); } return functionCall(b, ud, blockArgument, nested); }
final boolean needParenthesis = (blockArgument ^ args.isEmpty()) || isSingleMap(args) || isSingleList(args) || nested; object2Groovy(b, args.values().iterator().next(), true); } else { mapWithoutBracket2groovy(b,args);
return list2groovy(b, (List<?>) o); return map2groovy(b, (Map) o); return ud2groovy(b,(UninstantiatedDescribable)o, false, nestedExp); if (!canUseMetaStep(nested)) failSimplification = true; combined.setModel(nested.getModel()); return ud2groovy(b, combined, blockArgument, nestedExp); return functionCall(b, uninst, blockArgument, nestedExp);
return HttpResponses.text("Cannot find a step corresponding to " + o.getClass().getName()); String groovy = step2Groovy(step); if (descriptor instanceof StepDescriptor && ((StepDescriptor) descriptor).isAdvanced()) { String warning = Messages.Snippetizer_this_step_should_not_normally_be_used_in();
@Issue("JENKINS-29922") @Test public void getQuasiDescriptors() throws Exception { String quasiDescriptors = new Snippetizer().getQuasiDescriptors(false).toString(); assertThat(quasiDescriptors, containsString("circle=Circle")); assertThat(quasiDescriptors, containsString("polygon=Polygon")); assertThat(quasiDescriptors, containsString("CO=CarbonMonoxide")); assertThat("State.moderate currently disqualifies this metastep", quasiDescriptors, not(containsString("california=California"))); }
if (v instanceof UninstantiatedDescribable) { return !canUseSymbol((UninstantiatedDescribable)v);
assertEquals(expected, Snippetizer.step2Groovy(step)); CompilerConfiguration cc = new CompilerConfiguration(); cc.setScriptBaseClass(DelegatingScript.class.getName());
/** * Can this symbol name be used to produce a short hand? */ private static boolean canUseMetaStep(UninstantiatedDescribable ud) { return canUseSymbol(ud) && StepDescriptor.metaStepsOf(ud.getSymbol()).size()==1; }
/** * Publicly accessible version of {@link #object2Groovy(StringBuilder, Object, boolean)} that translates an object into * the equivalent Pipeline Groovy string. * * @param o The object to translate. * @return A string translation of the object. */ public static String object2Groovy(Object o) throws UnsupportedOperationException { return object2Groovy(new StringBuilder(), o, false).toString(); }
private static StringBuilder list2groovy(StringBuilder b, List<?> o) { b.append('['); boolean first = true; for (Object elt : o) { if (first) { first = false; } else { b.append(", "); } object2Groovy(b, elt, true); } return b.append(']'); }
@Override @Nonnull public String toGroovy(@Nonnull TriggersDirective directive) { StringBuilder result = new StringBuilder("triggers {\n"); for (Trigger trigger : directive.triggers) { result.append(Snippetizer.object2Groovy(UninstantiatedDescribable.from(trigger))); result.append("\n"); } result.append("}\n"); return result.toString(); } }
@Override @Nonnull public String toGroovy(@Nonnull ParametersDirective directive) { StringBuilder result = new StringBuilder("parameters {\n"); for (ParameterDefinition param : directive.parameters) { result.append(Snippetizer.object2Groovy(UninstantiatedDescribable.from(param))); result.append("\n"); } result.append("}\n"); return result.toString(); } }
private static void mapWithoutBracket2groovy(StringBuilder b, Map<?, ?> map) { boolean first = true; for (Entry<?, ?> entry : map.entrySet()) { if (first) { first = false; } else { b.append(", "); } Object key = entry.getKey(); if (key instanceof String && SourceVersion.isName((String) key)) { b.append(key); } else { object2Groovy(b, key, true); } b.append(": "); object2Groovy(b, entry.getValue(), true); } }
@Override @Nonnull public String toGroovy(@Nonnull OptionsDirective directive) { StringBuilder result = new StringBuilder("options {\n"); for (Describable d : directive.options) { if (d instanceof Step) { String origGroovy = Snippetizer.object2Groovy(d); // Need to remove the block bit since we're cheating. result.append(origGroovy.substring(0, origGroovy.length() - " {\n // some block\n}".length())); result.append("\n"); } else { result.append(Snippetizer.object2Groovy(UninstantiatedDescribable.from(d))); result.append("\n"); } } result.append("}\n"); return result.toString(); } }
@Override @Nonnull public String toGroovy(@Nonnull LibrariesDirective directive) { StringBuilder result = new StringBuilder("libraries {\n"); if (!directive.getLibs().isEmpty()) { for (NameAndVersion l : directive.getLibs()) { result.append("lib(").append(Snippetizer.object2Groovy(l.getLibString())).append(")\n"); } } else { result.append("// No libraries specified\n"); } result.append("}\n"); return result.toString(); }
@Override @Nonnull public String toGroovy(@Nonnull ToolsDirective directive) { StringBuilder result = new StringBuilder("tools {\n"); if (!directive.getTools().isEmpty()) { for (ToolInstallation tool : directive.getToolInstallations()) { result.append(DirectiveGenerator.getSymbolForDescriptor(tool.getDescriptor())).append(" "); result.append(Snippetizer.object2Groovy(tool.getName())).append("\n"); } } else { result.append("// No valid tools specified\n"); } result.append("}\n"); return result.toString(); } }
@Restricted(NoExternalUse.class) public static String mapToClosure(Map<String,?> args) { StringBuilder result = new StringBuilder("{\n"); for (Map.Entry<String,?> arg : args.entrySet()) { if (!(arg.getValue() instanceof String && arg.getValue().equals(""))) { result.append(arg.getKey()).append(" "); if (arg.getValue() instanceof Map) { result.append(mapToClosure((Map<String, ?>) arg.getValue())); } else if (arg.getValue() instanceof UninstantiatedDescribable) { result.append(mapToClosure(((UninstantiatedDescribable) arg.getValue()).getArguments())); } else if (arg.getValue() != null) { result.append(Snippetizer.object2Groovy(arg.getValue())).append("\n"); } } } result.append("}\n"); return result.toString(); }
@Nonnull private String conditionalToGroovy(@Nonnull DeclarativeStageConditional<?> conditional) { DeclarativeStageConditionalDescriptor descriptor = conditional.getDescriptor(); if (descriptor.getAllowedChildrenCount() == 0) { return Snippetizer.object2Groovy(UninstantiatedDescribable.from(conditional)) + "\n"; } else { StringBuilder result = new StringBuilder(); result.append(descriptor.getName()).append(" {\n"); for (DeclarativeStageConditional c : conditional.getChildren()) { result.append(conditionalToGroovy(c)); } result.append("}\n"); return result.toString(); } } }
if (directive.getMessage() != null) { StringBuilder result = new StringBuilder("input {\n"); result.append("message ").append(Snippetizer.object2Groovy(directive.getMessage())).append("\n"); if (!StringUtils.isEmpty(directive.getId())) { result.append("id ").append(Snippetizer.object2Groovy(directive.getId())).append("\n"); result.append("ok ").append(Snippetizer.object2Groovy(directive.getOk())).append("\n"); result.append("submitter ").append(Snippetizer.object2Groovy(directive.getSubmitter())).append("\n"); result.append("submitterParameter ").append(Snippetizer.object2Groovy(directive.getSubmitterParameter())).append("\n"); result.append(Snippetizer.object2Groovy(UninstantiatedDescribable.from(p))).append("\n");