Updating version ids in Blender addons automatically with Notepad++

Blender addons use a version identifier that consists of three numbers, for example:
bl_info = {
 "name": "Awesome object generator",
 "author": "Me",
 "version": (1, 0, 20150425140302),
 "blender": (2, 74, 0),
 "location": "View3D > Add > Mesh",
 "description": "Adds an awesome object at the 3d cursor",
 "warning": "",
 "wiki_url": "",
 "tracker_url": "",
 "category": "Add Mesh"}
I do virtually all addon development with the help of Notepad++ and I really wanted to have a way to automatically update this version information when I change something. Cvs (and svn) have functionality to update specific strings but I wanted to use this addon version information because having two different tags doesn't sound logical to me. Fortunately with the help of the Python Script plugin for Notepad++ it is quite simple to create a save event listener that does exactly what I want:
from datetime import datetime

# Just in case, we'll clear all the existing callbacks for FILEBEFORESAVE
notepad.clearCallbacks([NOTIFICATION.FILEBEFORESAVE])

def ts(m):
 dt=datetime.now()
 ds=dt.strftime("%Y%m%d%H%M%S")
 return m.group(1)+ds+m.group(3)
 
# Define the function to call just before the file is saved
def addSaveStamp(args):
 currentBufferID = notepad.getCurrentBufferID()
 notepad.activateBufferID(args["bufferID"])
 editor.rereplace(r"(version.*?\(\s*?\d+\s*,\s*\d+\s*,\s*)(\d+)(\s*\))", ts)
 notepad.activateBufferID(currentBufferID)
    
# ... and register the callback 
notepad.callback(addSaveStamp, [NOTIFICATION.FILEBEFORESAVE])
If you place that code in the startup.py file (typically in your roaming profile, for example: C:\Users\Michel\AppData\Roaming\Notepad++\plugins\config\PythonScript\scripts\startup.py ) and make sure the Python Script plugin is activated on startup (by selecting Plugins -> Python script -> Configuration -> Initialization = ATSTARTUP and restarting Notepad++) then any subsequent save will look for a version identifier and will replace the third number with a timestamp.

No comments:

Post a comment