-
Notifications
You must be signed in to change notification settings - Fork 2
/
project.sh
176 lines (151 loc) · 5.2 KB
/
project.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bash
if [ -n "$ZSH_VERSION" ]; then
src=$(dirname "${(%):-%N}")/listbox/listbox.sh
elif [ -n "$BASH_VERSION" ]; then
src=$(dirname "${BASH_SOURCE[0]}")/listbox/listbox.sh
else
src=$(dirname "$0")/listbox/listbox.sh
fi
source "$src"
# Create npm package and repo for it on github
# usage: project test
# for private repo: project -p test
project() {
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
echo "create local repo, github repo(public or private) and initialize package"
echo "Usage: project [options]"
echo "Example:"
echo " project -p test"
echo "Options:"
echo " -h, --help help"
echo " -p, --private create private github repository"
echo " -f, --folder your projects folder(defaults to ~/projects)"
echo " -n, --no-init avoid initializing package"
return 0
;;
-p|--private)
local private=1
;;
-f|--folder)
local projects="$2"
shift 1
;;
-n|--no-init)
local noinit=1
;;
*)
local name="$1"
;;
esac
shift
done
if [ -n "$projects" ]; then
local projectFolder="$projects"
elif [ -n "$PROJECTS_HOME" ]; then
local projectFolder="$PROJECTS_HOME"
else
local projectFolder=~/projects
fi
# go to last project
if [[ $name == "-" ]]; then
if [[ -n "$OLDPROJECTPWD" ]]; then
local oldProject="$OLDPROJECTPWD"
if [[ $PWD =~ "^$projectFolder" && $PWD != "$projectFolder" ]]; then
export OLDPROJECTPWD="$PWD"
fi
cd "$oldProject"
echo -e "\n switched to project folder: $oldProject"
else
cd $projectFolder
fi
return 0
fi
if [ -z "$name" ]; then
if [[ -f $projectFolder ]]; then
echo -e "\n you already have a file called «$projectFolder»\n please specify PROJECTS_HOME variable\n see https://github.com/gko/project#projects-path\n"
elif [[ ! -d $projectFolder ]]; then
echo -e "\n creating and switching to «$projectFolder»\n"
mkdir -p $projectFolder
cd $projectFolder
else
echo -e "\n switching to «$projectFolder»\n"
cd $projectFolder
fi
return 0
fi
# only for zsh since read is a bit of a mess on bash
if [[ ! -d $projectFolder/$name && -n "$ZSH_VERSION" ]]; then
echo -e ""
read "? «$projectFolder/$name» doesn't exist. Do you want to create it? (y/n) " answer </dev/tty
# y or just Enter
if ! [[ "$answer" =~ [yY] || "$answer" = "" ]]; then
return 0;
fi
fi
echo -e "\n switched to project folder: $projectFolder/$name"
mkdir -p "$projectFolder/$name"
if [[ $PWD =~ "^$projectFolder" && $PWD != "$projectFolder" ]]; then
export OLDPROJECTPWD="$PWD"
fi
cd "$projectFolder/$name"
if [[ -d ./.git || -f ./.git ]]; then
return 0
fi
echo -e "\n initializing repository..."
git init .
if [ -z $noinit ]; then
local package=$(listbox -t "Choose package (Ctrl + C to exit):" -o "npm|cargo|gem|pip|none" | tee /dev/tty | tail -n 1)
case "$package" in
npm*)
if npm -v > /dev/null 2>&1; then
npm init
fi
;;
cargo*)
if cargo -V > /dev/null 2>&1; then
cargo init
fi
;;
gem*)
if gem -v > /dev/null 2>&1; then
gem install gem-path bundler
local bundler=$(gem path bundler)
local output=$(cd ../ && "$bundler"/exe/bundle gem "$name")
echo "$output"
fi
;;
pip*)
git clone https://github.com/pypa/sampleproject.git
rm -rf ./sampleproject/.git
mv ./sampleproject/* ./sampleproject/.git* ./
rm -rf ./sampleproject
;;
*)
esac
fi
if ! gh --version > /dev/null 2>&1; then
echo -e "\n gh is not installed. Please refer to https://cli.github.com/"
return 1
fi
if [[ $private -eq 1 ]]; then
echo -e "\n creating private github repository..."
gh repo create --private $name --source=. --remote=origin
else
local repo=$(listbox -t "Create github repo (Ctrl + C to exit):" -o "private|public" | tee /dev/tty | tail -n 1)
case "$repo" in
private*)
echo -e "\n creating private github repository..."
gh repo create --private $name --source=. --remote=origin
;;
public*)
echo -e "\n creating public github repository..."
gh repo create --public $name --source=. --remote=origin
;;
*)
esac
fi
}