Book a Demo

Author Topic: Script that Deletes Packages  (Read 7420 times)

Chris Allport

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Script that Deletes Packages
« on: November 01, 2014, 07:17:03 am »
Hi, All-

I have a method that deletes a package.  The problem is that the Project Browser is never refreshed.  The only thing that seems to work is calling Repository.RefreshModelView(0), but that reopens the model and kills script execution.

Here is the method:

Code: [Select]
function clearPackage( pkg ) {
      var parent as EA.Package;
      parent = Repository.GetPackageByID(pkg.ParentID);
      
      var pkgList as EA.Collection;
      pkgList = parent.Packages;

       for (var i = 0; i < pkgList.Count; i++) {
             var p as EA.Package;
            p = pkgList.GetAt(i);
            
            if (p.PackageGUID == pkg.PackageGUID) {
                   pkgList.Delete(i);
                   pkgList.Refresh();
                   parent.Update();  // have tried with and without
                  return;
            }
      }
}


Inspecting pkgList.Count before and after pkgList.Refresh does show a change in size.  Again, the problem seems limited to the Project Browser.

Any ideas on how to refresh the Project Browser?

(Cross post: http://stackoverflow.com/questions/26682852/project-browser-not-updating-with-package-deletion-script)
« Last Edit: November 01, 2014, 07:25:23 am by callport »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Script that Deletes Packages
« Reply #1 on: November 01, 2014, 09:50:53 am »
Answered over there:
Code: [Select]
my $p = $rep->GetTreeSelectedObject();
my $par = $rep->GetPackageByID ($p->ParentID);
my $idx = 0;
for my $sp (in $par->Packages) {
  if ($sp->PackageID == $p->PackageID) {
    $par->Packages->DeleteAt ($idx, 1);
    last;
  }
  $idx++;
}
$rep->RefreshModelView ($par->PackageID);

q.

Chris Allport

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Script that Deletes Packages
« Reply #2 on: November 03, 2014, 01:32:43 pm »
Continuing the thread:

Thank you, but I do not really see the difference in this code (except only refreshing the parent), this still reloads the whole model if the package is hanging off the root package. [In this case, Package ID of parent is 0.]  Also, DeleteAt does not appear to make a difference [I implemented with this the first time through] - the second parameter (refresh) is documented as not being implemented.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Script that Deletes Packages
« Reply #3 on: November 03, 2014, 06:00:14 pm »
The 2nd parameter is mandatory (and not used - it's EA). Actually it's the same then as Delete :-/

Regarding the stop of the script execution at a Refresh(0) you should send a bug report. Does not seem ok. Since I ran my test script stand-alone I did not notice that in the first place.

q.

P.S. In order to overcome the Refresh issue just postpone that with a flag to the end of the whole script. If you need to iterate through the cleaned list of sub-packages just load the object once again (use GetPackageByID or -GUID). I never use Refresh in any case. This is a most superfluous method.
« Last Edit: November 03, 2014, 06:21:20 pm by qwerty »

Chris Allport

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Script that Deletes Packages
« Reply #4 on: November 04, 2014, 02:08:27 am »
Postponing is a great suggestion, but what I am trying to overcome is the "underlying model has changed" warning that pops up mid-script run. For now, I am just clearing that error and the script continues.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Script that Deletes Packages
« Reply #5 on: November 04, 2014, 03:30:19 am »
Doesn't that error just pop up once you're done and you try to access data via the GUI?

q.

Chris Allport

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Script that Deletes Packages
« Reply #6 on: November 04, 2014, 08:39:31 am »
Sometimes, but sometimes it appears during script execution.

A little more about what I am doing.  I am creating a processed copy of the model in another package.  When a user goes to run the script again, if they use the same name for the duplicate package, it removes the old package and then creates a new package with the same name.  

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Script that Deletes Packages
« Reply #7 on: November 04, 2014, 10:03:12 am »
Another idea would be to move the old package to some temp location first. While you can not move a view manually into a package (or view) you can do that via automation. (The reason that EA blocks this is for very, very historic reasons only.) So at the end you can remove that.

q.

Chris Allport

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Script that Deletes Packages
« Reply #8 on: November 05, 2014, 08:43:29 am »
So far so good.  The package is now renamed to "_deleting_" + pkg.Name before I delete it.  Relatively low chance of collision with a name like that.  

Anyway, oddly enough, EA hasn't thrown this error on me in a couple of days now.  I did restart EA before and it didn't go away...so the script must be getting lucky.

Thanks for the help!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Script that Deletes Packages
« Reply #9 on: November 05, 2014, 09:42:51 am »
Yeah. One of those cobble stones you find and fall down. And when you look around you don't find them again. They must be moving  ;)

q.