Skip to content

Commit c30ef4e

Browse files
committed
Add CLI versions to workspace Cargo.toml and Solana check scripts
1 parent 2e17eb9 commit c30ef4e

File tree

12 files changed

+142
-45
lines changed

12 files changed

+142
-45
lines changed

.changeset/cuddly-cheetahs-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-solana-program": patch
3+
---
4+
5+
Add CLI versions to workspace Cargo.toml and Solana check scripts

template/base/.github/actions/setup/action.yml.njk

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Setup environment
33
inputs:
44
{% if programFramework === 'anchor' %}
55
anchor:
6-
description: The Anchor version to install. Skips if not provided.
6+
description: Install Anchor if `true`. Defaults to `false`.
77
required: false
88
{% endif %}
99
cargo-cache-key:
@@ -18,14 +18,11 @@ inputs:
1818
clippy:
1919
description: Install Clippy if `true`. Defaults to `false`.
2020
required: false
21-
node:
22-
description: The Node.js version to install. Required.
23-
required: true
2421
rustfmt:
2522
description: Install Rustfmt if `true`. Defaults to `false`.
2623
required: false
2724
solana:
28-
description: The Solana version to install. Skips if not provided.
25+
description: Install Solana if `true`. Defaults to `false`.
2926
required: false
3027

3128
runs:
@@ -37,7 +34,7 @@ runs:
3734
- name: Setup Node.js
3835
uses: actions/setup-node@v4
3936
with:
40-
node-version: {% raw %}${{ inputs.node }}{% endraw %}
37+
node-version: 18
4138
cache: 'pnpm'
4239

4340
- name: Install Dependencies
@@ -50,13 +47,13 @@ runs:
5047

5148
{% if solanaVersion.withoutPatch === '2.0' %}
5249
- name: Install Protobuf Compiler (Temporary Workaround for Solana 2.0)
53-
if: {% raw %}${{ inputs.solana || inputs.rustfmt == 'true' || inputs.clippy == 'true' }}{% endraw %}
50+
if: {% raw %}${{ inputs.solana == 'true' || inputs.rustfmt == 'true' || inputs.clippy == 'true' }}{% endraw %}
5451
shell: bash
5552
run: |
5653
sudo apt-get update
5754
sudo apt-get install -y protobuf-compiler
58-
{% endif %}
5955

56+
{% endif %}
6057
- name: Install Rustfmt
6158
if: {% raw %}${{ inputs.rustfmt == 'true' }}{% endraw %}
6259
uses: dtolnay/rust-toolchain@master
@@ -72,21 +69,21 @@ runs:
7269
components: clippy
7370

7471
- name: Install Solana
75-
if: {% raw %}${{ inputs.solana }}{% endraw %}
72+
if: {% raw %}${{ inputs.solana == 'true' }}{% endraw %}
7673
uses: metaplex-foundation/actions/install-solana@v1
7774
with:
78-
version: {% raw %}${{ inputs.solana }}{% endraw %}
75+
version: {% raw %}${{ env.SOLANA_VERSION }}{% endraw %}
7976
cache: true
8077

8178
{% if programFramework === 'anchor' %}
8279
- name: Install Anchor
83-
if: {% raw %}${{ inputs.anchor != '' }}{% endraw %}
80+
if: {% raw %}${{ inputs.anchor == 'true' }}{% endraw %}
8481
uses: metaplex-foundation/actions/install-anchor-cli@v1
8582
with:
86-
version: {% raw %}${{ inputs.anchor }}{% endraw %}
83+
version: {% raw %}${{ env.ANCHOR_VERSION }}{% endraw %}
8784
cache: true
88-
{% endif %}
8985

86+
{% endif %}
9087
- name: Cache Cargo Dependencies
9188
if: {% raw %}${{ inputs.cargo-cache-key && !inputs.cargo-cache-fallback-key }}{% endraw %}
9289
uses: actions/cache@v4

template/base/.github/workflows/main.yml.njk

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ on:
66
pull_request:
77
branches: [main]
88

9-
env:
10-
NODE_VERSION: 18
11-
SOLANA_VERSION: {{ solanaVersion.full }}
12-
{% if programFramework === 'anchor' %}
13-
ANCHOR_VERSION: {{ anchorVersion.full }}
14-
{% endif %}
15-
169
jobs:
1710
format_and_lint_programs:
1811
name: Format & Lint Programs
@@ -25,7 +18,6 @@ jobs:
2518
uses: ./.github/actions/setup
2619
with:
2720
clippy: true
28-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
2921
rustfmt: true
3022

