Hana 9e216da9ef go.mod: add go.mod and move pygments to third_party
After go1.16, go will use module mode by default,
even when the repository is checked out under GOPATH
or in a one-off directory. Add go.mod, go.sum to keep
this repo buildable without opting out of the module
mode.

> go mod init github.com/mmcgrana/gobyexample
> go mod tidy
> go mod vendor

In module mode, the 'vendor' directory is special
and its contents will be actively maintained by the
go command. pygments aren't the dependency the go will
know about, so it will delete the contents from vendor
directory. Move it to `third_party` directory now.

And, vendor the blackfriday package.

Note: the tutorial contents are not affected by the
change in go1.16 because all the examples in this
tutorial ask users to run the go command with the
explicit list of files to be compiled (e.g.
`go run hello-world.go` or `go build command-line-arguments.go`).
When the source list is provided, the go command does
not have to compute the build list and whether it's
running in GOPATH mode or module mode becomes irrelevant.
2021-02-15 16:45:26 -05:00

97 lines
2.5 KiB
Groovy

// This source code comes from http://www.odelia-technologies.com/node/200
package com.odelia.groovy.simpleworkflow
class SimpleWorkflowEngine {
def workflowMap = [:]
def context = [:]
def beforeActivityName = 'beforeActivity'
def afterActivityName = 'afterActivity'
SimpleWorkflowEngine(workflow, context = [:]) {
this.context = context
parseWorkflow(workflow)
}
def parseWorkflow(workflow) {
workflowMap = new WorkflowParser().parse(workflow)
}
def getActivityValue(activity) {
assert activity instanceof String
if (!workflowMap[activity])
throw new RuntimeException("$activity activity doesn't exist")
workflowMap[activity]
}
def execute(activity, pause) {
if (workflowMap[beforeActivityName]) {
getActivityValue(beforeActivityName)(context, activity)
}
def activityValue = getActivityValue(activity)
// Determine the next activity to execute
def nextActivity
switch (activityValue) {
case String: nextActivity = activityValue; break
case Closure: nextActivity = activityValue(context); break
case Class: nextActivity = activityValue.newInstance()(context)
}
if (workflowMap[afterActivityName]) {
getActivityValue(afterActivityName)(context, activity, nextActivity)
}
if (!pause && nextActivity)
call(nextActivity)
else
nextActivity
}
def call(activity) {
execute(activity, false)
}
def nextActivity(activity) {
execute(activity, true)
}
static void main(String[] args) {
if (args.size() != 2) {
println 'Usage: com.odelia.groovy.simpleworkflow.SimpleWorkflowEngine <dsl_filename> <activity_name>'
return
}
SimpleWorkflowEngine.newInstance(new File(args[0]))(args[1])
}
}
private class WorkflowParser {
def map = [:]
def methodMissing(String name, args) {
map[name] = args[0]
}
def parse(Closure wf) {
wf.delegate = this
wf.resolveStrategy = Closure.DELEGATE_FIRST
wf()
map
}
def workflow = { it ->
it.delegate = this
it.resolveStrategy = Closure.DELEGATE_FIRST
it()
}
def parse(File workflowDef) {
def binding = new Binding([workflow: workflow])
def shell = new GroovyShell(binding)
shell.evaluate(workflowDef)
map
}
}