Moved nbb tasks to another repo

They are useful outside of logseq development and don't need to be
embedded in the Logseq app
pull/5420/head
Gabriel Horner 2022-06-01 01:17:25 -04:00
parent b74b64bc8b
commit 29f73549aa
7 changed files with 13 additions and 130 deletions

9
bb.edn
View File

@ -4,7 +4,9 @@
{:git/url "https://github.com/babashka/spec.alpha"
:sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}
logseq/bb-tasks
{:local/root "deps/bb-tasks"}
#_{:local/root "../bb-tasks"}
{:git/url "https://github.com/logseq/bb-tasks"
:git/sha "4b3e623fb475cacb992425aa9dac770d6dd63e38"}
logseq/graph-parser
{:local/root "deps/graph-parser"}}
:pods
@ -32,7 +34,10 @@
logseq.tasks.dev/lint
nbb:watch
logseq.bb-tasks.nbb.watch/watch-dir
logseq.bb-tasks.nbb.watch/watch
nbb:portal-watch
logseq.bb-tasks.nbb.watch/portal-watch
test:load-namespaces-with-nbb
logseq.tasks.nbb/load-compatible-namespaces

View File

@ -1,33 +0,0 @@
## Description
Library of reusable https://github.com/babashka/babashka tasks
## Install
Add a git dependency to your `bb.edn`:
```clojure
:deps
{logseq/bb-tasks
{:git/url "https://github.com/logseq/logseq"
:git/sha "FILL IN"
:deps/root "deps/bb-tasks"}}
```
## Usage
### nbb:watch
Given a graph directory and an nbb script, the nbb script will run when either the
script or a file in the directory is saved.
For example, from root of logseq repo, run the following:
```
$ bb nbb:watch /path/to/graph deps/graph-parser/examples/parse_file.cljs
Watching /path/to/graph ...
```
See [this demo
clip](https://www.loom.com/share/20debb49fdd64e77ae83056289750b0f) to see it in
action.

View File

@ -1,6 +0,0 @@
{:paths ["src"]
:pods
{org.babashka/fswatcher {:version "0.0.3"}}
:tasks
{nbb:watch
logseq.bb-tasks.nbb.watch/watch-dir}}

View File

@ -1,2 +0,0 @@
;; Allows for this lib to be pulled in as a gitlib
{}

View File

@ -1,35 +0,0 @@
(ns logseq.bb-tasks.nbb.watch
"To use tasks in this ns, first install nbb-logseq:
`npm install -g @logseq/nbb-logseq`"
(:require [pod.babashka.fswatcher :as fw]
[babashka.tasks :refer [shell]]
[babashka.classpath :as classpath]))
(def last-file (atom nil))
(defn- run-script
[nbb-script dir file]
(shell "nbb-logseq -cp" (classpath/get-classpath) nbb-script dir file))
(defn watch-dir
"Watch a graph dir and nbb script and run nbb script when either changes.
Nbb takes graph dir and last modified graph file.
NOTE: If the script fails, the watcher stops watching"
[& args]
(when-not (= 2 (count args))
(throw (ex-info "Usage: $0 DIR NBB-SCRIPT" {})))
(let [[dir nbb-script] args]
(println "Watching" dir "...")
(fw/watch dir
(fn [event]
;; Don't use :chmod as it sometimes triggers twice on osx
(when (#{:write|chmod :write} (:type event))
(run-script nbb-script dir (:path event))
(reset! last-file (:path event))))
{:recursive true})
;; Get live-editing experience by re-parsing last file
(fw/watch nbb-script
(fn [event]
(when (#{:write|chmod :write} (:type event))
(run-script nbb-script dir @last-file))))
(deref (promise))))

View File

@ -1,52 +0,0 @@
(ns parse-file
(:require [logseq.graph-parser.cli :as gp-cli]
[clojure.pprint :as pprint]
[datascript.core :as d]))
(defn- colorize-or-pretty-print
[results]
(if (zero? (.-status (gp-cli/sh ["which" "puget"] {})))
(gp-cli/sh ["puget"] {:input (pr-str results)
:stdio ["pipe" "inherit" "inherit"]})
(pprint/pprint results)))
(defn- get-all-page-properties
[db]
(->> (d/q '[:find (pull ?b [*])
:where
[?b :block/properties]]
db)
(map first)
(map (fn [m] (zipmap (keys (:block/properties m)) (repeat 1))))
(apply merge-with +)
(into {})))
(defn- analyze-file
[db file]
(let [results (map first
(d/q '[:find (pull ?b [:db/id :block/content])
:in $ ?path
:where
[?b :block/page ?page]
[?page :block/file ?file]
[?file :file/path ?path]]
db
file))]
(colorize-or-pretty-print results)
(println "Block count:" (count results))
(println "Properties count:" (get-all-page-properties db))))
(defn -main
"Prints blocks for given file along with basic file stats"
[& args]
(when-not (= 2 (count args))
(throw (ex-info "Usage: $0 DIR FILE" {})))
(println "Parsing...")
(let [[dir file] args
{:keys [conn]} (gp-cli/parse-graph dir
{:verbose false
:files [{:file/path file
:file/content (gp-cli/slurp file)}]})]
(analyze-file @conn file)))
(apply -main *command-line-args*)

View File

@ -135,3 +135,9 @@ Specs should go under `src/main/frontend/spec/` and be compatible with clojure
and clojurescript. See `frontend.spec.storage` for an example. By following
these conventions, specs should also be usable by babashka. This is helpful as it
allows for third party tools to be written with logseq's data model.
## Development Tools
There are some babashka tasks under `nbb:` which are useful for inspecting
database changes in realtime. See [these
docs](https://github.com/logseq/bb-tasks#logseqbb-tasksnbbwatch) for more info.