Skip to content

Implement shell script for creating folder structure for new programming languages #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 2, 2019
Merged

Implement shell script for creating folder structure for new programming languages #25

merged 4 commits into from
Aug 2, 2019

Conversation

darrylbalderas
Copy link
Contributor

This PR is to address #24

This commit was made to create a script to eliminate the
time to copy and paste folders for new programming languages

Signed-off-by: darrylbalderas <[email protected]>
This commit was made to update the readme with
a comment on how to use the createFolder.sh shell script

Signed-off-by: darrylbalderas <[email protected]>
createFolders.sh Outdated
#!/bin/bash

PROJECT_NAME=data-and-algorithms
DIRECTORY=${PWD}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can avoid most of the boilerplate and validation by using

DIRECTORY="$(readlink -f -- "$(dirname -- "$0")")"

yes, all those quotes are needed. otherwise you'll break people who clone this repository in a path that has spaces!

createFolders.sh Outdated
cp -a ./Python/ ./$rootfileName/
rm ./$rootfileName/*/*/*.py
else
echo "Missing folder name: sh createFolders.sh <folder_name>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use the guard clause pattern

if [[ $# -lt 1 ]]; then
  echo "Usage: bash createFolders.sh <programming language>" > &2
  exit 1
fi
# The rest. You can now use $1 confidently.

the >&2 prints to stderr.

README.md Outdated

Feel free to add your solutions in your channel of your choice 😃

Run this script to create the folder structure for your favorite programming language
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use bash instead of sh. otherwise you lose a lot of functionality!

createFolders.sh Outdated
echo "Make sure you are in $PROJECT_NAME project"
exit
fi
rootfileName=$1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should always quote all your variables. otherwise if they contain a space, they will be expanded to unexpected stuff!

bash createFolders.sh "hello world"

would cause this line to expand to

rootfileName=hello world

which would try to invoke world with the rootfileName=hello environment variable.

also, if this is the programming language, maybe call it programmingLanguage.

createFolders.sh Outdated
exit
fi
rootfileName=$1
mkdir ./$rootfileName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you can do

mkdir "${DIRECTORY}/${rootfileName}"

you should always try to brace the variables to make it super clear where they end. otherwise things like $myvar_would confuse readers. is the intention to be a variable called myvar_ or is it $myvar followed by an underscore?

also, by using the rsync or find alternatives sugegsted below, this mkdir is unneeded.

createFolders.sh Outdated
fi
rootfileName=$1
mkdir ./$rootfileName
cp -a ./Python/ ./$rootfileName/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to use the long forms of flags: https://xkcd.com/1168/

createFolders.sh Outdated
rootfileName=$1
mkdir ./$rootfileName
cp -a ./Python/ ./$rootfileName/
rm ./$rootfileName/*/*/*.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's better to not copy the files in the first place:

rsync --archive --include='*/' --exclude='*' "${DIRECTORY}/Python/" "${DIRECTORY}/${programmingLanguage}"

source: https://stackoverflow.com/a/40586240/688337

or if you want to avoid rsync for whatever reason

(cd "${DIRECTORY}/Python/" && \
 find . -type d -depth -exec mkdir -p "${DIRECTORY}/${programmingLanguage}/{}" \;)

README.md Outdated

Feel free to add your solutions in your channel of your choice 😃

Run this script to create the folder structure for your favorite programming language
`sh createFolders.sh <Programming Language Name>`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny naming nit: in POSIX, these are called directories, not folders.

@darrylbalderas
Copy link
Contributor Author

Let me add these changes. Gracias amigo 👍

createFolders.sh Outdated
fi

programmingLanguage=$1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remember the double quotes or you'll get weird results if somebody decides to pass in spaces within an argument. https://google.github.io/styleguide/shell.xml?showone=Quoting#Quoting

Rule of thumb is: every single var should be double quoted AND braced (except for the bash builtin ones, so $1 and $# are fine. https://google.github.io/styleguide/shell.xml?showone=Variable_expansion#Variable_expansion

and in general read that whole style guide, it's great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll stay consistent and add the double quotes 💯

createFolders.sh Outdated

programmingLanguage=$1
mkdir "${DIRECTORY}/${programmingLanguage}"
rsync --archive --include='*/' --exclude='*' "${DIRECTORY}/Python/" "${DIRECTORY}/${programmingLanguage}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: break long lines for better readability.

Suggested change
rsync --archive --include='*/' --exclude='*' "${DIRECTORY}/Python/" "${DIRECTORY}/${programmingLanguage}"
rsync --archive --include='*/' --exclude='*' "${DIRECTORY}/Python/" \
"${DIRECTORY}/${programmingLanguage}"

createFolders.sh Outdated
fi

programmingLanguage=$1
mkdir "${DIRECTORY}/${programmingLanguage}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should be able to drop this now since rsync will create it. if it doesn't, it might be because i forgot to add a trailing / to the destination directory ^^;

createFolders.sh Outdated
@@ -0,0 +1,12 @@
#!/bin/bash

DIRECTORY="$(dirname "$(readlink "$0")")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that you do need to use the boilerplate i shared as-is to avoid some issues on some POSIX systems:

Suggested change
DIRECTORY="$(dirname "$(readlink "$0")")"
DIRECTORY="$(readlink --canonicalize -- "$(dirname -- "$0")")"

the --canonicalize flag removes any symlinks in the path, and the -- are needed because if some component of the path starts with -, the commands may interpret as a flag instead of a path.

Copy link
Contributor Author

@darrylbalderas darrylbalderas Feb 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does readlink have a canonicalize flag ? I didn't see that option in terminal manual file for readlink. I have a mac os machine and the -f does exist but it is for formatting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh. does it have an -f flag? i was trying to avoid the shorthand names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screen shot 2019-02-04 at 10 22 31 am

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just see the -f option for the stat command

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yuck, portable bash programming is hard.

seems like you'll have to live with some platforms being broken. or convert this into Python code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was going to be my first choice in using python but I was like well let's give bash a try 😉

Copy link
Contributor

@lhchavez lhchavez Feb 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just realized there's a better alternative:

Suggested change
DIRECTORY="$(dirname "$(readlink "$0")")"
DIRECTORY="$(git rev-parse --show-toplevel)"

this should work everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works like a charm 👍

createFolders.sh Outdated
DIRECTORY="$(dirname "$(readlink "$0")")"

if [[ $# -lt 1 ]]; then
echo "Usage: bash createFolders.sh <programming language>" >&2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about renaming this file to createDirectories for consistency with the POSIX terminology?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is fair to have it be consistent with POSIX terminology

Copy link
Contributor

@lhchavez lhchavez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/

README.md Outdated
@@ -1,19 +1,31 @@
# data-and-algorithms
Let's go over data structures and algorithms together!
We will use Cracking the Coding Interview as reference.
We will use [Cracking the Coding Interview Book](http://www.crackingthecodinginterview.com/) as reference.

Feel free to add your solutions in your channel of your choice 😃
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that you're editing this, can you change channel for programming language?

fi

programmingLanguage="$1"
rsync --archive --include='*/' --exclude='*' "${DIRECTORY}/Python/" \
Copy link
Contributor

@lhchavez lhchavez Feb 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i just forgot a crucial detail: git never stores empty directories! so you might want to create empty files afterwards:

find "${DIRECTORY}/${programmingLanguage}/" -mindepth 2 -maxdepth 2 \
  -exec touch -- "{}/empty.txt" \;
git add "${DIRECTORY}/${programmingLanguage}/"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's right 😢

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just got merge privileges back! do you want to fix this before it's merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me do that

Signed-off-by: darrylbalderas <[email protected]>
@lhchavez lhchavez merged commit 90396a3 into techqueria:master Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants