Skip to content

Commit

Permalink
unified rust script
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Dec 18, 2024
1 parent c723895 commit dcfe283
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 180 deletions.
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
"js:lint": "tsx ./scripts/js/lint.mts",
"js:publish": "tsx ./scripts/js/publish.mts",
"js:test": "tsx ./scripts/js/test.mts",
"rust:format": "tsx ./scripts/rust/format.mts clients/rust",
"rust:lint": "tsx ./scripts/rust/lint.mts clients/rust",
"rust:lint:clippy": "tsx ./scripts/rust/lint-clippy.mts clients/rust",
"rust:lint:docs": "tsx ./scripts/rust/lint-docs.mts clients/rust",
"rust:lint:features": "tsx ./scripts/rust/lint-features.mts clients/rust",
"rust:publish": "tsx ./scripts/rust/publish.mts clients/rust",
"rust:test": "tsx ./scripts/rust/test.mts clients/rust",
"interface:format": "tsx ./scripts/rust/format.mts interface",
"interface:lint": "tsx ./scripts/rust/lint.mts interface",
"interface:lint:clippy": "tsx ./scripts/rust/lint-clippy.mts interface",
"interface:lint:docs": "tsx ./scripts/rust/lint-docs.mts interface",
"interface:lint:features": "tsx ./scripts/rust/lint-features.mts interface",
"interface:publish": "tsx ./scripts/rust/publish.mts interface",
"interface:test": "tsx ./scripts/rust/test.mts interface",
"interface:wasm": "tsx ./scripts/rust/wasm.mts interface",
"rust:format": "tsx ./scripts/rust.mts format clients/rust",
"rust:lint": "tsx ./scripts/rust.mts lint clients/rust",
"rust:lint:clippy": "tsx ./scripts/rust.mts lint-clippy clients/rust",
"rust:lint:docs": "tsx ./scripts/rust.mts lint-docs clients/rust",
"rust:lint:features": "tsx ./scripts/rust.mts lint-features clients/rust",
"rust:publish": "tsx ./scripts/rust.mts publish clients/rust",
"rust:test": "tsx ./scripts/rust.mts test clients/rust",
"interface:format": "tsx ./scripts/rust.mts format interface",
"interface:lint": "tsx ./scripts/rust.mts lint interface",
"interface:lint:clippy": "tsx ./scripts/rust.mts lint-clippy interface",
"interface:lint:docs": "tsx ./scripts/rust.mts lint-docs interface",
"interface:lint:features": "tsx ./scripts/rust.mts lint-features interface",
"interface:publish": "tsx ./scripts/rust.mts publish interface",
"interface:test": "tsx ./scripts/rust.mts test interface",
"interface:wasm": "tsx ./scripts/rust.mts wasm interface",
"template:upgrade": "tsx ./scripts/helpers/upgrade-template.ts"
},
"devDependencies": {
Expand Down
35 changes: 35 additions & 0 deletions scripts/helpers/utils.mts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ export function partitionArguments(
: [args, []];
}

export function partitionArgumentsV2(
args: string[],
delimiter: string,
defaultArgs?: string[],
): [string[], string[]] {
const [providedCargoArgs, providedCommandArgs] = partitionArguments(args, delimiter);
if (defaultArgs) {
const [defaultCargoArgs, defaultCommandArgs] = partitionArguments(defaultArgs, delimiter);
return [
[...defaultCargoArgs, ...providedCargoArgs],
[...defaultCommandArgs, ...providedCommandArgs],
];
}
return [providedCargoArgs, providedCommandArgs];
}

export async function getInstalledSolanaVersion(): Promise<string | undefined> {
try {
const { stdout } = await $`solana --version`.quiet();
Expand All @@ -144,3 +160,22 @@ export function parseCliArguments(): { manifestPath: string; args: string[] } {
args,
};
}

export function parseCliArgumentsV2(): { command: string, manifestPath: string; args: string[] } {
const command = process.argv[2];
const args = process.argv.slice(3);

// Extract the relative crate directory from the command-line arguments. This
// is the only required argument.
const relativePath = args.shift();

if (!relativePath) {
throw new Error('Missing relative manifest path');
}

return {
command,
manifestPath: path.join(workingDirectory, relativePath, 'Cargo.toml'),
args,
};
}
154 changes: 154 additions & 0 deletions scripts/rust.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env zx

// Script for working with Rust projects.

import 'zx/globals';
import {
getCargo,
getToolchainArgument,
parseCliArgumentsV2,
partitionArgumentsV2,
popArgument,
workingDirectory,
} from './helpers/utils.mts';

enum Command {
Format = 'format',
LintClippy = 'lint-clippy',
LintDocs = 'lint-docs',
LintFeatures = 'lint-features',
Lint = 'lint',
Test = 'test',
Wasm = 'wasm',
Publish = 'publish',
}

const { command, manifestPath, args } = parseCliArgumentsV2();

async function cargo(
toolchain: string,
command: string | string[],
defaultArgs?: string[],
variables?: [string, string][],
) {
const [cargoArgs, commandArgs] = partitionArgumentsV2(args, '--', defaultArgs);
variables?.forEach(([k, v]) => $.env[k] = v);
await $`cargo ${toolchain} ${command} --manifest-path ${manifestPath} ${cargoArgs} -- ${commandArgs}`;
}

async function format() {
return cargo(
getToolchainArgument('format'),
'fmt',
popArgument(args, '--fix') ? [] : ['--', '--check'],
);
}

async function lintClippy() {
return cargo(
getToolchainArgument('lint'),
'clippy',
popArgument(args, '--fix') ? ['--fix'] : [],
);
}

async function lintDocs() {
return cargo(
getToolchainArgument('lint'),
'doc',
['--all-features', '--no-deps'],
[['RUSTDOCFLAGS', '--cfg docsrs -D warnings']],
);
}

async function lintFeatures() {
return cargo(
getToolchainArgument('lint'),
['hack', 'check'],
['--feature-powerset', '--all-targets'],
);
}

async function test() {
return cargo(
getToolchainArgument('test'),
'test',
['--all-features'],
[['SBF_OUT_DIR', path.join(workingDirectory, 'target', 'deploy')]]
);
}

async function wasm() {
await $`wasm-pack build --target nodejs --dev ${path.dirname(manifestPath)} --features bincode ${args}`;
}

async function publish() {
const dryRun = argv['dry-run'] ?? false;
const [level] = args;
if (!level) {
throw new Error('A version level — e.g. "path" — must be provided.');
}

// Go to the client directory and install the dependencies.
cd(path.dirname(manifestPath));

// Publish the new version.
const releaseArgs = dryRun
? []
: ['--no-push', '--no-tag', '--no-confirm', '--execute'];
await $`cargo release ${level} ${releaseArgs}`;

// Stop here if this is a dry run.
if (dryRun) {
process.exit(0);
}

// Get the crate information.
const toml = getCargo(path.dirname(manifestPath));
const crateName = toml.package['name'];
const newVersion = toml.package['version'];

// Expose the new version to CI if needed.
if (process.env.CI) {
await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
}

// Soft reset the last commit so we can create our own commit and tag.
await $`git reset --soft HEAD~1`;

// Commit the new version.
await $`git commit -am "Publish ${crateName} v${newVersion}"`;

// Tag the new version.
await $`git tag -a ${crateName}@v${newVersion} -m "${crateName} v${newVersion}"`;
}


switch (command) {
case Command.Format:
await format();
break;
case Command.LintClippy:
await lintClippy();
break;
case Command.LintDocs:
await lintDocs();
break;
case Command.LintFeatures:
await lintFeatures();
break;
case Command.Lint:
await Promise.all([lintClippy(), lintDocs(), lintFeatures()]);
break;
case Command.Test:
await test();
break;
case Command.Wasm:
await wasm();
break;
case Command.Publish:
await publish();
break;
default:
throw new Error(`Unknown command: ${command}`);
}
20 changes: 0 additions & 20 deletions scripts/rust/format.mts

This file was deleted.

22 changes: 0 additions & 22 deletions scripts/rust/lint-clippy.mts

This file was deleted.

12 changes: 0 additions & 12 deletions scripts/rust/lint-docs.mts

This file was deleted.

13 changes: 0 additions & 13 deletions scripts/rust/lint-features.mts

This file was deleted.

17 changes: 0 additions & 17 deletions scripts/rust/lint.mts

This file was deleted.

45 changes: 0 additions & 45 deletions scripts/rust/publish.mts

This file was deleted.

25 changes: 0 additions & 25 deletions scripts/rust/test.mts

This file was deleted.

Loading

0 comments on commit dcfe283

Please sign in to comment.