Service menu entry to create KDevelop file template

I wrote a ridiculously simple file template service menu installer for Plasma/Dolphin. This isn’t a difficult job to do manually but I use it a lot and this makes it really fast. Perhaps someone might find this useful so I thought I’d share it.

It will overwrite the template of the same name so it’s a bit of a sharp knife. I prefer this because I can fix a bite or two, right click, and push the updated template into place.

I have a folder of templates in my development directory. Each template has a folder that looks similar to this:

/.class.cpp /class.h /.desktop

.desktop:

[General]
Name=
Comment=
Category=C++/Qt
Language=C++
Type=Class
Files=Header,Implementation

[Header]
Name=Public Header
File=class.h
OutputFile={{ name }}.h

[Implementation]
Name=Implementation
File=class.cpp
OutputFile={{ name }}.cpp

The service menu consists of two parts:

~/.local/bin/KDevFileTemplateInstall

#!/bin/bash

TEMPLATE=$1
TMP=/tmp
TEMPLATES_FILE=~/.local/share/kdevfiletemplates/templates/
DIR=$(basename “$TEMPLATE”)
PATH_TEMPLATE=$(dirname “$TEMPLATE”)

pwd >> /tmp/temp.log
echo “$1” >> /tmp/temp.log

if [ -d “$TEMPLATE” ]; then

if compgen -G "$TEMPLATE"/*.desktop > /dev/null; then

    tar -cvjf "$TMP/$DIR.tar.bz2" -C "$PATH_TEMPLATE/$DIR" .
    mv "$TMP/$DIR.tar.bz2" "$TEMPLATES_FILE"

else

    echo "$1 is not a template directory"

fi

else

echo "$1 is not a directory or does not exist"

fi

~/.local/share/kio/servicemenus

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=inode/directory
Actions=InstallTemplate

[Desktop Action InstallTemplate]
Name=Install as KDevelop File Template
Icon=folder
Exec=KDevFileTemplateInstall “%f”

I hope this helps someone.

2 Likes

For anyone on an Arch variant, by the way, I made an AUR package for both KDevelop app and KDevelop file service menu entries.

yay -Ss kdev-templatetools

aur/kdev-templatetools 0.1.5-1 (+0 0.00) (Installed)
KDevelop app and file utilities for building and installing your own templates.

The package is currently undocumented. That will be corrected in the next few days. In the mean time, it’s pretty obvious. Create a directory structure, probably by copying the contents of an existing template under one of the following two directories:

KDev App Templates - global - /usr/share/kdevappwizard/templates

KDev File Templates - global - /usr/share/kdevfiletemplates/templates

Unpack something that seems similar to your goal, manipulate it as you wish, then right click on the directory in Dolphin, select Actions → Install as KDevelop Template.

Don’t forget to change the name of the directory and the name in the template file.

KDevelop will happily display two identically named templates and you’ll have to guess which is yours. If that happens: delete yours, correct any naming collisions, right click, and push the corrected template into place.

File templates update dynamically, so no need to restart KDevelop when you change or add file templates.

App templates do not update dynamically, so you’ll need to restart KDevelop to pick up any changes.

I doubt I’m using the system as intended but I have a lot of file templates that could probably be snippets. I prefer to use file templates as it’s way faster and easier. Perhaps I’m abusing this feature but KDevelop doesn’t complain.

Happy coding!

1 Like