forked from logseq/logseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Parser now parses all graph files like the app does, not just pages and journals. This required extracting another fn from repo-handler - Add and tweak CI steps that are specific to graph-parser. All namespaces in this library are checked for nbb compatibility - Cleaned up parser cli API so only one fn is needed for scripts - Tests were updated to match new parsing behavior - large_vars.clj can run with a smaller max-line-count after only refactoring two fns - Add docs
- Loading branch information
1 parent
1e29905
commit b142327
Showing
30 changed files
with
338 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
name: logseq graph-parser CI | ||
|
||
on: | ||
# Path filters ensure jobs only kick off if a change is made to graph-parser | ||
push: | ||
branches: [master] | ||
paths: | ||
|
@@ -47,10 +48,10 @@ jobs: | |
with: | ||
cli: ${{ env.CLOJURE_VERSION }} | ||
|
||
# - name: Setup Babashka | ||
# uses: turtlequeue/[email protected] | ||
# with: | ||
# babashka-version: ${{ env.BABASHKA_VERSION }} | ||
- name: Setup Babashka | ||
uses: turtlequeue/[email protected] | ||
with: | ||
babashka-version: ${{ env.BABASHKA_VERSION }} | ||
|
||
- name: Clojure cache | ||
uses: actions/cache@v2 | ||
|
@@ -64,20 +65,20 @@ jobs: | |
|
||
- name: Fetch Clojure deps | ||
if: steps.clojure-deps.outputs.cache-hit != 'true' | ||
run: clojure -A:test -P | ||
run: cd deps/graph-parser && clojure -A:test -P | ||
|
||
- name: Fetch yarn deps | ||
run: cd deps/graph-parser && yarn install --frozen-lockfile | ||
|
||
- name: Run ClojureScript tests | ||
run: clojure -M:test | ||
run: cd deps/graph-parser && clojure -M:test | ||
|
||
- name: Run nbb-logseq tests | ||
run: cd deps/graph-parser && yarn nbb-logseq -cp src:test -m logseq.graph-parser.nbb-test-runner/run-tests | ||
|
||
# # In this job because it depends on an npm package | ||
# - name: Load nbb compatible namespaces | ||
# run: bb test:load-nbb-compatible-namespaces | ||
# In this job because it depends on an npm package | ||
- name: Load namespaces into nbb-logseq | ||
run: bb test:load-all-namespaces-with-nbb deps/graph-parser src | ||
|
||
lint: | ||
runs-on: ubuntu-latest | ||
|
@@ -105,8 +106,8 @@ jobs: | |
- name: Run clj-kondo lint | ||
run: cd deps/graph-parser && clojure -M:clj-kondo --parallel --lint src test | ||
|
||
- name: Lint for vars that are too large | ||
run: scripts/large_vars.clj deps/graph-parser/src | ||
|
||
- name: Carve lint for unused vars | ||
run: cd deps/graph-parser && ../../scripts/carve.clj | ||
|
||
- name: Lint for vars that are too large | ||
run: scripts/large_vars.clj deps/graph-parser/src '{:max-lines-count 75}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
## Description | ||
|
||
This library parses a logseq graph directory and returns it as a datascript | ||
database connection. This library powers the Logseq app and also runs from the | ||
commandline, _independent_ of the app. This is powerful as this can run anywhere | ||
that a Node.js script has access to a Logseq graph e.g. on CI processes like | ||
Github Actions. This library is compatible with ClojureScript and with | ||
[nbb-logseq](https://github.com/logseq/nbb-logseq) to respectively provide | ||
frontend and commandline functionality. | ||
|
||
## API | ||
|
||
This library is under the parent namespace `logseq.graph-parser`. This library | ||
provides two main namespaces for parsing, `logseq.graph-parser` and | ||
`logseq.graph-parser.cli`. `logseq.graph-parser/parse-file` is the main fn for | ||
the frontend. `logseq.graph-parser.cli/parse-graph` is the main fn for node.js | ||
CLIs. | ||
|
||
## Usage | ||
|
||
See `logseq.graph-parser.cli-test` for now. A real world example is coming soon. | ||
|
||
## Dev | ||
|
||
This follows the practices that [the Logseq frontend | ||
follows](/docs/dev-practices.md). Most of the same linters are used, with | ||
configurations that are specific to this library. See [this library's CI | ||
file](/.github/workflows/graph-parser.yml) for linting examples. | ||
|
||
### Setup | ||
|
||
To run linters and tests, you'll want to install yarn dependencies once: | ||
``` | ||
yarn install | ||
``` | ||
|
||
This step is not needed if you're just running the application. | ||
|
||
### Testing | ||
|
||
Since this file is compatible with cljs and nbb-logseq, tests are run against both languages. | ||
|
||
ClojureScript tests use https://github.com/Olical/cljs-test-runner. To run tests: | ||
``` | ||
clojure -M:test | ||
``` | ||
|
||
To see available options that can run specific tests or namespaces: `clojure -M:test --help` | ||
|
||
To run nbb-logseq tests: | ||
``` | ||
yarn nbb-logseq -cp src:test -m logseq.graph-parser.nbb-test-runner/run-tests | ||
``` | ||
|
||
### Managing dependencies | ||
|
||
The package.json dependencies are just for testing and should be updated if there is | ||
new behavior to test. | ||
|
||
The deps.edn dependecies are used by both ClojureScript and nbb-logseq. Their | ||
versions should be backwards compatible with each other with priority given to | ||
the frontend. _No new dependency_ should be introduced to this library without | ||
an understanding of the tradeoffs of adding this to nbb-logseq. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,66 @@ | ||
(ns logseq.graph-parser.cli | ||
"Ns only for use by CLIs as it uses node.js libraries" | ||
"Primary ns to parse graphs with node.js based CLIs" | ||
(:require ["fs" :as fs] | ||
["child_process" :as child-process] | ||
[clojure.edn :as edn] | ||
[logseq.graph-parser :as graph-parser])) | ||
[clojure.string :as string] | ||
[logseq.graph-parser :as graph-parser] | ||
[logseq.graph-parser.config :as gp-config] | ||
[logseq.graph-parser.db :as gp-db])) | ||
|
||
(defn- slurp | ||
"Like clojure.core/slurp" | ||
[file] | ||
(str (fs/readFileSync file))) | ||
|
||
(defn- sh | ||
"Run shell cmd synchronously and print to inherited streams by default. Aims | ||
to be similar to babashka.tasks/shell | ||
TODO: Fail fast when process exits 1" | ||
[cmd opts] | ||
(child-process/spawnSync (first cmd) | ||
(clj->js (rest cmd)) | ||
(clj->js (merge {:stdio "inherit"} opts)))) | ||
|
||
(defn build-graph-files | ||
"Given a git graph directory, returns allowed file paths and their contents in | ||
preparation for parsing" | ||
[dir] | ||
(let [files (->> (str (.-stdout (sh ["git" "ls-files"] | ||
{:cwd dir :stdio nil}))) | ||
string/split-lines | ||
(map #(hash-map :file/path (str dir "/" %))) | ||
graph-parser/filter-files)] | ||
(mapv #(assoc % :file/content (slurp (:file/path %))) files))) | ||
|
||
(defn- read-config | ||
"Commandline version of frontend.handler.common/read-config without graceful | ||
handling of broken config. Config is assumed to be at $dir/logseq/config.edn " | ||
[dir] | ||
(if (fs/existsSync (str dir "/logseq/config.edn")) | ||
(-> (str dir "/logseq/config.edn") fs/readFileSync str edn/read-string) | ||
{})) | ||
(let [config-file (str dir "/" gp-config/app-name "/config.edn")] | ||
(if (fs/existsSync config-file) | ||
(-> config-file fs/readFileSync str edn/read-string) | ||
{}))) | ||
|
||
(defn- parse-files | ||
[conn files {:keys [config] :as options}] | ||
(let [extract-options (merge {:date-formatter (gp-config/get-date-formatter config)} | ||
(select-keys options [:verbose]))] | ||
(doseq [{:file/keys [path content]} files] | ||
(graph-parser/parse-file conn path content {:extract-options extract-options})))) | ||
|
||
(defn parse | ||
"Main entry point for parsing" | ||
[dir db files] | ||
(graph-parser/parse db | ||
files | ||
{:config (read-config dir)})) | ||
(defn parse-graph | ||
"Parses a given graph directory and returns a datascript connection and all | ||
files that were processed. The directory is parsed as if it were a new graph | ||
as it can't assume that the metadata in logseq/ is up to date. Directory is | ||
assumed to be using git" | ||
([dir] | ||
(parse-graph dir {})) | ||
([dir options] | ||
(let [files (build-graph-files dir) | ||
conn (gp-db/start-conn) | ||
config (read-config dir)] | ||
(println "Parsing" (count files) "files...") | ||
(parse-files conn files (merge options {:config config})) | ||
{:conn conn | ||
:files (map :file/path files)}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.