Skip to content

Commit

Permalink
Merge pull request logseq#5420 from logseq/enhance/graph-parser-part-…
Browse files Browse the repository at this point in the history
…four

Enhance: Finish up graph-parser
  • Loading branch information
tiensonqin authored Jun 2, 2022
2 parents 26ef041 + 29f7354 commit f49a28f
Show file tree
Hide file tree
Showing 81 changed files with 1,536 additions and 851 deletions.
4 changes: 1 addition & 3 deletions .carve/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
;; Ignore b/c too many false positives
frontend.db
;; Used for debugging
frontend.db.debug
;; carve doesn't detect nbb only usage
logseq.graph-parser.log]
frontend.db.debug]
:report {:format :ignore}}
17 changes: 6 additions & 11 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
{:unresolved-symbol {:exclude [goog.DEBUG
goog.string.unescapeEntities
;; TODO:lint: Fix when fixing all type hints
object
;; TODO: Remove parse-* and update-* when https://github.com/clj-kondo/clj-kondo/issues/1694 is done
parse-long
parse-double
parse-uuid
update-keys
update-vals]}
object]}
;; TODO:lint: Remove node-path excludes once we have a cleaner api
:unresolved-var {:exclude [frontend.util/node-path.basename
frontend.util/node-path.dirname
Expand All @@ -26,10 +20,12 @@
frontend.db.query-react query-react
frontend.util util
frontend.util.property property
frontend.util.text text-util
frontend.config config
frontend.format.mldoc mldoc
frontend.format.block block
frontend.handler.extract extract
logseq.graph-parser graph-parser
logseq.graph-parser.text text
logseq.graph-parser.block gp-block
logseq.graph-parser.mldoc gp-mldoc
Expand All @@ -39,12 +35,10 @@
logseq.graph-parser.date-time-util date-time-util}}}

:hooks {:analyze-call {rum.core/defc hooks.rum/defc
rum.core/defcs hooks.rum/defcs}}
rum.core/defcs hooks.rum/defcs}}
:lint-as {promesa.core/let clojure.core/let
promesa.core/loop clojure.core/loop
promesa.core/recur clojure.core/recur
garden.def/defstyles clojure.core/def
garden.def/defkeyframes clojure.core/def
rum.core/defcc rum.core/defc
rum.core/defcontext clojure.core/def
clojure.test.check.clojure-test/defspec clojure.core/def
Expand All @@ -55,4 +49,5 @@
frontend.namespaces/import-vars potemkin/import-vars
;; src/test
frontend.react/defc clojure.core/defn}
:skip-comments true}
:skip-comments true
:output {:progress true}}
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ jobs:
yarn cljs:test
node static/tests.js
- name: Run nbb tests for graph-parser
run: yarn nbb-logseq -cp src/main: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
run: bb test:load-namespaces-with-nbb

lint:
runs-on: ubuntu-latest
Expand Down
113 changes: 113 additions & 0 deletions .github/workflows/graph-parser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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:
- 'deps/graph-parser/**'
- '!deps/graph-parser/**.md'
pull_request:
branches: [master]
paths:
- 'deps/graph-parser/**'
- '!deps/graph-parser/**.md'

env:
CLOJURE_VERSION: '1.10.1.727'
# setup-java@v2 dropped support for legacy Java version syntax.
# This is the same as 1.8.
JAVA_VERSION: '8'
# This is the latest node version we can run.
NODE_VERSION: '16'
BABASHKA_VERSION: '0.8.2'

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'yarn'
cache-dependency-path: deps/graph-parser/yarn.lock

- name: Set up Java
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: ${{ env.JAVA_VERSION }}

- name: Set up Clojure
uses: DeLaGuardo/setup-clojure@master
with:
cli: ${{ env.CLOJURE_VERSION }}

- name: Setup Babashka
uses: turtlequeue/[email protected]
with:
babashka-version: ${{ env.BABASHKA_VERSION }}

- name: Clojure cache
uses: actions/cache@v2
id: clojure-deps
with:
path: |
~/.m2/repository
~/.gitlibs
key: ${{ runner.os }}-clojure-deps-${{ hashFiles('deps.edn') }}
restore-keys: ${{ runner.os }}-clojure-deps-

- name: Fetch Clojure deps
if: steps.clojure-deps.outputs.cache-hit != 'true'
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: 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 namespaces into nbb-logseq
run: bb test:load-all-namespaces-with-nbb deps/graph-parser src

lint:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Java
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: ${{ env.JAVA_VERSION }}

- name: Set up Clojure
uses: DeLaGuardo/setup-clojure@master
with:
cli: ${{ env.CLOJURE_VERSION }}

- name: Setup Babashka
uses: turtlequeue/[email protected]
with:
babashka-version: ${{ env.BABASHKA_VERSION }}

- name: Run clj-kondo lint
run: cd deps/graph-parser && clojure -M:clj-kondo --parallel --lint src test

