Skip to content

Commit b5faf92

Browse files
committed
Hooks infrastructure
1 parent 234b1af commit b5faf92

File tree

7 files changed

+212
-40
lines changed

7 files changed

+212
-40
lines changed

isc/git/GitLab.cls

+20-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Class isc.git.GitLab
1+
Class isc.git.GitLab Extends isc.util.LogUtils
22
{
33

44
ClassMethod getDir() [ CodeMode = expression ]
@@ -18,11 +18,13 @@ ClassMethod load()
1818
{
1919
try {
2020
set dir = ..getDir()
21+
do ..executeHooks(.hooks, "before")
22+
2123
do ..log("Importing dir " _ dir)
2224
do $system.OBJ.ImportDir(dir, ..getExtWildcard(), "c", .errors, 1)
2325
throw:$get(errors,0)'=0 ##class(%Exception.General).%New("Load error")
2426

25-
do ..init()
27+
do ..executeHooks(.hooks, "after")
2628

2729
$$$TOE(sc, ##class(isc.git.Settings).setSetting("commit", ..getCommit()))
2830

@@ -46,6 +48,8 @@ ClassMethod loadDiff()
4648
halt
4749
} else {
4850
set dir = ..getDir()
51+
do ..executeHooks(.hooks, "before")
52+
4953
set newCommit = ..getCommit()
5054
do ..log("Importing dir " _ dir)
5155
do ..log($$$FormatText("Loading diff between %1 and %2", oldCommit, newCommit))
@@ -69,13 +73,13 @@ ClassMethod loadDiff()
6973
// To-Do delete
7074
set deleteCode = ##class(isc.git.Settings).getSetting("delete")
7175
if (($ll(deleted)>0) && (deleteCode '="")) {
72-
do $classmethod($p(deleteCode, ":"), $p(deleteCode, ":", 2))
76+
do $classmethod($p(deleteCode, ":"), $p(deleteCode, ":", 2), deleted)
7377
}
7478

7579
throw:$$$ISERR(sc) ##class(%Exception.StatusException).CreateFromStatus(sc)
7680
throw:$get(errors,0)'=0 ##class(%Exception.General).%New("Load error")
7781

78-
do ..init()
82+
do ..executeHooks(.hooks, "after")
7983

8084
$$$TOE(sc, ##class(isc.git.Settings).setSetting("commit", ..getCommit()))
8185

@@ -88,14 +92,20 @@ ClassMethod loadDiff()
8892
}
8993
}
9094

91-
ClassMethod init()
95+
ClassMethod executeHooks(Output hooks As %String, method As %String(VALUELIST=",before,after,rollback"))
9296
{
93-
set init = ##class(isc.git.Settings).getSetting("init")
94-
if (init'="") {
95-
do ..log("Running init code: " _ init)
96-
do $classmethod($p(init, ":"), $p(init, ":", 2))
97+
set hooksDir = ##class(isc.git.Settings).getSetting("hooks")
98+
if (hooksDir'="") {
99+
do ..log("Running init hooks: " _ method)
100+
101+
if method = "before" {
102+
set dir = ..getDir()
103+
$$$TOE(sc, ##class(isc.git.hook.Manager).execute(dir _ hooksDir, .hooks, "before"))
104+
} elseif method = "after" {
105+
$$$TOE(sc, ##class(isc.git.hook.Manager).execute(, .hooks, "after"))
106+
}
97107
} else {
98-
do ..log("No init code")
108+
do ..log("No hooks")
99109
}
100110
}
101111

@@ -190,33 +200,5 @@ If you are not redirected automatically, follow this <a href='!!!'>link to tests
190200
</html>
191201
}
192202

193-
ClassMethod logVar(var = "", name As %String = "") As %String
194-
{
195-
do ..log("Variable " _ name)
196-
zw var
197-
/*if $isObject(var) {
198-
zw var
199-
} elseif $listValid(var) {
200-
write $lts(var, ", ")
201-
} else {
202-
write var
203-
}*/
204-
}
205-
206-
ClassMethod logException(ex As %Exception.AbstractException)
207-
{
208-
do ..logStatus(ex.AsStatus())
209-
}
210-
211-
ClassMethod logStatus(sc As %Status)
212-
{
213-
do ..log($System.Status.GetErrorText(sc))
214-
}
215-
216-
ClassMethod log(msg As %String)
217-
{
218-
write !, $$$FormatText("[%1] %2", $zdatetime($ztimestamp, 3, 1, 3), msg), !
219-
}
220-
221203
}
222204

isc/git/Settings.cls

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ ClassMethod setSetting(name As %String = "", value As %String = "") As %Status
3131
set:'$listValid(value) sc = $$$ERROR($$$GeneralError, "Extension list should be in $lb format.")
3232
} elseif name = "commit" {
3333
// TO-DO commit validation.
34-
} elseif name = "init" {
35-
set:$length(value, ":")'=2 sc = $$$ERROR($$$GeneralError, "Init should be in a format: 'class:method'")
3634
} elseif name = "delete" {
3735
set:$length(value, ":")'=2 sc = $$$ERROR($$$GeneralError, "Delete should be in a format: 'class:method'")
3836
} elseif name = "url" {
3937
// TO-DO url validation.
4038
// "http://127.0.0.1:57772"
39+
} elseif name = "hooks" {
40+
// Path relative from the repo root to hooks
4141
} else {
4242
set sc = $$$ERROR($$$GeneralError, $$$FormatText("Setting '%1' does not exist", name))
4343
}

isc/git/hook/Abstract.cls

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/// Base hook class.
2+
/// You should not subclass directly.
3+
/// Extend Global or Local hook classes
4+
Class isc.git.hook.Abstract
5+
{
6+
7+
/// Code executed before main code load/compile.
8+
/// Do not modify.
9+
ClassMethod before() As %Status
10+
{
11+
try {
12+
set sc = ..onBefore()
13+
} catch ex {
14+
set sc = ex.AsStatus()
15+
}
16+
quit sc
17+
}
18+
19+
/// Code executed before main code load/compile.
20+
/// Overwrite this method.
21+
ClassMethod onBefore() As %Status
22+
{
23+
quit $$$OK
24+
}
25+
26+
/// Code executed after main code load/compile.
27+
/// Do not modify.
28+
ClassMethod after() As %Status
29+
{
30+
try {
31+
set sc = ..onAfter()
32+
} catch ex {
33+
set sc = ex.AsStatus()
34+
}
35+
quit sc
36+
}
37+
38+
/// Code executed before main code load/compile.
39+
/// Overwrite this method.
40+
ClassMethod onAfter() As %Status
41+
{
42+
quit $$$OK
43+
}
44+
45+
/// Code executed during rollback.
46+
/// Do not modify.
47+
ClassMethod rollback() As %Status
48+
{
49+
try {
50+
set sc = ..onRollback()
51+
} catch ex {
52+
set sc = ex.AsStatus()
53+
}
54+
quit sc
55+
}
56+
57+
/// Code executed during rollback.
58+
/// Overwrite this method.
59+
ClassMethod onRollback() As %Status
60+
{
61+
quit $$$OK
62+
}
63+
64+
}
65+

isc/git/hook/Global.cls

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// Subclass this class to create a hook
2+
/// that would be executed each time the CI is ran.
3+
Class isc.git.hook.Global Extends isc.git.hook.Abstract
4+
{
5+
6+
}
7+

isc/git/hook/Local.cls

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Class isc.git.hook.Local Extends isc.git.hook.Abstract
2+
{
3+
4+
/// Code executed after main code load/compile.
5+
/// Do not modify.
6+
ClassMethod after() As %Status
7+
{
8+
try {
9+
set sc = ..onAfter()
10+
do ##class(isc.git.hook.Log).add($classname())
11+
} catch ex {
12+
set sc = ex.AsStatus()
13+
}
14+
quit sc
15+
}
16+
17+
}
18+

isc/git/hook/Manager.cls

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// Manages hooks
2+
/// Class to store local hooks execution
3+
Class isc.git.hook.Manager Extends isc.util.LogUtils
4+
{
5+
6+
Parameter GLVN = "^isc.git.Hooks";
7+
8+
ClassMethod add(class As %Dictionary.CacheClassname)
9+
{
10+
set @..#GLVN@(class) = ""
11+
}
12+
13+
ClassMethod isDone(class As %Dictionary.CacheClassname) As %Boolean [ CodeMode = expression ]
14+
{
15+
$data(@..#GLVN@(class))
16+
}
17+
18+
/// do ##class(isc.git.hook.Manager).execute()
19+
ClassMethod execute(directory As %String = "", ByRef hooks As %String = "", method As %String(VALUELIST=",before,after,rollback")) As %Status
20+
{
21+
try {
22+
#dim sc As %Status = $$$OK
23+
do:directory'="" ..load(directory, .loadedHooks)
24+
merge hooks = loadedHooks
25+
26+
do ..log("Executing hooks, method: " _ method)
27+
28+
/// Execute global hooks
29+
do ..execute(hooks, method, "isc.git.hook.Global")
30+
31+
/// Execute local hooks
32+
do ..execute(hooks, method, "isc.git.hook.Local")
33+
} catch ex {
34+
set sc = ex.AsStatus()
35+
}
36+
quit sc
37+
}
38+
39+
ClassMethod executeInternal(ByRef hooks As %String = "", method As %String(VALUELIST=",before,after"), type As %String(VALUELIST=",isc.git.hook.Global,isc.git.hook.Local"))
40+
{
41+
/// Execute hooks
42+
set key = ""
43+
for {
44+
set key=$order(hooks(key))
45+
quit:key=""
46+
continue:$p(key, ".", *)'="cls"
47+
set class = $p(key, ".", 1, *-1)
48+
continue:'$classmethod(class, "%IsA", type)
49+
continue:class=type
50+
continue:((type="isc.git.hook.Local") && (..isDone(class)))
51+
52+
$$$TOE(sc, $classmethod(class, type))
53+
do:((type="isc.git.hook.Local") && (type="after")) ..add(class)
54+
}
55+
}
56+
57+
/// do ##class(isc.git.hook.Manager).load(,.h)
58+
ClassMethod load(directory As %String, Output hooks As %String)
59+
{
60+
do ..log("Importing hooks dir " _ directory)
61+
set hooks = ""
62+
do $system.OBJ.ImportDir(directory, ##class(isc.git.GitLab).getExtWildcard(), "cukb", .errors, 1, .hooks)
63+
throw:$get(errors,0)'=0 ##class(%Exception.General).%New("Hooks load error")
64+
}
65+
66+
}
67+

isc/util/LogUtils.cls

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Class isc.util.LogUtils
2+
{
3+
4+
ClassMethod logVar(var = "", name As %String = "") As %String
5+
{
6+
do ..log("Variable " _ name)
7+
zw var
8+
/*if $isObject(var) {
9+
zw var
10+
} elseif $listValid(var) {
11+
write $lts(var, ", ")
12+
} else {
13+
write var
14+
}*/
15+
}
16+
17+
ClassMethod logException(ex As %Exception.AbstractException)
18+
{
19+
do ..logStatus(ex.AsStatus())
20+
}
21+
22+
ClassMethod logStatus(sc As %Status)
23+
{
24+
do ..log($System.Status.GetErrorText(sc))
25+
}
26+
27+
ClassMethod log(msg As %String)
28+
{
29+
write !, $$$FormatText("[%1] %2", $zdatetime($ztimestamp, 3, 1, 3), msg), !
30+
}
31+
32+
}
33+

0 commit comments

Comments
 (0)