3123
- name: Format Programs
@@ -44,8 +36,6 @@ jobs:
4436

4537
- name: Setup Environment
4638
uses: ./.github/actions/setup
47-
with:
48-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
4939

5040
- name: Format Client JS
5141
run: pnpm clients:js:format
@@ -66,7 +56,6 @@ jobs:
6656
uses: ./.github/actions/setup
6757
with:
6858
clippy: true
69-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
7059
rustfmt: true
7160

7261
- name: Format Client Rust
@@ -88,10 +77,9 @@ jobs:
8877
uses: ./.github/actions/setup
8978
with:
9079
cargo-cache-key: cargo-programs
91-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
92-
solana: {% raw %}${{ env.SOLANA_VERSION }}{% endraw %}
80+
solana: true
9381
{% if programFramework === 'anchor' %}
94-
anchor: {% raw %}${{ env.ANCHOR_VERSION }}{% endraw %}
82+
anchor: true
9583
{% endif %}
9684

9785
- name: Build Programs
@@ -123,10 +111,9 @@ jobs:
123111
with:
124112
cargo-cache-key: cargo-program-tests
125113
cargo-cache-fallback-key: cargo-programs
126-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
127-
solana: {% raw %}${{ env.SOLANA_VERSION }}{% endraw %}
114+
solana: true
128115
{% if programFramework === 'anchor' %}
129-
anchor: {% raw %}${{ env.ANCHOR_VERSION }}{% endraw %}
116+
anchor: true
130117
{% endif %}
131118

132119
- name: Test Programs
@@ -145,10 +132,9 @@ jobs:
145132
with:
146133
cargo-cache-key: cargo-programs
147134
cargo-cache-local-key: cargo-local
148-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
149135
{% if programFramework === 'anchor' %}
150-
solana: {% raw %}${{ env.SOLANA_VERSION }}{% endraw %}
151-
anchor: {% raw %}${{ env.ANCHOR_VERSION }}{% endraw %}
136+
solana: true
137+
anchor: true
152138
{% endif %}
153139

154140
- name: Generate IDLs
@@ -171,7 +157,6 @@ jobs:
171157
- name: Setup Environment
172158
uses: ./.github/actions/setup
173159
with:
174-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
175160
rustfmt: true
176161

177162
- name: Generate Clients
@@ -195,8 +180,7 @@ jobs:
195180
- name: Setup Environment
196181
uses: ./.github/actions/setup
197182
with:
198-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
199-
solana: {% raw %}${{ env.SOLANA_VERSION }}{% endraw %}
183+
solana: true
200184

201185
- name: Restore Program Builds
202186
uses: actions/cache/restore@v4
@@ -221,8 +205,7 @@ jobs:
221205
uses: ./.github/actions/setup
222206
with:
223207
cargo-cache-key: cargo-rust-client
224-
node: {% raw %}${{ env.NODE_VERSION }}{% endraw %}
225-
solana: {% raw %}${{ env.SOLANA_VERSION }}{% endraw %}
208+
solana: true
226209

227210
- name: Restore Program Builds
228211
uses: actions/cache/restore@v4

template/base/Cargo.toml.njk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ members = ["program"]
1111
overflow-checks = true
1212
{% endif %}
1313

14+
[workspace.metadata.cli]
15+
{% if programFramework === 'anchor' %}
16+
anchor = "{{ anchorVersion.full }}"
17+
{% endif %}
18+
solana = "{{ solanaVersion.full }}"
19+
1420
# Specify Rust toolchains for rustfmt, clippy, and build.
1521
# Any unprovided toolchains default to stable.
1622
[workspace.metadata.toolchains]