- 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}'
6 changes: 5 additions & 1 deletion CODEBASE_OVERVIEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ After cloning the [Logseq repository](https://github.com/logseq/logseq), there a

- `src/main/frontend/` contains code that powers the Logseq editor. Folders and files inside are organized by features or functions. For example, `components` contains all the UI components and `handler` contains all the event-handling code. You can explore on your own interest.

- `src/main/logseq/` contains the api used by plugins and the graph-parser.
- `src/main/logseq/` contains the api used by plugins.

- `deps/` contains dependencies or libraries used by the frontend.

- `deps/graph-parser/` is a library that parses a Logseq graph and saves it to a database.

## Data Flow

Expand Down
22 changes: 19 additions & 3 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
:deps
{org.babashka/spec.alpha
{:git/url "https://github.com/babashka/spec.alpha"
:sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}}
:sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}
logseq/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
{clj-kondo/clj-kondo {:version "2022.02.09"}}
{clj-kondo/clj-kondo {:version "2022.02.09"}
org.babashka/fswatcher {:version "0.0.3"}}
:tasks
{dev:watch
logseq.tasks.dev/watch
Expand All @@ -26,9 +33,18 @@
dev:lint
logseq.tasks.dev/lint

test:load-nbb-compatible-namespaces
nbb:watch
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

test:load-all-namespaces-with-nbb
logseq.tasks.nbb/load-all-namespaces

lang:list
logseq.tasks.lang/list-langs

Expand Down
6 changes: 3 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.2"}
instaparse/instaparse {:mvn/version "1.4.10"}
nubank/workspaces {:mvn/version "1.1.1"}
frankiesardo/linked {:mvn/version "1.3.0"}
org.clojars.mmb90/cljs-cache {:mvn/version "0.1.4"}}
org.clojars.mmb90/cljs-cache {:mvn/version "0.1.4"}
logseq/graph-parser {:local/root "deps/graph-parser"}}

:aliases {:cljs {:extra-paths ["src/dev-cljs/" "src/test/" "src/electron/"]
:extra-deps {org.clojure/clojurescript {:mvn/version "1.11.54"}
Expand All @@ -47,5 +47,5 @@
:main-opts ["-m" "shadow.cljs.devtools.cli"]}

;; Use :replace-deps for tools. See https://github.com/clj-kondo/clj-kondo/issues/1536#issuecomment-1013006889
:clj-kondo {:replace-deps {clj-kondo/clj-kondo {:mvn/version "2022.01.13"}}
:clj-kondo {:replace-deps {clj-kondo/clj-kondo {:mvn/version "2022.05.28"}}
:main-opts ["-m" "clj-kondo.main"]}}}
9 changes: 9 additions & 0 deletions deps/graph-parser/.carve/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{:paths ["src"]
:api-namespaces [
;; carve doesn't detect nbb only usage
logseq.graph-parser.log
;; Used by logseq but not worth splitting up
logseq.graph-parser.db.schema
;; Used in tests
logseq.graph-parser.test.docs-graph-helper]
:report {:format :ignore}}
4 changes: 4 additions & 0 deletions deps/graph-parser/.carve/ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
;; For CLI
logseq.graph-parser.cli/parse-graph
;; For CLI
logseq.graph-parser.mldoc/ast-export-markdown
13 changes: 13 additions & 0 deletions deps/graph-parser/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{:linters
{:consistent-alias
{:aliases {datascript.core d
logseq.graph-parser graph-parser
logseq.graph-parser.text text
logseq.graph-parser.block gp-block
logseq.graph-parser.mldoc gp-mldoc
logseq.graph-parser.util gp-util
logseq.graph-parser.property gp-property
logseq.graph-parser.config gp-config
logseq.graph-parser.date-time-util date-time-util}}}
:skip-comments true
:output {:progress true}}
3 changes: 3 additions & 0 deletions deps/graph-parser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.clj-kondo/.cache
cljs-test-runner-out
/test/docs
63 changes: 63 additions & 0 deletions deps/graph-parser/README.md
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.
23 changes: 23 additions & 0 deletions deps/graph-parser/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{:paths ["src"]
:deps
;; Deps should be kept in sync with https://github.com/logseq/nbb-logseq/blob/main/bb.edn
{datascript/datascript {:mvn/version "1.3.8"}
frankiesardo/linked {:mvn/version "1.3.0"}
com.andrewmcveigh/cljs-time {:git/url "https://github.com/logseq/cljs-time" ;; fork
:sha "5704fbf48d3478eedcf24d458c8964b3c2fd59a9"}
;; stubbed in nbb
com.lambdaisland/glogi {:mvn/version "1.1.144"}
;; built in to nbb
cljs-bean/cljs-bean {:mvn/version "1.5.0"}}

:aliases
;; This runs tests with nodejs. Would be nice to run this with in a browser env
;; since this is how its normally run in the app but this requires more setup
;; with karma, shadow-cljs.edn and headless mode on CI
{:test {:extra-paths ["test"]
:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}
org.clojure/clojurescript {:mvn/version "1.11.54"}}
:main-opts ["-m" "cljs-test-runner.main"]}

:clj-kondo {:replace-deps {clj-kondo/clj-kondo {:mvn/version "2022.05.28"}}
:main-opts ["-m" "clj-kondo.main"]}}}
11 changes: 11 additions & 0 deletions deps/graph-parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@logseq/graph-parser",
"version": "1.0.0",
"private": true,
"devDependencies": {
"@logseq/nbb-logseq": "^0.5.103"
},
"dependencies": {
"mldoc": "^1.3.3"
}
}
Loading

0 comments on commit f49a28f

Please sign in to comment.