-
Notifications
You must be signed in to change notification settings - Fork 0
/
.projux_cmds
executable file
·3922 lines (3649 loc) · 131 KB
/
.projux_cmds
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
# Copyright 2012-2018 Mike Dreves
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at:
#
# http://opensource.org/licenses/eclipse-1.0.php
#
# By using this software in any fashion, you are agreeing to be bound
# by the terms of this license. You must not remove this notice, or any
# other, from this software. Unless required by applicable law or agreed
# to in writing, software distributed under the License is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied.
###############################################################################
################################################################################
# Projux command (see help and examples for more info).
#
# REQUIREMENTS: project entries have been added to ~/.projects
#
# Project Settings Subcommands
# ls
# settings [<project>]
# bin
# flags
# var {get|set|update|clear} {:test|:run} [+-]<test_targets|run_flags>
#
# Remote Project Management Subcommands
# sync [<project>]
# ssh [<project>]
# sftp [<project>]
#
# Project Session Management Subcommmands:"
# sessions
# attach <project> [<instance>]
# detach [<project>] [<instance>]
# kill [<project>] [<instance>]
# clear
# reload
# win [<win>|:default|:delete [<win>]|:new <win>]
# pane [<pane>|:delete [<pane>]|:split|:vsplit]
#
# Project File and Directory Management Subcommmands:
# cd {:bin|:gen [#]|:project|:src [#]|:test [#]}
# pushd {:bin|:gen [#]|:project|:src [#]|:test [#]}
#
# Project Tools Subcommmands:
# format [-p <project>] [<targets>]
# lint [-p <project>] [<targets>]
# build [-p <project>] [<targets>]
# test [-p <project>] [<targets>]
# coverage [-p <project>] [<targets>]
# run [-p <project>] [<flags>]
# clean [-p <project>]
# package [-p <project>]
# deploy [-p <project>]
# gendocs [-p <project>]
# search [-p <project>] <tags>
# sanity [-p <project>]
# share [-p <project>] [<prog>]
# backup [-p <project>]
# vimserver [-p <project>]
# sbt
#
# Project Output Subcommands:
# errors [-p <project>] {:lint|:build|:test|:coverage|<file>}
# url [-p <project>] {:build|:test|:coverage|:bug|:review}
# goto [-p <projec>] {:build|:test|:coverage|:bug|:review}
# cat [:lint|:build|:test|:coverage]
# vi [:lint|:build|:test|:coverage]
################################################################################
function projux() {
shopt -q nocasematch
local orig_nocasematch=$?
shopt -s nocasematch
##############################################################################
# Help
#
if [[ "${1}" == "help" ]]; then
__projux_help
return $?
elif [[ "${1}" == "examples" ]]; then
__projux_examples ${*:2}
return $?
fi
##############################################################################
##############################################################################
# These commands don't need the active session info
#
if [[ "${1}" == "backup" ]]; then
__projux_backup ${*:2}
return $?
elif [[ "${1}" == "bin" ]]; then
__projux_bin_path ${*:2}
return $?
elif [[ "${1}" == "build" ]]; then
__projux_build ${*:2}
return $?
elif [[ "${1}" == "cat" ]]; then
__projux_cat ${*:2}
return $?
elif [[ "${1}" == "cd" ]]; then
__projux_cd ${*:2}
return $?
elif [[ "${1}" == "clear" ]]; then
__projux_project_clear "${PROJECT_NAME}"
return $?
elif [[ "${1}" == "clean" ]]; then
__projux_clean ${*:2}
return $?
elif [[ "${1}" == "coverage" ]]; then
__projux_coverage ${*:2}
return $?
elif [[ "${1}" == "deploy" ]]; then
__projux_deploy ${*:2}
return $?
elif [[ "${1}" == "errors" ]]; then
__projux_errors ${*:2}
return $?
elif [[ "${1}" == "flags" ]]; then
__projux_bin_flags ${*:2}
return $?
elif [[ "${1}" == "format" ]]; then
__projux_format ${*:2}
return $?
elif [[ "${1}" == "gendocs" ]]; then
__projux_gendocs ${*:2}
return $?
elif [[ "${1}" == "lint" ]]; then
__projux_lint ${*:2}
return $?
elif [[ "${1}" == "ls" ]]; then
__projux_project_ls ${*:2}
return $?
elif [[ "${1}" == "package" ]]; then
__projux_package ${*:2}
return $?
elif [[ "${1}" == "pane" ]]; then
__projux_pane ${*:2}
return $?
elif [[ "${1}" == "pushd" ]]; then
__projux_pushd ${*:2}
return $?
elif [[ "${1}" == "reload" ]]; then
__projux_project_init "${PROJECT_NAME}"
return $?
elif [[ "${1}" == "run" ]]; then
__projux_run ${*:2}
return $?
elif [[ "${1}" == "sanity" ]]; then
__projux_sanity ${*:2}
return $?
elif [[ "${1}" == "sbt" ]]; then
__projux_sbt ${*:2}
return $?
elif [[ "${1}" == "search" ]]; then
__projux_search ${*:2}
return $?
elif [[ "${1}" == "sessions" ]]; then
tmux ls -F"#{session_name}"
return $?
elif [[ "${1}" == "sftp" ]]; then
__projux_sftp ${*:2}
return $?
elif [[ "${1}" == "share" ]]; then
__projux_share ${*:2}
return $?
elif [[ "${1}" == "sync" ]]; then
__projux_sync ${*:2}
return $?
elif [[ "${1}" == "goto" ]]; then
__projux_goto ${*:2}
return $?
elif [[ "${1}" == "ssh" ]]; then
__projux_ssh ${*:2}
return $?
elif [[ "${1}" == "test" ]]; then
__projux_test ${*:2}
return $?
elif [[ "${1}" == "url" ]]; then
__projux_url ${*:2}
return $?
elif [[ "${1}" == "var" ]]; then
__projux_var ${*:2}
return $?
elif [[ "${1}" == "vi" ]]; then
__projux_vi ${*:2}
return $?
elif [[ "${1}" == "vimserver" ]]; then
__projux_vimserver ${*:2}
return $?
elif [[ "${1}" == "win" ]]; then
__projux_win ${*:2}
return $?
fi
##############################################################################
# Gets session and project information
local cur_session=""
if [[ -n "${TMUX}" ]]; then
cur_session=$(tmux display-message -p "#S" 2> /dev/null)
fi
if [[ -z "${1}" ]]; then
if [[ -n "${cur_session}" ]]; then
echo "${cur_session} (attached)"
else
echo "Not attached"
fi
return $?
fi
local project_name="${PROJECT_NAME-$(__projux_default_project)}"
local instance=""
if [[ "${1}" == "attach" || "${1}" == "backup" || "${1}" == "detach" || \
"${1}" == "kill" || "${1}" == "settings" ]]; then
if [[ -n "${2}" ]]; then
if [[ "${2}" =~ ^[0-9]+$ ]]; then
instance="${2}" # e.g. xxx 2
else
project_name="${2}" # e.g. xxx foo
if [[ -n "${3}" ]]; then
instance="${3}" # e.g. xxx foo 2
fi
fi
elif [[ "${cur_session}" =~ _[0-9]+$ ]]; then
instance="${cur_session:${#cur_session}-1}" # default project/instance
fi
# This feature is no longer supported
#
# Previously, we supported 'projux <project>' as shortcut for
# 'projux attach <project>', but it is better to be explicit and avoid
# namespace collisions
# elif [[ -n "${1}" ]]; then
# if [[ "${1}" =~ _[0-9]+$ ]]; then # foo_2
# project_name="${1:0:${#1}-2}" # foo
# instance="${1:${#1}-1}" # 2
# else
# project_name="${1}" # foo
# if [[ "${2}" =~ ^[0-9]+$ ]]; then
# instance="${2}" # e.g. foo 2
# fi
# fi
fi
# Validate the project exists
if ! __projux_project_exists "${project_name}"; then
local msg
msg="Project entry for '${project_name}' not found in ~/.projects"
echo "$(tput setaf 1)${msg}$(tput sgr0)"
if [[ $orig_nocasematch -eq 1 ]]; then shopt -u nocasematch; fi
return 1
fi
# Session id is used by many commands, save it in var
if [[ -z "${instance}" || "${instance}" == "1" ]]; then
local session="${project_name}"
else
local session="${project_name}_${instance}"
fi
##############################################################################
# These commands require project name
#
if [[ "${1}" == "attach" ]]; then
# Connect to project (this handles cases where we are already in project)
# NOTE: Always set project vars before cmd (may be project specific host)
env_vars=$(__projux_project_vars "${project_name}")
eval "${env_vars} __projux_attach ${project_name} ${instance}"
elif [[ "${1}" == "detach" ]]; then
if [[ -z "${2}" ]]; then
tmux detach # no session specified, only detach this client
else
tmux detach -s "${session}" # detach all clients attach to passed session
fi
elif [[ "${1}" == "kill" ]]; then
if [[ ! "${2}" =~ [0-9] && ! "${3}" =~ [0-9] ]]; then
# Kill sub-sessions too
for s in `tmux ls -F"#{session_name}" | grep ${project_name}`; do
# Don't kill cur session or will lose session before sub-sessions killed
if [[ "${cur_session}" != "${s}" ]]; then
tmux kill-session -t "${s}"
fi
done
fi
tmux kill-session -t "${session}" &> /dev/null
elif [[ "${1}" == "settings" ]]; then
__projux_project_settings "${project_name}"
# This feature is no longer supported
#
# Previously, we supported running outside of tmux or without using the
# attach command. This is deprecated.
# else
# if [[ -z "${cur_session}" ]]; then
# # This block allows running outside of tmux
# if [[ -n "${instance}" ]]; then
# local msg="ERROR: Must be attached to use multiple project instances"
# echo "$(tput setaf 1)${msg}$(tput sgr0)"
# return 1
# else
# # Not attached, just init proj
# __projux_project_init "${project_name}"
# fi
# else
# env_vars=$(__projux_project_vars "${project_name}" 1)
# eval "${env_vars} __projux_attach ${project_name} ${instance}"
# fi
else
echo "$(tput setaf 1)ERROR: unknown projux command '${1}'$(tput sgr0)"
return 1
fi
##############################################################################
if [[ $orig_nocasematch -eq 1 ]]; then shopt -u nocasematch; fi
return 0
}
################################################################################
# Help.
################################################################################
function __projux_help() {
echo ""
echo "Syntax:"
echo " projux <subcommand>"
echo ""
echo "Project Settings Subcommands:"
echo " ls - list projects"
echo " settings [<project>] - show project settings"
echo " bin - show full path to run binary"
echo " flags - show flags for run binary"
echo " var get {:test [+-]<test_targets>|:run [+/-]<run_flags>}"
echo " var set {:test [+-]<test_targets>|:run [+/-]<run_flags>}"
echo " var update {:test [+-]<test_targets>|:run [+/-]<run_flags>}"
echo " var clear {:test [+-]<test_targets>|:run [+/-]<run_flags>}"
echo ""
echo "Remote Project Management Subcommands:"
echo " sync [<project>] - sync project files"
echo " ssh [<project>] - ssh to project server"
echo " sftp [<project>] - sftp to project server"
echo ""
echo "Project Session Management Subcommmands:"
echo " sessions - list attached sessions"
echo " attach <project> [<instance>] - attach to projux in tmux"
echo " detach [<project>] [<instance>] - detach from tmux"
echo " kill [<project>] [<instance>] - kill tmux"
echo " clear - clears project settings"
echo " reload - reloads project settings"
echo " win [<win>|:default|:delete <win>|:new <win>] - tmux win management"
echo " pane [<pane>|:delete [<pane>]|:split|:vsplit] - tmux pane management"
echo ""
echo "Project File and Directory Management Subcommands:"
echo " cd {:bin|:gen [#]|:project|:src [#]|:test [#]}"
echo " pushd {:bin|:gen [#]|:project|:src [#]|:test [#]}"
echo ""
echo "Project Tools Subcommmands: <cmd> [-p <project>] <opts>"
echo " format [<targets>] - formats targets"
echo " lint [<targets>] - lints targets"
echo " build [<targets>] - build targets"
echo " test [<targets>] - runs test targets"
echo " coverage [<targets>] - runs code coverage"
echo " run [<flags>] - runs program"
echo " clean - clears project tmp files"
echo " package - packages project output"
echo " deploy - deploys project output"
echo " gendocs - generates project docs"
echo " search <tags> - searches code for tags"
echo " sanity - runs sanity check"
echo " share [<prog>] - copy prog to ~/Public"
echo " backup - backup files not in git"
echo " vimserver - run server mode vim"
echo " sbt - run scala sbt"
echo ""
echo "Project Output Subcommmands:"
echo " errors {:lint|:build|:test|:coverage|<file>}"
echo " url {:build|:test|:coverage|:bug|:review}"
echo " goto {:build|:test|:coverage|:bug|:review}"
echo " cat [:lint|:build|:test|:coverage]"
echo " vi [:lint|:build|:test|:coverage]"
echo ""
echo "For examples run: 'projux examples'"
echo ""
}
################################################################################
# Examples.
################################################################################
function __projux_examples() {
echo ""
case "${1}" in
"var")
echo '# Echo $PROJECT_TEST_TARGETS'
echo "projux var get :test"
echo ""
echo '# Set $PROJECT_TEST_TARGETS'
echo "projux var set :test foo_test ../common/bar_test"
echo ""
echo '# Update $PROJECT_TEST_TARGETS'
echo "projux var update :test +foo_test -../common/bar_test"
echo ""
echo '# Clear $PROJECT_TEST_TARGETS'
echo "projux var clear :test"
echo ""
echo '# Echo $PROJECT_RUN_CMDS'
echo "projux var get :run"
echo ""
echo '# Echo $PROJECT_RUN_CMDS command labeled with xxx::'
echo "projux var get :run xxx"
echo ""
echo '# Echo command labeled with xxx:: and add bar to my_flag'
echo "projux var get :run xxx -my_flag=+bar"
echo ""
echo '# Set $PROJECT_RUN_CMDS'
echo "projux var set :run xxx::prog -someflag=val yyy::prog2 -other"
echo ""
echo '# Update $PROJECT_RUN_CMDS'
echo "projux var update :run xxx::prog -someflag=+some_value,-other_value"
echo ""
echo '# Clear $PROJECT_RUN_CMDS'
echo "projux var clear :run"
;;
"format")
echo "Format:"
echo "-------------------------------------------------------------------"
echo "projux format # format current dir"
echo "projux format . # as above"
echo "projux format ... # format recursively"
echo "projux format foo.cc # format specific file"
echo "projux format :project # format project src/test dirs"
echo "cat foo | FILE_TYPE=cc projux format # format stdin"
;;
"lint")
echo "Lint:"
echo "-------------------------------------------------------------------"
echo "projux lint # lint current dir"
echo "projux lint . # as above"
echo "projux lint ... # lint recursively"
echo "projux lint foo.cc # lint specific file"
echo "projux lint :project # lint project src/test dirs"
echo "cat foo | FILE_TYPE=cc projux lint # lint stdin"
;;
"build")
echo "Build:"
echo "-------------------------------------------------------------------"
echo "projux build # default build"
echo "projux build . # build current dir"
echo "projux build ... # build recursively"
echo "projux build foo.scala # build specific file"
echo "projux build :project # build project src/test dirs"
echo "cat foo | FILE_TYPE=cc projux build # build stdin"
;;
"test")
echo "Test:"
echo "-------------------------------------------------------------------"
echo "projux test foo_class_test # test foo_class_test"
echo "projux test foo_class_test.cc # as above (auto removes .cc)"
echo "projux test # default test"
echo "projux test . # test current dir"
echo "projux test ... # test recursively"
echo "projux test ... # as above"
echo "projux test :project # files in project test dir"
;;
"coverage")
echo "Coverage:"
echo "-------------------------------------------------------------------"
echo "projux coverage foo_class_test # coverage of foo_class_test"
echo "projux coverage foo_class_test.cc # as above (auto removes .cc)"
echo "projux coverage # coverage of current dir"
echo "projux coverage . # as above"
echo "projux coverage ... # coverage recursively"
echo "projux coverage :project # files in project test dir"
;;
"run")
echo "Run:"
echo "-------------------------------------------------------------------"
echo "projux run # run 1st entry in ${PROJECT_RUN_CMDS}"
echo "projux run prg # run 'prg' entry in ${PROJECT_RUN_CMDS}"
echo "projux run prg -foo=+x,+y # add 'bar' and 'baz' to -foo flag"
echo "projux run prg -foo=-bar # remove 'bar' from -foo flag"
echo "projux run prg -foo=bar # run with -foo flag set to bar"
echo "projux run -shared prg # run copy of prg binary in ~/Public"
;;
"errors")
echo "Errors:"
echo "-------------------------------------------------------------------"
echo "projux errors :lint # display errors from last call to lint"
echo "projux errors :build # display errors from last call to build"
echo "projux errors :test # display errors from last call to test"
echo "projux errors :coverage # display errors from last coverage"
echo "projux errors foo.py # display current foo.py errors"
;;
"url")
echo "URL:"
echo "-------------------------------------------------------------------"
echo "projux url :review # display default code review url"
echo "projux url :review xxx # display code review url for xxx"
echo "projux url :bug # display default bug url for project"
echo "projux url :bug xxx # display bug url for xxx"
echo "projux url :build # display url for last build"
echo "projux url :test # display url for last test"
echo "projux url :coverage # display url for last coverage"
;;
"goto")
echo "GOTO:"
echo "-------------------------------------------------------------------"
echo "projux goto :review # open default code review url"
echo "projux goto :review xxx # open code review url for xxx"
echo "projux goto :bug # open default bug url for project"
echo "projux goto :bug xxx # open bug url for xxx"
echo "projux goto :build # open url for last build"
echo "projux goto :test # open url for last test"
echo "projux goto :coverage # open url for last coverage"
;;
*)
echo "Project Settings:"
echo "-------------------------------------------------------------------"
echo "projux # show current project"
echo "projux ls # show all known projects"
echo "projux settings # show project env var settings"
echo "projux settings foo # show project foo's env var settings"
echo "projux bin foo # print path to project foo's output binary"
echo "projux flags # pretty print flags for all commands"
echo "projux flags bar # pretty print flags for bar command"
echo ""
echo "Remote Project Management:"
echo "-------------------------------------------------------------------"
echo "projux sync # syncs local files with default remote host"
echo "projux sync foo # syncs files with project foo's remote host"
echo "projux ssh # SSH to default remote host for projects"
echo "projux ssh foo # SSH to remote host for project foo"
echo "projux sftp # SFTP to default remote host for projects"
echo "projux sftp foo # SFTP to remote host for project foo"
echo ""
echo "Project Session Management:"
echo "-------------------------------------------------------------------"
echo "projux sessions # show all active project sessions"
echo "projux attach foo 1 # attach/switch to session 1 of foo project"
echo "projux attach foo 3 # attach/switch to session 3 of foo project"
echo "projux detach # detach cur client from cur project"
echo "projux detach foo # detach all clients attached to foo project"
echo "projux detach foo 2 # detach all clients on 2nd session of foo"
echo "projux kill # kill all of current project's tmux session"
echo "projux kill foo # kill all of foo project's tmux sessions"
echo "projux kill foo 1 # kill main session of project foo"
echo "projux kill foo 2 # kill 2nd session of project foo"
echo "projux clear # clear current project sessions vars"
echo "projux reload # re-initialize project sessions vars"
echo "projux win # list windows"
echo "projux win bash # switch to bash window"
echo "projux win repl # switch to repl window"
echo "projux win :new baz # create new window named baz"
echo "projux win :default # print default win for session (foo_2 => 2)"
echo "projux pane # list panes"
echo "projux pane 3 # switch to pane 3 (ctrl-a o)"
echo "projux pane %5 # switch to pane with global id 5"
echo "projux pane :split # create new horizontal pane"
echo "projux pane :vsplit # create new vertial pane"
echo ""
echo "Project File and Directory Management:"
echo "-------------------------------------------------------------------"
echo "projux cd :project # cd to root project dir"
echo "projux cd :src # cd to $PROJECT_SRC_DIR/$PROJECT_PKGS[0]"
echo "projux cd :src 2 # cd to $PROJECT_SRC_DIR/$PROJECT_PKGS[1]"
echo "projux cd :test # cd to $PROJECT_TEST_DIR/$PROJECT_PKGS[0]"
echo "projux cd :test 3 # cd to $PROJECT_TEST_DIR/$PROJECT_PKGS[2]"
echo "projux cd :bin # cd to $PROJECT_BIN_DIR"
echo "projux cd :gen # cd to $PROJECT_GEN_DIR/$PROJECT_PKGS[0]"
echo "projux cd :gen 2 # cd to $PROJECT_GEN_DIR/$PROJECT_PKGS[1]"
echo "projux pushd :src 2 # pushd to $PROJECT_SRC_DIR/$PROJECT_PKGS[1]"
echo ""
echo "Project Tools:"
echo "-------------------------------------------------------------------"
echo "projux format # recursively format files"
echo "projux lint # recursively lint files"
echo "projux build # default build"
echo "projux test # default test"
echo "projux coverage # recursively run coverage on files"
echo "projux run # runs default project run command"
echo "projux clean # clears project temp (build, etc) files"
echo "projux package # runs project package command"
echo "projux deploy # runs project deploy command"
echo "projux gendocs # runs project gendocs command"
echo "projux search foo # search for 'foo' in project"
echo "projux sanity # run project sanity command"
echo "projux share prg # copies prg binary to $PROJECT_SHARE_DIR"
echo "projux backup # backup changed files to $PROJECT_BACKUP_DIR"
echo ""
echo "Project Output:"
echo "-------------------------------------------------------------------"
echo "projux errors :lint # prints errors from last lint run"
echo "projux url :build # prints summary URL associated with last build"
echo "projux goto :build # opens URL associated with last build"
echo "projux cat :lint # cat output of last lint"
echo "projux vi :build # vi output of last build"
echo
echo "More detailed examples:"
echo "-------------------------------------------------------------------"
echo "projux examples var"
echo "projux examples format"
echo "projux examples lint"
echo "projux examples build"
echo "projux examples test"
echo "projux examples coverage"
echo "projux examples run"
echo "projux examples errors"
echo "projux examples url"
echo "projux examples goto"
;;
esac
echo ""
}
################################################################################
# ** Project Settings **
################################################################################
################################################################################
# Lists projects.
################################################################################
function __projux_project_ls() {
while read line; do
if [[ "${line}" == "PROJECT_NAME"* ]]; then
echo "${line:13}"
fi
done < ~/.projects
}
################################################################################
# Initializes project.
#
# Args:
# $1: Project name
################################################################################
function __projux_project_init() {
if [[ -z "${1}" ]]; then
return
fi
local project_name="${1}"
# Reset the defaults (no values are set unless they are configured in
# ~/.projects for this project, otherwise the defaults will take effect)
__projux_project_clear ${PROJECT_NAME}
local found=0
while read line; do
if [[ -z "${line}" || "${line}" == "#"* ]]; then
continue
fi
if [[ "${line}" == "PROJECT_NAME=${project_name}" ]]; then
local found=1
elif [[ ${found} -eq 1 && "${line}" == "PROJECT_NAME="* ]]; then
break
fi
if [[ ${found} -eq 1 ]]; then
if [[ "${line}" == alias* ]]; then
# 'alias foo=bar' entries
eval "${line}" &> /dev/null
else
# variable definitions
local var=${line%%=*}
local value=${line#*=}
# make sure quoted
if [[ "${value:0:1}" == '"' ]]; then
eval "export ${line}" &> /dev/null
else
eval "export ${var}=\"${value}\"" &> /dev/null
fi
fi
fi
done < ~/.projects
}
################################################################################
# Shows project settings.
#
# Args:
# $1: Project name
################################################################################
function __projux_project_settings() {
local project_name="${1}"
local found=0
local padding=" "
local project_vars=$(__projux_project_vars ${project_name})
local vars_echoed=()
while read line; do
if [[ "${line}" == "PROJECT_NAME=${project_name}" ]]; then
local found=1
elif [[ ${found} -eq 1 && "${line}" == "PROJECT_NAME="* ]]; then
break
fi
if [[ ${found} -eq 1 && ! ${line} == "#"* && ! "${line}" == "" ]]; then
local var="${line%%=*}"
local val="${line#*=}"
# Note: the ${project_vars} variable stores a space separated list of
# var=value env settings for the chosen project. By passing this
# list prior to calling echo we can resolve variables defined in
# terms of other variables on a project specific basis (e.g. without
# altering the current projects settings). However, we need to do a
# double eval and put the expand of value of $val in single quotes or
# else the current project values will override.
if [[ ${val} == *"$"* && ! ${var} == alias* ]]; then
val=$(eval "${project_vars} eval echo '""${val}""'")
fi
echo "${var}${padding:${#var}} : ${val}"
vars_echoed+=("${var}")
fi
done < ~/.projects
# Add missing defaults (only important ones)
local defaults=("PROJECT_HOST" "PROJECT_WINDOWS")
for def in "${defaults[@]}"; do
found=0
for var in "${vars_echoed[@]}"; do
if [[ "${def}" == "${var}" ]]; then
found=1
break
fi
done
if [[ ${found} -eq 0 ]]; then
local val_name='$DEFAULT_'"${def}"
local val=$(eval "echo $val_name")
echo "${def}${padding:${#def}} : ${val}"
fi
done
}
################################################################################
# Clears all project vars.
#
# Args:
# $1: Project name
################################################################################
function __projux_project_clear() {
local project_name="${1}"
# Clear project specific vars
while read line; do
if [[ -z "${line}" || "${line}" == "#"* ]]; then
continue
fi
if [[ "${line}" == "PROJECT_NAME=${project_name}" ]]; then
local found=1
elif [[ ${found} -eq 1 && "${line}" == "PROJECT_NAME="* ]]; then
break
fi
if [[ ${found} -eq 1 ]]; then
if [[ "${line}" == alias* ]]; then
# 'unalias foo=bar' entries
eval "un${line%%=*}" &> /dev/null
else
# variable definitions
eval "unset ${line%%=*}" &> /dev/null
fi
fi
done < ~/.projects
unset PROJECT_LAST_OUT_DIR
# Leave at least the PROJECT_NAME var set so we can reload
export PROJECT_NAME="${project_name}"
}
################################################################################
# Returns current project name.
################################################################################
function __projux_cur_project() {
local tmux_session=$(tmux display-message -p "#S" 2> /dev/null)
if [[ -n "${tmux_session}" ]]; then
if (__projux_project_ls) | grep -q "${tmux_session}"; then
echo "${tmux_session}"
fi
fi
}
################################################################################
# Returns default project name.
#
# The default tmux session is defined in this order:
# * The current tmux session name matches a project in ~/.projects
# * The first entry in ~/.projects
# * "default"
################################################################################
function __projux_default_project() {
local tmux_session=$(tmux display-message -p "#S" 2> /dev/null)
if [[ -n "${tmux_session}" ]]; then
if (__projux_project_ls) | grep -q "${tmux_session}"; then
echo "${tmux_session}"
return
fi
fi
# head -1 would be easier, but can get broken pipe errors
local first=$(read -r line < <(__projux_project_ls); echo $line)
if [[ -n "$first" ]]; then
echo "${first}"
else
echo "default"
fi
}
################################################################################
# Returns $? == 0 if project exists.
#
# Args:
# $1: Project name.
################################################################################
function __projux_project_exists() {
shopt -q nocasematch
local orig_nocasematch=$?
shopt -s nocasematch
while read line; do
if [[ "${line}" == "PROJECT_NAME"* && "${line:13}" == "${1}" ]]; then
if [[ $orig_nocasematch -eq 1 ]]; then shopt -u nocasematch; fi
return 0
fi
done < ~/.projects
if [[ $orig_nocasematch -eq 1 ]]; then shopt -u nocasematch; fi
return 1
}
################################################################################
# Returns list of project vars (A=B C=D etc).
#
# Args:
# $1: Project name
# $2 (optional): 1 to not expand paths
################################################################################
function __projux_project_vars() {
local project_name="${1}"
local found=0
local vars=()
# Reset the defaults (these will be overridden if values are configured in
# ~/.projects for this project, otherwise these defaults will take effect)
vars+=("PROJECT_HOST=\"${DEFAULT_PROJECT_HOST}\"")
vars+=("PROJECT_WINDOWS=\"${DEFAULT_PROJECT_WINDOWS}\"")
vars+=("PROJECT_SHARE_DIR=\"${DEFAULT_PROJECT_SHARE_DIR}/${project_name}\"")
vars+=("PROJECT_BACKUP_DIR=\"${DEFAULT_PROJECT_BACKUP_DIR}/${project_name}\"")
vars+=("PROJECT_SYNC_LIST=\"${DEFAULT_PROJECT_SYNC_LIST}\"")
vars+=("PROJECT_SYNC_DESTS=\"${DEFAULT_PROJECT_SYNC_DESTS}\"")
vars+=("PROJECT_INCLUDE_FILTER=\"${DEFAULT_PROJECT_INCLUDE_FILTER}\"")
vars+=("PROJECT_EXCLUDE_FILTER=\"${DEFAULT_PROJECT_EXCLUDE_FILTER}\"")
vars+=("PROJECT_FORMAT_CMDS=\"${DEFAULT_PROJECT_FORMAT_CMDS}\"")
vars+=("PROJECT_LINT_CMDS=\"${DEFAULT_PROJECT_LINT_CMDS}\"")
vars+=("PROJECT_COMPILE_CMDS=\"${DEFAULT_PROJECT_COMPILE_CMDS}\"")
vars+=("PROJECT_TEST_CMDS=\"${DEFAULT_PROJECT_TEST_CMDS}\"")
vars+=("PROJECT_COVERAGE_CMDS=\"${DEFAULT_PROJECT_COVERAGE_CMDS}\"")
vars+=("PROJECT_RUN_CMDS=\"${DEFAULT_PROJECT_RUN_CMDS}\"")
vars+=("PROJECT_PACKAGE_CMDS=\"${DEFAULT_PROJECT_PACKAGE_CMDS}\"")
vars+=("PROJECT_DEPLOY_CMDS=\"${DEFAULT_PROJECT_DEPLOY_CMDS}\"")
vars+=("PROJECT_GENDOCS_CMDS=\"${DEFAULT_PROJECT_GENDOCS_CMDS}\"")
vars+=("PROJECT_SEARCH_CMDS=\"${DEFAULT_PROJECT_SEARCH_CMDS}\"")
vars+=("PROJECT_FORMAT_FN=\"${DEFAULT_PROJECT_FORMAT_FN}\"")
vars+=("PROJECT_LINT_FN=\"${DEFAULT_PROJECT_LINT_FN}\"")
vars+=("PROJECT_COMPILE_FN=\"${DEFAULT_PROJECT_COMPILE_FN}\"")
vars+=("PROJECT_TEST_FN=\"${DEFAULT_PROJECT_TEST_FN}\"")
vars+=("PROJECT_COVERAGE_FN=\"${DEFAULT_PROJECT_COVERAGE_FN}\"")
vars+=("PROJECT_GETERRORS_FN=\"${DEFAULT_PROJECT_GETERRORS_FN}\"")
vars+=("PROJECT_GETURL_FN=\"${DEFAULT_PROJECT_GETURL_FN}\"")
vars+=("PROJECT_SANITY_FN=\"${DEFAULT_PROJECT_SANITY_FN}\"")
while read line; do
if [[ "${line}" == "PROJECT_NAME=${project_name}" ]]; then
local found=1
elif [[ ${found} -eq 1 && "${line}" == "PROJECT_NAME="* ]]; then
break
fi
if [[ ${found} -eq 1 && ! "${line}" == "#"* && ! "${line}" == "" ]] && \
[[ "${line}" != alias* ]] && \
[[ "${2}" == "" || ${2} =~ "${line%%=*}" ]]; then
local var="${line%%=*}"
local val="${line#*=}"
if [[ "${val:0:1}" == '"' ]]; then
vars+=("${var}=${val}")
else
vars+=("${var}=\"${val}\"")
fi
fi
done < ~/.projects
echo "${vars[@]}"
}
################################################################################
# Returns a specific project var value.
#
# Args:
# $1: Project name
# $2: Project var name
################################################################################
function __projux_project_var() {
local project_name="${1}"
local var_name="${2}"
# All project vars are passed prior to request specific var so that we can
# fully resolve vars made up of other vars
local project_vars=$(__projux_project_vars "${project_name}")
val=$(eval "${project_vars} eval echo '$""${var_name}""'")
echo "${val}"
}
################################################################################
# Prints full path to project binary.
#
# REQUIREMENTS: project env vars are properly set (via 'projux attach <name>')
#
# Example:
# projux bin foo # show bin used by command foo
################################################################################
function __projux_bin_path() {
local project_name="${PROJECT_NAME}"
if [[ "${1}" == "-p" ]]; then
shift
project_name="${1}"
shift
fi
if [[ "${project_name%%:*}" != "${PROJECT_NAME}" || ! is_dev_local ]]; then
if [[ "${1}" != "get" ]]; then
__projux_tmux_send \
"$(__projux_default_project_win ${project_name})" \
"__projux_bin_path" $*
return $?
fi
fi
local bin_dir=$(\
__projux_project_var ${project_name%%:*} PROJECT_BIN_DIR)
cmd=${bin_dir}/$(__projux_get_run_cmd ${project_name%%:*} $*)
echo ${cmd[0]}
}
################################################################################
# Prints flags related to project binaries.
#
# REQUIREMENTS: project env vars are properly set (via 'projux attach <name>')
#
# Example:
# projux flags # pretty print flags for each cmd
# projux flags foo # pretty print flags for foo command
################################################################################
function __projux_bin_flags() {
local project_name="${PROJECT_NAME}"
if [[ "${1}" == "-p" ]]; then
shift
project_name="${1}"
shift
fi
if [[ "${project_name%%:*}" != "${PROJECT_NAME}" || ! is_dev_local ]]; then
if [[ "${1}" != "get" ]]; then
__projux_tmux_send \
"$(__projux_default_project_win ${project_name})" \
"__projux_bin_flags" $*
return $?
fi
fi
local cur=""
local cmd=""
if [[ $# -eq 0 ]]; then
cmd=$(__projux_project_var ${project_name%%:*} PROJECT_RUN_CMDS)
else
local bin_dir=$(\
__projux_project_var ${project_name%%:*} PROJECT_BIN_DIR)
cmd=${bin_dir}/$(__projux_get_run_cmd ${project_name%%:*} $*)
fi
# print each flag on a separate line
for arg in ${cmd}; do
if [[ -z "${cur}" ]]; then
cur=${arg}
elif [[ "${arg}" =~ "::" ]]; then
echo "${cur}"
cur=${arg}
else
echo "${cur}"
cur=" ${arg}"
fi
done
echo "${cur}"
}
################################################################################
# Environment Variable Management Commands