-
Notifications
You must be signed in to change notification settings - Fork 101
/
init.sh
executable file
·86 lines (71 loc) · 2.5 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
#
# init.sh
#
# Automate these steps to get yourself up and running with Codelab:
# * Create boilerplate for Codelab
# * Configure a nodemon watch command to rebuild your codelab on save
# - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + -
command_exists() {
# check if command exists and fail otherwise
command -v "$1" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Note: $1 Does not exist. Please install it if you would like to run the codelab locally"
fi
}
cd `dirname $0`
# validate that a codelab name was included as an argument
if [ "$#" -ne 1 ]; then
echo "USAGE: $0 <CODELAB_NAME>"
echo ""
exit 1
fi
# env variables
CODELAB_NAME=$1
AUTHOR_NAME=`git config user.name`
# local variables
codelab_markdown_filename="markdown/$CODELAB_NAME/$CODELAB_NAME.md"
codelab_package_json_filename="markdown/$CODELAB_NAME/package.json"
markdown_template="markdown/template/markdown.template"
package_json_template="markdown/template/package.json"
#in MacOS sed creates a backup file if zero length extension is not specefied e.g. ''
backup_md="$codelab_markdown_filename-e"
backup_package_json="markdown/$CODELAB_NAME/package.json-e"
# validate that markdown template and package.json exist
if [ ! -f "$markdown_template" ] || [ ! -f "$package_json_template" ]; then
msg "ERROR!"
echo "Could not find one of the following files:"
echo " - $markdown_template"
echo " - $package_json_template"
echo ""
exit 0
fi
# Create a new directory for the codelba
mkdir markdown/$CODELAB_NAME
cp -r markdown/template/* markdown/$CODELAB_NAME/
rm markdown/$CODELAB_NAME/markdown-iguide.template
# rename markdown template file
mv markdown/$CODELAB_NAME/markdown.template $codelab_markdown_filename
# replace placeholder codelab id in markdown template file with name provided by command line argument
sed -i \
-e "s/CODELAB_NAME.*/$CODELAB_NAME/g" \
$codelab_markdown_filename
# replace placeholder authorname with git username=
sed -i \
-e "s/AUTHOR_NAME.*/$AUTHOR_NAME/g" \
$codelab_markdown_filename
# replace placeholder codelab name in the watch command with name provided in command line argument
sed -i \
-e "s/CODELAB_NAME/$CODELAB_NAME/g" \
$codelab_package_json_filename
# Remove backup files generated by sed in MacOs
if [ -f "$backup_md" ]; then
rm $backup_md
fi
if [ -f "$backup_package_json" ]; then
rm $backup_package_json
fi
echo "Markdown file created! Find it at $PWD/markdown/$CODELAB_NAME"
command_exists claat
command_exists go
cd $PWD/markdown/$CODELAB_NAME