Skip to content

Commit 59a04d9

Browse files
authored
Merge pull request #348 from isc-pbarton/baseline-export
Added baseline utility
2 parents c6cbad3 + 6ac4638 commit 59a04d9

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Pre-release support for IPM v0.9.0+
1313
- Items mapped from database other than namespace's default routine database are now ignored by default when exporting or adding files
1414
- New setting to configure whether mapped items should be should be treated as read-only
15+
- Command-line utility to do a baseline export of items in a namespace
1516

1617
## [2.3.1] - 2024-04-30
1718

cls/SourceControl/Git/API.cls

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,12 @@ ClassMethod Unlock()
5656
quit ##class(SourceControl.Git.Utils).Locked(0)
5757
}
5858

59+
/// Run in terminal to baseline a namespace by adding all items to source control.
60+
/// - pCommitMessage: if defined, all changes in namespace context will be committed.
61+
/// - pPushToRemote: if defined, will run a git push to the specified remote
62+
ClassMethod BaselineExport(pCommitMessage = "", pPushToRemote = "") As %Status
63+
{
64+
quit ##class(SourceControl.Git.Utils).BaselineExport(pCommitMessage, pPushToRemote)
5965
}
6066

67+
}

cls/SourceControl/Git/Utils.cls

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ ClassMethod AddToServerSideSourceControl(InternalName As %String) As %Status
513513

514514
ClassMethod AddToSourceControl(InternalName As %String) As %Status
515515
{
516+
do ##class(SourceControl.Git.PackageManagerContext).ForInternalName(InternalName)
516517
set settings = ##class(SourceControl.Git.Settings).%New()
517518
#dim i as %Integer
518519
#dim ec as %Status = $$$OK
@@ -2062,5 +2063,47 @@ ClassMethod ResetSourceControlClass()
20622063
do ##class(%Studio.SourceControl.Interface).SourceControlClassSet("")
20632064
}
20642065

2066+
ClassMethod BaselineExport(pCommitMessage = "", pPushToRemote = "") As %Status
2067+
{
2068+
set sc = $$$OK
2069+
try {
2070+
write !, "Exporting items..."
2071+
set rs = ##class(%Library.RoutineMgr).StudioOpenDialogFunc(
2072+
"*.mac,*.int,*.inc,*.cls,*.csp" // Spec
2073+
, , ,0 // SystemFiles
2074+
,1 // Flat
2075+
,0 // NotStudio
2076+
,0 // ShowGenerated
2077+
, , ,0 // Mapped
2078+
)
2079+
throw:rs.%SQLCODE<0 ##class(%Exception.SQL).CreateFromSQLCODE(rs.%SQLCODE,rs.%Message)
2080+
while rs.%Next(.sc) {
2081+
$$$ThrowOnError(sc)
2082+
set internalName = rs.Name
2083+
// exclude items in a non-default IPM package
2084+
set context = ##class(SourceControl.Git.PackageManagerContext).ForInternalName(internalName)
2085+
continue:($isobject(context.Package) && 'context.IsInDefaultPackage)
2086+
$$$ThrowOnError(..AddToSourceControl(internalName))
2087+
}
2088+
if pCommitMessage '= "" {
2089+
// switch to default context
2090+
do ##class(SourceControl.Git.PackageManagerContext).ForInternalName("")
2091+
do ..RunGitWithArgs(.errStream, .outStream, "add", "--all")
2092+
do ..PrintStreams(errStream, outStream)
2093+
set username = ..GitUserName()
2094+
set email = ..GitUserEmail()
2095+
set author = username_" <"_email_">"
2096+
do ..RunGitWithArgs(.errStream, .outStream, "commit", "--author", author, "-m", pCommitMessage)
2097+
do ..PrintStreams(errStream, outStream)
2098+
$$$ThrowOnError(##class(SourceControl.Git.Change).RefreshUncommitted(,,,1))
2099+
if (pPushToRemote '= "") {
2100+
$$$ThrowOnError(..Push(pPushToRemote))
2101+
}
2102+
}
2103+
} catch err {
2104+
set sc = err.AsStatus()
2105+
}
2106+
return sc
20652107
}
20662108

2109+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Class UnitTest.SourceControl.Git.BaselineExport Extends %UnitTest.TestCase
2+
{
3+
4+
Method TestBaselineExport()
5+
{
6+
// create a mac routine
7+
if '##class(%Routine).Exists("test.mac") {
8+
set r = ##class(%Routine).%New("test.mac")
9+
do r.WriteLine(" write 22,!")
10+
do r.Save()
11+
do r.Compile()
12+
}
13+
// create an inc routine
14+
if '##class(%Routine).Exists("test.inc") {
15+
set r = ##class(%Routine).%New("test.inc")
16+
do r.WriteLine(" ; test include routine")
17+
do r.Save()
18+
do r.Compile()
19+
}
20+
// create a class
21+
if '##class(%Dictionary.ClassDefinition).%OpenId("TestPkg.Class") {
22+
set class = ##class(%Dictionary.ClassDefinition).%New()
23+
set class.Name = "TestPkg.Class"
24+
$$$ThrowOnError(class.%Save())
25+
do $system.OBJ.Compile("TestPkg.Class")
26+
}
27+
do $$$AssertNotTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.mac"))
28+
do $$$AssertNotTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.inc"))
29+
do $$$AssertNotTrue(##class(SourceControl.Git.Utils).IsInSourceControl("TestPkg.Class.cls"))
30+
do $$$AssertStatusOK(##class(SourceControl.Git.API).BaselineExport())
31+
do $$$AssertTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.mac"))
32+
do $$$AssertTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.inc"))
33+
do $$$AssertTrue(##class(SourceControl.Git.Utils).IsInSourceControl("TestPkg.Class.cls"))
34+
}
35+
36+
Property InitialExtension As %String [ InitialExpression = {##class(%Studio.SourceControl.Interface).SourceControlClassGet()} ];
37+
38+
Property SourceControlGlobal [ MultiDimensional ];
39+
40+
Method %OnNew(initvalue) As %Status
41+
{
42+
Merge ..SourceControlGlobal = ^SYS("SourceControl")
43+
Kill ^SYS("SourceControl")
44+
Set settings = ##class(SourceControl.Git.Settings).%New()
45+
Set settings.namespaceTemp = ##class(%Library.File).TempFilename()_"dir"
46+
Set settings.Mappings("MAC","*")="rtn/"
47+
Set settings.Mappings("CLS","*")="cls/"
48+
Do settings.%Save()
49+
Do ##class(%Studio.SourceControl.Interface).SourceControlClassSet("SourceControl.Git.Extension")
50+
Quit ##super(initvalue)
51+
}
52+
53+
Method %OnClose() As %Status [ Private, ServerOnly = 1 ]
54+
{
55+
Do ##class(%Studio.SourceControl.Interface).SourceControlClassSet(..InitialExtension)
56+
Kill ^SYS("SourceControl")
57+
Merge ^SYS("SourceControl") = ..SourceControlGlobal
58+
Quit $$$OK
59+
}
60+
61+
}

0 commit comments

Comments
 (0)