Skip to content

Instantly share code, notes, and snippets.

@steven-terrana
Last active March 11, 2021 15:47
Show Gist options
  • Save steven-terrana/4b5beadcb3bff86c5179dc9b92a85825 to your computer and use it in GitHub Desktop.
Save steven-terrana/4b5beadcb3bff86c5179dc9b92a85825 to your computer and use it in GitHub Desktop.
package git
import org.boozallen.plugins.jte.util.TemplateLogger
abstract class GitServerProvider{
abstract void doThingOne()
void doThingTwo(){
echo "doing thing 2 from base class"
}
void echo(String message){
TemplateLogger.createDuringRun().printWarning(message)
}
}
package github
import example.GitServerProvider
class gitHub extends GitServerProvider{
@Override
void doThingOne(){
echo "GitHub doing thing one"
}
}
package gitlab
import example.GitServerProvider
class GitLab extends GitServerProvider{
@Override
void doThingOne(){
echo "GitLab doing thing one"
}
}
import example.GitServerProvider
ExtensionPoint.lookup(GitServerProvider).each{ s ->
def provider = s.newInstance()
provider.doThingOne()
provider.doThingTwo()
}
libraries{
sdp
git
gitlab
github
}
package sdp
import hudson.FilePath
import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner
import java.lang.SecurityManager
import org.boozallen.plugins.jte.util.TemplateLogger
import org.codehaus.groovy.reflection.ReflectionUtils
class ExtensionPoint implements Serializable{
@NonCPS
static List<Class> lookup(Class extension){
CpsThread thread = CpsThread.current()
FlowExecutionOwner flowOwner = thread.getExecution().getOwner()
FilePath buildRoot = new FilePath(flowOwner.getRootDir())
FilePath src = buildRoot.child("jte/src")
List<Class> classes = []
recurseChildren(src){ file ->
String fullPath = file.getRemote()
if(fullPath.endsWith(".groovy")){
String clazz = ((fullPath - ".groovy") - "${src.getRemote()}/").split("/").join(".")
Class c = Class.forName(clazz)
if(c in extension && c != extension){
classes << c
}
}
}
return classes
}
@NonCPS
static void recurseChildren(FilePath file, Closure action){
if(file.exists()){
file.list().each{ child ->
if(child.isDirectory()){
recurseChildren(child, action)
} else{
action(child)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment