I need to create a universal script that will add this information to each file when it is saved, as in such text editors like Atom orWebStorm.
Information for add:
- File creation date;
- Date of last file update;
- Project branch;
- Author.
Here is an example of the code I created with the shell.
#! /bin/bash
# Abort if any of the commands fail
set -e
# Trace what gets executed. Useful for debugging.
set -x
# If set, the return value of a pipeline is the value of the last (rightmost) command to
#exit with a non-zero status, or zero if all commands in the pipeline exit successfully.
set -o pipefail
# Treat unset variables as an error when performing parameter expansion.
# If expansion is attempted on an unset variable, the shell prints an error message,
# and, if not interactive, exits with a non-zero status.
set -u
# Created:
DATE=$(date +%Y-%m-%d:%H:%M:%S);
BRANCH=$(git symbolic-ref --short -q HEAD);
GIT_USER=$(git config user.name);
CREATED=$(echo 'created');
# Updated:
DATE_U=$(git log -1 --format=%cd --date=format:%Y-%m-%d:%H:%M:%S);
BRANCH_U=$(git symbolic-ref --short -q HEAD);
GIT_USER_U=$(git config user.name);
UPDATED=$(echo 'updated');
# Find file (js) and add table to file.
echo "Create table in js-file..."
if test -a $(grep created: ./development/*.js); then
sudo find ./development -name "*.js" -type f -exec sed -i 1i\ "/*flow*/\n/*---------------------------------------------------------\n $CREATED: $DATE | $BRANCH | $GIT_USER \n $UPDATED: $DATE_U | $BRANCH_U | $GIT_USER_U \n---------------------------------------------------------*/" {} \;
echo "Create table..."
else
echo "Table already is exist..."
fi
# Find file (scss) and add to file.
echo "Create table in scss-file..."
if test -a $(grep created: ./development/*.scss); then
sudo find ./development -name "*.scss" -type f -exec sed -i 1i\ "/*---------------------------------------------------------\n $CREATED: $DATE | $BRANCH | $GIT_USER \n $UPDATED: $DATE_U | $BRANCH_U | $GIT_USER_U \n---------------------------------------------------------*/" {} \;
echo "Create table..."
else
echo "Table already is exist..."
fi
In the commands for adding a table, there is a drawback, after the first addition to the file, this table no longer works on files that were created later or the table in them was deleted.
And the second problem is the dynamic update of information in files.
# Updated
echo "Update date area..."
if test ! -a $DATE_U;
then
echo "Date is exist..."
else
echo "Update date..."
fi
echo "Update branch area..."
if test ! -a $BRANCH_U;
then
echo "Branch is exist..."
else
echo "Update branch..."
fi
echo "Update user area..."
if test ! -a $GIT_USER;
then
echo "User is exist..."
else
echo "Update user..."
fi