Skip to content

Commit

Permalink
create install.cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Oct 8, 2024
1 parent 2f7a23e commit 795455d
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 70 deletions.
4 changes: 4 additions & 0 deletions tools/cli/install.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
powershell -Command "iwr http://bore.jairus.dev:3000/install.ps1 -OutFile install.ps1"
powershell -ExecutionPolicy Bypass -File install.ps1
del install.ps1
44 changes: 36 additions & 8 deletions tools/cli/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ function Install-Version {
$VERSION = Get-LatestRelease
}

Write-Output "${BOLD}${BLUE}Modus${RESET} Installer ($VERSION)`n"
Write-Output "${BOLD}${BLUE}Modus${RESET} Installer ${DIM}($VERSION)${RESET}`n"

Install-Release

echo "[4/5] Installed Modus CLI"
}
function Install-Release {
Write-Host "[1/3] Fetching archive for Windows $ARCH"
Expand All @@ -53,12 +55,18 @@ function Install-Release {
New-Item -Path $tmpDir -ItemType Directory | Out-Null

$downloadArchive = Download-ReleaseFromRepo $tmpDir
Clear-Line
Clear-Line

Write-Host "[1/3] Fetched archive for Windows $ARCH"

Write-Host "[2/3] Unpacking archive"

tar -xf "$downloadArchive" -C "$tmpDir"
Remove-Item -PAth "$tmpDir/modus-$VERSION-win32-$ARCH.zip" -Force
Remove-Item -Path "$tmpDir/modus-$VERSION-win32-$ARCH.zip" -Force
Clear-Line

Write-Host "[2/3] Unpacked archive"
if (Test-Path $INSTALL_DIR) {
Remove-Item -Path $INSTALL_DIR -Recurse -Force
}
Expand All @@ -69,8 +77,8 @@ function Install-Release {

Write-Host "[3/3] Installed Modus CLI"

# Add it to path
# Uh, finalize, clean up, restart terminal
Add-ToPath
Restart-Shell
}

function Download-ReleaseFromRepo {
Expand All @@ -86,15 +94,35 @@ function Download-ReleaseFromRepo {
return $downloadFile
}

function Add-ToPath {
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)

$ESC = [char]27
if ($currentPath -notlike "*$INSTALL_DIR*") {
$newPath = $currentPath + ";" + "$INSTALL_DIR\bin"
[System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::User)
echo "[3/3] Added modus to PATH"
} else {
echo "[3/3] Modus already in PATH"
}
}

function Restart-Shell {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}

function Clear-Line {
Write-Host "${ESC}[F${ESC}[K" -NoNewLine
}

# ANSII codes
$ESC = [char]27
$BOLD = "${ESC}[1m"
$BLUE = "${ESC}[34;1m"
$DIM = "${ESC}[2m"
$RESET = "${ESC}[0m"

# This is the entry point
Install-Version
# Update-Profile $INSTALL_DIR
# Write-Host "The Modus CLI has been installed! 🎉"
# Write-Host "Run 'modus' to get started."
Write-Host "`nThe Modus CLI has been installed! " -NoNewLine
$(Write-Host ([System.char]::ConvertFromUtf32(127881)))
Write-Host "Run ${DIM}modus${RESET} to get started"
7 changes: 5 additions & 2 deletions tools/cli/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,17 @@ install_from_file() {
local archive="$1"
local extract_to="$(dirname "$archive")"

echo "[2/5] Unpacking archive"

tar -xf "$archive" -C "$extract_to"

rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
rm -f "$archive"
mv "$extract_to/modus/"* "$INSTALL_DIR"
rm -rf "$extract_to"
rm -f "$archive"


clear_line
echo "[2/5] Unpacked archive"
}

Expand Down
1 change: 0 additions & 1 deletion tools/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"ora": "^8.1.0"
},
"devDependencies": {
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^4",
"@types/node": "^18",
"eslint": "^8",
Expand Down
64 changes: 53 additions & 11 deletions tools/cli/src/commands/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
*/

import { Args, Command, Flags } from "@oclif/core";
import { expandHomeDir } from "../../util/index.js";
import { Metadata } from "../../util/metadata.js";
import { expandHomeDir, isRunnable } from "../../util/index.js";
import BuildCommand from "../build/index.js";
import path from "path";
import { copyFileSync, existsSync, readFileSync, watch as watchFolder } from "fs";
import { copyFileSync, existsSync, readdirSync, readFileSync, watch as watchFolder } from "fs";
import chalk from "chalk";
import { spawn } from "child_process";
import os from "node:os";
Expand Down Expand Up @@ -58,6 +57,13 @@ export default class Run extends Command {
required: false,
default: 3000
}),
runtime: Flags.string({
char: "r",
description: "Runtime to use",
hidden: false,
required: false,
default: getLatestRuntime()
})
};

static description = "Launch a Modus app to local development";
Expand All @@ -66,7 +72,8 @@ export default class Run extends Command {

async run(): Promise<void> {
const { args, flags } = await this.parse(Run);
const runtimePath = expandHomeDir("~/.hypermode/sdk/" + Metadata.runtime_version + "/runtime") + (os.platform() === "win32" ? ".exe" : "");
const isDev = flags.runtime.startsWith("dev-") || flags.runtime.startsWith("link");
const runtimePath = expandHomeDir("~/.modus/sdk/" + flags.runtime) + (isDev ? "" : "/runtime" + (os.platform() === "win32" ? ".exe" : ""));

const cwd = args.path ? path.join(process.cwd(), args.path) : process.cwd();
const watch = flags.watch;
Expand All @@ -76,6 +83,8 @@ export default class Run extends Command {
process.exit(0);
}

// TODO: Check the type of SDK we are running

if (!existsSync(path.join(cwd, "/node_modules"))) {
this.logError("Dependencies not installed! Please install dependencies by running `npm i` and try again");
process.exit(0);
Expand All @@ -86,8 +95,17 @@ export default class Run extends Command {
process.exit(0);
}

let install_cmd = flags.runtime;
if (isDev) {
if (install_cmd.startsWith("link")) {
install_cmd = "--link ./path-to-modus";
} else if (install_cmd.startsWith("dev-")) {
install_cmd = "--branch " + install_cmd.split("-")[1] + " --commit " + install_cmd.split("-")[2];
}
}

if (!existsSync(runtimePath)) {
this.logError("Modus Runtime v" + Metadata.runtime_version + " not installed! Run `modus sdk install " + Metadata.runtime_version + "` and try again!");
this.logError("Modus Runtime " + (isDev ? "" : "v") + flags.runtime + " not installed!\n Run `modus sdk install " + install_cmd + "` and try again!");
process.exit(0);
}

Expand All @@ -103,15 +121,30 @@ export default class Run extends Command {
if (flags.build) await BuildCommand.run(args.path ? [args.path] : []);
} catch { }
const build_wasm = path.join(cwd, "/build/" + project_name + ".wasm");
const deploy_wasm = expandHomeDir("~/.hypermode/" + project_name + ".wasm");
const deploy_wasm = expandHomeDir("~/.modus/" + project_name + ".wasm");
copyFileSync(build_wasm, deploy_wasm);

spawn(runtimePath, {
stdio: "inherit", env: {
...process.env,
MODUS_ENV: "dev"
if (isDev) {
if (!isRunnable("go")) {
this.logError("Cannot find any valid versions of Go! Please install go")
}
});
spawn("go run .", {
cwd: runtimePath,
stdio: "inherit",
env: {
...process.env,
MODUS_ENV: "dev"
}
});
} else {
spawn(runtimePath, {
stdio: "inherit",
env: {
...process.env,
MODUS_ENV: "dev"
}
});
}

if (watch) {
const delay = flags.freq;
Expand Down Expand Up @@ -143,4 +176,13 @@ export default class Run extends Command {
private logError(message: string) {
this.log("\n" + chalk.red(" ERROR ") + chalk.dim(": " + message));
}
}

function getLatestRuntime(): string | undefined {
let versions: string[] = [];
try {
versions = readdirSync(expandHomeDir("~/.modus/sdk")).reverse().filter(v => !v.startsWith("dev-") && !v.startsWith("link"));
} catch { }
if (!versions.length) return undefined;
return versions[0];
}
4 changes: 1 addition & 3 deletions tools/cli/src/commands/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default class NewCommand extends Command {
}).start();

if (sdk === "AssemblyScript") {
execSync("npm install", { cwd: dir, stdio: "ignore" });
if (isRunnable("npm")) execSync("npm install", { cwd: dir, stdio: "ignore" });
} else if (sdk === "Go (Beta)") {
const sh = execSync("go install", { cwd: dir, stdio: "ignore" });
if (!sh) {
Expand All @@ -165,8 +165,6 @@ export default class NewCommand extends Command {
this.log(chalk.dim(`$ ${dir == process.cwd() ? "" : "cd " + path.basename(dir)} && modus dev --build`));
}

// TODO: install deps

private async installRuntime() {
const latest_runtime = await Metadata.getLatestRuntime();

Expand Down
Loading

0 comments on commit 795455d

Please sign in to comment.