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

feat: adding new forcBuildFlags property to fuels config #1788

Merged
merged 18 commits into from
Feb 23, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/hot-onions-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": patch
---

Adding new `forcBuildFlags` property to `fuels` config
5 changes: 5 additions & 0 deletions apps/demo-fuels/fuels.config.full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export default createConfig({
fuelCorePort: 4000,
// #endregion fuelCorePort

// #region forcBuildFlags
// Default: []
forcBuildFlags: ['--release'],
// #endregion forcBuildFlags

// #region deployConfig-fn
deployConfig: async (options: ContractDeployOptions) => {
// ability to fetch data remotely
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/guide/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ npx fuels build --deploy
Using the `--deploy` flag will additionally:

1. Auto-start a short-lived `fuel-core` node if _needed_ ([docs](./config-file.md#autostartfuelcore))
2. Run `deploy` on that node
1. Run `deploy` on that node

> _This is useful when working with contracts because a contract's ID is generated only on deployment._

Expand Down
14 changes: 14 additions & 0 deletions apps/docs/src/guide/cli/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ Port to use when starting a local `fuel-core` node.

<<< @../../../demo-fuels/fuels.config.full.ts#fuelCorePort{ts:line-numbers}

## `forcBuildFlags`

> - _Used by [`fuels build`](./commands.md#fuels-build) and [`fuels deploy`](./commands.md#fuels-deploy)_.

Sway programs are compiled in `debug` mode by default.

Here you can customize all build flags, e.g. to build programs in `release` mode.

<<< @../../../demo-fuels/fuels.config.full.ts#forcBuildFlags{ts:line-numbers}

Check also:

- [Forc docs](https://docs.fuel.network/docs/forc/commands/forc_build/#forc-build)

## `deployConfig`

You can supply a ready-to-go deploy configuration object:
Expand Down
3 changes: 2 additions & 1 deletion packages/fuels/src/cli/commands/build/buildSwayProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const buildSwayProgram = async (config: FuelsConfig, path: string) => {
const builtInForcPath = findBinPath('fuels-forc', __dirname);

const command = config.useBuiltinForc ? builtInForcPath : 'forc';
const forc = spawn(command, ['build', '-p', path], { stdio: 'pipe' });
const args = ['build', '-p', path].concat(config.forcBuildFlags);
const forc = spawn(command, args, { stdio: 'pipe' });

if (loggingConfig.isLoggingEnabled) {
forc.stderr?.pipe(process.stderr);
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels/src/cli/commands/build/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function generateTypesForProgramType(
) {
debug('Generating types..');

const filepaths = await getABIPaths(paths);
const filepaths = await getABIPaths(paths, config);
const pluralizedDirName = `${String(programType).toLocaleLowerCase()}s`;

runTypegen({
Expand Down
6 changes: 3 additions & 3 deletions packages/fuels/src/cli/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export async function deploy(config: FuelsConfig) {

for (let i = 0; i < contractsLen; i++) {
const contractPath = config.contracts[i];
const binaryPath = getBinaryPath(contractPath);
const abiPath = getABIPath(contractPath);
const storageSlotsPath = getStorageSlotsPath(contractPath);
const binaryPath = getBinaryPath(contractPath, config);
const abiPath = getABIPath(contractPath, config);
const storageSlotsPath = getStorageSlotsPath(contractPath, config);
const projectName = getContractName(contractPath);
const contractName = getContractCamelCase(contractPath);
const deployConfig = await getDeployConfig(config.deployConfig, {
Expand Down
18 changes: 10 additions & 8 deletions packages/fuels/src/cli/config/forcUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import camelCase from 'lodash.camelcase';
import { join } from 'path';
import toml from 'toml';

import type { FuelsConfig } from '../types';

export type ForcToml = {
project: {
authors?: string[];
Expand Down Expand Up @@ -70,21 +72,21 @@ export function getContractCamelCase(contractPath: string) {
return camelCase(projectName);
}

export function getBinaryPath(contractPath: string) {
export function getBinaryPath(contractPath: string, { buildMode }: FuelsConfig) {
const projectName = getContractName(contractPath);
return join(contractPath, `/out/debug/${projectName}.bin`);
return join(contractPath, `/out/${buildMode}/${projectName}.bin`);
}

export function getABIPath(contractPath: string) {
export function getABIPath(contractPath: string, { buildMode }: FuelsConfig) {
const projectName = getContractName(contractPath);
return join(contractPath, `/out/debug/${projectName}-abi.json`);
return join(contractPath, `/out/${buildMode}/${projectName}-abi.json`);
}

export function getABIPaths(paths: string[]) {
return Promise.all(paths.map((path) => getABIPath(path)));
export function getABIPaths(paths: string[], config: FuelsConfig) {
return Promise.all(paths.map((path) => getABIPath(path, config)));
}

export const getStorageSlotsPath = (contractPath: string) => {
export const getStorageSlotsPath = (contractPath: string, { buildMode }: FuelsConfig) => {
const projectName = getContractName(contractPath);
return join(contractPath, `/out/debug/${projectName}-storage_slots.json`);
return join(contractPath, `/out/${buildMode}/${projectName}-storage_slots.json`);
arboleya marked this conversation as resolved.
Show resolved Hide resolved
};
8 changes: 7 additions & 1 deletion packages/fuels/src/cli/config/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export async function loadConfig(cwd: string): Promise<FuelsConfig> {
const useBuiltinForc = userConfig.useBuiltinForc ?? shouldUseBuiltinForc();
const useBuiltinFuelCore = userConfig.useBuiltinFuelCore ?? shouldUseBuiltinFuelCore();

// Start clone-object while initializiung optional props
const { forcBuildFlags = [] } = userConfig;
const releaseFlag = forcBuildFlags.find((f) => f === '--release');
const buildMode = releaseFlag ? 'release' : 'debug';
nedsalk marked this conversation as resolved.
Show resolved Hide resolved

// Start clone-object while initializing optional props
const config: FuelsConfig = {
contracts: [],
scripts: [],
Expand All @@ -59,6 +63,8 @@ export async function loadConfig(cwd: string): Promise<FuelsConfig> {
useBuiltinForc,
useBuiltinFuelCore,
configPath,
forcBuildFlags,
buildMode,
};

// Resolve the output path on loaded config
Expand Down
10 changes: 10 additions & 0 deletions packages/fuels/src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export type UserFuelsConfig = {
*/
fuelCorePort?: number;

/**
* Aditional forc build flags to be used when compiling contracts.
* Default: []
* Example:
* forcBuildFlags: ['--release'];
*/
forcBuildFlags?: string[];

/**
* Function callback, will be called after a successful run
* @param event - The event that triggered this execution
Expand Down Expand Up @@ -112,8 +120,10 @@ export type FuelsConfig = UserFuelsConfig &
| 'useBuiltinFuelCore'
| 'autoStartFuelCore'
| 'providerUrl'
| 'forcBuildFlags'
>
> & {
basePath: string;
configPath: string;
buildMode: 'debug' | 'release';
};
42 changes: 40 additions & 2 deletions packages/fuels/test/features/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';

import * as deployMod from '../../src/cli/commands/deploy/index';
Expand Down Expand Up @@ -74,7 +74,7 @@ describe(

await runInit({
root: paths.root,
workspace: paths.workspaceDir,
contracts: paths.contractsDir,
output: paths.outputDir,
});

Expand Down Expand Up @@ -110,6 +110,44 @@ describe(
expect(deploy).toHaveBeenCalledTimes(1);
expect(killChildProcess).toHaveBeenCalledTimes(1);
});

it("should run `build` with `forcBuildFlags: ['--release']`", async () => {
const { autoStartFuelCore, killChildProcess, deploy } = mockAll();

await runInit({
root: paths.root,
workspace: paths.workspaceDir,
output: paths.outputDir,
});

// inject `forcBuildFlags: ['--release']` in config file
const configFilepath = join(paths.root, 'fuels.config.ts');
const configContents = readFileSync(configFilepath, 'utf-8');

const search = " output: './output',";
const replace = [search, " forcBuildFlags: ['--release'],"].join('\n');
const configContentsNew = configContents.replace(search, replace);

writeFileSync(configFilepath, configContentsNew);

// moving on
await runBuild({ root: paths.root });

const files = [
'contracts/FooBarAbi.hex.ts',
'contracts/FooBarAbi.d.ts',
'contracts/factories/FooBarAbi__factory.ts',
'contracts/index.ts',
'index.ts',
].map((f) => join(paths.outputDir, f));

files.forEach((file) => expect(existsSync(file)).toBeTruthy());

expect(autoStartFuelCore).toHaveBeenCalledTimes(0);
expect(deploy).toHaveBeenCalledTimes(0);
expect(killChildProcess).toHaveBeenCalledTimes(0);
});
},

{ timeout: 180000 }
);
2 changes: 2 additions & 0 deletions packages/fuels/test/fixtures/fuels.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export const fuelsConfig: FuelsConfig = {
fuelCorePort: 4000,
providerUrl: FUEL_NETWORK_URL,
configPath: __filename,
forcBuildFlags: [],
buildMode: 'debug',
};
Loading