root/main.scm

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
(use-modules
 (srfi srfi-1) ;; map/fold
 (srfi srfi-19) ;; date/time
 (srfi srfi-26) ;; partial application
 (srfi srfi-69) ;; hash-table
 (sxml simple)
 (ice-9 match)
 (generate-git-svg)
 (generate-svg-gregorian)
 (generate-svg-discord))

(define (write-svg name css-files data)
  (with-output-to-file name
    (lambda ()
      (for-each (lambda (css-file)
                  (display (string-append
                            "<?xml-stylesheet type=\"text/css\" href=\""
                            css-file
                            "\" ?>")))
                css-files)
      (sxml->xml data))))

(define (parse-arguments args)
  (define program (car args))
  (define rest (cdr args))
  (if (null? rest)
      default-arguments
      (parse-arguments-itr (car rest) (cdr rest) default-arguments)))
(define (parse-arguments-itr arg rest acc)
  (define (next-arg) (if (null? rest) #f (car rest)))
  (define (next-rest) (if (null? rest) '() (cdr rest)))
  (define (next-arg2) (if (null? (next-rest)) #f (car (next-rest))))
  (define (next-rest2) (if (null? (next-rest)) '() (cdr (next-rest))))
  (define (get-acc key)
    (define result (assoc key acc))
    (if result (cdr result) #f))
  (define (continue new-arg new-rest new-acc)
    (if (not new-arg)
        new-acc
        (parse-arguments-itr new-arg new-rest new-acc)))
  (match arg
    ("--year"
     (continue (next-arg2)
               (next-rest2)
               (cons (cons 'year (string->number (next-arg)))
                     acc)))
    ("--output"
     (continue (next-arg2)
               (next-rest2)
               (cons (cons 'output (next-arg))
                     acc)))
    ("-o"
     (continue (next-arg2)
               (next-rest2)
               (cons (cons 'output (next-arg))
                     acc)))
    ("--calendar"
     (continue (next-arg2)
               (next-rest2)
               (cons (cons 'calendar
                           (cons (string->symbol (next-arg))
                                 (get-acc 'directories)))
                     acc)))
    ("--css"
     (continue (next-arg2)
               (next-rest2)
               (cons (cons 'css
                           (cons (next-arg)
                                 (get-acc 'directories)))
                     acc)))
    ("--help" (display "Exports your git activity for a year into a svg.

\x1B[1mHOW TO USE:\x1B[0m
    git-log-calendar [list of git project directories, ...]
                     [--year 1984] # creates calendar for the year 1984
                     # By default both gregorian and discordian svgs
                       are generated, just to spite you, and to counterbalance
                       those idiots at redhat who removed ddate from util-linux.
                       Corkscrew them.
                       Use --calendar gregorian to only create one svg.
                     [--calendar discordian]
                     [--calendar gregorian]
                     [--output | -o out.svg] # only works for one calendar atm
                     # The svg will include this css
                     [--css svg.css]
\x1B[1mEXAMPLE:\x1B[0m
    ./git-log-calendar ~/some-repo/ ~/some-other-repo/
    ./git-log-calendar .
    ./git-log-calendar ~/some-repo/.git
    ./git-log-calendar ~/some-repo/ --css ../css/svg.css --year 2015\\
                       --output /tmp/greg --calendar gregorian

\x1B[1mSEE ALSO\x1B[0m
       ddate(1),
       \x1B[3mMalaclypse the Younger\x1B[0m, \x1B[4mPrincipia Discordia, Or How I Found Goddess\x1B[0m
                               \x1B[4mAnd What I Did To Her When I Found Her\x1B[0m
       \x1B[1mRobert Shea\x1B[0m and \x1B[1mRobert Anton Wilson\x1B[0m, The \x1B[2m\x1B[4m\x1B[53mIlluminatus!\x1B[0m Trilogy
")
     (exit 2))
    (else (continue (next-arg)
                    (next-rest)
                    (cons (cons 'directories
                                (cons arg
                                      (get-acc 'directories)))
                          acc)))))

(define default-arguments
  `((year . ,(date-year (current-date)))
    (directories . ())
    (css . ())))

(define (main args)
  (define arguments (parse-arguments args))
  (define (get-arg key)
    (define result (assoc key arguments))
    (if result (cdr result) #f))
  (define directories (get-arg 'directories))
  (define year (get-arg 'year))
  (define days (fold (cut generate-days-table year <> <>)
                     (make-hash-table)
                     directories))
  (define gyear-name (number->string year))
  (define dyear-name (number->string (+ year 1166)))
  (define goutput (or (get-arg 'output)
                      (string-append "img/git-activity-" gyear-name ".svg")))
  (define doutput (or (get-arg 'output)
                      (string-append "img/git-discord-" dyear-name ".svg")))
  (define create-all-outputs? (not (get-arg 'calendar)))

  (when (null? directories)
    (display "Error: Please call with a list of repo directories\n")
    (display "Call ./git-log-calendar --help for more info")
    (display "Example:\n")
    (display "./git-log-calendar . ~/some-repo/ ~/some-other-repo/.git\n")
    (exit 1))

  (when (not (file-exists? "img/"))
    (mkdir "img"))

  (when (or create-all-outputs?
            (member 'gregorian (get-arg 'calendar))
            (member 'greg (get-arg 'calendar)))
    (write-svg goutput
               (get-arg 'css)
               (generate-svg generate-svg-gregorian year days)))
  (when (or create-all-outputs?
            (member 'discordian (get-arg 'calendar))
            (member 'disc (get-arg 'calendar)))
    (write-svg doutput
               (get-arg 'css)
               (generate-svg generate-svg-discord year days))))