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

rustup and wasm32-unknown-unknown auto setup #632

Merged
merged 28 commits into from Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1951393
rustup setup and wasm32-unknown-unknown setup added
volovyks Dec 20, 2020
33920cc
PATH configuration added and logging added to rustup setup
volovyks Dec 21, 2020
6c56579
Windows rust instalation instrucions added
volovyks Dec 22, 2020
dcd6d1f
rust instalation script refactored
volovyks Dec 24, 2020
405df11
windows hints for rustup instalation updated
volovyks Jan 4, 2021
5519b85
PATH fixed in rust-setup script
volovyks Jan 4, 2021
9526498
source cargo advice added to rust setup script
volovyks Jan 4, 2021
f6f8359
rustup windows message modified
volovyks Jan 4, 2021
e1b12df
typo fix
volovyks Jan 5, 2021
f9f37b1
rustup setup refactored with early return approach
volovyks Jan 6, 2021
003da48
realine used instead of readline-sync dep
volovyks Jan 6, 2021
0f46144
Merge branch 'master' into feature/rustup
volovyks Mar 25, 2021
3b86050
=== instead of ==
volovyks Mar 25, 2021
d9e19ee
Update utils/rust-setup.js
volovyks Mar 25, 2021
461a24c
Update utils/rust-setup.js
volovyks Mar 25, 2021
6aaeba7
Update utils/rust-setup.js
volovyks Mar 25, 2021
8548aad
Update utils/rust-setup.js
volovyks Mar 25, 2021
02445f2
Update utils/rust-setup.js
volovyks Mar 25, 2021
af6404a
Update utils/rust-setup.js
volovyks Mar 25, 2021
317d94e
Update utils/rust-setup.js
volovyks Mar 25, 2021
33e8fb7
wasm target console.log() added
volovyks Mar 25, 2021
a0589f0
variable moved to a proper place
volovyks Mar 25, 2021
dc07d06
Update utils/rust-setup.js
volovyks Mar 25, 2021
c66a84a
Update utils/rust-setup.js
volovyks Mar 25, 2021
bfcc169
Update utils/rust-setup.js
volovyks Mar 25, 2021
1acb868
Update utils/rust-setup.js
volovyks Mar 25, 2021
83d5378
Y/n logic updated
volovyks Mar 26, 2021
e1d75b8
lint fix
volovyks Mar 26, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 21 additions & 6 deletions index.js
Expand Up @@ -10,6 +10,7 @@ const chalk = require('chalk')
const which = require('which')
const sh = require('shelljs')
const path = require('path')
const rustSetup = require('./utils/rust-setup');

const renameFile = async function(oldPath, newPath) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -119,6 +120,12 @@ const createProject = async function({ contract, frontend, projectDir, veryVerbo
await replaceInFiles({ files: `${projectDir}/README.md`, from: /npm\b( run)?/g, to: 'yarn' })
}

// setup rust
let wasRustupInstalled = false;
if (contract === 'rust') {
wasRustupInstalled = await rustSetup.setupRustAndWasm32Target();
}

if (hasNpm || hasYarn) {
console.log('Installing project dependencies...')
spawn.sync(hasYarn ? 'yarn' : 'npm', ['install'], { cwd: projectDir, stdio: 'inherit' })
Expand All @@ -145,13 +152,21 @@ Inside that directory, you can run several commands:
Also deploys web frontend using GitHub Pages.
Consult with {bold README.md} for details on how to deploy and {bold package.json} for full list of commands.

We suggest that you begin by typing:

{bold cd ${projectDir}}
{bold ${runCommand} dev}
We suggest that you begin by typing:`);

if (wasRustupInstalled) {
console.log(chalk`
{bold source $HOME/.cargo/env}
{bold cd ${projectDir}}
{bold ${runCommand} dev}`);
} else {
console.log(chalk`
{bold cd ${projectDir}}
{bold ${runCommand} dev}`);
}

Happy hacking!
`)
console.log(chalk`
Happy hacking!`);
}

const opts = yargs
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"chalk": "^4.0.0",
"cross-spawn": "^7.0.1",
"ncp": "^2.0.0",
"os": "^0.1.1",
"replace-in-files": "^3.0.0",
"shelljs": "^0.8.3",
"which": "^2.0.2",
Expand Down
122 changes: 122 additions & 0 deletions utils/rust-setup.js
@@ -0,0 +1,122 @@
const chalk = require('chalk');
const os = require('os');
const readline = require('readline');
const sh = require('shelljs');

