-
Notifications
You must be signed in to change notification settings - Fork 14
/
code-compass.el
1902 lines (1705 loc) · 85.1 KB
/
code-compass.el
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
;;; code-compass.el --- Navigate software aided by metrics and visualization -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Andrea
;; Author: Andrea <[email protected]>
;; Version: 0.1.4
;; Package-Requires: ((emacs "26.1") (s "1.12.0") (dash "2.13") (async "1.9.7") (simple-httpd "1.5.1"))
;; Keywords: tools, extensions, help
;; Homepage: https://github.com/ag91/code-compass
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>
;;; Commentary:
;; Make Emacs your compass in a sea of software complexity.
;;
;; This tool puts the power and knowledge of your repository history in your hands.
;; You can find what analyses are supported with `code-compass-cheatsheet'.
;; A good way to start is:
;; - `code-compass-show-hotspots':
;; show hotspots in code repository as a circle diagram.
;; Circles are packages or modules.
;; The redder the circle, the more it has been modified lately. The bigger the more code it contains.
;;
;; If you are having trouble with dependencies, try `code-compass-doctor' to get some clarity.
;;
;; See documentation at https://github.com/ag91/code-compass
;;; Code:
(require 'dash)
(require 's)
(require 'simple-httpd)
(require 'async)
(require 'url)
(require 'vc)
(defgroup code-compass nil
"Options specific to code-compass."
:tag "code-compass"
:group 'code-compass)
(defcustom code-compass-default-port 8888
"Default port on which to serve analyses files."
:type 'int
:group 'code-compass)
(defcustom code-compass-default-periods
'("beginning" "1d" "2d" "3d" "6d" "12d" "18d" "24d" "1m" "2m" "6m")
"Starting date to reduce the Git log for analysis.
'beginning' is a keyword to say to not reduce.
'Nd' means to start after N days, where N is a positive number.
'Nm' means to start after N months, where N is a positive number."
:group 'code-compass
:type 'string)
(defcustom code-compass-snapshot-periods
'("1d" "3m" "6m" "9m" "12m" "15m")
"A list of snapshots periods to show evolution of analyses over time."
:group 'code-compass
:type 'string)
(defconst code-compass-path-to-code-compass (file-name-directory (or load-file-name (buffer-file-name)))
"Path to code compass.")
(defun code-compass--expand-file-name (file-name)
"Expand FILE-NAME with `code-compass-path-to-code-compass'."
(expand-file-name file-name code-compass-path-to-code-compass))
(defcustom code-compass-tmp-directory "/tmp"
"Directory to store temporary files generated by code-compass."
:group 'code-compass
:type 'string)
(defcustom code-compass-docker-data-directory "/data"
"Directory to store temporary docker files generated by code-compass."
:group 'code-compass
:type 'string)
(defcustom code-compass-download-directory (code-compass--expand-file-name "dependencies")
"Directory to store downloaded dependencies."
:group 'code-compass
:type 'string)
(defcustom code-compass-code-maat-command
(format "java -jar %s/code-maat-1.0.1-standalone.jar" (code-compass--expand-file-name "dependencies"))
"Command to run Code-maat (https://github.com/adamtornhill/code-maat).
Currently defaults to use docker because easier to setup."
:group 'code-compass
:type 'string
:options `(,(format "java -jar %s/code-maat-1.0.1-standalone.jar" code-compass-download-directory)
,(format "docker run -v %s/:%s code-maat-app" code-compass-tmp-directory code-compass-docker-data-directory)))
(defcustom code-compass-preferred-browser
"chromium"
"Browser to use to open graphs served by webserver."
:group 'code-compass
:type 'string)
(defcustom code-compass-exclude-directories
'("node_modules" "bower_components" "vendor" "tmp")
"A list of directory patterns to exclude from reports.
Contents are passed to the cloc executable via its --exclude-dir argument."
:group 'code-compass
:type 'list)
(defcustom code-compass-calculate-coupling-project-key-fn
(lambda (repository)
(concat
repository
"-"
(s-trim
(shell-command-to-string
(format "cd %s; git rev-parse --short HEAD" repository)))))
"Function taking a REPOSITORY path and returning a string."
:group 'code-compass
:type 'function)
(defcustom code-compass-authors-colors (list
"red"
"blue"
"orange"
"gray"
"green"
"violet"
"pink"
"brown"
"aquamarine"
"blueviolet"
"burlywood"
"cadetblue"
"chartreuse"
"chocolate"
"coral"
"cornflowerblue"
"cyan"
"darkblue"
"darkcyan"
"darkgoldenrod"
"darkgray"
"darkgreen"
"darkkhaki"
"darkmagenta"
"darkolivegreen"
"darkorange"
"darkorchid"
"darkred"
"darksalmon"
"darkseagreen"
"darkslateblue"
"darkslategray"
"darkturquoise"
"darkviolet"
"deeppink"
"deepskyblue"
"dimgray"
"dodgerblue"
"firebrick"
"forestgreen"
"fuchsia"
"gold"
"goldenrod"
"greenyellow"
"hotpink"
"indianred"
"indigo"
"lawngreen"
"lightcoral"
"lightgray"
"lightgreen"
"lightpink"
"lightsalmon"
"lightseagreen"
"lightskyblue"
"lightslategray"
"lightsteelblue"
"lime"
"limegreen"
"linen"
"magenta"
"maroon"
"mediumaquamarine"
"mediumblue"
"mediumorchid"
"mediumpurple"
"mediumseagreen"
"mediumslateblue"
"mediumspringgreen"
"mediumturquoise"
"mediumvioletred"
"midnightblue"
"mintcream"
"mistyrose"
"moccasin"
"navajowhite"
"navy"
"oldlace"
"olive"
"olivedrab"
"orangered"
"orchid"
"palegoldenrod"
"palegreen"
"paleturquoise"
"palevioletred"
"papayawhip"
"peachpuff"
"peru"
"plum"
"powderblue"
"purple"
"rosybrown"
"royalblue"
"saddlebrown"
"salmon"
"sandybrown"
"seagreen"
"seashell"
"sienna"
"silver"
"skyblue"
"slateblue"
"slategray"
"snow"
"springgreen"
"steelblue"
"tan"
"teal"
"thistle"
"tomato"
"turquoise"
"wheat"
"whitesmoke"
"yellow"
"yellowgreen")
"Colors to use for authors."
:group 'code-compass
:type 'list)
(defcustom code-compass-pie-or-bar-chart-command "python3 csv-to-pie-graph.py %s"
"Command to visualize chart."
:group 'code-compass
:type 'string
:options '("python3 csv-to-pie-graph.py %s" "graph %s --bar --width 0.4 --offset='-0.2,0.2'"))
(defcustom code-compass-gource-command
"gource"
"Command to the gource utility. See https://gource.io/ for more information on how to install."
:group 'code-compass
:type 'string)
(defcustom code-compass-gource-seconds-per-day
0.5
"How long each Git history day should take."
:type 'float
:group 'code-compass)
(defcustom code-compass-display-icon
't
"Display an icon in modeline showing growth trend of code.
A pointing up icon means the code has been growing,
a pointing down arrow has been decreasing."
:group 'code-compass
:type 'bool)
(defcustom code-compass-icon-trends
'(
:period "3"
:always-additions (propertize "⬆" 'face `(:background "DarkOrange"))
:always-deletions (propertize "⬇" 'face `(:background "SpringGreen"))
:more-additions (propertize "↗" 'face `(:background "Gold"))
:more-deletions (propertize "↘" 'face `(:background "GreenYellow")))
"Icon and period of evaluation for trend."
:group 'code-compass
:type 'plist)
(defcustom code-compass-display-file-contributors 't
"Enable the listing of contributors for a file in the *Messages* buffer."
:group 'code-compass
:type 'bool)
(defcustom code-compass-cache-file (concat user-emacs-directory ".code-compass/cache")
"Directory to store a cache of coupling files."
:group 'code-compass
:type 'string)
;; these definitions are just to satisfy the linting tools
(defvar browse-url-generic-program)
(defvar slack-current-team)
(declare-function slack-team-users "ext:slack.el")
(declare-function slack-create-user-profile-buffer "ext:slack.el")
(declare-function slack-buffer-display-im "ext:slack.el")
;;;###autoload
(defun code-compass-doctor ()
"Report if and what dependencies are missing."
(interactive)
(let ((git-p (executable-find "git"))
(python-p (executable-find "python3"))
(java-p (executable-find "java"))
(graph-cli-p (executable-find "graph"))
(cloc-p (executable-find "cloc"))
(gource-p (executable-find "gource"))
(docker-p (executable-find "docker"))
(doctor-buffer (get-buffer-create "code-compass dependencies check")))
(with-current-buffer doctor-buffer
(read-only-mode -1)
(erase-buffer)
(insert "Welcome to Code Compass doctor!\n\n")
(insert "Required dependencies for minimal functionality:\n")
(insert (format "- Git: %s\n" (if git-p "OK" "MISSING")))
(insert (format "- Python: %s\n" (if python-p "OK" "MISSING")))
(insert (format "- Java: %s\n" (if java-p "OK" "MISSING")))
(insert (format "- Cloc: %s\n" (if cloc-p "OK" "MISSING")))
(insert "\n\nOptional dependencies:\n")
(insert (format "- Graph-cli: %s\n" (if graph-cli-p "OK" "MISSING")))
(insert (format "- Gource: %s\n" (if gource-p "OK" "MISSING")))
(insert (format "- Docker: %s\n" (if docker-p "OK" "MISSING")))
(read-only-mode))
(switch-to-buffer-other-window doctor-buffer)))
(define-obsolete-function-alias 'c/doctor #'code-compass-doctor "0.1.2")
(defun code-compass--subtract-to-now (n month|day &optional time)
"Subtract N * MONTH|DAY to current time.
Optionally give TIME from which to start."
(time-subtract
(or time (current-time))
(seconds-to-time (* 60 60 month|day n))))
(defun code-compass-request-date (days|months &optional time)
"Request date in days or months by asking how many DAYS|MONTHS ago.
Optionally give TIME from which to start.
>> (code-compass-request-date \"1d\" '(25610 2072 776840 400000))
=> \"2023-03-08\"
>> (code-compass-request-date \"1m\" '(25610 2072 776840 400000))
=> \"2023-02-06\""
(interactive
(list (completing-read "From how long ago? " code-compass-default-periods)))
(when (not (string= days|months "beginning"))
(format-time-string
"%F"
(apply
#'code-compass--subtract-to-now
(-concat
(if (s-contains-p "m" days|months)
(list (string-to-number (s-replace "m" "" days|months)) (* 24 31))
(list (string-to-number (s-replace "d" "" days|months)) 24))
(list time))))))
(define-obsolete-function-alias 'c/request-date #'code-compass-request-date "0.1.2")
(defun code-compass--first (l)
"Get first element of L."
(car l))
(defun code-compass--second (l)
"Get second element of L."
(nth 1 l))
(defun code-compass--third (l)
"Get third element of L."
(nth 2 l))
(defun code-compass--filename (file)
"Get filename of FILE.
>> (code-compass--filename \"some/file.txt\")
=> \"file.txt\""
(file-name-nondirectory (directory-file-name file)))
(defun code-compass--temp-dir (repository)
"Format temporary directory in which store analyses assets for REPOSITORY.
>> (code-compass--temp-dir \"~/some-repo/\")
=> \"/tmp/code-compass-some-repo/\""
(format "%s/code-compass-%s/" code-compass-tmp-directory (code-compass--filename repository)))
(defmacro code-compass--in-directory (directory &rest body)
"Execute BODY in DIRECTORY.
Temporarily changes current buffer's default directory to DIRECTORY."
`(let ((default-directory ,directory))
(unwind-protect
,@body)))
(defmacro code-compass--in-temp-directory (repository &rest body)
"Execute BODY in temporary directory created for analysed REPOSITORY."
`(progn
(mkdir (code-compass--temp-dir ,repository) t)
(code-compass--in-directory
(code-compass--temp-dir ,repository)
,@body)))
(defun code-compass--shell-command-error-handler (command buffer-name)
"Run COMMAND with `shell-command' but check if errors are output in BUFFER-NAME."
(ignore-errors (kill-buffer buffer-name))
(shell-command
command
nil
(get-buffer-create buffer-name))
(let ((contents
(with-current-buffer buffer-name
(buffer-string))))
(when (> (length contents) 1) (error (concat buffer-name "\n\n" contents)))))
(defun code-compass-produce-git-report (repository date &optional before-date authors)
"Create git report for REPOSITORY with a Git log starting at DATE.
Define optionally a BEFORE-DATE.
The knowledge analysis allow to filter by AUTHORS when set."
(interactive
(list (call-interactively #'code-compass-request-date)))
(message "Producing git report...")
(let ((git-command
(s-concat
(format "git -C %s" repository)
" log --all --numstat --date=short --pretty=format:'--%h--%ad--%aN' --no-renames "
(when authors
(format "--perl-regexp --author='%s' " authors))
(when date
(format "--after=%s " date))
(when before-date
(format "--before=%s " before-date))
(when code-compass-exclude-directories
(s-join " " (--map (format "':(exclude)%s'" it) code-compass-exclude-directories)))
" > gitreport.log")))
(message "Running %s" git-command)
(code-compass--shell-command-error-handler git-command "*code-compass-produce-git-report-errors*"))
repository)
(define-obsolete-function-alias 'c/produce-git-report #'code-compass-produce-git-report "0.1.2")
(defun code-compass--run-code-maat (command repository)
"Run code-maat's COMMAND on REPOSITORY."
(message "Producing code-maat %s report for %s..." command repository)
(let ((source-file (format "%s/code-maat-1.0.1-standalone.jar" code-compass-download-directory))
(maat-jar-p (s-contains-p "jar" code-compass-code-maat-command)))
(when (and maat-jar-p (not (file-exists-p (code-compass--expand-file-name source-file))))
(mkdir code-compass-download-directory t)
(url-copy-file "https://github.com/smontanari/code-forensics/raw/v3.0.0/lib/analysers/code_maat/code-maat-1.0.1-standalone.jar" (code-compass--expand-file-name source-file) t))
(code-compass--shell-command-error-handler
(format
"%1$s -l %4$s/code-compass-%2$s/gitreport.log -c git2 -a %3$s > %3$s.csv"
code-compass-code-maat-command
(code-compass--filename repository)
command
(if maat-jar-p code-compass-tmp-directory code-compass-docker-data-directory))
"*code-compass--run-code-maat-errors*")))
(defun code-compass--produce-code-maat-revisions-report (repository)
"Create code-maat revisions report for REPOSITORY."
(code-compass--run-code-maat "revisions" repository)
repository)
(defun code-compass--produce-cloc-report (repository)
"Create cloc report for REPOSITORY.
To filter specific subdirectories out of this report,
edit the variable `code-compass-exclude-directories'."
(let ((cloc-command (format "(cd %s; PERL_BADLANG=0 cloc ./ --timeout 0 --by-file --csv --quiet --exclude-dir=%s) > cloc.csv" repository (string-join code-compass-exclude-directories ","))))
(message (concat
"Producing cloc report with "
cloc-command
"..."))
(code-compass--shell-command-error-handler
cloc-command
"*code-compass--produce-cloc-report-errors*"))
repository)
(defun code-compass--copy-file (file-name directory)
"Copy FILE-NAME to DIRECTORY."
(copy-file (code-compass--expand-file-name file-name) directory t)
(set-file-modes (concat directory "/" (file-name-nondirectory file-name)) (file-modes-symbolic-to-number "u=rw,go=r")))
(defun code-compass--generate-merger-script (repository)
"Generate a Python script to give weights to the circle diagram of REPOSITORY."
(code-compass--copy-file "./scripts/csv_as_enclosure_json.py" (code-compass--temp-dir repository))
repository)
(defun code-compass--generate-d3-v3-lib (repository)
"Make available the D3 library for REPOSITORY.
This is just to not depend on a network connection."
(mkdir "d3" t)
(let ((source-file (format "%s/d3.v3.min.js" code-compass-download-directory)))
(unless (file-exists-p (code-compass--expand-file-name source-file))
(mkdir code-compass-download-directory t)
(url-copy-file "http://d3js.org/d3.v3.min.js" (code-compass--expand-file-name source-file) t))
(code-compass--copy-file source-file "d3/"))
repository)
(defun code-compass--generate-d3-v4-lib (repository)
"Make available the D3 v4 library for REPOSITORY.
This is just to not depend on a network connection."
(mkdir "d3" t)
(let ((source-file (format "%s/d3.v4.min.js" code-compass-download-directory)))
(unless (file-exists-p (code-compass--expand-file-name source-file))
(mkdir code-compass-download-directory t)
(url-copy-file "http://d3js.org/d3.v4.min.js" (code-compass--expand-file-name source-file) t))
(code-compass--copy-file source-file "d3/"))
repository)
(defun code-compass--produce-json (repository)
"Produce json for REPOSITORY."
(message "Produce json...")
(code-compass--shell-command-error-handler
"python3 csv_as_enclosure_json.py --structure cloc.csv --weights revisions.csv > hotspot_proto.json"
"*code-compass--produce-json-errors*")
repository)
(defun code-compass--generate-host-enclosure-diagram-html (repository)
"Generate host html from REPOSITORY."
(code-compass--copy-file "./pages/enclosure-diagram/style.css" (code-compass--temp-dir repository))
(code-compass--copy-file "./pages/enclosure-diagram/script.js" (code-compass--temp-dir repository))
(code-compass--copy-file "./pages/enclosure-diagram/zoomable.html" (code-compass--temp-dir repository))
repository)
(defun code-compass--navigate-to-localhost (repository &optional port)
"Navigate to served directory for REPOSITORY, optionally at specified PORT."
(let ((port (or port code-compass-default-port))
(browse-url-browser-function #'browse-url-generic)
(browse-url-generic-program code-compass-preferred-browser))
(browse-url (format "http://localhost:%s/zoomable.html" port)))
repository)
(defun code-compass--run-server (repository &optional port)
"Serve directory for REPOSITORY, optionally at PORT."
(let ((httpd-host 'local)
(httpd-port (or port code-compass-default-port)))
(httpd-stop)
(ignore-errors (httpd-serve-directory (code-compass--temp-dir repository))))
repository)
(defun code-compass--run-server-and-navigate (repository &optional port)
"Serve and navigate to REPOSITORY, optionally at PORT."
(when port
(code-compass--run-server repository port)
(code-compass--navigate-to-localhost repository port)))
(defun code-compass--async-run (command repository date &optional port do-not-serve authors)
"Run asynchronously COMMAND taking a REPOSITORY and a DATE, optionally at PORT.
Optional argument DO-NOT-SERVE skips serving contents on localhost.
Optional argument AUTHORS to filter AUTHORS for knowledge analysis."
(async-start
`(lambda ()
(setq load-path ',load-path)
(load-file ,(symbol-file command))
(setq code-compass-code-maat-command ,code-compass-code-maat-command)
(setq code-compass-pie-or-bar-chart-command ,code-compass-pie-or-bar-chart-command)
(setq code-compass-calculate-coupling-project-key-fn ',code-compass-calculate-coupling-project-key-fn)
(setq code-compass-authors-colors ',code-compass-authors-colors)
(setq code-compass-exclude-directories ',code-compass-exclude-directories)
(setq code-compass-preferred-browser ,code-compass-preferred-browser)
(setq code-compass-snapshot-periods ',code-compass-snapshot-periods)
(setq code-compass-default-periods ',code-compass-default-periods)
(setq code-compass-tmp-directory ',code-compass-tmp-directory)
(setq code-compass-docker-data-directory ',code-compass-docker-data-directory)
(setq code-compass-download-directory ',code-compass-download-directory)
(setq code-compass-default-port ',code-compass-default-port)
(let ((browse-url-browser-function #'browse-url-generic)
(browse-url-generic-program ,code-compass-preferred-browser))
(condition-case err
(funcall ',command ,repository ,date ',authors)
(error
(funcall ',command ,repository ,date)))))
`(lambda (result)
(when (not ,do-not-serve) (code-compass--run-server-and-navigate ,(expand-file-name repository) (or ,port code-compass-default-port))))))
;;;###autoload
(defun code-compass-show-hotspots-sync (repository date &optional port)
"Show REPOSITORY enclosure diagram for hotspots starting at DATE.
Optionally served at PORT."
(interactive
(list
(read-directory-name "Choose git repository directory:" (vc-root-dir))
(call-interactively #'code-compass-request-date)
code-compass-default-port))
(code-compass--in-temp-directory
repository
(--> repository
(code-compass-produce-git-report it date)
code-compass--produce-code-maat-revisions-report
code-compass--produce-cloc-report
code-compass--generate-merger-script
code-compass--generate-d3-v3-lib
code-compass--produce-json
code-compass--generate-host-enclosure-diagram-html
(code-compass--run-server-and-navigate it port))))
(define-obsolete-function-alias 'c/show-hotspots-sync #'code-compass-show-hotspots-sync "0.1.2")
;;;###autoload
(defun code-compass-show-hotspots (repository date &optional port)
"Show REPOSITORY enclosure diagram for hotspots.
Starting DATE reduces scope of Git log and
PORT define where the html is served."
(interactive
(list
(read-directory-name "Choose git repository directory:" (vc-root-dir))
(call-interactively #'code-compass-request-date)))
(code-compass--async-run #'code-compass-show-hotspots-sync repository date port))
(define-obsolete-function-alias 'c/show-hotspots #'code-compass-show-hotspots "0.1.2")
(defun code-compass-show-hotspot-snapshot-sync (repository)
"Snapshot COMMAND over REPOSITORY over the last year every three months."
(interactive
(list
(read-directory-name "Choose git repository directory:" (vc-root-dir))))
(--each code-compass-snapshot-periods (code-compass-show-hotspots-sync repository (code-compass-request-date it) code-compass-default-port)))
(define-obsolete-function-alias 'c/show-hotspot-snapshot-sync #'code-compass-show-hotspot-snapshot-sync "0.1.2")
;; BEGIN indentation
(defun code-compass--split-on-newlines (code)
"Split CODE over newlines."
(s-split "\n" code))
(defun code-compass--remove-empty-lines (lines)
"Remove empty LINES.
>> (code-compass--remove-empty-lines '(\"line\" \" \" \" \" \"\"))
=> (\"line\")\""
(--remove (eq (length (s-trim it)) 0) lines))
(defun code-compass--remove-text-after-indentation (lines)
"Remove text in LINES that is not indentation characters..
>> (code-compass--remove-text-after-indentation '(\" one indent\"))
=> (\" \")"
(--map
(apply #'string (--take-while (or (eq ?\s it) (eq ?\t it)) (string-to-list it)))
lines))
(defun code-compass--find-indentation (lines-without-text)
"Infer indentation level in LINES-WITHOUT-TEXT.
If no indentation present in file, defaults to 2.
>> (code-compass--find-indentation '(\" \" \" \" \" \"))
=> 3
>> (code-compass--find-indentation '(\" \"))
=> 1"
(or (--> lines-without-text
(--map (list (s-count-matches "\s" it) (s-count-matches "\t" it)) it)
(let ((spaces-ind (-sort #'< (--remove (eq 0 it) (-map 'code-compass--first it))))
(tabs-ind (-sort #'< (--remove (eq 0 it) (-map 'code-compass--second it)))))
(if (> (length spaces-ind) (length tabs-ind))
(code-compass--first spaces-ind)
(code-compass--first tabs-ind))))
2))
(defun code-compass--convert-tabs-to-spaces (line-without-text n)
"Replace tabs in LINE-WITHOUT-TEXT with N spaces."
(s-replace "\t" (make-string n ?\s) line-without-text))
(defun code-compass--calculate-complexity (line-without-text indentation)
"Calculate indentation complexity.
This divides length of LINE-WITHOUT-TEXT by INDENTATION.
>> (code-compass--calculate-complexity \" \" 2)
=> 2.0"
(/ (+ 0.0 (length line-without-text)) indentation))
(defun code-compass--as-logical-indents (lines &optional opts)
"Calculate logical indentations of LINES.
Try to infer how many space is an indent unless OPTS provides it."
(let ((indentation (or opts (code-compass--find-indentation lines))))
(list
(--map
(--> it
(code-compass--convert-tabs-to-spaces it indentation)
(code-compass--calculate-complexity it indentation))
lines)
indentation)))
(defun code-compass--stats-from (complexities-indentation)
"Return stats from COMPLEXITIES-INDENTATION."
(let* ((complexities (code-compass--first complexities-indentation))
(mean (/ (-sum complexities) (length complexities)))
(sd (sqrt (/ (-sum (--map (expt (- it mean) 2) complexities)) (length complexities)))))
`((total . ,(-sum complexities))
(n-lines . ,(length complexities))
(max . ,(-max complexities))
(mean . ,mean)
(standard-deviation . ,sd)
(used-indentation . ,(code-compass--second complexities-indentation)))))
(defun code-compass-calculate-complexity-stats (code &optional opts)
"Return complexity of CODE based on indentation.
If OPTS is provided, use these settings to define what is the indentation.
Return nil for empty CODE.
>> (-take 3 (code-compass-calculate-complexity-stats \"1\"))
=> ((total . 0.0) (n-lines . 1) (max . 0.0))"
(ignore-errors
(--> code
;; TODO maybe add line numbers, so that I can also open the most troublesome (max-c) line automatically?
code-compass--split-on-newlines
code-compass--remove-empty-lines
code-compass--remove-text-after-indentation
(code-compass--as-logical-indents it opts)
code-compass--stats-from)))
(define-obsolete-function-alias 'c/calculate-complexity-stats #'code-compass-calculate-complexity-stats "0.1.2")
;;;###autoload
(defun code-compass-calculate-complexity-current-buffer (&optional indentation)
"Calculate complexity of the current buffer contents.
Optionally you can provide the INDENTATION level of the file. The
code can infer it automatically."
(interactive)
(code-compass-calculate-complexity-stats
(buffer-substring-no-properties (point-min) (point-max)) indentation))
(define-obsolete-function-alias 'c/calculate-complexity-current-buffer #'code-compass-calculate-complexity-current-buffer "0.1.2")
;; END indentation
;; BEGIN complexity over commits
(defun code-compass--retrieve-commits-up-to-date-touching-file (file &optional date)
"Retrieve list of commits touching FILE from DATE."
(s-split
"\n"
(shell-command-to-string
(s-concat
"git log --format=format:%H --reverse "
(if date
(s-concat "--after=" date " ")
"")
file))))
(defun code-compass--retrieve-file-at-commit-with-git (file commit)
"Retrieve FILE contents at COMMIT."
(let* ((git-dir (with-current-buffer (find-file-noselect file)
(expand-file-name (vc-root-dir))))
(git-file
(string-remove-prefix
git-dir
(expand-file-name file))))
(shell-command-to-string (format "git show %s:\"%s\"" commit git-file))))
(defun code-compass--git-hash-to-date (commit)
"Return the date of the COMMIT.
Note this is the date of merging in, not of the code change."
(s-replace "\n" "" (shell-command-to-string (s-concat "git show --no-patch --no-notes --pretty='%cd' --date=short " commit))))
(defun code-compass--calculate-complexity-over-commits (file &optional opts)
"Calculate complexity of FILE over commits.
Optional argument OPTS defines things like the indentation to use."
(--> (call-interactively #'code-compass-request-date)
(code-compass--retrieve-commits-up-to-date-touching-file file it)
(--map
(--> it
(list it (code-compass--retrieve-file-at-commit-with-git file it))
(list (code-compass--first it) (code-compass-calculate-complexity-stats (code-compass--second it) opts)))
it)))
(define-obsolete-function-alias 'c/calculate-complexity-over-commits #'code-compass--calculate-complexity-over-commits "0.1.2")
(defun code-compass--plot-csv-file-with-graph-cli (file)
"Plot CSV FILE with graph-cli."
(async-shell-command
(format "graph --xtick-angle 90 %s" file)))
(defun code-compass--plot-lines-with-graph-cli (data)
"Plot DATA from lists as a graph."
(let ((tmp-file (format "%s/data-file-graph-cli.csv" code-compass-tmp-directory)))
(with-temp-file tmp-file
(insert "commit-date,total-complexity,loc\n")
(insert (s-join "\n" (--map (s-replace-all '((" " . ",") ("(" . "") (")" . "")) (format "%s" it)) data))))
(code-compass--plot-csv-file-with-graph-cli tmp-file)))
;;;###autoload
(defun code-compass-show-complexity-over-commits (file &optional opts)
"Make a graph plotting complexity out of a FILE.
Optionally give file indentation in OPTS."
(interactive (list (read-file-name "Select file:" nil nil nil (buffer-file-name))))
(code-compass--plot-lines-with-graph-cli
(--map
(list (code-compass--git-hash-to-date (code-compass--first it))
(alist-get 'total (code-compass--second it))
(alist-get 'n-lines (code-compass--second it)))
(code-compass--calculate-complexity-over-commits file opts))))
(define-obsolete-function-alias 'c/show-complexity-over-commits #'code-compass-show-complexity-over-commits "0.1.2")
;; END complexity over commits
;; BEGIN code churn
(defun code-compass--produce-code-maat-abs-churn-report (repository)
"Create code-maat abs-churn report for REPOSITORY."
(code-compass--run-code-maat "abs-churn" repository)
repository)
(defun code-compass-show-code-churn-sync (repository date)
"Show how much code was added and removed from REPOSITORY from a DATE."
(interactive (list
(read-directory-name "Choose git repository directory:" (vc-root-dir))
(call-interactively #'code-compass-request-date)))
(code-compass--in-temp-directory
repository
(progn
(--> repository
(code-compass-produce-git-report it date)
code-compass--produce-code-maat-abs-churn-report)
(code-compass--plot-csv-file-with-graph-cli "abs-churn.csv"))))
(define-obsolete-function-alias 'c/show-code-churn-sync #'code-compass-show-code-churn-sync "0.1.2")
;;;###autoload
(defun code-compass-show-code-churn (repository date)
"Show how much code was added and removed from REPOSITORY from a DATE."
(interactive (list
(read-directory-name "Choose git repository directory:" (vc-root-dir))
(call-interactively #'code-compass-request-date)))
(code-compass--async-run #'code-compass-show-code-churn-sync repository date nil 't))
(define-obsolete-function-alias 'c/show-code-churn #'code-compass-show-code-churn "0.1.2")
;; END complexity over commits
;; BEGIN change coupling
(defun code-compass--produce-code-maat-coupling-report (repository)
"Create code-maat coupling report for REPOSITORY."
(code-compass--run-code-maat "coupling" repository)
repository)
(defun code-compass--generate-coupling-json-script (repository)
"Generate script to produce a weighted graph for REPOSITORY."
(code-compass--copy-file "./scripts/coupling_csv_as_edge_bundling.py" (code-compass--temp-dir repository))
repository)
(defun code-compass--produce-coupling-json (repository)
"Produce coupling json needed by d3 for REPOSITORY."
(message "Produce coupling json...")
(code-compass--shell-command-error-handler
"python3 coupling_csv_as_edge_bundling.py --coupling coupling.csv > edgebundling.json"
"*code-compass--produce-coupling-json-errors*")
repository)
(defun code-compass--generate-host-edge-bundling-html (repository)
"Generate host html from REPOSITORY."
(code-compass--copy-file "./pages/edge-bundling/script.js" (code-compass--temp-dir repository))
(code-compass--copy-file "./pages/edge-bundling/style.css" (code-compass--temp-dir repository))
(code-compass--copy-file "./pages/edge-bundling/zoomable.html" (code-compass--temp-dir repository))
repository)
(defun code-compass-show-coupling-graph-sync (repository date &optional port)
"Show REPOSITORY edge bundling synchronously for code coupling up to DATE.
Serve graph on PORT."
(interactive (list
(read-directory-name "Choose git repository directory:" (vc-root-dir))
(call-interactively #'code-compass-request-date)
8888))
(code-compass--in-temp-directory
repository
(--> repository
(code-compass-produce-git-report it nil date)
code-compass--produce-code-maat-coupling-report
code-compass--generate-coupling-json-script
code-compass--generate-d3-v4-lib
code-compass--produce-coupling-json
code-compass--generate-host-edge-bundling-html
(code-compass--run-server-and-navigate it port))))
(define-obsolete-function-alias 'c/show-coupling-graph-sync #'code-compass-show-coupling-graph-sync "0.1.2")
;;;###autoload
(defun code-compass-show-coupling-graph (repository date &optional port)
"Show REPOSITORY edge bundling for code coupling up to DATE. Serve graph on PORT."
(interactive (list
(read-directory-name "Choose git repository directory:" (vc-root-dir))
(call-interactively #'code-compass-request-date)))
(code-compass--async-run #'code-compass-show-coupling-graph-sync repository date port))
(define-obsolete-function-alias 'c/show-coupling-graph #'code-compass-show-coupling-graph "0.1.2")
;; END change coupling
;; BEGIN find coupled files
(defun code-compass--add-filename-to-analysis-columns (repository analysis)
"Add filepath from REPOSITORY to ANALYSIS columns."
(--> analysis
(s-split "\n" it 't)
;(--remove (s-blank? (s-trim it)) it)
(-concat
(list (car it))
(--map
(--> (s-split "," it)
(-concat
(list (s-concat repository "/" (code-compass--first it)))
(list
(if (or (null (code-compass--second it)) (not (s-contains-p "/" (code-compass--second it))))
(code-compass--second it)
(s-concat repository "/" (code-compass--second it))))
(cdr (cdr it)))
(s-join "," it))
(cdr it)))))
(defun code-compass--get-coupling-alist-sync (repository)
"Get list of coupled files in REPOSITORY async."
(code-compass--in-temp-directory
repository
(--> repository
(code-compass-produce-git-report it nil)
code-compass--produce-code-maat-coupling-report)
(--> (code-compass--get-analysis-as-string-from-csv "coupling")
(code-compass--add-filename-to-analysis-columns repository it)
(--map (s-split "," it) (cdr it)))))
(defun code-compass--get-coupling-alist (repository fun)
"FUN takes a list of coupled files in REPOSITORY."
(async-start
`(lambda ()
(setq load-path ',load-path)
(load-file ,(symbol-file 'code-compass--get-coupling-alist))
(code-compass--get-coupling-alist-sync ,repository))
fun))
(defvar code-compass-coupling-project-map
(make-hash-table :test 'equal)
"Hash table to contain coupling files list.")
(defun code-compass--get-coupled-files-alist (repository fun)
"Run FUN on the coupled files for REPOSITORY."
(let* ((key (funcall code-compass-calculate-coupling-project-key-fn repository))
(code-compass-files (gethash key code-compass-coupling-project-map)))
(if code-compass-files
(funcall fun code-compass-files)
(message "Building coupling cache asynchronously...")
(code-compass--get-coupling-alist
repository
`(lambda (result-files)
(message "Coupling Cache Built")
(puthash ,key result-files code-compass-coupling-project-map)
(funcall ,fun result-files)
;; Save cache to local storage to access it faster next time if nothing has changed
(with-temp-file ,code-compass-cache-file
(prin1 code-compass-coupling-project-map (current-buffer))))))))
(defun code-compass-clear-coupling-project-map ()
"Clear `code-compass-coupling-project-map' and deletes cache file."
(interactive)
(clrhash code-compass-coupling-project-map)
(delete-file code-compass-cache-file))
(define-obsolete-function-alias 'c/clear-coupling-project-map #'code-compass-clear-coupling-project-map "0.1.2")
(defun code-compass-get-coupled-files-alist-hook-fn ()
"Calculate coupled files asynchronously."
(let ((git-root (ignore-errors (vc-root-dir))))
(when git-root
(code-compass--get-coupled-files-alist
git-root
`(lambda (x)
(message
"Finished to update coupled files for %s and found %s coupled files."
,git-root
(length x)))))))
(define-obsolete-function-alias 'c/get-coupled-files-alist-hook-fn #'code-compass-get-coupled-files-alist-hook-fn "0.1.2")
(defun code-compass--coupling-completions (file-name coupled-files root)
"Get a list of files coupled to FILE-NAME.
The coupling information is provided by COUPLED-FILES.
ROOT is the VCS project path.
>> (code-compass--coupling-completions \"\" nil \"\")
=> nil
>> (code-compass--coupling-completions
(concat code-compass-path-to-code-compass \"code-compass.el\")
(list
(list
(concat code-compass-path-to-code-compass \"/README.org\")
\"code-compass.el\"
31
65))
code-compass-path-to-code-compass)
=> (\"README.org\")"
(let ((default-directory root)
(root (s-chop-suffixes '("/" "/" "/") root)))
(--> coupled-files
(--sort (> (string-to-number (nth 3 it)) (string-to-number (nth 3 other))) it) ;; sort by number of commits
(--sort (> (string-to-number (nth 2 it)) (string-to-number (nth 2 other))) it) ;; sort then by how often this file has changed
(-keep
(lambda (file)
(let ((src-coupled-file-name (expand-file-name (car file)))
(target-coupled-file-name-src (expand-file-name (nth 1 file)))
(file-name (expand-file-name file-name)))
(when (and
(file-exists-p src-coupled-file-name)
(file-exists-p target-coupled-file-name-src)
(or (string= file-name src-coupled-file-name)
(string= file-name target-coupled-file-name-src)))
(s-replace ; this replace is just removing the root prefix, so the completions are human readable
(concat root "/")
""
(expand-file-name
(car
;; this picks only the coupled files, ignoring the file we are matching against (file-name)
(--remove
(string= (expand-file-name file-name) (expand-file-name it))