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

Improve Solana version link script #81

Merged
merged 1 commit into from
Jul 30, 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
7 changes: 7 additions & 0 deletions .changeset/plenty-tools-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-solana-program": patch
---

Improve Solana version link script

`pnpm solana:link` now also asks you to download the required Solana version if it's not already installed. Additionally, if the required Solana version is equal to or greater than `1.18.19`, the install URL will use `release.anza.xyz` instead of `release.solana.com`.
8 changes: 7 additions & 1 deletion template/base/scripts/check-solana-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';
const expectedVersion = getSolanaVersion();
const installedVersion = await getInstalledSolanaVersion();

if (installedVersion !== expectedVersion) {
if (!installedVersion) {
echo(
chalk.red('[ ERROR ]'),
`No Solana installation found. Please install Solana ${expectedVersion} before proceeding.`
);
process.exit(1);
} else if (installedVersion !== expectedVersion) {
echo(
chalk.yellow('[ WARNING ]'),
`The installed Solana version ${installedVersion} does not match the expected version ${expectedVersion}.`
Expand Down
66 changes: 42 additions & 24 deletions template/base/scripts/link-solana-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
import 'zx/globals';
import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';

const installedVersion = await getInstalledSolanaVersion();
const expectedVersion = getSolanaVersion();

if (installedVersion === expectedVersion) {
echo(
chalk.green('[ SUCCESS ]'),
`The expected Solana version ${expectedVersion} is installed.`
);
process.exit(0);
}
const installedVersion = await getInstalledSolanaVersion();

const installPath = path.join(
os.homedir(),
Expand All @@ -29,28 +21,54 @@ const releasePath = path.join(
const activeReleasePath = path.join(installPath, 'active_release');
const hasRelease = await fs.exists(releasePath);

if (hasRelease) {
if (!installedVersion) {
echo(
chalk.red('[ ERROR ]'),
`No Solana installation found. Solana ${expectedVersion} is required for this project.`
);
await askToInstallSolana(expectedVersion);
} else if (installedVersion === expectedVersion) {
echo(
chalk.green('[ SUCCESS ]'),
`The expected Solana version ${expectedVersion} is installed.`
);
} else if (hasRelease) {
await $`rm -f "${activeReleasePath}"`;
await $`ln -s "${releasePath}" "${activeReleasePath}"`;
echo(
chalk.green('[ SUCCESS ]'),
`Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
);
process.exit(0);
} else {
echo(
chalk.yellow('[ WARNING ]'),
`Cannot switch from Solana version ${installedVersion} to ${expectedVersion} because it is not installed.`
);
await askToInstallSolana(expectedVersion);
}

echo(
chalk.yellow('[ WARNING ]'),
`Cannot switch from Solana version ${installedVersion} to ${expectedVersion} because it is not installed.`
);

const installRelease = await question('Should we install it now? [y/N] ');
if (installRelease === 'y') {
echo(`Installing Solana ${expectedVersion}...`);
await $`sh -c "$(curl -sSfL https://release.solana.com/v${expectedVersion}/install)"`;
async function askToInstallSolana(version) {
const installRelease = await question('Should we install it now? [y/N] ');
if (installRelease === 'y') {
await installSolana(version);
echo(
chalk.green('[ SUCCESS ]'),
`Successfully installed Solana version ${version}.`
);
} else {
process.exit(1);
}
}

echo(
chalk.green('[ SUCCESS ]'),
`Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
);
async function installSolana(version) {
echo(`Installing Solana ${version}...`);
const cutoff = '1.18.19';
const isBeforeCutoff =
(await $`[[ "$(printf '%s\n' "${cutoff}" "${version}" | sort -V | head -n1)" = "${version}" ]] && [[ "${cutoff}" != "${version}" ]]`.quiet()
.exitCode) == 0;
if (isBeforeCutoff) {
await $`sh -c "$(curl -sSfL https://release.solana.com/v${version}/install)"`;
} else {
await $`sh -c "$(curl -sSfL https://release.anza.xyz/v${version}/install)"`;
}
}
6 changes: 1 addition & 5 deletions template/base/scripts/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ export async function getInstalledSolanaVersion() {
const { stdout } = await $`solana --version`.quiet();
return stdout.match(/(\d+\.\d+\.\d+)/)?.[1];
} catch (error) {
echo(
chalk.red('[ ERROR ]'),
`No Solana installation found. Please install Solana ${getSolanaVersion()} before proceeding.`
);
process.exit(1);
return '';
}
}