// script from https://rustup.rs/ with auto-accept flag "-y"
const installRustupScript = "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y";
const updatePath = 'source $HOME/.cargo/env';
const addWasm32TargetScript = "rustup target add wasm32-unknown-unknown";
// We should update PATH in the same script because every new Bash scripts are executed in a separate shell
const updatePathAndAddWasm32TargetScript = updatePath + ' && ' + addWasm32TargetScript;

const isWindows = os.platform() === 'win32'

function isRustupInstalled() {
console.log(chalk`Checking if {bold rustup} is installed...`);
const isInstalled = sh.exec('rustup --version &> /dev/null').code === 0;
console.log(chalk`{bold rustup} is`, isInstalled ? 'installed\n' : 'not installed\n');
return isInstalled;
}

function isWasmTargetAdded() {
console.log(chalk`Checking installed {bold Rust targets}..`);
const addedTargets = sh.exec('rustup target list --installed').stdout;
chadoh marked this conversation as resolved.
Show resolved Hide resolved
const isWasmTargetAdded = addedTargets.includes('wasm32-unknown-unknown');
console.log(chalk`{bold wasm32-unknown-unknown} target `, isWasmTargetAdded ? 'already added' : 'is not added');
return isWasmTargetAdded;
}

function installRustup() {
console.log(chalk`Installing {bold rustup}...`);
sh.exec(installRustupScript);
}

function addWasm32Target() {
console.log(chalk`Adding {bold wasm32-unknown-unknown} target...`);
sh.exec(updatePathAndAddWasm32TargetScript);
}

async function askYesNoQuestionAndRunFunction(question, functionToRun = null) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
for (let attempt = 0; attempt < 4; attempt++) {
const answer = await new Promise((resolve) => {
rl.question(question, (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'y' || userInput === '') {
volovyks marked this conversation as resolved.
Show resolved Hide resolved
if (functionToRun) functionToRun();
resolve(true);
}
if (userInput === 'n') {
resolve(false);
}
resolve(undefined);
});
});
if (answer !== undefined) {
return answer;
}
}
} finally {
rl.close();
}
return false;
}

const installRustupDisclaimer = chalk`In order to work with {bold rust} smart contracts we recommend you install {bold rustup}, the Rust toolchain installer.`;
const addWasm32TargetDisclaimer = chalk`To build Rust smart contracts you need to add {bold WebAssembly} compiler target to Rust toolchain.`;

const installRustupQuestion = chalk`
${installRustupDisclaimer} We can run the following command to do it for you:

{bold ${installRustupScript}}

Continue with installation (Y/n)?: `;

const addWasm32TargetQuestion = chalk`
${addWasm32TargetDisclaimer} We can run the following command to do it for you:

{bold ${addWasm32TargetScript}}

Continue with installation (Y/n)?: `;

const rustupAndWasm32WindowsInstallationInstructions = chalk`
${installRustupDisclaimer}
1. Go to https://rustup.rs
2. Download {bold rustup-init.exe}
3. Install it on your system

${addWasm32TargetDisclaimer}

Run the following command to do it:

{bold ${addWasm32TargetScript}}

Press {bold Enter} to continue project creation.`;

async function setupRustAndWasm32Target() {
try {
if (isWindows) {
await askYesNoQuestionAndRunFunction(rustupAndWasm32WindowsInstallationInstructions);
return false;
}
let wasRustupInstalled = false;
if (!isRustupInstalled()) {
wasRustupInstalled = await askYesNoQuestionAndRunFunction(installRustupQuestion, installRustup);
}
if (!isWasmTargetAdded()) {
await askYesNoQuestionAndRunFunction(addWasm32TargetQuestion, addWasm32Target);
}
return wasRustupInstalled;
} catch (e) {
console.log(chalk`Failed to run {bold rust} setup script`, e);
}
}

module.exports = {
setupRustAndWasm32Target,
};
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -3075,6 +3075,11 @@ os-tmpdir@~1.0.2:
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
integrity sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=

os@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/os/-/os-0.1.1.tgz#208845e89e193ad4d971474b93947736a56d13f3"
integrity sha1-IIhF6J4ZOtTZcUdLk5R3NqVtE/M=

p-cancelable@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-1.1.0.tgz#d078d15a3af409220c886f1d9a0ca2e441ab26cc"
Expand Down