template/base/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"programs:clean": "zx ./scripts/program/clean.mjs",
77
"programs:format": "zx ./scripts/program/format.mjs",
88
"programs:lint": "zx ./scripts/program/lint.mjs",
9-
"generate:idls": "zx ./scripts/generate-idls.mjs"
9+
"generate:idls": "zx ./scripts/generate-idls.mjs",
10+
"solana:check": "zx ./scripts/check-solana-version.mjs",
11+
"solana:link": "zx ./scripts/link-solana-version.mjs"
1012
},
1113
"devDependencies": {
1214
"@iarna/toml": "^2.2.5",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';
4+
5+
const expectedVersion = getSolanaVersion();
6+
const installedVersion = await getInstalledSolanaVersion();
7+
8+
if (installedVersion !== expectedVersion) {
9+
echo(
10+
chalk.yellow('[ WARNING ]'),
11+
`The installed Solana version ${installedVersion} does not match the expected version ${expectedVersion}.`
12+
);
13+
} else {
14+
echo(
15+
chalk.green('[ SUCCESS ]'),
16+
`The expected Solana version ${expectedVersion} is installed.`
17+
);
18+
}

template/base/scripts/ci/set-env.mjs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env zx
2+
{% if programFramework === 'anchor' %}
3+
import { getCargoMetadata, getSolanaVersion, getToolchain } from '../utils.mjs';
4+
{% else %}
5+
import { getSolanaVersion, getToolchain } from '../utils.mjs';
6+
{% endif %}
7+
8+
{% if programFramework === 'anchor' %}
9+
await $`echo "ANCHOR_VERSION=${getCargoMetadata()?.cli?.anchor}" >> $GITHUB_ENV`;
10+
{% endif %}
11+
await $`echo "SOLANA_VERSION=${getSolanaVersion()}" >> $GITHUB_ENV`;
12+
await $`echo "TOOLCHAIN_FORMAT=${getToolchain('format')}" >> $GITHUB_ENV`;
13+
await $`echo "TOOLCHAIN_LINT=${getToolchain('lint')}" >> $GITHUB_ENV`;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';
4+
5+
const installedVersion = await getInstalledSolanaVersion();
6+
const expectedVersion = getSolanaVersion();
7+
8+
if (installedVersion === expectedVersion) {
9+
echo(
10+
chalk.green('[ SUCCESS ]'),
11+
`The expected Solana version ${expectedVersion} is installed.`
12+
);
13+
process.exit(0);
14+
}
15+
16+
const installPath = path.join(
17+
os.homedir(),
18+
'.local',
19+
'share',
20+
'solana',
21+
'install'
22+
);
23+
const releasePath = path.join(
24+
installPath,
25+
'releases',
26+
expectedVersion,
27+
'solana-release'
28+
);
29+
const activeReleasePath = path.join(installPath, 'active_release');
30+
const hasRelease = await fs.exists(releasePath);
31+
32+
if (hasRelease) {
33+
await $`rm -f "${activeReleasePath}"`;
34+
await $`ln -s "${releasePath}" "${activeReleasePath}"`;
35+
echo(
36+
chalk.green('[ SUCCESS ]'),
37+
`Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
38+
);
39+
process.exit(0);
40+
}
41+
42+
echo(
43+
chalk.yellow('[ WARNING ]'),
44+
`Cannot switch from Solana version ${installedVersion} to ${expectedVersion} because it is not installed.`
45+
);
46+
47+
const installRelease = await question('Should we install it now? [y/N] ');
48+
if (installRelease === 'y') {
49+
echo(`Installing Solana ${expectedVersion}...`);
50+
await $`sh -c "$(curl -sSfL https://release.solana.com/v${expectedVersion}/install)"`;
51+
}
52+
53+
echo(
54+
chalk.green('[ SUCCESS ]'),
55+
`Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
56+
);

template/base/scripts/utils.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export function getCargoMetadata(folder) {
8484
return folder ? cargo?.package?.metadata : cargo?.workspace?.metadata;
8585
}
8686

87+
export function getSolanaVersion() {
88+
return getCargoMetadata()?.cli?.solana;
89+
}
90+
8791
export function getToolchain(operation) {
8892
return getCargoMetadata()?.toolchains?.[operation];
8993
}
@@ -111,3 +115,16 @@ export function partitionArguments(args, delimiter) {
111115
? [args.slice(0, index), args.slice(index + 1)]
112116
: [args, []];
113117
}
118+
119+
export async function getInstalledSolanaVersion() {
120+
try {
121+
const { stdout } = await $`solana --version`.quiet();
122+
return stdout.match(/(\d+\.\d+\.\d+)/)?.[1];
123+
} catch (error) {
124+
echo(
125+
chalk.red('[ ERROR ]'),
126+
`No Solana installation found. Please install Solana ${getSolanaVersion()} before proceeding.`
127+
);
128+
process.exit(1);
129+
}
130+
}

template/clients/base/scripts/start-validator.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
getProgramFolders,
1111
} from './utils.mjs';
1212

13+
// Check Solana version.
14+
await $`pnpm solana:check`;
15+
1316
// Options and arguments.
1417
const restart = argv['restart'];
1518

template/clients/js/clients/js/package.json.njk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
"typescript": "^5.5.3"
5555
},
5656
"ava": {
57-
"nodeArguments": ["--no-warnings"],
57+
"nodeArguments": [
58+
"--no-warnings"
59+
],
5860
"require": [
5961
"@solana/webcrypto-ed25519-polyfill"
6062
],

0 commit comments

Comments
 (0)