Skip to content

Instantly share code, notes, and snippets.

@janderudder
Last active August 16, 2021 11:33
Show Gist options
  • Save janderudder/79a0d910f58e9030da5d28018f49c45e to your computer and use it in GitHub Desktop.
Save janderudder/79a0d910f58e9030da5d28018f49c45e to your computer and use it in GitHub Desktop.
VSC : script for booting up new dev project : it copies a template folder and launches Visual Studio Code
#!/bin/bash
##
## Create dev project and launch Visual Studio Code
##
if [[ $1 == '-h' || $1 == '--help' || $1 == '-?' ]]
then
Base_name="$(basename -s '.sh' $0)"
cat <<-EOD
Create a dev project based on a template and launch Visual Studio Code.
USAGE
(1) ${Base_name} --new TEMPLATE-NAME FOLDER-PATH
(2) ${Base_name} [FOLDER-PATH]
(1) Create a new directory at location FOLDER-PATH, configured according to
TEMPLATE-NAME, then launches VSC.
(2) Just launches VSC in FOLDER-PATH, or in the current directory if none is
specified.
TEMPLATES
- cpp C++17
- cpp-premake (cpm) C++17 with premake5 support
- cpp-premake-inc (cpmi) C++17 with premake5 support and 'include/' dir.
- sfml SFML 2.5.1 x C++17 x stdc++fs
- sfml-extended (sfe) same as above along with a mini engine
- sfml-premake (sfpm) sfml project with premake5 support
- sfml-premake-inc (sfpmi) same with include & src folder structure
- qt Qt project with qmake
- qt-window (qtw) Qt project with qmake and a window class
- qt-sfml (qtsf) Qt project linking also to SFML
- qt-shared (qtso) Qt project configured for producing a shared lib
EXAMPLES
$ vsc --new cppmi MyProject # creates './MyProject/' with relevant files
$ vsc # equivalent to \$(code .)
EOD
exit 0
fi
# output utilities
errout()
{
\echo "$@" 1>&2
}
stdout()
{
\echo "$@" 2>&1
}
# main section
if [[ $1 == '--new' || $1 == '-n' ]]
then
shift
[[ $# -lt 1 ]] \
&& errout "need more arguments" \
&& exit 1
# handle positional arguments
declare Proj_type="$1"
declare Template_name=""
declare Template_description=""
declare Template_path="$CPPDEV/0_snippets"
# extra paths
declare Sfml_ext_lib_path="$CPPDEV/9_my-lib/sfml-ext"
# booleans
declare Using_premake=false
declare Using_sfml=false
declare Using_qmake=false
declare Using_include_dir=false
# do the work
[[ ! -d "$Template_path" ]] \
&& errout "template directory doesn't exist" \
&& exit 2
case "${Proj_type}" in
'cpp' | 'cpp17')
Template_name='cpp17-project'
Template_description='C++17'
;;
'cpp-premake' | 'cpm' | 'cppm')
Template_name='cpp17-premake5-project'
Template_description='C++17-with-premake'
Using_premake=true
;;
'cpp-premake-inc' | 'cpmi' | 'cppmi')
Template_name='cpp17-premake5-with-include-project'
Template_description='C++17-with-premake-and-include'
Using_premake=true
Using_include_dir=true
;;
'sfml' | 'sfml-debug')
Template_name='sfml-debug-project'
Template_description='SFML-2.5.1'
Using_sfml=true
;;
'sfml-extended' | 'sfe')
Template_name='sfml-extended-project'
Template_description='SFML-2.5.1-extended'
Using_sfml=true
;;
'sfml-premake' | 'sfml-premake5' | 'sfpm')
Template_name='sfml-premake5-project'
Template_description='SFML-2.5.1-with-premake'
Using_premake=true
Using_sfml=true
;;
'sfml-premake-inc' | 'sfpmi')
Template_name='sfml-premake5-with-include-project'
Template_description='SFML-2.5.1-with-premake-and-include'
Using_premake=true
Using_sfml=true
Using_include_dir=true
;;
'qt')
Template_name='qt-vscode-project'
Template_description='Qt project with qmake'
Using_qmake=true
Using_include_dir=true
;;
'qt-window' | 'qt-win' | 'qtwin' | 'qtw')
Template_name='qt-vscode-project-window'
Template_description='Qt project with qmake and a window class'
Using_qmake=true
Using_include_dir=true
;;
'qt-sfml' | 'qtsf')
Template_name='qt-and-sfml-project'
Template_description='Qt project linking also to SFML'
Using_qmake=true
Using_include_dir=true
;;
'qt-shared' | 'qtso')
Template_name='qt-shared-library'
Template_description='Qt project configured for producing a shared lib'
Using_qmake=true
Using_include_dir=true
;;
*)
errout "Template name is not recognized: ${Proj_type}"
exit 3
;;
esac
Template_path="${Template_path}/${Template_name}"
declare Target_dir="$2"
[[ ! -d "$(dirname "$Target_dir")" ]] \
&& errout 'last argument must be a valid path' \
&& exit 4
[[ -d "$Target_dir" ]] \
&& errout 'Warning: the target path already exists.' \
&& errout 'Aborting' && exit 5
stdout "Creating a $Template_name project in" "$(realpath "${Target_dir}")"
/bin/cp -r "$Template_path" "$Target_dir"
if $Using_premake
then
# configure project name in premake5.lua then run premake
declare Target_folder_name="$(basename $(realpath ${Target_dir}))"
sed -i "s/__PROJECT_NAME__/""${Target_folder_name}""/g" \
"${Target_dir}/build/premake5.lua"
elif $Using_qmake
then
# rename Qt's .pro file, configure the project name in it
declare Target_folder_name="$(basename $(realpath ${Target_dir}))"
declare Pro_file_source_path="${Target_dir}/${Template_name}.pro"
declare Pro_file_target_path="${Target_dir}/${Target_folder_name}.pro"
mv "${Pro_file_source_path}" "${Pro_file_target_path}"
sed -i "s/__PROJECT_NAME__/""${Target_folder_name}""/g" \
"${Pro_file_target_path}"
fi
if $Using_sfml
then
sed -i "s/window-name/$(basename $(realpath ${Target_dir}))/" \
"${Target_dir}/src/main.cpp"
if $Using_include_dir
then
/bin/cp "${Sfml_ext_lib_path}/window/"*.hpp \
"${Target_dir}/include/sfext/"
/bin/cp "${Sfml_ext_lib_path}/window/"*.cpp \
"${Target_dir}/src/sfext/"
else
/bin/cp "${Sfml_ext_lib_path}/window/"* \
"${Target_dir}/src/sfext/"
fi
fi
stdout "Openning $Target_dir in Visual Studio Code"
code "$Target_dir"
exit 0
fi
if [[ $# -eq 0 ]]; then
stdout "launching Visual Studio Code in current directory."
code .
exit 0
else
code "$@"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment