-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from NatLabs/renaming
[feat] add new options object to support field key renaming
- Loading branch information
Showing
66 changed files
with
1,016 additions
and
219 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 |
---|---|---|
|
@@ -16,4 +16,3 @@ | |
# frontend code | ||
node_modules/ | ||
dist/ | ||
|
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,160 @@ | ||
#!/usr/bin/env zx | ||
|
||
import fs, { createReadStream, existsSync, statSync } from "fs"; | ||
import events from "events"; | ||
import { readFile, stat } from "fs/promises"; | ||
import readline from "readline"; | ||
import { spawn } from "child_process"; | ||
import path from "path"; | ||
// import { chalk } from "zx"; | ||
// import 'zx/globals' | ||
|
||
Array.prototype.last = function () { | ||
return this[this.length - 1]; | ||
}; | ||
|
||
const getImports = async (file) => { | ||
let rl = readline.createInterface({ | ||
input: fs.createReadStream(file), | ||
crlfDelay: Infinity, | ||
}); | ||
|
||
let import_paths = []; | ||
|
||
rl.on("line", (line) => { | ||
if (line.startsWith("import")) { | ||
let path = line.split(" ").last(); | ||
path = path.slice(1, path.length - 2); | ||
import_paths.push(path); | ||
} else if (line.length !== 0 && !line.startsWith("//")) { | ||
rl.close(); | ||
rl.removeAllListeners(); | ||
} | ||
}); | ||
|
||
await events.once(rl, "close"); | ||
|
||
return import_paths; | ||
}; | ||
|
||
const wasm_path = (file) => { | ||
let segments = file.split("/"); | ||
segments.splice(1, 0, ".wasm"); | ||
let basename = segments.last().replace(".mo", ".wasm"); | ||
segments[segments.length - 1] = basename; | ||
return segments.join("/"); | ||
}; | ||
|
||
let test_files = await glob("./test?(s)/**/*.(test|Test).mo"); | ||
|
||
let moc = (await $`dfx cache show`).stdout.toString().trim() + "/moc"; | ||
let mops_sources = (await $`mops sources`).stdout.toString().split("\n"); | ||
|
||
let packages = {}; | ||
let package_args = []; | ||
|
||
|
||
for (const source of mops_sources) { | ||
let segments = source.split(" "); | ||
package_args.push(...segments); | ||
packages[segments[1]] = segments[2]; | ||
} | ||
|
||
const compile_motoko = async (src, dest) => { | ||
await $`${moc} ${package_args} -wasi-system-api ${src} -o ${dest}`; | ||
}; | ||
|
||
if (test_files.length) { | ||
let wasm_dir = test_files[0].split("/")[0] + "/.wasm"; | ||
await $`mkdir -p ${wasm_dir}`; | ||
} | ||
|
||
const last_modified_cache = {}; | ||
|
||
const is_recently_modified = (file, time) => { | ||
let cached_mtime = last_modified_cache[file]; | ||
|
||
if (cached_mtime) { | ||
return cached_mtime > time; | ||
} | ||
|
||
let file_mtime = statSync(file).mtimeMs; | ||
last_modified_cache[file] = file_mtime; | ||
|
||
return file_mtime > time; | ||
}; | ||
|
||
const is_dep_tree_recently_modified = async (file, wasm_mtime, visited) => { | ||
let modified = is_recently_modified(file, wasm_mtime); | ||
|
||
if (modified) { | ||
return true; | ||
} | ||
|
||
let imports = await getImports(file); | ||
|
||
// console.log({file, imports}) | ||
|
||
for (let imp_path of imports) { | ||
if (imp_path.startsWith("mo:")) { | ||
let pkg = imp_path.slice(3).split("/")[0]; | ||
let pkg_path = packages[pkg]; | ||
|
||
pkg_path = (await glob(pkg_path + "/**/*.mo"))[0]; | ||
|
||
if (pkg_path && !visited.has(pkg_path)) { | ||
visited.add(pkg_path); | ||
modified = is_recently_modified(pkg_path, wasm_mtime); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
imp_path = path.resolve(path.dirname(file), imp_path); | ||
|
||
if (existsSync(imp_path.concat(".mo"))) { | ||
imp_path = imp_path.concat(".mo"); | ||
} else { | ||
imp_path = imp_path.concat("/lib.mo"); | ||
} | ||
|
||
if (visited.has(imp_path)) { continue; } | ||
|
||
visited.add(imp_path); | ||
|
||
// console.log({imp_path}) | ||
|
||
if (await is_dep_tree_recently_modified(imp_path, wasm_mtime, visited)) { | ||
return true | ||
}; | ||
|
||
} | ||
|
||
return false; | ||
}; | ||
|
||
const compile_test = async (test_file) => { | ||
const wasm_file = wasm_path(test_file); | ||
|
||
let should_compile = !existsSync(wasm_file); | ||
|
||
if (!should_compile) { | ||
let wasm_mtime = statSync(wasm_file).mtimeMs; | ||
should_compile = await is_dep_tree_recently_modified(test_file, wasm_mtime, new Set()); | ||
} | ||
|
||
if (should_compile) { | ||
console.log(`Compiling ${test_file}`); | ||
await compile_motoko(test_file, wasm_file); | ||
} | ||
|
||
return wasm_file; | ||
} | ||
|
||
const wasm_files = await Promise.all( | ||
test_files.map(compile_test) | ||
); | ||
|
||
for (const wasm_file of wasm_files) { | ||
await $`wasmtime ${wasm_file}`; | ||
} |
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,5 @@ | ||
{ | ||
"version": 1, | ||
"dfx": "0.12.1", | ||
"defaults": { | ||
"build": { | ||
"packtool": "mops sources", | ||
|
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,25 @@ | ||
# Candid/Decoder | ||
|
||
## Type `Options` | ||
``` motoko no-repl | ||
type Options = { renameKeys : [(Text, Text)] } | ||
``` | ||
|
||
|
||
## Function `decode` | ||
``` motoko no-repl | ||
func decode(blob : Blob, record_keys : [Text], options : ?Options) : [Candid] | ||
``` | ||
|
||
Decodes a blob encoded in the candid format into a list of the [Candid](./Types.mo#Candid) type in motoko | ||
|
||
### Inputs | ||
- **blob** - A blob encoded in the candid format | ||
**record_keys** - The record keys to use when decoding a record. | ||
**options** - An optional arguement to specify options for decoding. | ||
|
||
## Function `fromArgs` | ||
``` motoko no-repl | ||
func fromArgs(args : [Arg], recordKeyMap : TrieMap.TrieMap<Nat32, Text>) : [Candid] | ||
``` | ||
|
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,19 @@ | ||
# Candid/Encoder | ||
|
||
## Function `encode` | ||
``` motoko no-repl | ||
func encode(candid_values : [Candid]) : Blob | ||
``` | ||
|
||
|
||
## Function `encodeOne` | ||
``` motoko no-repl | ||
func encodeOne(candid : Candid) : Blob | ||
``` | ||
|
||
|
||
## Function `toArgs` | ||
``` motoko no-repl | ||
func toArgs(candid_values : [Candid]) : [Arg] | ||
``` | ||
|
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,7 @@ | ||
# Candid/Parser/Array | ||
|
||
## Function `arrayParser` | ||
``` motoko no-repl | ||
func arrayParser(valueParser : () -> Parser<Char, Candid>) : Parser<Char, Candid> | ||
``` | ||
|
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,7 @@ | ||
# Candid/Parser/Blob | ||
|
||
## Function `blobParser` | ||
``` motoko no-repl | ||
func blobParser() : Parser<Char, Candid> | ||
``` | ||
|
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,7 @@ | ||
# Candid/Parser/Bool | ||
|
||
## Function `boolParser` | ||
``` motoko no-repl | ||
func boolParser() : Parser<Char, Candid> | ||
``` | ||
|
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,49 @@ | ||
# Candid/Parser/Common | ||
|
||
## Function `ignoreSpace` | ||
``` motoko no-repl | ||
func ignoreSpace<A>(parser : P.Parser<Char, A>) : P.Parser<Char, A> | ||
``` | ||
|
||
|
||
## Function `removeUnderscore` | ||
``` motoko no-repl | ||
func removeUnderscore<A>(parser : P.Parser<Char, A>) : P.Parser<Char, List<A>> | ||
``` | ||
|
||
|
||
## Function `any` | ||
``` motoko no-repl | ||
func any<T>() : Parser<T, T> | ||
``` | ||
|
||
|
||
## Function `hexChar` | ||
``` motoko no-repl | ||
func hexChar() : Parser<Char, Char> | ||
``` | ||
|
||
|
||
## Function `consIf` | ||
``` motoko no-repl | ||
func consIf<T, A>(parserA : Parser<T, A>, parserAs : Parser<T, List<A>>, cond : (A, List<A>) -> Bool) : Parser<T, List<A>> | ||
``` | ||
|
||
|
||
## Function `fromHex` | ||
``` motoko no-repl | ||
func fromHex(char : Char) : Nat8 | ||
``` | ||
|
||
|
||
## Function `toText` | ||
``` motoko no-repl | ||
func toText(chars : List<Char>) : Text | ||
``` | ||
|
||
|
||
## Function `listToNat` | ||
``` motoko no-repl | ||
func listToNat(digits : List<Char>) : Nat | ||
``` | ||
|
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,7 @@ | ||
# Candid/Parser/Float | ||
|
||
## Function `floatParser` | ||
``` motoko no-repl | ||
func floatParser() : Parser<Char, Candid> | ||
``` | ||
|
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,13 @@ | ||
# Candid/Parser/Int | ||
|
||
## Function `intParser` | ||
``` motoko no-repl | ||
func intParser() : Parser<Char, Candid> | ||
``` | ||
|
||
|
||
## Function `parseInt` | ||
``` motoko no-repl | ||
func parseInt() : Parser<Char, Int> | ||
``` | ||
|
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,7 @@ | ||
# Candid/Parser/IntX | ||
|
||
## Function `intXParser` | ||
``` motoko no-repl | ||
func intXParser() : Parser<Char, Candid> | ||
``` | ||
|
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,13 @@ | ||
# Candid/Parser/Nat | ||
|
||
## Function `natParser` | ||
``` motoko no-repl | ||
func natParser() : Parser<Char, Candid> | ||
``` | ||
|
||
|
||
## Function `parseNat` | ||
``` motoko no-repl | ||
func parseNat() : Parser<Char, Nat> | ||
``` | ||
|
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,7 @@ | ||
# Candid/Parser/NatX | ||
|
||
## Function `natXParser` | ||
``` motoko no-repl | ||
func natXParser() : Parser<Char, Candid> | ||
``` | ||
|
Oops, something went wrong.