rranelli / auto-package-update.el

Automatically update Emacs packages.
151 stars 26 forks source link

New feature: delete old version directories #23

Closed JesusMtnez closed 8 years ago

JesusMtnez commented 8 years ago

I have implemented a new feature: delete old version directories when updating. To acomplish this feature I have made the following changes:

I fix a little issue : the list contaning the packages to update had duplicates. To solve this I made this little change (5dc840ea6bf44ce433ed5995b399c79b3a93a496):

(defun apu--packages-to-install ()
  (delete-dups (-filter 'apu--package-out-of-date-p package-activated-list)))

I define a variable auto-package-update-delete-old-versions that controls the feature's behaviour: when it is nil, the feature is ignored, other cases, the feature is applied.

I define a variable apu--old-versions-dirs-list that will save old version directory of every package that it is updated.

To add the old version directory of package to this list, I implement apu--add-to-old-versions-dirs-list:

(defun apu--add-to-old-versions-dirs-list (package)
  "Add package old version dir to apu--old-versions-dirs-list"
  (let ((desc (cadr (assq package package-alist))))
    (add-to-list 'apu--old-versions-dirs-list (package-desc-dir desc))))

This function is invocated everytime that a package is installed:

(defun apu--safe-package-install (package)
  ...
        (when auto-package-update-delete-old-versions
          (apu--add-to-old-versions-dirs-list package))
        (package-install-from-archive (cadr (assoc package package-archive-contents)))
  ...

To delete old version directories of the list when all package are updated, I implement apu--delete-old-versions-dirs-list:

(defun apu--delete-old-versions-dirs-list ()
  "Delete package old version dirs saved in variable apu--old-versions-dirs-list"
  (dolist (old-version-dir-to-delete apu--old-versions-dirs-list)
    (delete-directory old-version-dir-to-delete t))
  ;; Clear list
  (setq apu--old-versions-dirs-list ()))

This function is invocated after all packages have been updated:

 (defun apu--safe-install-packages (packages)
   (let (apu--package-installation-results)
     (dolist (package-to-update packages)
       (apu--safe-package-install package-to-update))
     (when auto-package-update-delete-old-versions
       (apu--delete-old-versions-dirs-list))
     apu--package-installation-results))

Finally, I update the documentation including this new feature (ec2c83ec7eaf2d3b169ea7ae1c8b341707b085b5), along with some code indentation fix (fd8a1c1dff4438cfb52405984a47ce3f67487a29) and a minor documentation fix (ec2c83ec7eaf2d3b169ea7ae1c8b341707b085b5).

Any questions and suggestions are welcome.

rranelli commented 8 years ago

Wow, that's an awesome addition, thanks! The code looks good too.

I thought about adding it some time ago but didn't for some reason I can't recall.

I will test the feature to see if everything is Ok and merge the pr right after.

Thank you once again :heart:

rranelli commented 8 years ago

Everything seems fine, I will merge the PR.

Thanks for the through description :+1: