Skip to content

Instantly share code, notes, and snippets.

@wallabra
Created March 28, 2020 00:12
Show Gist options
  • Save wallabra/3c5756134ba542b9a588a6156e37f3bc to your computer and use it in GitHub Desktop.
Save wallabra/3c5756134ba542b9a588a6156e37f3bc to your computer and use it in GitHub Desktop.
Some project folder setup script.
#!/bin/bash
# Registers a project into the project management lifesystem.
# This automatically sets up Git, may add to lookup tables (if
# AUTHOR, LANGUAGE or MONTH parameters are specified).
# It will not set up the link from @public for you. To do this,
# you will want to instead use:
#
# ./@@publish my_repo
#
# It can be reverted using './@@unpublish'.
# Public repositories may be served via HTTP.
#------
set -o errexit
# Important values
PROJDIR="$(pwd)"
REPODIR="$(readlink -f "$PROJDIR")/$1"
REPONAME="$(basename "$1")"
REPONAME_GIT="$(echo "$REPONAME" | tr -d ' ')"
REPODIR_GIT="$(readlink -f "$PROJDIR")/@git/$REPONAME_GIT.git"
# Common functions
function register_by {
FOLDER="$1"
PARAMETER="$2"
if [ -n "$PARAMETER" ]; then
mkdir -pv "$FOLDER/$PARAMETER"
if [ ! -f "$FOLDER/$PARAMETER/$REPONAME" ]; then
ln -sv "$REPODIR" "$FOLDER/$PARAMETER/$REPONAME"
fi
fi
}
function fix_register_by {
FOLDER="$1"
PARAMETER="$2"
if [ -n "$PARAMETER" ]; then
if [ ! -L "$FOLDER/$PARAMETER/$REPONAME" ]; then
register_by "$1" "$2"
printf 'F'
fi
fi
}
function setup_git {
# Makes a bare repository, which is where changes
# to your project will be first pushed. This repository
# may be relayed to an external service, like GitHub.
# distribution via HTTP, FTP, or SSH.
mkdir -pv "@git"
(
cd "$PROJDIR/@git"
git clone --bare --mirror "$REPODIR" "$REPONAME_GIT.git"
cd "$REPONAME_GIT.git"
if [ -n "$GITHUB" ]; then
# Adds a GitHub remote to this repository.
# Note: $GITHUB must be in the format "<username>/<reponame>"
# where 'username' is the GitHub user, and 'reponame'
# is the name of the GitHub repository.
git remote add github "https://www.github.com/$GITHUB"
echo "git push gitlab \$(git rev-parse --symbolic --abbrev-ref \$1)" >>"hooks/post-receive"
fi
if [ -n "$GITLAB" ]; then
# Adds a GitLab remote to this repository.
# Note: $GITLAB must be in the format "<username>/<reponame>",
# where 'username' is the GitLab user, and 'reponame'
# is the name of the GitLab repository.
git remote add gitlab "https://www.gitlab.com/$GITLAB"
echo "git push gitlab \$(git rev-parse --symbolic --abbrev-ref \$1)" >>"hooks/post-receive"
fi
touch ".registered" # this prevents registering the same project multiple times
)
}
function fix_git_setup {
if [ ! -d "$REPODIR_GIT" ]; then
setup_git
printf 'F'
fi
}
# Prevent multiple registrations of the same project.
if [ -f "@git/$REPONAME_GIT/.registered" ]; then
>&2 echo "This project was already registered! Checking for basic fixes to apply..."
FIXED="$(
fix_register_by "@by/@author" "$AUTHOR"
fix_register_by "@by/@language" "$LANGUAGE"
fix_register_by "@by/@month" "$MONTH"
fix_git_folder
)"
if [ -n "$FIXED" ]; then
FIXES="$(printf "%s" "$FIXED" | wc -c)"
echo "Applied $FIXES basic fixes."
fi
echo "Please note this is not a watertight solution. You may still need to"
echo "manually set up ."
fi
# Add project to lookup directories.
register_by "@by/@author" "$AUTHOR"
register_by "@by/@language" "$LANGUAGE"
register_by "@by/@month" "$MONTH"
# Run git commands on subshells, to prevent changing
# the working directory of the main script.
setup_git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment