Skip to content

Added baseline utility #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Pre-release support for IPM v0.9.0+
- Items mapped from database other than namespace's default routine database are now ignored by default when exporting or adding files
- New setting to configure whether mapped items should be should be treated as read-only
- Command-line utility to do a baseline export of items in a namespace

## [2.3.1] - 2024-04-30

Expand Down
7 changes: 7 additions & 0 deletions cls/SourceControl/Git/API.cls
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,12 @@ ClassMethod Unlock()
quit ##class(SourceControl.Git.Utils).Locked(0)
}

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

}
43 changes: 43 additions & 0 deletions cls/SourceControl/Git/Utils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ ClassMethod AddToServerSideSourceControl(InternalName As %String) As %Status

ClassMethod AddToSourceControl(InternalName As %String) As %Status
{
do ##class(SourceControl.Git.PackageManagerContext).ForInternalName(InternalName)
set settings = ##class(SourceControl.Git.Settings).%New()
#dim i as %Integer
#dim ec as %Status = $$$OK
Expand Down Expand Up @@ -2062,5 +2063,47 @@ ClassMethod ResetSourceControlClass()
do ##class(%Studio.SourceControl.Interface).SourceControlClassSet("")
}

ClassMethod BaselineExport(pCommitMessage = "", pPushToRemote = "") As %Status
{
set sc = $$$OK
try {
write !, "Exporting items..."
set rs = ##class(%Library.RoutineMgr).StudioOpenDialogFunc(
"*.mac,*.int,*.inc,*.cls,*.csp" // Spec
, , ,0 // SystemFiles
,1 // Flat
,0 // NotStudio
,0 // ShowGenerated
, , ,0 // Mapped
)
throw:rs.%SQLCODE<0 ##class(%Exception.SQL).CreateFromSQLCODE(rs.%SQLCODE,rs.%Message)
while rs.%Next(.sc) {
$$$ThrowOnError(sc)
set internalName = rs.Name
// exclude items in a non-default IPM package
set context = ##class(SourceControl.Git.PackageManagerContext).ForInternalName(internalName)
continue:($isobject(context.Package) && 'context.IsInDefaultPackage)
$$$ThrowOnError(..AddToSourceControl(internalName))
}
if pCommitMessage '= "" {
// switch to default context
do ##class(SourceControl.Git.PackageManagerContext).ForInternalName("")
do ..RunGitWithArgs(.errStream, .outStream, "add", "--all")
do ..PrintStreams(errStream, outStream)
set username = ..GitUserName()
set email = ..GitUserEmail()
set author = username_" <"_email_">"
do ..RunGitWithArgs(.errStream, .outStream, "commit", "--author", author, "-m", pCommitMessage)
do ..PrintStreams(errStream, outStream)
$$$ThrowOnError(##class(SourceControl.Git.Change).RefreshUncommitted(,,,1))
if (pPushToRemote '= "") {
$$$ThrowOnError(..Push(pPushToRemote))
}
}
} catch err {
set sc = err.AsStatus()
}
return sc
}

}
61 changes: 61 additions & 0 deletions test/UnitTest/SourceControl/Git/BaselineExport.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Class UnitTest.SourceControl.Git.BaselineExport Extends %UnitTest.TestCase
{

Method TestBaselineExport()
{
// create a mac routine
if '##class(%Routine).Exists("test.mac") {
set r = ##class(%Routine).%New("test.mac")
do r.WriteLine(" write 22,!")
do r.Save()
do r.Compile()
}
// create an inc routine
if '##class(%Routine).Exists("test.inc") {
set r = ##class(%Routine).%New("test.inc")
do r.WriteLine(" ; test include routine")
do r.Save()
do r.Compile()
}
// create a class
if '##class(%Dictionary.ClassDefinition).%OpenId("TestPkg.Class") {
set class = ##class(%Dictionary.ClassDefinition).%New()
set class.Name = "TestPkg.Class"
$$$ThrowOnError(class.%Save())
do $system.OBJ.Compile("TestPkg.Class")
}
do $$$AssertNotTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.mac"))
do $$$AssertNotTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.inc"))
do $$$AssertNotTrue(##class(SourceControl.Git.Utils).IsInSourceControl("TestPkg.Class.cls"))
do $$$AssertStatusOK(##class(SourceControl.Git.API).BaselineExport())
do $$$AssertTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.mac"))
do $$$AssertTrue(##class(SourceControl.Git.Utils).IsInSourceControl("test.inc"))
do $$$AssertTrue(##class(SourceControl.Git.Utils).IsInSourceControl("TestPkg.Class.cls"))
}

Property InitialExtension As %String [ InitialExpression = {##class(%Studio.SourceControl.Interface).SourceControlClassGet()} ];

Property SourceControlGlobal [ MultiDimensional ];

Method %OnNew(initvalue) As %Status
{
Merge ..SourceControlGlobal = ^SYS("SourceControl")
Kill ^SYS("SourceControl")
Set settings = ##class(SourceControl.Git.Settings).%New()
Set settings.namespaceTemp = ##class(%Library.File).TempFilename()_"dir"
Set settings.Mappings("MAC","*")="rtn/"
Set settings.Mappings("CLS","*")="cls/"
Do settings.%Save()
Do ##class(%Studio.SourceControl.Interface).SourceControlClassSet("SourceControl.Git.Extension")
Quit ##super(initvalue)
}

Method %OnClose() As %Status [ Private, ServerOnly = 1 ]
{
Do ##class(%Studio.SourceControl.Interface).SourceControlClassSet(..InitialExtension)
Kill ^SYS("SourceControl")
Merge ^SYS("SourceControl") = ..SourceControlGlobal
Quit $$$OK
}

}
Loading