initial commit
This commit is contained in:
commit
6bd1c0449f
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
tmpl
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2021 Alessandro Mauri
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
20
makefile
Normal file
20
makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
VERSION = 0.1
|
||||||
|
PREFIX = /usr/local
|
||||||
|
MANPREFIX = ${PREFIX}/share/man
|
||||||
|
|
||||||
|
tmpl: tmpl.sh
|
||||||
|
|
||||||
|
install: tmpl
|
||||||
|
mkdir -p ${DESTDIR}${PREFIX}/bin
|
||||||
|
cp -f tmpl ${DESTDIR}${PREFIX}/bin/tmpl
|
||||||
|
chmod 755 ${DESTDIR}${PREFIX}/bin/tmpl
|
||||||
|
mkdir -p ${DESTDIR}${MANPREFIX}/man1
|
||||||
|
sed "s/VERSION/${VERSION}/g" < tmpl.1 > ${DESTDIR}${MANPREFIX}/man1/tmpl.1
|
||||||
|
chmod 644 ${DESTDIR}${MANPREFIX}/man1/tmpl.1
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
rm -f ${DESTDIR}${PREFIX}/bin/tmpl\
|
||||||
|
${DESTDIR}${MANPREFIX}/man1/tmpl.1
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f tmpl
|
83
tmpl.1
Normal file
83
tmpl.1
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
.TH TMPL\-VERSION 1 "JUNE 2021" "Alessandro Mauri"
|
||||||
|
|
||||||
|
.SH NAME
|
||||||
|
tmpl \- touch with templates
|
||||||
|
|
||||||
|
.SH SYNOPSIS
|
||||||
|
.SY tmpl
|
||||||
|
.OP \-hf
|
||||||
|
.OP \-c dir
|
||||||
|
.I filename
|
||||||
|
.YS
|
||||||
|
|
||||||
|
.SH DESCRIPTION
|
||||||
|
.PP
|
||||||
|
tmpl is a POSIX shell script that creates a new file, filling it withe the
|
||||||
|
content of an appropriate template found in the template directory.
|
||||||
|
|
||||||
|
.SH OPTIONS
|
||||||
|
.IP "\-c dir"
|
||||||
|
Override the default template folder with
|
||||||
|
.I dir
|
||||||
|
Prints help information
|
||||||
|
.IP \-f
|
||||||
|
Forces override when creating the file
|
||||||
|
|
||||||
|
.SH USAGE
|
||||||
|
|
||||||
|
.PP
|
||||||
|
The principle behind tmpl is similar to touch(1), tmpl creates a new file
|
||||||
|
.I filename
|
||||||
|
and fills it with an appropriate template or nothing if no template is
|
||||||
|
available. Templates are chosen when their name matches
|
||||||
|
.I filename
|
||||||
|
or when the extensions match.
|
||||||
|
|
||||||
|
.SH FILES
|
||||||
|
|
||||||
|
.PP
|
||||||
|
The templates are searched in the following paths:
|
||||||
|
.EX
|
||||||
|
$HOME/.config/tmpl
|
||||||
|
$HOME/.tmpl
|
||||||
|
.EE
|
||||||
|
Subdirectories are not scanned
|
||||||
|
|
||||||
|
.PP
|
||||||
|
Templates must be regular files, other than that they can have any name and
|
||||||
|
any content.
|
||||||
|
|
||||||
|
.SH EXAMPLES
|
||||||
|
|
||||||
|
.PP
|
||||||
|
A simple example, and the main reason this tool was made is to prefill C source
|
||||||
|
files with a main().
|
||||||
|
Inside a file called template.c in one of the above mentioned paths put:
|
||||||
|
|
||||||
|
.EX
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
.EE
|
||||||
|
|
||||||
|
.PP
|
||||||
|
Now when you run:
|
||||||
|
|
||||||
|
.EX
|
||||||
|
$ tmpl foo.c
|
||||||
|
.EE
|
||||||
|
|
||||||
|
A new file called foo.c will be created with the contents of template.c
|
||||||
|
|
||||||
|
.SH AUTHOR
|
||||||
|
Alessandro Mauri <alemauri001@gmail.com>
|
||||||
|
|
||||||
|
.SH "SEE ALSO"
|
||||||
|
.BR touch(1)
|
86
tmpl.sh
Normal file
86
tmpl.sh
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# MIT License
|
||||||
|
# Copyright (c) 2021 Alessandro Mauri
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
printf 'tmpl [-hf] [-c dir] filename\n'
|
||||||
|
printf '\t-h: print this message\n'
|
||||||
|
printf '\t-f: force override\n'
|
||||||
|
printf '\t-c dir: change template directory location\n'
|
||||||
|
}
|
||||||
|
|
||||||
|
unset DIR
|
||||||
|
FORCE=false
|
||||||
|
|
||||||
|
while getopts 'hfc:' c
|
||||||
|
do
|
||||||
|
case "$c" in
|
||||||
|
h) usage; exit 0 ;;
|
||||||
|
f) FORCE=true ;;
|
||||||
|
c) DIR="$OPTARG" ;;
|
||||||
|
*) ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
if ! [ "$1" ]; then
|
||||||
|
echo "Not enough arguments"
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# get the right directory
|
||||||
|
if [ "$DIR" ]; then
|
||||||
|
if ! [ -d "$DIR" ]; then
|
||||||
|
echo "Cannot find template directory"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif ! [ -d "$DIR" ]; then
|
||||||
|
DIR="$HOME"/.config/tmpl
|
||||||
|
if ! [ -d "$DIR" ]; then
|
||||||
|
DIR="$HOME"/.tmpl
|
||||||
|
if ! [ -d "$DIR" ]; then
|
||||||
|
echo "Cannot find template directory"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
tmp="$(mktemp)"
|
||||||
|
find "$DIR" -maxdepth 1 -type f > "$tmp"
|
||||||
|
iext="${1##*.}"
|
||||||
|
while IFS= read -r file; do
|
||||||
|
fn="${file##*/}"
|
||||||
|
ext="${fn##*.}"
|
||||||
|
if [ "$fn" = "$1" ] || [ "$ext" = "$iext" ]; then
|
||||||
|
if [ $FORCE ]; then
|
||||||
|
cp "$file" "$1"
|
||||||
|
else
|
||||||
|
cp -i "$file" "$1"
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
done < "$tmp"
|
||||||
|
rm "$tmp"
|
||||||
|
|
||||||
|
echo "No template found, touching"
|
||||||
|
touch "$1"
|
||||||
|
exit 0
|
Loading…
Reference in New Issue
Block a user