Cleanup Old Plugin Versions?
-
What's a good strategy to automatically cleanup old versions of a plugin when a new version has been installed?
I am going to try to cleanup my plugins and consolidate them into a single folder rather than have them all directly in the Plugins folder. This would mean renaming or deleting many of the old files. I plan on converting them all to .rbz archives to ease installation and upgrades.
Is it OK to just delete the old files?
-
I have done that with a number of my tools.
Let's say I had an old version namedxxxx.rb
previously auto-loading from within the Plugins folder, but my new equivalent file [perhaps with the same name] is now installed inside its own tool's subfolder and loaded by some newxxxx_loader.rb
script that is installed in Plugins, that does auto-load.
I don't want the old version of the file to auto-load because it might break/overwrite the new files functions or add outdated methods etc.
Although I can tell users to remove this old version, some users just won't !
So I need to auto-run some code in the 'loader' script to find the old file, and if it exists delete it. My tool then pops up a UI.dialog saying what it's deleted the old file. This only happens once because the next time it loads it doesn't find anything to delete... After a while the 'find/deletion code' could perhaps be commented out of any newer versions of the loader as most users will have updated previously...Incidentally, there are sometimes problems when a user messes up an install and manually moves a tool's .rb files from the tool's subfolder into the Plugins folder, which then auto-load incorrectly at startup...
I was wondering if naming all of the script files with a tool's subfolder, as say 'xxxx.rb**~**
', would be a better protocol; they won't auto-load when misplaced bay a kack-handed newbie, and if in the wrong folder they can be more readily found in the Plugins folder by sorting a listing by type, and they can still be required or loaded etc from another scripts, by using their subfolder in the path.
I suggest.rb**~**
because I already rename files within my Plugins folder as.rb**!**
to stop then auto-loading; and using.txt
or other common 'extnames' would preclude a simple finding of misplaced files 'by type'... -
I am not sure about using a tilde character is wise. Windows uses it for contracted directory names, and Unix-like systems use it for a contracted HOME path.
Another unique character that is valid on both systems ?
-
I only plan on distributing and supporting .rbz archives, in which case botched installs should not be a problem.
-
I was looking at releasing a new version of my plugin comp2layerScene with new features and considering to make the dir and file names compatible with the SU Warehouse specifications. Thus I would need the new version to delete the old version.
Could you show a sample code used to delete/clean up old files & dir? as I am not sure how to do this.Thanks Keith
-
@ktkoh said:
I was looking at releasing a new version of my plugin comp2layerScene with new features and considering to make the dir and file names compatible with the SU Warehouse specifications. Thus I would need the new version to delete the old version.
Could you show a sample code used to delete/clean up old files & dir? as I am not sure how to do this.Thanks Keith
You don't need to do that as the [reworded] loader can remain as 'K2WS_Comp2LayerScene_ext.rb' [overwriting the old version] and its new subfolder [now referred to i it's code] should be 'K2WS_Comp2LayerScene_ext'.The old 'K2WS' subfolder becomes redundant / unused by the new loader.
If you really want to tidy up...
To remove an entire folder you need to first delete is contents...
FileUtils can do that for you BUT it is unlikely to be installed for every user...
So you probably need to add an extra rb file into your new subfolder, which then auto-runs from the ext-loader...
Add this just below the other 'require' lines in the Plugins folder's 'ext-loader' K2WS_Comp2LayerScene_ext.rb...
require(File.join(File.dirname(__FILE__), 'K2WS_Comp2LayerScene_ext', 'deleteK2WS.rb'))
ThendeleteK2WS.rb
contains this code:module K2WS def self.delete_folder(d) return nil unless File.directory?(d) Dir.entries(d).each{|f| next if f=='.' || f=='..' x=File.join(d, f) if File.directory?(x) self.delete_folder(x) else ### file File.delete(x) end } begin Dir.delete(d) rescue ### fails if it's not empty! self.delete_folder(d) end end#def ### auto-start process with K2WS subfolder self.delete_folder(File.join(File.dirname(File.dirname(__FILE__)), 'K2WS')) ### end#module
Untested - USE WITH CAUTION !
Perhaps get it to 'puts' the info rather than delete things, when first testing it
On subsequent startups after the deletions, the K2WS folder doesn't exist, so it returns nil at the start of the processing. -
Thanks I will work on that this afternoon.
Keith
-
I worked with the code yesterday and this am. I found it worked well if the file was in the plugin directory. However I had no success when I tried to run the file from the directory Plugins/k2ws_Comp2LayerScene directory. I tried changing the line replacing the FILE with a filename that was in the Plugins
self.delete_folder(File.join(File.dirname(__FILE__), 'K2WS'))
self.delete_folder(File.join(File.dirname("k2ws_Comp2LayerScene.rb"), 'K2WS'))
but that made the folder ./K2WS and thus it was not found. I read some more on ruby but none of the things I tried fixed this.
I looks like I would need to add the clean up code to the end of the file that creates the extension so it would start from the correct directory and I am not sure this is allowed in the warehouse.
Keith
-
The first line gives you the (incomplete) directory path
Plugins/k2ws_Comp2LayerScene/K2WS
.The second line gives you correctly
./K2WS
because "k2ws_Comp2LayerScene.rb" has no file path. Nobody can no where this file name is flying around, and so Ruby does not know as well. If no absolute path is provided, it is interpreted as a relative path to the current directory (./
) which is then joined with K2WS.Using the dynamic variable
__FILE__
is absolutely fine (at was probably not written here as a placeholder). This variable will always be the file path of the file that is being loaded. -
If you read the code it never said that.
It said:
self.delete_folder(File.join(File.dirname(File.dirname(__FILE__)), 'K2WS'))
Assuming that the rb doing the deletion is in theK2WS_Comp2LayerScene_ext
subfolder.
The path finds the dirname of its folder and then the dirname of that folder's folder - which should typically be Plugins...
Advertisement