Skip to content

Commit 4a55c62

Browse files
authored
Detect installed Rust version (#71)
1 parent 1270ba4 commit 4a55c62

25 files changed

+372
-230
lines changed

.changeset/nine-sheep-wash.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+
Detect installed Rust version

index.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#!/usr/bin/env node
22

3-
import * as path from 'node:path';
43
import * as fs from 'node:fs';
4+
import * as path from 'node:path';
55

6-
import { createOrEmptyTargetDirectory } from './utils/fsHelpers';
7-
import { getInputs } from './utils/getInputs';
8-
import { getLanguage } from './utils/getLanguage';
9-
import { logBanner, logDone, logStep } from './utils/getLogs';
10-
import { RenderContext, getRenderContext } from './utils/getRenderContext';
6+
import { createOrEmptyTargetDirectory } from './utils/filesystem';
7+
import { getInputs } from './utils/inputs';
8+
import { getLanguage } from './utils/localization';
9+
import { logBanner, logDone, logStep } from './utils/logs';
10+
import { RenderContext, getRenderContext } from './utils/renderContext';
1111
import { renderTemplate } from './utils/renderTemplates';
12-
import {
13-
detectAnchorVersion,
14-
detectSolanaVersion,
15-
generateKeypair,
16-
patchSolanaDependencies,
17-
} from './utils/solanaCli';
12+
import { generateKeypair, patchSolanaDependencies } from './utils/solanaCli';
13+
import { detectAnchorVersion } from './utils/version-anchor';
14+
import { detectRustVersion } from './utils/version-rust';
15+
import { detectSolanaVersion } from './utils/version-solana';
16+
import { Version } from './utils/version-core';
1817

1918
(async function init() {
2019
logBanner();
@@ -36,8 +35,14 @@ import {
3635
() => detectSolanaVersion(language)
3736
);
3837

38+
// Detect the Rust version.
39+
const rustVersionDetected = await logStep(
40+
language.infos.detectRustVersion,
41+
() => detectRustVersion()
42+
);
43+
3944
// Detect the Anchor version.
40-
let anchorVersionDetected: string | undefined;
45+
let anchorVersionDetected: Version | undefined;
4146
if (inputs.programFramework === 'anchor') {
4247
anchorVersionDetected = await logStep(
4348
language.infos.detectAnchorVersion,
@@ -64,6 +69,7 @@ import {
6469
inputs,
6570
programAddress,
6671
solanaVersionDetected,
72+
rustVersionDetected,
6773
anchorVersionDetected,
6874
});
6975

locales/en-US.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@
5050
"errors": {
5151
"anchorCliNotFound": "Command `$command` unavailable. Please install the Anchor CLI.",
5252
"cannotOverrideDirectory": "Cannot override target directory \"$targetDirectory\". Run with option --force to override.",
53-
"invalidSolanaVersion": "Invalid Solana version: $version.",
54-
"operationCancelled": "Operation cancelled",
53+
"invalidVersion": "Invalid $tool version: $version.",
54+
"operationCancelled": "Operation cancelled.",
55+
"rustVersionIncompatibleWithSolana": "Solana version $solanaVersion requires at least Rust $minimumRustVersion installed, found Rust $rustVersion.",
5556
"solanaCliNotFound": "Command `$command` unavailable. Please install the Solana CLI.",
56-
"solanaKeygenFailed": "Failed to generate program keypair"
57+
"solanaKeygenFailed": "Failed to generate program keypair."
5758
},
5859
"defaultToggleOptions": {
5960
"active": "Yes",
@@ -65,6 +66,7 @@
6566
},
6667
"infos": {
6768
"detectAnchorVersion": "Detect Anchor version",
69+
"detectRustVersion": "Detect Rust version",
6870
"detectSolanaVersion": "Detect Solana version",
6971
"generateKeypair": "Generate program keypair",
7072
"scaffold": "Scaffold project in $targetDirectory",

locales/fr-FR.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@
5353
"errors": {
5454
"anchorCliNotFound": "Commande `$command` indisponible. Veuillez installer Anchor dans votre terminal.",
5555
"cannotOverrideDirectory": "Impossible de remplacer le répertoire cible \"$targetDirectory\". Exécutez avec l'option --force pour remplacer.",
56-
"invalidSolanaVersion": "Version Solana invalide\u00a0: $version.",
57-
"operationCancelled": "Operation annulée",
56+
"invalidVersion": "Version de $tool invalide\u00a0: $version.",
57+
"operationCancelled": "Operation annulée.",
58+
"rustVersionIncompatibleWithSolana": "La version de Solana $solanaVersion nécessite au moins Rust $minimumRustVersion installé, Rust $rustVersion trouvé.",
5859
"solanaCliNotFound": "Commande `$command` indisponible. Veuillez installer Solana dans votre terminal.",
59-
"solanaKeygenFailed": "La génération de la paire de clés a échoué"
60+
"solanaKeygenFailed": "La génération de la paire de clés a échoué."
6061
},
6162
"defaultToggleOptions": {
6263
"active": "Oui",
@@ -67,8 +68,9 @@
6768
"multiselect": "[↑/↓]: Sélectionner / [espace]: Basculer la sélection / [a]: Basculer tout / [entrée]: Valider"
6869
},
6970
"infos": {
70-
"detectAnchorVersion": "Détect la version d'Anchor",
71-
"detectSolanaVersion": "Détect la version de Solana",
71+
"detectAnchorVersion": "Détecte la version d'Anchor",
72+
"detectRustVersion": "Détecte la version de Rust",
73+
"detectSolanaVersion": "Détecte la version de Solana",
7274
"generateKeypair": "Génére la paire de clés du program",
7375
"scaffold": "Génére le projet dans $targetDirectory",
7476
"done": "Terminé. Exécutez maintenant\u00a0:"

template/anchor/base/program/Cargo.toml.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ cpi = ["no-entrypoint"]
2020
idl-build = ["anchor-lang/idl-build"]
2121

2222
[dependencies]
23-
anchor-lang = "{{ anchorVersion }}"
24-
solana-program = "~{{ solanaVersionWithoutPatch }}"
23+
anchor-lang = "{{ anchorVersion.full }}"
24+
solana-program = "~{{ solanaVersion.withoutPatch }}"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ runs:
4848
shell: bash
4949
run: pnpm zx ./scripts/ci/set-env.mjs
5050

51-
{% if solanaVersionWithoutPatch === '2.0' %}
51+
{% if solanaVersion.withoutPatch === '2.0' %}
5252
- name: Install Protobuf Compiler (Temporary Workaround for Solana 2.0)
5353
if: {% raw %}${{ inputs.solana || inputs.rustfmt == 'true' || inputs.clippy == 'true' }}{% endraw %}
5454
shell: bash

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ on:
88

99
env:
1010
NODE_VERSION: 18
11-
SOLANA_VERSION: {{ solanaVersion }}
11+
SOLANA_VERSION: {{ solanaVersion.full }}
1212
{% if programFramework === 'anchor' %}
13-
ANCHOR_VERSION: {{ anchorVersion }}
13+
ANCHOR_VERSION: {{ anchorVersion.full }}
1414
{% endif %}
1515

1616
jobs:

template/clients/rust/clients/rust/Cargo.toml.njk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ num-derive = "^0.3"
1515
num-traits = "^0.2"
1616
serde = { version = "^1.0", features = ["derive"], optional = true }
1717
serde_with = { version = "^3.0", optional = true }
18-
solana-program = "~{{ solanaVersionWithoutPatch }}"
18+
solana-program = "~{{ solanaVersion.withoutPatch }}"
1919
thiserror = "^1.0"
2020

2121
[dev-dependencies]
2222
assert_matches = "1.5.0"
23-
solana-program-test = "~{{ solanaVersionWithoutPatch }}"
24-
solana-sdk = "~{{ solanaVersionWithoutPatch }}"
23+
solana-program-test = "~{{ solanaVersion.withoutPatch }}"
24+
solana-sdk = "~{{ solanaVersion.withoutPatch }}"

template/shank/base/program/Cargo.toml.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ borsh = "^0.10"
1919
shank = "^0.4.2"
2020
num-derive = "^0.3"
2121
num-traits = "^0.2"
22-
solana-program = "~{{ solanaVersionWithoutPatch }}"
22+
solana-program = "~{{ solanaVersion.withoutPatch }}"
2323
thiserror = "^1.0"
File renamed without changes.

utils/fsHelpers.ts renamed to utils/filesystem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as fs from 'node:fs';
22
import * as path from 'node:path';
33

4-
import { Language } from './getLanguage';
5-
import { logErrorAndExit } from './getLogs';
4+
import { Language } from './localization';
5+
import { logErrorAndExit } from './logs';
66

77
export function createOrEmptyTargetDirectory(
88
language: Language,

utils/getRenderContext.ts

Lines changed: 0 additions & 128 deletions
This file was deleted.

utils/getInputs.ts renamed to utils/inputs.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import * as fs from 'node:fs';
33
import { parseArgs } from 'node:util';
44
import prompts from 'prompts';
55

6-
import { Language } from './getLanguage';
7-
import { kebabCase } from './stringHelpers';
8-
import { toMinorSolanaVersion } from './solanaCli';
6+
import { Language } from './localization';
7+
import { kebabCase } from './strings';
98

109
export const allClients = ['js', 'rust'] as const;
1110
export type Client = (typeof allClients)[number];
@@ -20,14 +19,15 @@ export type Inputs = {
2019
programName: string;
2120
rustClient: boolean;
2221
rustClientCrateName: string;
22+
rustVersion?: string;
2323
shouldOverride: boolean;
2424
solanaVersion?: string;
2525
targetDirectoryName: string;
2626
useDefaults: boolean;
2727
};
2828

2929
export async function getInputs(language: Language): Promise<Inputs> {
30-
const argInputs = getInputsFromArgs(language);
30+
const argInputs = getInputsFromArgs();
3131
const defaultInputs = getDefaultInputs(argInputs);
3232

3333
if (argInputs.useDefaults) {
@@ -212,7 +212,7 @@ async function getInputsFromPrompts(
212212
}
213213
}
214214

215-
function getInputsFromArgs(language: Language): Partial<Inputs> {
215+
function getInputsFromArgs(): Partial<Inputs> {
216216
type ArgInputs = {
217217
address?: string;
218218
anchorProgram: boolean;
@@ -221,6 +221,7 @@ function getInputsFromArgs(language: Language): Partial<Inputs> {
221221
noClients: boolean;
222222
organizationName?: string;
223223
programName?: string;
224+
rustVersion?: string;
224225
shankProgram: boolean;
225226
solanaVersion?: string;
226227
useDefaults: boolean;
@@ -235,11 +236,8 @@ function getInputsFromArgs(language: Language): Partial<Inputs> {
235236
inputs.organizationName = kebabCase(argInputs.organizationName);
236237
if (argInputs.programName)
237238
inputs.programName = kebabCase(argInputs.programName);
238-
if (argInputs.solanaVersion)
239-
inputs.solanaVersion = toMinorSolanaVersion(
240-
language,
241-
argInputs.solanaVersion
242-
);
239+
if (argInputs.rustVersion) inputs.rustVersion = argInputs.rustVersion;
240+
if (argInputs.solanaVersion) inputs.solanaVersion = argInputs.solanaVersion;
243241
if (argInputs.targetDirectoryName)
244242
inputs.targetDirectoryName = argInputs.targetDirectoryName;
245243
if (argInputs.force) inputs.shouldOverride = true;
@@ -273,6 +271,7 @@ function getInputsFromArgs(language: Language): Partial<Inputs> {
273271
force: { type: 'boolean' },
274272
'no-clients': { type: 'boolean' },
275273
org: { type: 'string' },
274+
rust: { type: 'string' },
276275
shank: { type: 'boolean' },
277276
solana: { type: 'string' },
278277
},
@@ -287,6 +286,7 @@ function getInputsFromArgs(language: Language): Partial<Inputs> {
287286
noClients: options['no-clients'] ?? false,
288287
organizationName: options.org,
289288
programName: positionals[1],
289+
rustVersion: options.rust,
290290
shankProgram: options.shank ?? false,
291291
solanaVersion: options.solana,
292292
useDefaults: options.default ?? false,

utils/getLanguage.ts renamed to utils/localization.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export interface Language {
3131
errors: {
3232
anchorCliNotFound: string;
3333
cannotOverrideDirectory: string;
34-
invalidSolanaVersion: string;
34+
invalidVersion: string;
3535
operationCancelled: string;
36+
rustVersionIncompatibleWithSolana: string;
3637
solanaCliNotFound: string;
3738
solanaKeygenFailed: string;
3839
};
@@ -46,6 +47,7 @@ export interface Language {
4647
};
4748
infos: {
4849
detectAnchorVersion: string;
50+
detectRustVersion: string;
4951
detectSolanaVersion: string;
5052
generateKeypair: string;
5153
scaffold: string;

0 commit comments

Comments
 (0)