This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docthis.sh
executable file
·1760 lines (1371 loc) · 50 KB
/
docthis.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
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
#!/bin/bash
#
# @file docthis
# @brief Generate documentation with Sphinx, from root run: ./docthish.sh.
# Path to project used as source, defaults to current path.
PROJECT_PATH=$(pwd)
# Python executable to use: python or python3. Empty by default.
PYTHON_EXEC=''
# Install requirements or not.
INSTALL_REQUIREMENT=false
# requirements.txt file contents.
REQUIREMENTS_PIP_CONTENTS='sphinx
sphinxcontrib-restbuilder
sphinxcontrib-globalsubs
Sphinx-Substitution-Extensions
sphinx_rtd_theme'
# conf.py file contents.
CONFIGURATION_CONTENTS='# Configuration for Sphinx documentation builder.
import os
import sys
project = "|PROJECT_GENERATED_NAME|"
copyright = "|YEAR_GENERATED_VALUE|, |AUTHOR_GENERATED_NAME|"
author = "|AUTHOR_GENERATED_NAME|"
version = "0.0.1"
release = "0.0.1"
sys.path.insert(0, os.path.abspath("../.."))
extensions = [
"sphinxcontrib.restbuilder",
"sphinxcontrib.globalsubs",
"sphinx-prompt",
"sphinx_substitution_extensions"
]
templates_path = ["_templates"]
exclude_patterns = []
html_static_path = ["_static"]
html_theme = "sphinx_rtd_theme"
master_doc = "index"
img_base_url = "https://raw.githubusercontent.com/"
img_url = img_base_url + author + "/" + project + "/master/img/"
author_img = ".. image:: " + img_url + "/author.png\\n :alt: author"
author_slogan = "The Travelling Vaudeville Villain."
github_base_url = "https://github.com/"
github_url = github_base_url + author + "/" + project
github_badge = github_url + "workflows/CI/badge.svg\\n :alt: github_ci"
github_ci_url = github_url + "/actions"
github_ci_link = "`Github CI <" + github_ci_url + ">`_."
github_link = "`Github <" + github_url + ">`_."
gitlab_base_url = "https://gitlab.com/"
gitlab_url = gitlab_base_url + author + "/" + project
gitlab_badge = gitlab_url + "/badges/master/pipeline.svg\\n :alt: pipeline"
gitlab_ci_url = gitlab_url + "/pipelines"
gitlab_ci_link = "`Gitlab CI <" + gitlab_ci_url + ">`_."
gitlab_link = "`Gitlab <" + gitlab_url + ">`_."
travis_base_url = "https://travis-ci.org/"
travis_url = travis_base_url + author + "/" + project
travis_badge = ".. image:: " + travis_url + ".svg\\n :alt: travis"
travis_ci_url = travis_url
travis_link = "`Travis CI <" + travis_url + ">`_."
readthedocs_url = "https://" + project + ".readthedocs.io"
readthedocs_badge = "/projects/" + project + "/badge\\n :alt: readthedocs"
readthedocs_link = "`Readthedocs <" + readthedocs_url + ">`_."
gh_cover_base_url = "https://coveralls.io/repos/github/" + author + "/"
gh_cover_url = gh_cover_base_url + project + "/badge.svg"
gl_cover_base_url = "https://gitlab.com/" + author + "/" + project
gl_cover_url = gl_cover_base_url + "/badges/master/coverage.svg"
global_substitutions = {
"AUTHOR_IMG": author_img,
"AUTHOR_SLOGAN": author_slogan,
"COVERAGE_GITHUB_BADGE": ".. image:: " + gh_cover_url
+ "\\n :alt: coverage",
"COVERAGE_GITLAB_BADGE": ".. image:: " + gl_cover_url
+ "\\n :alt: coverage_gitlab",
"GITHUB_BADGE": ".. image:: " + github_badge,
"GITHUB_CI_LINK": github_ci_link,
"GITHUB_LINK": github_link,
"GITLAB_BADGE": ".. image:: " + gitlab_badge,
"GITLAB_CI_LINK": gitlab_ci_link,
"GITLAB_LINK": gitlab_link,
"PROJECT": project,
"READTHEDOCS_BADGE": ".. image:: https://rtfd.io" + readthedocs_badge,
"READTHEDOCS_LINK": readthedocs_link,
"TRAVIS_BADGE": travis_badge,
"TRAVIS_LINK": travis_link
}
substitutions = [
("|AUTHOR|", author),
("|PROJECT|", project)
]
'
# .readthedocs.yml file contents.
READTHEDOCS_CONTENTS="---
version: 2
sphinx:
configuration: doc/source/conf.py
python:
version: 3.7
install:
- requirements: doc/requirements.txt
submodules:
include: all"
# index.rst file contents.
INDEX_CONTENTS="|PROJECT_GENERATED_NAME|
============================================================================
|GITHUB_BADGE|
|GITLAB_BADGE|
|TRAVIS_BADGE|
|READTHEDOCS_BADGE|
|COVERAGE_GITHUB_BADGE|
|COVERAGE_GITLAB_BADGE|
My project short description.
Full documentation on |READTHEDOCS_LINK|.
Source code on:
|GITHUB_LINK|
|GITLAB_LINK|
Contents
============================================================================
.. toctree::
description
usage
variable
requirement
compatibility
license
link
author"
# description.rst file contents.
DESCRIPTION_CONTENTS='Description
----------------------------------------------------------------------------
Describe me.'
# usage.rst file contents
USAGE_CONTENTS="Usage
----------------------------------------------------------------------------
Download the script, give it execution permissions and execute it:
.. substitution-code-block:: bash
wget https://raw.githubusercontent.com/|AUTHOR|/|PROJECT|/master/|PROJECT|.sh
chmod +x |PROJECT|.sh
./|PROJECT|.sh -h"
# variable.rst file contents.
VARIABLE_CONTENTS="Variables
----------------------------------------------------------------------------
The following variables are supported:
- *-h* (help): Show help message and exit.
.. substitution-code-block:: bash
./|PROJECT|.sh -h
- *-p* (path): Optional path to project root folder.
.. substitution-code-block:: bash
./|PROJECT|.sh -p /home/username/myproject"
# requirement.rst file contents.
REQUIREMENT_CONTENTS='Requirements
----------------------------------------------------------------------------
- Python 3.'
# compatibility.rst file contents.
COMPATIBILITY_CONTENTS='Compatibility
----------------------------------------------------------------------------
- Debian buster.
- Raspbian buster.
- Ubuntu bionic.'
# license.rst file contents.
LICENSE_CONTENTS='License
----------------------------------------------------------------------------
MIT. See the LICENSE file for more details.'
# link.rst file contents.
LINK_CONTENTS='Links
----------------------------------------------------------------------------
- |GITHUB_LINK|
- |GITHUB_CI_LINK|
- |GITLAB_LINK|
- |GITLAB_CI_LINK|
- |TRAVIS_LINK|'
# author.rst file contents.
AUTHOR_CONTENTS='Author
----------------------------------------------------------------------------
|AUTHOR_IMG|
|AUTHOR_SLOGAN|'
# @description Shows an error message.
#
# @arg $1 string Error name: custom, execution, path, sudo, <name>.
# @arg $2 string Optional text to use on error messages.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
#
# @stdout 'Error message'
function error_message() {
[[ -z $1 ]] && return 1
case $1 in
custom)
if ! [[ -z $2 ]]; then
echo "$2"
else
echo 'Unknown error ocurred.'
fi
;;
execution)
if ! [[ -z $2 ]]; then
echo "An error occurred executing $2."
else
echo 'An error ocurred during execution.'
fi
;;
path)
if ! [[ -z $2 ]]; then
echo "$2 is not an existent directory."
else
echo 'Inexistent directory used.'
fi
;;
sudo)
echo "It's not possible to adquire administrative permissions."
echo 'It could be one of the following causes:'
echo '- The program "sudo" is not installed.'
echo '- Your user is not on the "root" or "sudo" groups.'
echo 'Run:'
echo 'su -c "apt install -y sudo" && su -c "sudo adduser $(whoami) sudo" && su'
;;
uninstall)
if ! [[ -z $2 ]]; then
echo "Cannot uninstall $2."
else
echo 'Cannot uninstall package.'
fi
;;
*)
echo ' __.....__'
echo ' ."" _ o "`.'
echo ' ." O (_) () o`.'
echo ' . O .'
echo ' . () o__...__ O .'
echo '. _.--""" """--._ .'
echo ':" ";'
echo ' `-.__ : : __.-"'
echo ' """-: :-"""'
echo ' J L'
echo ' : :'
echo ' J L'
echo ' : :'
echo ' `._____."'
echo "$1 needs to be installed ..."
echo "Run:"
echo "./$(basename $0).sh -i"
;;
esac
return 0
}
# @description Escape especial characters.
#
# The escaped characters are:
#
# - Period.
# - Slash.
# - Colon.
#
# @arg $1 string Text to scape.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout Escaped input.
function escape() {
[[ -z $1 ]] && echo '' && return 0
local escaped=$(sanitize "$1")
# Escape period.
escaped="${escaped//\./\\.}"
# Escape slash.
escaped="${escaped//\//\\/}"
# Escape colon.
escaped="${escaped//\:/\\:}"
echo "$escaped" && return 0
}
# @description Setup Sphinx and generate html and rst documentation,
# generates a single README-single file that can be used on github
# or gitlab.
#
# @arg $1 string Optional project path. Default to current path.
# @arg $2 string Optional CI service to use for generating a coverage badge.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout *README-single* rst on project's root directory.
function generate() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
local doc_path=$(get_doc_path $project_path)
local conf_path=$doc_path/source/conf.py
# Valid values: 'github' or 'gitlab'.
local coverage_ci='github'
if ! [[ -z $2 ]]; then
if [[ "$2" == 'github' ]] || [[ "$2" == 'gitlab' ]]; then
coverage_ci="$2"
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
local project_year=$(date +"%Y")
# Setup everything for new projects.
if ! [[ -f $conf_path ]]; then
# Directory layout.
mkdir -p $doc_path/source/_static &>/dev/null
mkdir -p $doc_path/source/_templates &>/dev/null
mkdir -p $doc_path/build/html &>/dev/null
mkdir -p $doc_path/build/rst &>/dev/null
# Create requirements.txt file.
if ! [[ -f $doc_path/requirements.txt ]];
then
printf "$REQUIREMENTS_PIP_CONTENTS" > $doc_path/requirements.txt
fi
# Create conf.py file.
if ! [[ -f $doc_path/source/conf.py ]];
then
printf "$CONFIGURATION_CONTENTS" > $conf_path
fi
# Create source files.
if ! [[ -f $doc_path/source/index.rst ]]; then
printf "$INDEX_CONTENTS" > $doc_path/source/index.rst
printf "$DESCRIPTION_CONTENTS" > $doc_path/source/description.rst
printf "$USAGE_CONTENTS" > $doc_path/source/usage.rst
printf '%s' "$VARIABLE_CONTENTS" > $doc_path/source/variable.rst
printf '%s' "$REQUIREMENT_CONTENTS" > $doc_path/source/requirement.rst
printf '%s' "$COMPATIBILITY_CONTENTS" > $doc_path/source/compatibility.rst
printf "$LICENSE_CONTENTS" > $doc_path/source/license.rst
printf '%s' "$LINK_CONTENTS" > $doc_path/source/link.rst
printf "$AUTHOR_CONTENTS" > $doc_path/source/author.rst
sed -i -E "s/\|AUTHOR_GENERATED_NAME\|/$author/g" $doc_path/source/*.*
sed -i -E "s/\|PROJECT_GENERATED_NAME\|/$project/g" $doc_path/source/*.*
sed -i -E "s/\|YEAR_GENERATED_VALUE\|/$project_year/g" $doc_path/source/*.*
fi
# Create .readthedocs.yml configuration file.
if ! [[ -f $project_path/.readthedocs.yml ]]; then
printf '%s' "$READTHEDOCS_CONTENTS" > $project_path/.readthedocs.yml
fi
# Copy docthis.sh if not exists.
if ! [[ -f $project_path/docthis.sh ]]; then
cp "$( cd "$(dirname "$0")" ; pwd -P )"/docthis.sh $project_path/docthis.sh
fi
fi # New project?.
if [[ $INSTALL_REQUIREMENT == 'true' ]]; then
# Try to install Python requirements, stop if can't install them.
install_pip $doc_path
if [ $? -eq 1 ]; then
error_message 'custom' "Python packages not installed."
return 1
fi
fi
# Before generating, validate that sphinx is installed.
if [[ $(validate_apt "${python_exec}-sphinx") == 'false' ]] &&
[[ $(validate_pip 'sphinx') == 'false' ]]; then
error_message "sphinx"
return 1
fi
# Cleanup old files.
rm -rf $doc_path/build &>/dev/null
rm -f $doc_path/source/index-tmp.rst &>/dev/null
# Generate documentation.
local python_exec=$(get_python_exec)
$python_exec -m sphinx -b html $doc_path/source/ $doc_path/build/html
generate_rst $project_path $coverage_ci
return 0
}
# @description Generate rst documentation using sphinx.
#
# This function will extract each filename to include from the
# index.rst file and concatenate all files into a single
# README-single.rst file.
#
# This function assumes:
# - The project has a file structure as created by generate().
# - The index.rst file contains the :toctree: directive.
#
# @arg $1 string Optional project path. Default to current path.
# @arg $2 string Optional CI service to use for generating a coverage badge.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout *README-single* rst on project's root directory.
function generate_rst() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
# Valid values: 'gitlab' or 'travis'.
local coverage_ci='travis'
if ! [[ -z $2 ]]; then
if [[ "$2" == 'gitlab' ]] || [[ "$2" == 'travis' ]]; then
coverage_ci="$2"
fi
fi
local project=$(get_project $project_path)
local author=$(get_author $project_path)
local doc_path=$(get_doc_path $project_path)
local python_exec=$(get_python_exec)
# This flag indicates if menu items where found on index.rst (toctree).
local items_found=false
# Clean files first.
rm -r $doc_path/build/rst/*.rst &>/dev/null
# Build rst files.
$python_exec -m sphinx -b rst $doc_path/source/ $doc_path/build/rst
# Construct the README-single file, add the index first.
# Verify if index.rst exists.
if [[ -f $doc_path/build/rst/index.rst ]]; then
# Convert index.rst from readthedocs format to standard rst.
readthedocs_to_rst $doc_path/build/rst/index.rst $project_path $coverage_ci
# Add index.rst to README-single.rst.
cat $doc_path/build/rst/index.rst > $project_path/README-single.rst
printf '\n' >> $project_path/README-single.rst
fi
# Copy index.rst to index-tmp.rst and add a newline to it, this to
# ensure to visit all lines because Bash expects a newline at the
# end (C standard).
cp -f $doc_path/source/index.rst $doc_path/source/index-tmp.rst
echo '' >> $doc_path/source/index-tmp.rst
# Here the index.rst file will be readed to search for menu items,
# The directive :toctree: MUST be present or an empty file will
# be generated.
while read LINE
do
# The directive :toctree: of the index.rst file
# activates the search for menu item lines within that file.
[[ $LINE == *'toctree::'* ]] && items_found=true && continue
if [[ $items_found == true ]] && ! [[ -z "$LINE" ]]; then
# Verify if the current file (described on LINE) exists.
if [[ -f $doc_path/build/rst/${LINE}.rst ]]; then
# Copy current to current-tmp and add newline (Bash issue).
cp -f $doc_path/build/rst/${LINE}.rst $doc_path/build/rst/${LINE}-tmp.rst
echo '' >> $doc_path/build/rst/${LINE}-tmp.rst
# Convert current file from readthedocs to common rst.
readthedocs_to_rst $doc_path/build/rst/${LINE}-tmp.rst $project_path $coverage_ci
# Add current file to README-single.
cat $doc_path/build/rst/${LINE}-tmp.rst >> $project_path/README-single.rst
printf "\n" >> $project_path/README-single.rst
# Remove temporary file.
rm $doc_path/build/rst/${LINE}-tmp.rst
fi
fi
done < $doc_path/source/index-tmp.rst
rm $doc_path/source/index-tmp.rst
return 0
}
# @description Get the author's name.
#
# @arg $1 string Path to the configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo author's name.
function get_author() {
local conf_path=$1
# Path to configuration file.
if [[ -f $conf_path ]]; then
local author=$(get_variable 'author' $conf_path)
! [[ -z $author ]] && echo $author && return 0
fi
whoami && return 0
}
# @description Obtains the project's documentation directory.
#
# This function tries:
# - Read a *.readthedocs.yml* file.
# - Search for the *./docs* directory.
# - Default to *./doc* directory.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout Path to the documentation directory
function get_doc_path() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
# Read documentation path from Readthedocs configuration.
if [[ -f $project_path/.readthedocs.yml ]]; then
local config_line=$(cat .readthedocs.yml | grep 'configuration')
if ! [[ -z config_line ]]; then
# Remove the 'configuration:' part.
config_line=${config_line//configuration\: /}
config_line=$(dirname $config_line)
# Remove the 'source/' part.
config_line=${config_line//source/}
# Sanitize the string.
config_line=$(sanitize "$config_line")
# Get the full path.
config_line=$(realpath -s $config_line)
echo $config_line
return 0
fi
fi
# Try /docs.
[[ -d $project_path/docs ]] && echo "$project_path/docs" && return 0
# Default /doc.
echo "$project_path/doc" && return 0
}
# @description Get the continuous integration repository URL for Github.
#
# If the URL cannot be found, then a default URL is returned.
#
# This function assumes:
# - The project has a file structure as created by generate().
#
# @arg $1 string Path to the configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo Github continuous integration URL.
function get_github_ci_url() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local github_ci_url=$(get_variable 'github_ci_url' $conf_path)
if ! [[ -z $github_ci_url ]]; then
if [[ "$github_ci_url" =~ 'github_url' ]]; then
local github_url=$(get_variable 'github_url' $conf_path)
if ! [[ -z $github_url ]]; then
github_ci_url="${github_ci_url//github_url/$github_url}"
fi
fi
if [[ "$github_ci_url" =~ 'github_base_url' ]]; then
local github_base_url=$(get_variable 'github_base_url' $conf_path)
if ! [[ -z $github_base_url ]]; then
github_ci_url="${github_ci_url//github_base_url/$github_base_url}"
fi
fi
echo "$github_ci_url"
return 0
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
echo "https://github.com/$author/$project/actions"
return 0
}
# @description Get the continuous integration repository URL for Gitlab.
#
# If the URL cannot be found, then a default URL is returned.
#
# This function assumes:
# - The project has a file structure as created by generate().
#
# @arg $1 string Path to the configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo Gitlab continuous integration URL.
function get_gitlab_ci_url() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local gitlab_ci_url=$(get_variable 'gitlab_ci_url' $conf_path)
if ! [[ -z $gitlab_ci_url ]]; then
if [[ "$gitlab_ci_url" =~ 'gitlab_url' ]]; then
local gitlab_url=$(get_variable 'gitlab_url' $conf_path)
if ! [[ -z $gitlab_url ]]; then
gitlab_ci_url="${gitlab_ci_url//gitlab_url/$gitlab_url}"
fi
fi
if [[ "$gitlab_ci_url" =~ 'gitlab_base_url' ]]; then
local gitlab_base_url=$(get_variable 'gitlab_base_url' $conf_path)
if ! [[ -z $gitlab_base_url ]]; then
gitlab_ci_url="${gitlab_ci_url//gitlab_base_url/$gitlab_base_url}"
fi
fi
echo "$gitlab_ci_url"
return 0
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
echo "https://gitlab.com/$author/$project/pipelines"
return 0
}
# @description Get the coverage badge URL for Github (coveralls).
#
# @arg $1 string Path to the configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo Github coverage (coveralls) badge URL.
function get_gh_cover_url() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local gh_cover_url=$(get_variable 'gh_cover_url' $conf_path)
if ! [[ -z $gh_cover_url ]]; then
if [[ "$gh_cover_url" =~ 'gh_cover_base_url' ]]; then
local gh_cover_base_url=$(get_variable 'gh_cover_base_url' $conf_path)
if ! [[ -z $gh_cover_base_url ]]; then
gh_cover_url="${gh_cover_url//gh_cover_base_url/$gh_cover_base_url}"
fi
fi
echo "$gh_cover_url"
return 0
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
echo "https://coveralls.io/repos/github/$author/$project/badge.svg"
return 0
}
# @description Get the coverage badge URL for Gitlab.
#
# @arg $1 string Path to the configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo Gitlab coverage badge URL.
function get_gl_cover_url() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local gl_cover_url=$(get_variable 'gl_cover_url' $conf_path)
if ! [[ -z $gl_cover_url ]]; then
if [[ "$gl_cover_url" =~ 'gl_cover_base_url' ]]; then
local gl_cover_base_url=$(get_variable 'gl_cover_base_url' $conf_path)
if ! [[ -z $gl_cover_base_url ]]; then
gl_cover_url="${gl_cover_url//gl_cover_base_url/$gl_cover_base_url}"
fi
fi
echo "$gl_cover_url"
return 0
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
echo "https://gitlab.com/$author/$project/badges/master/coverage.svg"
return 0
}
# @description Get the images repository URL.
#
# If the URL cannot be found, then a default Github URL is returned.
#
# This function assumes:
# - The project has a file structure as created by generate().
#
# @arg $1 Path to the configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo repository images URL.
function get_img_url() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local img_url=$(get_variable 'img_url' $conf_path)
if ! [[ -z $img_url ]]; then
# Verify if img_url contains ../../ (local files).
if [[ "$img_url" == *'../..'* ]]; then
img_url="${img_url//\.\.\/\.\.\//\.\/}"
fi
echo "$img_url"
return 0
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
echo "https://raw.githubusercontent.com/$author/$project/master/img/"
return 0
}
# @description Get bash parameters.
#
# Accepts:
#
# - *h* (help).
# - *i* (requirements).
# - *p* <project-path>.
# - *x* <python-executable>.
#
# @arg '$@' string Bash arguments.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
function get_parameters() {
# Obtain parameters.
while getopts 'h;i;p:x:' opt; do
OPTARG=$(sanitize "$OPTARG")
case "$opt" in
h) help && exit 0;;
i) INSTALL_REQUIREMENT=true;;
p) PROJECT_PATH="${OPTARG}";;
x) [[ "${OPTARG}" == *'python'* ]] && PYTHON_EXEC="${OPTARG}";;
esac
done
return 0
}
# @description Get the project's name.
#
# @arg $1 string Path to configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo project's name.
function get_project() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local project=$(get_variable 'project' $conf_path)
if [ $? -eq 0 ]; then
! [[ -z $project ]] && echo $project && return 0
fi
fi
basename $(pwd) && return 0
}
# @description Obtains the Python executable to use: python or python3.
#
# This function tries:
# - Use the $PYTHON_EXEC variable if not empty and like 'python'.
# - Use 'python3' is available.
# - Use 'python' if available.
#
# @args noargs
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout the python executable (python or python3).
function get_python_exec() {
# Try user passed exec.
if ! [[ -z $PYTHON_EXEC ]]; then
if [[ $(validate_apt "$PYTHON_EXEC") == 'true' ]]; then
echo "$PYTHON_EXEC" && return 0
fi
# Check if python was installed by compiling or other method.
if [[ $($PYTHON_EXEC --version) -eq 0 ]]; then
echo "$PYTHON_EXEC" && return 0
fi
fi
# Try python3.
if [[ $(validate_apt 'python3') == 'true' ]]; then
echo 'python3' && return 0
fi
python3 --version &>/dev/null
[ $? -eq 0 ] && echo 'python3' && return 0
# Try python.
if [[ $(validate_apt 'python') == 'true' ]]; then
echo 'python' && return 0
fi
python --version &>/dev/null
[ $? -eq 0 ] && echo 'python' && return 0
# Python is not installed.
echo '' && return 1
}
# @description Get the continuous integration repository URL for Travis.
#
# If the URL cannot be found, then a default URL is returned.
#
# This function assumes:
# - The project has a file structure as created by generate().
#
# @arg $1 string Path to configuration file.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout echo continuous integration URL.
function get_travis_ci_url() {
local conf_path=$1
if [[ -f $conf_path ]]; then
local travis_ci_url=$(get_variable 'travis_ci_url' $conf_path)
if ! [[ -z $travis_ci_url ]]; then
if [[ "$travis_ci_url" =~ 'travis_url' ]]; then
local travis_url=$(get_variable 'travis_url' $conf_path)
if ! [[ -z $travis_url ]]; then
travis_ci_url="${travis_ci_url//travis_url/$travis_url}"
fi
fi
if [[ "$travis_ci_url" =~ 'travis_base_url' ]]; then
local travis_base_url=$(get_variable 'travis_base_url' $conf_path)
if ! [[ -z $travis_base_url ]]; then
travis_ci_url="${travis_ci_url//travis_base_url/$travis_base_url}"
fi
fi
echo "$travis_ci_url"
return 0
fi
fi
local author=$(get_author $conf_path)
local project=$(get_project $conf_path)
echo "https://travis-ci.org/$author/$project"
return 0