Author Topic: writing a hierarchical get latest script  (Read 1695 times)

yonatan.lehman

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
writing a hierarchical get latest script
« on: June 19, 2022, 10:28:58 pm »
Hi
As far as I know, EA (14) does not have a way to do "get latest" for a tree of packages.
You can either do get all  latest  on the entire project tree or get latest on a single package.
I tried to write a script to do this :

Code: [Select]
var forceImport = true;
function process_package(aPackage) {
var thePackage As EA.Package
thePackage = aPackage;

if (thePackage.IsVersionControlled) {
thePackage.VersionControlGetLatest(forceImport);
thePackage.Update();
}

for (var p = 0; p < thePackage.Packages.Count; p++ ){
pkg = thePackage.Packages.GetAt(p);
process_package(pkg);
}
thePackage.Update();
}

Problems
1) After the call to thePackage.VersionControlGetLatest the package has no sub packages (thePackage.Packages.Count == 0) - even though after the script runs I can see the sub-projects in the project tree, and indeed when I run the script again, this time the script sees the children.

I can run the script multiple times, but this is slow, doing get latest on each package multiple times, - and I can't think of a simple way of knowing if I've already done get latest on a package and I've reached the bottom level packages of the tree.

Any solutions?


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13288
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: writing a hierarchical get latest script
« Reply #1 on: June 20, 2022, 04:04:31 pm »
You might need to reload the package before the script can see the new subPackages.

This is what we use:

https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Projects/Project%20A/Model%20Management/Get%20latest%20from%20TFS.vbs

Code: [Select]
'[path=\Projects\Project A\Model Management]
'[group=Model Management]
option explicit

!INC Local Scripts.EAConstants-VBScript
!INC Wrappers.Include

'
' Script Name: Get latest from TFS
' Author: Geert Bellekens
' Purpose: Does GetLatest on all version controlled packages in the tree.
' Date: 2017-10-19
'

const outPutName = "Get latest from TFS"

sub main
'create output tab
Repository.CreateOutputTab outPutName
Repository.ClearOutput outPutName
Repository.EnsureOutputVisible outPutName
'get the selected package
dim package as EA.Package
set package = Repository.GetTreeSelectedPackage()
'let the user know we started
Repository.WriteOutput outPutName, now() & " Starting get latest from TFS for package '"& package.Name &"'", 0
'ask the user if he is sure
dim userIsSure
userIsSure = Msgbox("Do you really want to get latest from TFS for package '" &package.Name & "' ?", vbYesNo+vbQuestion, "Get latest from TFS?")
if userIsSure = vbYes then
'actually do the getlatest
getLatestFromTFS package
'do a reconcile to make sure all relations are there
Repository.WriteOutput outPutName, now() & " Reconciling...", 0
Repository.ScanXMIAndReconcile
'make sure the output is visible again
Repository.EnsureOutputVisible outPutName
end if
'let the user know it is finished
Repository.WriteOutput outPutName, now() & " Finished get latest from TFS for package '"& package.Name &"'", 0
end sub

function getLatestFromTFS(package)
'first process this package
if package.IsVersionControlled then
Repository.WriteOutput outPutName, now() & " Getting latest version for package '"& package.Name &"'", 0
dim isReadWrite
if isRequireUserLockEnabled() then
isReadWrite = package.ApplyUserLockRecursive(true, true, true)
else
isReadWrite = true
end if
'then add this package to version control
if not isReadWrite then
Repository.WriteOutput outPutName, now() & " ERROR: Cannot proceed as package '"& package.Name &"' cannot be locked", package.Element.ElementID
exit function
end if
package.VersionControlGetLatest(true) 'true for force import
end if
'make sure we have an up-to set of subPackages
dim newPackage as EA.Package
set newPackage = Repository.GetPackageByGuid(package.PackageGUID)
'then process subPackages
dim subPackage
for each subPackage in newPackage.Packages
getLatestFromTFS subPackage
next
end function

main

Geert

yonatan.lehman

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Re: writing a hierarchical get latest script
« Reply #2 on: June 20, 2022, 04:50:30 pm »
Ah - the secret is in the
set newPackage = Repository.GetPackageByGuid(package.PackageGUID)
_ I tried ByID and not ByGuid....

Regarding the following lines - I assume isRequireUserLockEnabled is your function and not part of EA.
In what scenario do you use UserLock ? (as opposed to check-in/out)
Code: [Select]
    if isRequireUserLockEnabled() then
isReadWrite = package.ApplyUserLockRecursive(true, true, true)
   else
isReadWrite = true
   end if


Thanks as always Geert - You're the greatest - pity you use VB - what an ugly language   ;)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13288
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: writing a hierarchical get latest script
« Reply #3 on: June 20, 2022, 04:57:49 pm »
We use security with the option "Require user lock to edit" together with version control.

Not all of our models are version controlled, only the critical parts, or the parts that need to be shared with other repositories.

I know VBScript is ugly, and a bit weird, but that's just the syntax.
The advantage of VBScript over other scripting languages is that you can use the same code (almost) in Excel VBA as in EA scripts.
+ it has the native InputBox

Geert