Skip to content

Commit

Permalink
Merge pull request #55 from berty/html-mod-cli
Browse files Browse the repository at this point in the history
feat: module creation cli
  • Loading branch information
n0izn0iz authored Feb 12, 2022
2 parents 1130845 + 307533e commit 960cfc8
Show file tree
Hide file tree
Showing 49 changed files with 2,510 additions and 1,399 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ all:
clean.gen:
for api in $$(ls api); do rm -fr \
go/pkg/$${api}/$${api}_grpc.pb.go \
go/pkg/$${api}/ipfsman.pb.go \
go/pkg/$${api}/$${api}.pb.go \
rn/src/api/$${api} \
; done
rm -f go/bind/labs/modules.go
rm -f rn/src/api/index.ts
.PHONY: clean-gen

Expand All @@ -29,6 +30,7 @@ generate:
echo "export * as $${api} from './$${api}/v1'" >> rn/src/api/index.ts; \
done
$(MAKE) -C rn lint.fix
cd rn && npx ts-node src/create-mod/gen-go-modules-list
.PHONY: generate

regen: clean.gen
Expand Down
67 changes: 64 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,80 @@ TODO

## Getting Started

TODO
See [Modules](#modules) if you don't want to dive into the code

### Troubleshooting

_(please use [issues](https://github.com/berty/REPLACEME))_

## Development

TODO
If you want to quickly try native mobile IPFS without writing any go, make an [HTML module](#html-module)

### Architecture

TODO
TODO: Explain modules architecture

## Modules<a id='modules'></a>

Modules are automatically added to the home tool list in the app

They allow you to run custom Go or JavaScript and programatically access a Gomobile-IPFS backed IPFS shell on mobile very quickly

You don't need to know JavaScript to create or run a Go module and you don't need to know Go to create or run a JavaScript module

### HTML<a id='html-module'></a>

HTML modules in a nutshell:
- Living at `rn/html-mods/`
- Statically served at the root of a Go `http.FileServer` started automatically by the Labs bridge
- Accessed with a `react-native-webview` pointed at the embedded static server (the `rn/src/screens/HTMLModule.tsx` screen)
- If the build of an HTML module fails, it will be skipped and building the app will continue

Create a new Labs HTML module by running

```sh
cd rn
make create-module
```

And choosing one of `bare`, `git` or `react`

It will ask you a few questions and create the module boilerplate, every step is logged so you can understand what is going on

If you choose the `react` preset, you can use the dev-server from mobile with the `Browser` Labs tool

Or:
- Create a directory at `rn/html-mods/<your-module-name>`
- Add a Makefile at `rn/html-mods/<your-module-name>/Makefile` with the first rule creating:
- The `rn/html-mods.bundle/<your-module-name>/index.html` root site file
- The `rn/html-mods.bundle/<your-module-name>/info.json` file containing a JSON representation of the `blmod.ModuleInfo` type

### Go

Go modules in a nutshell:
- Living at `go/mod/`
- Need to be registered in `go/bind/labs/modules.go` which can be done automatically by running `make generate`
- Accessed with a generic UI that allows to run them, cancel runs and view their output (the `rn/src/screens/GoModule.tsx` screen)
- If the build of a registered Go module fails, it will abort building the app

Create a new Labs Go module by running

```sh
cd rn
make create-module
```

And choosing `go` as preset

Or:
- Create a Go module at `go/mod/<your-module-name>`
- Implement the `berty.tech/labs/go/pkg/blmod.Module` interface
- Register the module in `go/bind/labs/modules.go`

To develop a Go module faster, you can:
- `go run ./go/cmd/daemon` to spawn a CLI Labs instance
- `go run ./go/cmd/client` to access the modules with a CLI

### Testing

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/ipfs/go-ds-flatfs v0.5.1
github.com/ipfs/go-ds-leveldb v0.5.0
github.com/ipfs/go-ds-sql v0.3.0
github.com/ipfs/go-ipfs-api v0.3.0
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-config v0.18.0
github.com/multiformats/go-multiaddr v0.5.0
Expand Down Expand Up @@ -91,7 +92,6 @@ require (
github.com/ipfs/go-fs-lock v0.0.7 // indirect
github.com/ipfs/go-graphsync v0.11.0 // indirect
github.com/ipfs/go-ipfs v0.11.0 // indirect
github.com/ipfs/go-ipfs-api v0.3.0 // indirect
github.com/ipfs/go-ipfs-blockstore v0.2.1 // indirect
github.com/ipfs/go-ipfs-cmds v0.6.0 // indirect
github.com/ipfs/go-ipfs-delay v0.0.1 // indirect
Expand Down Expand Up @@ -256,3 +256,5 @@ require (
lukechampine.com/blake3 v1.1.6 // indirect
nhooyr.io/websocket v1.8.6 // indirect
)

replace golang.org/x/mobile => github.com/aeddi/mobile v0.0.3-silicon
8 changes: 3 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/Stebalien/go-bitfield v0.0.1 h1:X3kbSSPUaJK60wV2hjOPZwmpljr6VGCqdq4cB
github.com/Stebalien/go-bitfield v0.0.1/go.mod h1:GNjFpasyUVkHMsfEOk8EFLJ9syQ6SI+XWrX9Wf2XH0s=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/aeddi/mobile v0.0.3-silicon h1:wOm6xG8FinN1kQW8aQNZAS9QWMUOPstVU6WV1iIEI4A=
github.com/aeddi/mobile v0.0.3-silicon/go.mod h1:jFTmtFYCV0MFtXBU+J5V/+5AUeVS0ON/0WkE/KSrl6E=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -1570,7 +1572,6 @@ golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek=
Expand All @@ -1595,10 +1596,6 @@ golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRu
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816 h1:jhDgkcu3yQ4tasBZ+1YwDmK7eFmuVf1w1k+NGGGxfmE=
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
Expand Down Expand Up @@ -1852,6 +1849,7 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
Expand Down
7 changes: 0 additions & 7 deletions go/bind/labs/labs.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,3 @@ func (b *Labs) Close() error {
}
return nil
}

func safeLogger(l *zap.Logger) *zap.Logger {
if l == nil {
l = zap.NewNop()
}
return l
}
3 changes: 2 additions & 1 deletion go/bind/labs/modules.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package labs

import (
"berty.tech/labs/go/mod/datastorebench"
"berty.tech/labs/go/pkg/blmod"

"berty.tech/labs/go/mod/datastorebench"
)

var modules = []blmod.ModuleFactory{
Expand Down
4 changes: 3 additions & 1 deletion rn/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ android/app/gradlew.bat
/gomobile-ipfs/
/ios/vendored_pods/
/react-native-labs-bridge/ios/Labsbridge.xcframework
/html-mods.bundle
/html-mods.bundle
/html-mods/*/repo
.gomobile-cache
18 changes: 13 additions & 5 deletions rn/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ html-mods=$(wildcard html-mods/*)

html-mods.all:
mkdir -p html-mods.bundle
@for mod in $(html-mods); do echo "--- Making HTML module $$(basename $$mod) ---"; make -C $$mod; done
@for mod in $(html-mods); do echo "--- Making HTML module $$(basename $$mod) ---"; SKIP_PREFLIGHT_CHECK=true make -C $$mod; done
@echo --- Done making HTML modules ---
.PHONY: html-mods.all

Expand All @@ -38,11 +38,14 @@ ios: node_modules/.mkt ios/Pods/.mkt html-mods.all
$(IOS_RN_FLAGS)

$(IOS_CORE): ../go.mod ../go.sum $(shell find ../go -name '*.go')
$(GO) mod download
$(GO) install golang.org/x/mobile/cmd/gomobile
$(GO) install golang.org/x/mobile/cmd/gobind
$(GO) run golang.org/x/mobile/cmd/gomobile init -v
$(GO) run golang.org/x/mobile/cmd/gomobile bind $(GOMOBILE_OPT) -v -target=ios,iossimulator -o $(IOS_CORE) $(CORE_PACKAGE)
CGO_CPPFLAGS=-DHAVE_GETHOSTUUID=0 $(GO) run golang.org/x/mobile/cmd/gomobile bind \
$(GOMOBILE_OPT) \
-v \
-target=ios \
-o $(IOS_CORE) \
-cache "$(PWD)/ios/.gomobile-cache/labs" \
$(CORE_PACKAGE)

.link:
cd react-native-labs-bridge && yarn link
Expand Down Expand Up @@ -96,6 +99,11 @@ lint.fix: node_modules/.mkt
metro.start: node_modules/.mkt
npx react-native start --port=$(METRO_RN_PORT)


.PHONY: create-module
create-module: node_modules/.mkt
SKIP_PREFLIGHT_CHECK=true npx ts-node -TH --files ./src/create-mod

.PHONY: clean
clean:
$(MAKE) unlink
Expand Down
2 changes: 2 additions & 0 deletions rn/html-mods/ipfs-upload-file/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ DIST_DIR=$(REPO_ROOT)/dist
$(INDEX_HTML): $(REPO_TARGET) $(NODE_MODULES_TARGET) info.json
rm -fr $(DIST_DIR)
cd $(REPO_ROOT) && npm run build

rm -fr $(BUILD_TARGET)
mkdir -p $(BUNDLE_ROOT)
cp -r $(DIST_DIR) $(BUILD_TARGET)
cp info.json $(BUILD_TARGET)/info.json

Expand Down
4 changes: 2 additions & 2 deletions rn/html-mods/ipfs-upload-file/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"displayName": "IPFS File Upload",
"shortDescription": "Upload a file to IPFS",
"iconKind": "UTF",
"iconUTF": "⬆️"
"iconKind": 1,
"iconData": "4qyG77iP"
}
2 changes: 2 additions & 0 deletions rn/html-mods/ipns-publish/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ DIST_DIR=$(REPO_ROOT)/dist
$(INDEX_HTML): $(REPO_TARGET) $(NODE_MODULES_TARGET) info.json
rm -fr $(DIST_DIR)
cd $(REPO_ROOT) && npm run build

rm -fr $(BUILD_TARGET)
mkdir -p $(BUNDLE_ROOT)
cp -r $(DIST_DIR) $(BUILD_TARGET)
cp info.json $(BUILD_TARGET)/info.json

Expand Down
4 changes: 2 additions & 2 deletions rn/html-mods/ipns-publish/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"displayName": "IPNS Publish",
"shortDescription": "Publish to IPNS using JS and Go",
"iconKind": "UTF",
"iconUTF": "📰"
"iconKind": 1,
"iconData": "8J+TsA=="
}
4 changes: 4 additions & 0 deletions rn/html-mods/react-typescript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ DIST_DIR=build
SOURCES=$(shell find src public)

$(INDEX_HTML): $(NODE_MODULES_TARGET) $(SOURCES) info.json
@echo --- Build Module ---
rm -fr $(DIST_DIR)
npm run build

@echo --- Install Module ---
rm -fr $(BUILD_TARGET)
mkdir -p $(BUNDLE_ROOT)
cp -r $(DIST_DIR) $(BUILD_TARGET)
cp info.json $(BUILD_TARGET)/info.json

Expand Down
8 changes: 4 additions & 4 deletions rn/html-mods/react-typescript/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"displayName": "React Example",
"shortDescription": "Example React module with TypeScript",
"iconKind": "UTF",
"iconUTF": ""
"displayName": "React TypeScript",
"iconKind": 1,
"iconData": "8J+Uiw==",
"shortDescription": "Example React module with TypeScript"
}
Loading

0 comments on commit 960cfc8

Please sign in to comment.