Project Templates Script

Following a previous post that I did about standardization of projects in LabVIEW, I decided to create a script for doing this automatically using the given template.

My inspiration was a post in Sam’s Blog about automating this task with a script (special thanks). I did a few modifications to adapt my workflow here such as: changing to HTTPS, cloning first this template repository and not switching branches.

Options

So, now you have three options to create a project template in LabVIEW:

  • From LabVIEW Create Project Wizard
  • From GitLab Website
  • From Bash Script (now my favorite).

For this reason, you and your team will have no excuse to not follow project templates across your development repositories.

The Script

So, for using it, you must:

  • Adapt the initial variables to your use.
  • In Windows save the script “createrepo” to a file in “Git binary folder” (…/Git/usr/bin or …/Git/usr/local/bin);
#!/bin/bash

# Parameters
# From user is expected three different parameters
# $1 - Namespace
# $2 - Repository Name
# $3 - User e-mail - local

default_branch="main"
platform="gitlab.com"
user_name="YOUR NAME"

#Main function - Verify for parameters and then call main function, else display help.
main(){
if [ -z $3 ]; then
  display_help
  exit 1
else
  push_repository $1 $2 $3
fi
}

display_help(){
  echo -e "Usage: createrepo <namespace> <reponame> <user.email>\n
 The meaning of every argument is below:
          namespace\tGroup or Username in the Git Website.
          reponame\tName of the repository being created.
          user.email\tUser e-mail to the current repository"
}

push_repository(){
echo "Creating folder..."
mkdir $2
cd $2

echo "Cloning template repository..."
git clone --depth=1 https://gitlab.com/felipe_public/template.git .
rm -rf .git/

echo "Initializing Repository..."
git init

echo "Setting default branch..."
git symbolic-ref HEAD refs/heads/$default_branch

git config --local user.name "$user_name"
git config --local user.email $3

echo "Adding Template Files"
git add .

echo "Creating Initial Commit..."
git commit -m "Initial commit from template project"

echo "Pushing to new repo..."
git push --set-upstream https://$platform/$1/$2.git $default_branch

echo "Adding origin..."
git remote add origin https://$platform/$1/$2.git
}

#Call Main Function
main $1 $2 $3

After that execute:

$ createrepo namespace repository_name e-mail

Caveats

  • This script uses a local scope for User (because I work with different git websites, so I prefer that way);
  • You must have the permissions (defined by the owner/admin) to create new repositories in the destination namespace.

All that said, enjoy and leave your comments on what you think about this approach.

Edit: Updated script. Now it creates the repository folder before cloning.

3 thoughts on “Project Templates Script

  1. Well done. I like it. Gave me a few ideas for my own script, like optionally copying a template project as a starting point. I might adapt it to pass in a template url so I can have multiple templates. Also your bash scripting appears to be more advanced than mine. I need to brush up a little bit.

    Liked by 1 person

    1. Thanks, Sam. Just like you told me, it was very handy. I used a couple of times last week. About the bash script, I just did a small input handling, because I may pass this code to the team and this will avoid any errors. About the templates, I cared about the depth=1 to be faster, a little, but it makes a difference in some repositories.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s