aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/janet-pm
blob: 9c9fba3d432ca12aa9cd116252810d70fbad19c5 (plain) (blame)
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
#!/usr/bin/env janet

(import spork/argparse)
(import spork/path)
(import spork/pm)
(import spork/sh)
# Do not import declare-cc here - keep a clean separation for faster load times

(def help-text
    ```
    usage: janet-pm [--key=value, --flag] ... [subcommand] [args] ...

    Run from a directory containing a project.janet file to perform
    operations on a project, or from anywhere to do operations on the
    global module cache (modpath).  Commands that need write permission to
    the modpath are considered privileged commands - in some environments
    they may require super user privileges.  Other project-level commands
    need to have a ./project.janet file in the current directory.

    GLOBAL COMMANDS:

    help
        Show this help text.

    install (repo or name)...
        Install remote bundles, and any required dependencies.

    clear-cache
        Clear the git cache. Useful for updating dependencies.

    list-pkgs (search)
        List packages in the remote package listing that the contain the
        string search. If no search pattern is given, prints the
        entire package listing.

    new-project name
        Create a new Janet project in a directory `name`.

    new-c-project name
        Create a new C+Janet project in a directory `name`.

    new-exe-project name
        Create a new project for an executable in a directory `name`.

    PER PROJECT COMMANDS:

    deps
        Install dependencies for the current project.

    install (repos...)
        Install artifacts of the current project. Shorthand for `janet --install .`

    uninstall (repos...)
        Uninstall the current project's artifacts. Shorthand for `janet --uninstall {current-project-name }`

    prune
        Remove any bundles that have no dependents and are marked for auto-remove

    build
        Build all artifacts in the build/ directory. Shorthand for `run build`.

    clean
        Remove any generated files or artifacts. Calls the `(clean)` hook.

    test
        Run tests. Tests should be .janet files in the test/ directory
        relative to project.janet. Will patch the module paths to load
        built native code without installing it. Shorthand for `run check`.

    quickbin entry output
        Create an executable file from a script, statically linking in any dependencies found while compiling
        the script. The script should contain a main function that will serve as the main function for the generated
        executable.

    save-lockfile (dest)
        Save all currently installed bundles to a lockfile

    load-lockfile (src)
        Install all bundles in the given lockfile.

    ENVIRONMENT VARIABLES:

    janet-pm inherits all of the environment variables used by janet, as well some of its own. Many of these are
    only used in support of building bundles that use a project.janet.

    JANET_BINPATH
        Where to install executables and scripts. By default, will store them at in $SYSPATH/bin.

    JANET_BUILD_DIR
        Where to create build outputs when building a bundle has a project.janet. Defaults to _build.

    JANET_BUILD_TYPE
        What kind of build to make when building a bundle that has a project.janet.
        Should be "develop", "release", or "debug".

    JANET_CURL
        Where to get the "curl" command when handling project.janet. Defaults to "curl".

    JANET_GIT
        Where to get the "git" command when handling project.janet. Defaults to "git".

    JANET_MANPATH
        Where to install manpages. By default, will store them at in $SYSPATH/man.

    JANET_OFFLINE
        If set to 1, true, on, etc., will only look at packages in the local cache.

    JANET_PKGLIST
        The package listing repository to use to resolve package nicknames. Defaults to https://github.com/janet-lang/pkgs.git

    JANET_PREFIX
        Where to look for Janet headers and libjanet.so, janet.dll, etc. By default, will try to autodetect by searching through the
        syspath, the PREFIX environment variable, /usr/, and /usr/local. Most installs should not need to set this, but otherwise should
        be set to whatever PREFIX was when janet was installed.

    JANET_TAR
        Where to get the "tar" command. Defaults to "tar".

    JANET_TOOLCHAIN
        Name of the toolchain to use to compile project.janet based natives. Should be one of "gcc", "clang", "msvc", or "cc".
        Defaults to autodetecting based on the presence of other environment variabls MSVC, GCC, CLANG, and CC.
        Will then finally default to the value of `(os/compiler)`.

    VERBOSE
        Print full output from running commands

    WORKERS
        Number of processes to run in parallel when compiling C and C++ source code.

    ```)

