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

refactor!: use sdk provided filesystem util, remove aeproject util implementation #486

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ npm install -g @aeternity/aeproject
* [Unit Testing](docs/cli/test.md)
* [AEproject Library](docs/lib.md)
* [Migration from 3.x.x to 4.x.x](docs/migration-from-3.x.x-to-4.x.x.md)
* [Migration from 4.x.x to 5.x.x](docs/migration-from-4.x.x-to-5.x.x.md)
2 changes: 1 addition & 1 deletion docs/cli/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _Optionally_ a `folder` can be specified for the project to be initialized in, o
Creates a new project structure with a few folders in which the developer can create **contracts** and **tests**, as well as installing needed dependencies.

## Update existing project
For upgrade from old AEproject versions check out [Migration from 3.x.x to 4.x.x](../migration-from-3.x.x-to-4.x.x.md).
For upgrade from old AEproject versions check out [Migration from 3.x.x to 4.x.x](../migration-from-3.x.x-to-4.x.x.md) and [Migration from 4.x.x to 5.x.x](../migration-from-4.x.x-to-5.x.x.md).

```text
aeproject init --update
Expand Down
5 changes: 3 additions & 2 deletions docs/cli/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Helper and utilities for AEproject use, e.g. prefunded wallets, network definiti

```js
const { networks, utils, wallets } = require('@aeternity/aeproject');
const { getFileSystem } = require("@aeternity/aepp-sdk");
```

Read [AEproject Library](../lib.md) for a more detailed explanation about the usage of these imports.
Expand All @@ -42,7 +43,7 @@ aeSdk = await utils.getSdk();

Get the filesystem definition for (custom) `includes` of the given contract:
```js
const filesystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
const fileSystem = await getFileSystem(EXAMPLE_CONTRACT_SOURCE);
```

Read the contract source from the filesystem:
Expand All @@ -52,7 +53,7 @@ const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);

Initialize the contract instance:
```js
contract = await aeSdk.initializeContract({ sourceCode, filesystem });
contract = await aeSdk.initializeContract({ sourceCode, fileSystem });
```

Deploy the contract:
Expand Down
5 changes: 0 additions & 5 deletions docs/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ utils.getContractContent(contractPath);
```
Read the contract source from given path, just a wrapper for `fs.readFileSync` using `utf-8` encoding.

```javascript
utils.getFilesystem(contractPath);
```
Add the required filesystem imports for contract from given path, excluding the Sophia provided library imports.

```javascript
utils.get(url);
```
Expand Down
14 changes: 14 additions & 0 deletions docs/migration-from-4.x.x-to-5.x.x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Migration from 4.x.x to 5.x.x

## Changes
**AEproject** `v5.0.0` underwent some minor but breaking changes.

Install the new AEproject version
```
npm install -g @aeternity/aeproject
```

### Removed from libs
Following utils have been removed and cannot be used anymore:

- `utils.getFilesystem()` discontinued, as it is now natively available in the sdk via import, e.g. `const { getFileSystem } = require("@aeternity/aepp-sdk");`
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ nav:
- cli/test.md
- lib.md
- migration-from-3.x.x-to-4.x.x.md
- migration-from-4.x.x-to-5.x.x.md
3 changes: 2 additions & 1 deletion src/init/update-artifacts/test/exampleTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { assert } = require('chai');
const { utils } = require('@aeternity/aeproject');
const { getFileSystem } = require("@aeternity/aepp-sdk");

const EXAMPLE_CONTRACT_SOURCE = './contracts/ExampleContract.aes';

Expand All @@ -11,7 +12,7 @@ describe('ExampleContract', () => {
aeSdk = await utils.getSdk();

// a filesystem object must be passed to the compiler if the contract uses custom includes
const fileSystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);
const fileSystem = await getFileSystem(EXAMPLE_CONTRACT_SOURCE);

// get content of contract
const sourceCode = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);
Expand Down
45 changes: 1 addition & 44 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
import fs from 'fs';
import path from 'path';

import {
AeSdk, MemoryAccount, Node, CompilerHttp,
AeSdk, MemoryAccount, Node, CompilerHttp, getFileSystem,
} from '@aeternity/aepp-sdk';
import * as networks from './networks.json';
import wallets from './wallets.json';
import { get } from '../utils/utils';

export const getContractContent = (contractPath) => fs.readFileSync(contractPath, 'utf8');

export const getFilesystem = (contractPath) => {
const defaultIncludes = [
'List.aes', 'Option.aes', 'String.aes',
'Func.aes', 'Pair.aes', 'Triple.aes',
'BLS12_381.aes', 'Frac.aes', 'Set.aes',
'Bitwise.aes',
];

const rgx = /^include\s+"([\w/.-]+)"/gim;
const rgxIncludePath = /"([\w/.-]+)"/i;
const rgxMainPath = /.*\//g;

const contractContent = getContractContent(contractPath);
const filesystem = {};

const rootIncludes = contractContent.match(rgx);
if (!rootIncludes) return filesystem;
const contractPathMatch = rgxMainPath.exec(contractPath);

// eslint-disable-next-line no-restricted-syntax
for (const rootInclude of rootIncludes) {
const includeRelativePath = rgxIncludePath.exec(rootInclude);

// eslint-disable-next-line no-continue
if (defaultIncludes.includes(includeRelativePath[1])) continue;

// eslint-disable-next-line no-console
console.log(`==> Adding include to filesystem: ${includeRelativePath[1]}`);
const includePath = path.resolve(`${contractPathMatch[0]}/${includeRelativePath[1]}`);

try {
filesystem[includeRelativePath[1]] = fs.readFileSync(includePath, 'utf-8');
} catch (error) {
throw Error(`File to include '${includeRelativePath[1]}' not found.`);
}

Object.assign(filesystem, getFilesystem(includePath));
}

return filesystem;
};

export const getDefaultAccounts = () => wallets.map((keypair) => new MemoryAccount(keypair.secretKey));

export const getSdk = () => {
Expand Down
Loading