· 5 years ago · May 26, 2020, 07:08 PM
1[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
2
3[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
4
5[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
6
7# Colors for the prompt
8blue="\033[0;34m"
9white="\033[0;37m"
10green="\033[0;32m"
11red="\033[0;31m"
12
13# Brackets needed around non-printable characters in PS1
14ps1_blue='\['"$blue"'\]'
15ps1_green='\['"$green"'\]'
16ps1_white='\['"$white"'\]'
17
18export PATH="/usr/local/opt/mysql@5.6/bin:~/Library/Python/2.7/bin:$PATH"
19
20# I like tunning the colors of the prompt in the first place:
21export CLICOLOR='true'
22export LSCOLORS="gxfxcxdxbxCgCdabagacad"
23export EDITOR=vi
24
25# -------------------------------------------------------------------------------- #
26# -------------------------------- GITHUB FUNCTIONS ------------------------------ #
27# -------------------------------------------------------------------------------- #
28
29# Initialize the repository to mirror
30## Inputs: source_repository, target_repository
31# Example: create_mirror git@github.com:source-profile/source-repository.git git@github.com:target-profile/target-repository.git
32create_mirror() {
33 # Initializing the mirror.
34 git clone --mirror $1
35 cd `echo $1 | rev | cut -d '/' -f 1 | rev`
36 git remote set-url --push origin $2
37
38 # Updating the Mirror
39 git fetch -p origin
40 git show-ref | cut -d ' ' -f2 | grep 'pull' | xargs -L1 git update-ref -d # Delete PRs to let the push mirror work
41 git push --mirror
42}
43
44# Pushing changes to the mirror repository (Need to be inside the initialized repository)
45## Inputs: none
46# Example: update_mirror
47update_mirror() {
48 git fetch -p origin
49 git show-ref | cut -d ' ' -f2 | grep 'pull' | xargs -L1 git update-ref -d # Delete PRs to let the push mirror work
50 git push --mirror
51}
52
53
54# Git branch in good-looking prompt.
55parse_git_branch() {
56 gitstatus=`git status 2> /dev/null`
57 if [[ `echo $gitstatus | grep "Changes to be committed"` != "" ]]
58 then
59 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1***)/'
60 elif [[ `echo $gitstatus | grep "Changes not staged for commit"` != "" ]]
61 then
62 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1**)/'
63 elif [[ `echo $gitstatus | grep "Untracked"` != "" ]]
64 then
65 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1*)/'
66 elif [[ `echo $gitstatus | grep "nothing to commit"` != "" ]]
67 then
68 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
69 else
70 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1?)/'
71 fi
72}
73
74# Echo a non-printing color character depending on whether or not the current git branch is the master
75# Does NOT print the branch name
76# Use the parse_git_branch() function for that.
77parse_git_branch_color() {
78 br=$(parse_git_branch)
79 if [[ $br == "(master)" || $br == "(master*)" || $br == "(master**)" || $br == "(master***)" ]]; then
80 echo -e "${blue}"
81 else
82 echo -e "${green}"
83 fi
84}
85
86# Prompt with Git branch
87# Explanation of the weird lines: \u Username, \h Host, \w Path, tput setaf is the color definition
88export PS1='\[$(tput setaf 7)\]\u@\[$(tput setaf 2)\]\h:\[$(tput setaf 4)\]\w\[$(tput setaf 1)\]$(parse_git_branch)\[$(tput sgr0)\] $ '
89
90# Git autocompletion
91# Requires that you execute the following commented line:
92# curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
93test -f ~/.git-completion.bash && . $_
94export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"
95
96# history size
97export HISTFILESIZE=1000000
98export HISTSIZE=1000000
99
100
101# ------------------------------------------------------------------------------- #
102# -------------------------------- DRONE FUNCTIONS ------------------------------ #
103# ------------------------------------------------------------------------------- #
104
105# https://docs.drone.io/cli/install/
106export DRONE_SERVER=http://droneci.kaefer.com
107export DRONE_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXh0Ijoia2FlZmVyLWNpdC9kcG1zLXJhaWxzIiwidHlwZSI6Imhvb2sifQ.2bJ6E_bvahlj-o2vDi67QfG08YYxhFvrKVavbhC7IJM
108# drone info
109
110
111# ------------------------------------------------------------------------------- #
112# ------------------------------ Auxiliar Functions ----------------------------- #
113# ------------------------------------------------------------------------------- #
114
115pretty_print() {
116 echo -e "${green}################################################################################################"
117 echo -e "${green}##########################################${red} $1 ${green}##########################################"
118 echo -e "${green}################################################################################################${white}"
119}
120
121
122
123# ------------------------------------------------------------------------------- #
124# ------------------------- Victor's Automated Commands ------------------------- #
125# ------------------------------------------------------------------------------- #
126
127# Run Yugioh
128ygopro() {
129 open /Applications/YgoproLinks/Ygopro.app/Contents/MacOS/Ygopro
130}
131
132# Copy Parsec files from WEB library to MOBILE library.
133copy_parsec() {
134 # https://www.cyberciti.biz/faq/copy-command/
135 rm -rf /Users/victor/Desktop/mobile/parsecs-android/core/src/main/cpp/parser
136 cp -R /Users/victor/Desktop/equations-parser/parser /Users/victor/Desktop/mobile/parsecs-android/core/src/main/cpp
137}
138
139# Make and run a C++ or Kotlin file
140# run hello.cpp
141# run hello.kt
142run() {
143 if [[ $1 =~ ^.*\.cpp$ ]]; then
144 # Is a C++ file
145 name=${1%.cpp}
146 if [[ $((`ls | grep "$name.out" | wc -w`)) == 0 ]]; then
147 g++ $1 -o $name.out
148 ./$name.out
149 else
150 ./$name.out
151 fi
152 elif [[ $1 =~ ^.*\.kt$ ]]; then
153 # Is a Kotlin file
154 name=${1%.kt}
155 if [[ $((`ls | grep "$name.jar" | wc -w`)) == 0 ]]; then
156 kotlinc $1 -include-runtime -d $name.jar
157 java -jar $name.jar
158 else
159 java -jar $name.jar
160 fi
161 fi
162}
163
164# Make and run a C++ or Kotlin file, but re-create the binary file
165# run hello.cpp
166# run hello.kt
167run2() {
168 if [[ $1 =~ ^.*\.cpp$ ]]; then
169 # Is a C++ file
170 name=${1%.cpp}
171 g++ $1 -o $name.out
172 ./$name.out
173 elif [[ $1 =~ ^.*\.kt$ ]]; then
174 # Is a Kotlin file
175 name=${1%.kt}
176 kotlinc $1 -include-runtime -d $name.jar
177 java -jar $name.jar
178 fi
179}
180
181# Cmake, make and run the equations-parser
182parsec() {
183 cmake CMakeLists.txt;
184 make;
185 rlwrap ./example;
186}
187
188## Install all Mac dependencies.
189## You need to have brew installed
190# Usage: install_dependencies
191install_mac_dependencies() {
192 gem install rubocop
193
194 # video_to_gif() dependencies
195 brew install ffmpeg;
196 brew cask install x-quartz; #dependency for gifsicle, only required for mountain-lion and above
197 open /usr/local/Cellar/x-quartz/2.7.4/XQuartz.pkg; # runs the XQuartz installer
198 brew install gifsicle;
199
200 # The sed (a shell command) version update to include necessary new anchors
201 brew install gnu-sed
202 ln -s /usr/local/bin/gsed /usr/local/bin/sed
203
204 # hub - enhances the power to deal with git. It's possible to open PRs locally.
205 brew install hub
206
207 brew install rlwrap
208}
209
210# Download a m3u8 video, merging it's parts
211video() {
212 ffmpeg -i $1 -c copy -bsf:a aac_adtstoasc "video ($RANDOM).mp4"
213}
214
215
216
217# ------------------------------------------------------------------------------- #
218# --------------------------- Git's Automated Commands -------------------------- #
219# ------------------------------------------------------------------------------- #
220
221change_log() {
222 git log --graph --pretty=format:' %s ' --abbrev-commit --date=relative "$1..upstream/master"
223}
224
225# Creates the PR checks based on the git commits
226checks() {
227 commit_count=`git rev-list --count HEAD ^upstream/master`
228 commit_names=`git log --pretty='format:%Creset%s' --no-merges -"$commit_count"`
229 echo -e "Smoke Tests\n$commit_names" | tail -r | sed 's/^/- [x] /'
230}
231
232## Open github closed PRs page searching for the desired PR commit
233## Inputs: If one => the PR commit hash. If two => the file and the line.
234# Usage1: prs 813f014e2
235# Usage2: prs app/models/request.rb 48
236prs() {
237 if [[ $# == 0 ]]
238 then
239 echo "No arguments supplied"
240 elif [[ $# == 1 ]]
241 then
242 open "https://github.com/kaefer-cit/dpms-rails/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aclosed+$1"
243 open "chrome://inspect/#devices"
244 elif [[ $# == 2 ]]
245 then
246 commit_hash=`git blame -L $2,$2 $1 | cut -f 1 -d ' '`
247 open "https://github.com/kaefer-cit/dpms-rails/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aclosed+$commit_hash"
248 fi
249}
250
251# Same as prs(), but search in the old DPMS github repository, instead.
252prs2() {
253 if [[ $# == 0 ]]
254 then
255 echo "No arguments supplied"
256 elif [[ $# == 1 ]]
257 then
258 open "https://github.com/niltonvasques/pms-rails/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aclosed+$1"
259 elif [[ $# == 2 ]]
260 then
261 commit_hash=`git blame -L $2,$2 $1 | cut -f 1 -d ' '`
262 open "https://github.com/niltonvasques/pms-rails/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aclosed+$commit_hash"
263 fi
264}
265
266#### Use it inside a git versioned folder!
267## Clear all branches that are not the branch master
268# Usage: clear_branches
269clear_branches() {
270 git branch | grep -v 'master' | xargs git branch -D;
271
272 # Clear branches, except master and other ones
273 # git branch | egrep -v 'master|dpms6807_deny_now_performance_improvement|dpmsIWANTTOSEE' | xargs git branch -D;
274}
275
276# Fetch upstream on git and merge with upstream/master
277update1() {
278 git fetch upstream --prune; git checkout -- Gemfile.lock; git merge upstream/master
279}
280
281# Fetch everyone on git and merge with upstream/master
282update2() {
283 git fetch --all --prune; git checkout -- Gemfile.lock; git merge upstream/master
284}
285
286# Fix following git bug...
287# "Another git process seems to be running in this repository, e.g.""
288fix_git() {
289 rm -f .git/index.lock
290}
291
292
293# ------------------------------------------------------------------------------- #
294# -------------------------- REACT Automated Commands --------------------------- #
295# ------------------------------------------------------------------------------- #
296
297webpack() {
298 ./bin/webpack-dev-server
299}
300
301r_test_debugger() {
302 node --inspect-brk node_modules/.bin/jest --runInBand
303 # Open a chrome tab with the URL chrome://inspect and click in Open dedicated dev tool for node
304}
305
306r_test_report() {
307 chromium coverage/lcov-report/index.html
308}
309
310
311# ------------------------------------------------------------------------------- #
312# --------------------------- DPMS Automated Commands --------------------------- #
313# ------------------------------------------------------------------------------- #
314
315# Setup your environment variables!
316export DPMS_PATH=/Users/victor/Desktop/dpms-rails # Your DPMS main path
317export GOOGLE_TRANSLATE_API_KEY=AIzaSyCJ2GeOPxqQt3VALHOt9WA6jxxtt3fKXDg # Your google translate API key
318export MYSQL_USER=root # Your MySQL root
319export MYSQL_ROOT_PASSWORD=password # Your MySQL root password
320# export MYSQL_ROOT_PASSWORD=pms-rails-pwd
321export DATABASE_PATH=/Users/victor/Desktop/Bancos/release.sql # Your DPMS Database path
322export SIMPLECOV=locally # Necessary to generate the coverage report locally
323
324# Setup your aliases!
325alias edit='subl' # Change to edit='vim' if you want
326alias bashp='edit ~/.bash_profile'
327alias rspec='bundle exec rspec'
328
329# Opens DPMS path with your favorite editor
330work() {
331 edit $DPMS_PATH
332}
333
334# Run migrations and seeds on all servers
335migrations() {
336 rake db:migrate seed:migrate; RAILS_ENV=test rake db:drop db:drop_audit_db db:create db:create_audit_db db:migrate;
337}
338
339## Translate all languages from en.yml
340# Location: Inside dpms main folder
341# Usage: translate
342translate() {
343 i18n-tasks translate-missing --from en de, es, fr, nb, pt-BR; # Translate
344 sed -i "s/\b'\b/’/g" config/locales/fr.yml # Replace ' to ’ in french
345}
346
347translate2() {
348 i18n-tasks translate-missing --from en pt-BR;
349}
350
351## Clear all useless .orig files in whole project
352## Also clear all useless files and folders inside /coverage folder
353# Location: Inside dpms main folder
354# Usage: clear_files
355clear_files() {
356 find . -name *.orig -type f -delete;
357 rm -rf coverage/{*,.*};
358}
359
360## Open gems folder in your text editor
361# Usage: open_gems
362open_gems() {
363 edit $(echo $GEM_PATH | cut -d':' -f 1)/gems;
364}
365
366## Autofix the provided file by it's PATH.
367## This method dinamically discover if the file is a javascript or a ruby file to delegate it to the proper autofix function
368# Usage1: autofix app/models/asset.rb
369# Usage2: autofix app/assets/javascripts/application/screens/assets.js
370autofix() {
371 if [[ $1 =~ ^.*\.js$ ]]; then
372 echo 'Javascript file';
373 $DPMS_PATH/./node_modules/.bin/eslint $1 --fix;
374 elif [[ $1 =~ ^.*\.rb$ || $1 =~ ^.*\.rake$ ]]; then
375 echo 'Ruby file';
376 pretty_print 'RUBOCOP '
377 rubocop $1 --auto-correct;
378 pretty_print 'BRAKEMAN'
379 brakeman -q --only-files ${1#$DPMS_PATH};
380 pretty_print ' FLAY '
381 flay -v --diff $1;
382 else
383 echo 'Unknown file';
384 fi
385}
386
387# Conserta TUDOOO
388autofix_all() {
389 files=`git diff --name-only upstream/master`
390 while read -r line; do
391 autofix $line
392 done <<< "$files"
393}
394
395pronto_run() {
396 bundle exec pronto run -c upstream/master -r brakeman eslint_npm rubocop
397}
398
399#### Use it inside folder where the video is present!
400## Converts a video (.mov) to gif (.gif).
401## If no params is passed, the 'a.mov' video will be converted to 'a.gif' gif
402## If a param is passed, the 'param.mov' video will be converted to a 'param.gif' gif
403# Usage1: video_to_gif 'mymovie'
404# Usage2: video_to_gif
405video_to_gif() {
406 if [[ "${#1}" > 0 ]]; then
407 if [[ $((`ls | grep "$1.mov" | wc -m`)) > 0 ]]; then
408 ffmpeg -i "$1.mov" -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=10 > "$1.gif";
409 else
410 echo "There is no file named: $1.mov";
411 fi
412 else
413 if [[ $((`ls | grep 'a.mov' | wc -m`)) > 0 ]]; then
414 ffmpeg -i a.mov -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=10 > a.gif;
415 else
416 echo 'There is no file named: a.mov';
417 fi
418 fi
419 # Greetings to...
420 # https://gist.github.com/vitorleal/563771e821cef668eef5
421}
422
423# Create the MySQL admin user
424create_admin() {
425 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "FLUSH PRIVILEGES;
426 CREATE USER IF NOT EXISTS 'dpms_admin'@'%';
427 SET PASSWORD FOR 'dpms_admin'@'%' = PASSWORD('password');
428 FLUSH PRIVILEGES;"
429}
430
431#### GRANT admin priviledges to the dpms_admin user on the databases of a desired dump
432## It uses "discover_databases" which discovers the databases used in a dump and grant priviledges to them
433## Usage: grant_priviledges /Users/victor/Desktop/Bancos/production.sql
434grant_priviledges() {
435 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "FLUSH PRIVILEGES; REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'dpms_admin'@'%';"
436 databases=`discover_databases $1`
437 while read -r database; do
438 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "GRANT Select,Insert,Update,Delete,Create,Drop,Index,Alter,References,Lock Tables,Create View,Show View ON $database.* TO 'dpms_admin'@'%';"
439 done <<< "$databases"
440 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "FLUSH PRIVILEGES"
441}
442
443grant_all_priviledges() {
444 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "FLUSH PRIVILEGES; REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'dpms_admin'@'%';"
445 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "GRANT ALL PRIVILEGES ON *.* TO 'dpms_admin'@'%';"
446 mysql -u root -p$MYSQL_ROOT_PASSWORD -Bse "FLUSH PRIVILEGES;"
447}
448
449#### Discover the database(s) used(s) on the .sql file
450## Usage: discover_databases path/to/dump.sql
451discover_databases() {
452 ag -i -o '(?<=USE `)(\w*)(?=`;)' "$1" | sort -u
453}
454
455#### Use it inside dpms-rails main folder!
456## Drop the whole DPMS database and fullfill it with a new one
457## Also run migrations and seeds for both production and test environment
458## FIRST you need to setup your MYSQL_USER, MYSQL_ROOT_PASSWORD and DATABASE_PATH.
459# Usage: reset_database
460reset_database() {
461 # SHOW GRANTS FOR dpms_admin;
462 # sed -i "s/edimar\|marivaldo\|root/dpms_admin/g" marino_test.sql
463 rake db:drop db:drop_audit_db db:create db:create_audit_db;
464 # mysql -u $MYSQL_USER -p$MYSQL_ROOT_PASSWORD < "$DATABASE_PATH"
465 # mysql -u $MYSQL_USER -p$MYSQL_ROOT_PASSWORD -Bse "use pmsrails; SET autocommit=0; source $DATABASE_PATH; COMMIT;"
466
467 mysql -u $MYSQL_USER -p$MYSQL_ROOT_PASSWORD
468 use pmsrails; # test OR demo => Need to use other if we change the database
469 SET autocommit=0;
470 source /Users/victor/Desktop/Bancos/fernandos_bug.sql; # $MYSQL_ROOT_PASSWORD :'(
471 COMMIT;
472 exit;
473
474 rake db:migrate seed:migrate;
475 RAILS_ENV=test rake db:drop db:drop_audit_db db:create db:create_audit_db db:migrate;
476
477 # Consertar Erro do MySQL no Mac
478 # https://github.com/kaefer-cit/dpms-rails#you-should-stop-the-mysqld-server
479}
480
481replace_database() {
482 sed -i 's/pmsrails/test/g' release.sql # Alterar o banco de test para release
483 sed -i 's/pmsrailsdemo/demo/g' release.sql # Alterar o banco de demo para release
484 database.yml # Alterar na mão grande o valor de database para release
485}
486
487#### Use it inside dpms-rails main folder!
488## Remove views that cause bugs
489remove_views() {
490 rails c
491 ActiveRecord::Migration.drop_view :task_daily_progresses, revert_to_version: 2
492 ActiveRecord::Migration.drop_view :task_service_hours, revert_to_version: 1
493 ActiveRecord::Migration.drop_view :task_hours, revert_to_version: 1
494 ActiveRecord::Migration.drop_view :time_slot_service_hours, revert_to_version: 1
495 ActiveRecord::Migration.drop_view :time_slot_hours, revert_to_version: 1
496 exit
497}
498
499## Use it to discover the order of commits that should be entered from the MASTER to the RELEASE branch
500# before running it, you should create the "release_web.patch" file, which is a diff file, extracted by the meld program, which dictates which commits should go from the MASTER branch to the RELEASE branch
501discover_commits_order_for_release() {
502 # release_web.patch is the result of the merge between the MASTER and the RELEASE (Via meld)
503 git log --oneline | tac | nl > git_log_numbered # Save the number for each commit
504 grep '\- DPMS' release_web.patch | grep -o "DPMS.*" | xargs -I {} grep "{}" git_log_numbered | sort -h | awk '{ print $2 }' # Discover commit order
505}
506
507
508# ------------------------------------------------------------------------------- #
509# --------------------------------- LoL FUNCTIONS ------------------------------- #
510# ------------------------------------------------------------------------------- #
511
512# Open the LoL folder to create character items
513# https://lolmath.net/itemop.html
514lol() {
515 subl /Applications/League\ of\ Legends.app/Contents/LoL/Config
516}
517
518
519# ------------------------------------------------------------------------------- #
520# ------------------------------- Python FUNCTIONS ------------------------------ #
521# ------------------------------------------------------------------------------- #
522
523## Clear all useless __pycache__ files on the python project
524# Usage: clear_files
525clear_python() {
526 find . -type d -name "__pycache__" -exec rm -r {} +
527}