summaryrefslogtreecommitdiff
path: root/build.hy
blob: 85465189ee71a07d6aaa5b13fd32f28450aa5523 (plain)
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
#!/usr/bin/env hy

;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.org>
;;;
;;; 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/>.

(require [hy.contrib.walk [let]])

(import [ast [parse Return]])
(import [copy [deepcopy]])
(import [os [getenv]])
(import [os.path [splitext]])
(import [sys [argv exit]])

(import [hy.compiler [hy-compile]])
(import [hy.lex [hy-parse]])

(import DUCK.Serpentine.visitor)
(import [DUCK.Tools [buildhelper]])

(defn walk-ast [node]
  "Normalize the syntax tree for compatibility with DUCK."
  (when (and (isinstance node Return)
             (= node.value.id "None"))
    (setv node.value.id None))
  (when (hasattr node "body")
    (for [child node.body]
      (walk-ast child))))

(defn my-hy-compile [source]
  "Return a normalized syntax tree for SOURCE."
  (let [ast (hy-compile (hy-parse source) "<unknown>")]
    (walk-ast ast)
    ast))

(defclass HyVisitor [DUCK.Serpentine.visitor.Visitor]
  (defn load [self file-name &optional [imports None]]
    (let [imports (or imports {})]
      (setv self.imports imports)
      (setv self.file-name- file-name)
      (try
        (with [source (open file-name :encoding (.get-coding self file-name))]
          (setv self.root (if (.endswith file-name ".hy")
                              (my-hy-compile (.read source))
                              (parse (.read source)))))
        (except [err SyntaxError]
          (.write stderr (.format "In '{}': " file-name))
          (raise err)))
      (let [module (.visit self self.root)]
        (assoc self.imports (first (splitext file-name)) module)
        (return module)))))

(setv DUCK.Serpentine.visitor.Visitor HyVisitor)

(when (= __name__ "__main__")
  (let [app-name "HelloHy"
        package-name "com.example.hellohy"
        res-files {"drawable" {"ic_launcher" "icon.svg"}}
        code-file "hello.hy"
        include-paths []
        layout None
        features []
        permissions []
        docs-dir (getenv "DOCS_DIR")
        args (deepcopy argv)]
    (exit (.main buildhelper __file__ app-name package-name
                 res-files layout code-file include-paths
                 features permissions args :docs-dir docs-dir))))