Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator Templates #62

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator-test/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require("path");
const { processFolder, getFilesList } = require("../generator/src/processor");

(async () => {
const input = path.resolve("./cadence");
const output = path.resolve("./generated");
const fileList = await getFilesList(input);
console.log({ input, output, fileList });
await processFolder(input, output);
console.log("✅ Done!");
})();
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

/** pragma type contract **/

import {
getEnvironment,
replaceImportAddresses,
reportMissingImports,
deployContract,
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
pub contract Basic{
pub contract Basic{
pub let message: String
init(){
log("Basic deployed")
Expand All @@ -18,7 +19,7 @@ export const CODE = `
`;

/**
* Method to generate cadence code for Basic transaction
* Method to generate cadence code for Basic} contract
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const BasicTemplate = async (addressMap = {}) => {
Expand Down Expand Up @@ -46,5 +47,6 @@ export const deployBasic = async (props) => {
const code = await BasicTemplate(addressMap);
const name = "Basic"

return deployContract({ code, name, ...props })
}
return deployContract({ code, name, processed: true, ...props })
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** pragma type script **/

import {
Expand All @@ -6,25 +7,25 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
import Basic from 0x1
import Basic from 0x1

pub fun main(): String{
return Basic.message
}
`;

/**
* Method to generate cadence code for TestAsset
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
* Method to generate cadence code for basic script
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const basicTemplate = async (addressMap = {}) => {
const envMap = await getEnvironment();
const fullMap = {
...envMap,
...addressMap,
...envMap,
...addressMap,
};

// If there are any missing imports in fullMap it will be reported via console
Expand All @@ -33,11 +34,12 @@ export const basicTemplate = async (addressMap = {}) => {
return replaceImportAddresses(CODE, fullMap);
};

export const basic = async (props) => {
export const basic = async (props = {}) => {
const { addressMap = {}, args = [] } = props
const code = await basicTemplate(addressMap);

reportMissing("arguments", args.length, 0, `basic =>`);

return executeScript({code, ...props})
}
return executeScript({code, processed: true, ...props})
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** pragma type script **/

import {
Expand All @@ -6,24 +7,24 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
pub fun main(message: String): Int{
pub fun main(message: String): Int{
log(message)
return 42
}
`;

/**
* Method to generate cadence code for TestAsset
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
* Method to generate cadence code for log script
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const logTemplate = async (addressMap = {}) => {
const envMap = await getEnvironment();
const fullMap = {
...envMap,
...addressMap,
...envMap,
...addressMap,
};

// If there are any missing imports in fullMap it will be reported via console
Expand All @@ -32,11 +33,12 @@ export const logTemplate = async (addressMap = {}) => {
return replaceImportAddresses(CODE, fullMap);
};

export const log = async (props) => {
export const log = async (props = {}) => {
const { addressMap = {}, args = [] } = props
const code = await logTemplate(addressMap);

reportMissing("arguments", args.length, 1, `log =>`);

return executeScript({code, ...props})
}
return executeScript({code, processed: true, ...props})
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** pragma type script **/

import {
Expand All @@ -6,24 +7,24 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
pub fun main(metadata: {String:String}): String{
pub fun main(metadata: {String:String}): String{
let name = metadata["name"]!
return name
}
`;

/**
* Method to generate cadence code for TestAsset
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
* Method to generate cadence code for metadata script
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const metadataTemplate = async (addressMap = {}) => {
const envMap = await getEnvironment();
const fullMap = {
...envMap,
...addressMap,
...envMap,
...addressMap,
};

// If there are any missing imports in fullMap it will be reported via console
Expand All @@ -32,11 +33,12 @@ export const metadataTemplate = async (addressMap = {}) => {
return replaceImportAddresses(CODE, fullMap);
};

export const metadata = async (props) => {
export const metadata = async (props = {}) => {
const { addressMap = {}, args = [] } = props
const code = await metadataTemplate(addressMap);

reportMissing("arguments", args.length, 1, `metadata =>`);

return executeScript({code, ...props})
}
return executeScript({code, processed: true, ...props})
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** pragma type script **/

import {
Expand All @@ -6,23 +7,23 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
pub fun main(){
pub fun main(){
panic("Nope!")
}
`;

/**
* Method to generate cadence code for TestAsset
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
* Method to generate cadence code for panic script
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const panicTemplate = async (addressMap = {}) => {
const envMap = await getEnvironment();
const fullMap = {
...envMap,
...addressMap,
...envMap,
...addressMap,
};

// If there are any missing imports in fullMap it will be reported via console
Expand All @@ -31,11 +32,12 @@ export const panicTemplate = async (addressMap = {}) => {
return replaceImportAddresses(CODE, fullMap);
};

export const panic = async (props) => {
export const panic = async (props = {}) => {
const { addressMap = {}, args = [] } = props
const code = await panicTemplate(addressMap);

reportMissing("arguments", args.length, 0, `panic =>`);

return executeScript({code, ...props})
}
return executeScript({code, processed: true, ...props})
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** pragma type transaction **/

import {
Expand All @@ -6,10 +7,10 @@ import {
reportMissingImports,
reportMissing,
sendTransaction
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
transaction{
transaction{
prepare(signer: AuthAccount){
log(signer.address)
}
Expand Down Expand Up @@ -40,12 +41,13 @@ export const logTemplate = async (addressMap = {}) => {
* @param Array<*> props.args - list of arguments
* @param Array<*> props.signers - list of signers
*/
export const log = async (props) => {
export const log = async (props = {}) => {
const { addressMap, args = [], signers = [] } = props;
const code = await logTemplate(addressMap);

reportMissing("arguments", args.length, 0, `log =>`);
reportMissing("signers", signers.length, 1, `log =>`);

return sendTransaction({code, ...props})
}
return sendTransaction({code, processed: true, ...props})
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** pragma type transaction **/

import {
Expand All @@ -6,10 +7,10 @@ import {
reportMissingImports,
reportMissing,
sendTransaction
} from '../../../../generator/src'
} from 'flow-cadut'

export const CODE = `
transaction{
transaction{
prepare(signer: AuthAccount){
panic("Uh-oh!")
}
Expand Down Expand Up @@ -40,12 +41,13 @@ export const panicTemplate = async (addressMap = {}) => {
* @param Array<*> props.args - list of arguments
* @param Array<*> props.signers - list of signers
*/
export const panic = async (props) => {
export const panic = async (props = {}) => {
const { addressMap, args = [], signers = [] } = props;
const code = await panicTemplate(addressMap);

reportMissing("arguments", args.length, 0, `panic =>`);
reportMissing("signers", signers.length, 1, `panic =>`);

return sendTransaction({code, ...props})
}
return sendTransaction({code, processed: true, ...props})
}

2 changes: 1 addition & 1 deletion generator-test/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require = require("esm")(module/*, options*/)
module.exports = require("./src/run.js")
module.exports = require("./generate.js")
1 change: 1 addition & 0 deletions generator-test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions generator/TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Build using esbuild.
Multiple build targets possible via API and flags:
- https://javascript.plainenglish.io/esbuild-library-6ec5fdb26e2c
- https://medium.com/geekculture/build-a-library-with-esbuild-23235712f3c
2 changes: 1 addition & 1 deletion generator/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flow-cadut-generator",
"amdName": "flowCadutGenerator",
"version": "0.0.2",
"version": "0.0.3",
"private": true,
"author": "Maksim Daunarovich",
"license": "Apache-2.0",
Expand Down
3 changes: 1 addition & 2 deletions generator/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/

import fs from "fs";

import { processFolder, processGitRepo } from "./processor";
import "./templates";


// Initially we will support only GitHub repos
// TODO: support other urls. List can be found here:
Expand Down
Loading