(defn help
  []
  (print help-text))

(defn do-hook
  [hook & args]
  (pm/local-hook hook @{} ;args))

(defn build
  []
  (pm/local-hook "build" @{}))

(defn run
  [task]
  (pm/local-hook "run" task))

(defn clean
  []
  (pm/local-hook "clean" @{}))

(defn clean-all
  []
  (pm/local-hook "clean-all" @{}))

(defn install
  [& repo]
  (if (empty? repo)
    (pm/pm-install "file::." :force-update true :no-deps true)
    (each rep repo (pm/pm-install rep :force-update true))))

(defn uninstall
  [& repo]
  (if (empty? repo)
    (let [m (pm/load-project-meta ".")]
      (bundle/uninstall (get m :name)))
    (each rep repo
      (bundle/uninstall rep))))

(defn test
  []
  (pm/local-hook "check" @{}))

(defn deps
  []
  (pm/pm-install "file::." :no-install true))

(defn clear-cache
  []
  (def cache (path/join (dyn *syspath*) ".cache"))
  (sh/rm cache))

(defn list-pkgs
  [&opt search]
  (def [ok _] (module/find "pkgs"))
  (unless ok
    (eprint "no local package listing found. Run `jpm update-pkgs` to get listing.")
    (os/exit 1))
  (def pkgs-mod (require "pkgs"))
  (def ps
    (seq [p :keys (get-in pkgs-mod ['packages :value] [])
          :when (if search (string/find search p) true)]
      p))
  (sort ps)
  (each p ps (print p)))

(defn update-pkgs
  []
  (pm/pm-install (dyn pm/*pkglist* pm/default-pkglist)))

(defn load-lockfile
  [&opt path]
  (default path "lockfile.jdn")
  (pm/load-lockfile path))

(defn save-lockfile
  [&opt path]
  (default path "lockfile.jdn")
  (pm/save-lockfile path))

(defn new-project
  "Create a new project"
  [name]
  (pm/scaffold-project name {:c false}))

(defn new-c-project
  "Create a new C project"
  [name]
  (pm/scaffold-project name {:c true}))

(defn new-exe-project
  "Create a new executable project"
  [name]
  (pm/scaffold-project name {:c false :exe true}))

(defn quickbin
  "Create an executable file"
  [entry output]
  # Lazy load
  (def module
    (require (if (dyn :install-time-syspath)
               "@install-time-syspath/spork/declare-cc"
               "spork/declare-cc")))
  (def quickbin-fn (module/value module "quickbin"))
  (quickbin-fn entry output))

(def subcommands
  {"build" build
   "clean" clean
   "clean-all" clean-all
   "help" help
   "install" install
   "test" test
   "help" help
   "deps" deps
   "hook" do-hook
   "new-project" new-project
   "new-c-project" new-c-project
   "new-exe-project" new-exe-project
   "load-lockfile" load-lockfile
   "save-lockfile" save-lockfile
   "quickbin" quickbin
   "list-pkgs" list-pkgs
   "clear-cache" clear-cache
   "run" run
   "prune" bundle/prune
   "update-pkgs" update-pkgs
   "uninstall" uninstall})

(defn main
  [&]

  (def ap
    (argparse/argparse
      help-text

      "syspath"
      {:short "m"
       :kind :option
       :help "Set the syspath"}

      :default
      {:kind :accumulate
       :help "Commands to run"}))

  # Break on help text
  (unless ap (break))
  (unless (ap :default) (break (help)))
  (when-let [sp (get ap :syspath)] (put root-env *syspath* sp))

  # Do configuration
  (pm/read-env-variables root-env)

  (def [sub-command & command-args] (ap :default))
  (def cmd (get subcommands sub-command))
  (assertf cmd "unknown command %v, --help for help" sub-command)
  (cmd ;command-args))