Skip to content

Commit

Permalink
modus build should install SDK if not already installed (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Oct 30, 2024
1 parent f534cf3 commit 234d0ce
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 2024-10-29 - CLI Version 0.13.4

- `modus build` should install SDK if not already installed [#524](https://github.com/hypermodeinc/modus/pull/524)

## 2024-10-29 - CLI Version 0.13.3

- Fix Go not found on first install [#522](https://github.com/hypermodeinc/modus/pull/522)
Expand Down
5 changes: 5 additions & 0 deletions cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getHeader } from "../../custom/header.js";
import { getAppInfo } from "../../util/appinfo.js";
import { withSpinner } from "../../util/index.js";
import { execFileWithExitCode } from "../../util/cp.js";
import SDKInstallCommand from "../sdk/install/index.js";

export default class BuildCommand extends Command {
static args = {
Expand Down Expand Up @@ -63,6 +64,10 @@ export default class BuildCommand extends Command {
// pass chalk level to child processes so they can colorize output
process.env.FORCE_COLOR = chalk.level.toString();

if (!(await vi.sdkVersionIsInstalled(app.sdk, app.sdkVersion))) {
await SDKInstallCommand.run([app.sdk, app.sdkVersion, "--no-logo"]);
}

const results = await withSpinner("Building " + app.name, async () => {
const execOpts = {
cwd: appPath,
Expand Down
27 changes: 1 addition & 26 deletions cli/src/commands/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as fs from "../../util/fs.js";
import * as vi from "../../util/versioninfo.js";
import { execFile } from "../../util/cp.js";
import { isOnline } from "../../util/index.js";
import { GitHubOwner, GitHubRepo, MinGoVersion, MinNodeVersion, MinTinyGoVersion, ModusHomeDir, SDK, parseSDK } from "../../custom/globals.js";
import { MinGoVersion, MinNodeVersion, MinTinyGoVersion, SDK, parseSDK } from "../../custom/globals.js";
import { withSpinner } from "../../util/index.js";
import { extract } from "../../util/tar.js";
import SDKInstallCommand from "../sdk/install/index.js";
Expand Down Expand Up @@ -250,37 +250,12 @@ export default class NewCommand extends Command {

const sdkVersion = installedSdkVersion;
const sdkPath = vi.getSdkPath(sdk, sdkVersion);

const templatesArchive = path.join(sdkPath, "templates.tar.gz");
if (!(await fs.exists(templatesArchive))) {
this.logError(`Could not find any templates for ${sdkText} ${sdkVersion}`);
this.exit(1);
}

// Install build tools if needed
if (sdk == SDK.Go) {
const ext = os.platform() === "win32" ? ".exe" : "";
const buildTool = path.join(sdkPath, "modus-go-build" + ext);
if (!(await fs.exists(buildTool))) {
if (await isOnline()) {
const module = `github.com/${GitHubOwner}/${GitHubRepo}/sdk/go/tools/modus-go-build@${sdkVersion}`;
await withSpinner("Downloading the Modus Go build tool.", async () => {
await execFile("go", ["install", module], {
cwd: ModusHomeDir,
shell: true,
env: {
...process.env,
GOBIN: sdkPath,
},
});
});
} else {
this.logError("Could not find the Modus Go build tool. Please try again when you are online.");
this.exit(1);
}
}
}

// Create the app
this.log(chalk.dim(`Using ${sdkText} ${sdkVersion}`));
await withSpinner(`Creating a new Modus ${sdk} app.`, async () => {
Expand Down
3 changes: 3 additions & 0 deletions cli/src/commands/sdk/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ export default class SDKInstallCommand extends Command {
spinner.fail(chalk.red(`Failed to download ${sdkText}`));
throw e;
}

await installer.installBuildTools(sdk, sdkVersion);

spinner.succeed(chalk.dim(`Installed ${sdkText}`));
});
}
Expand Down
14 changes: 11 additions & 3 deletions cli/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import chalk from "chalk";
import { oraPromise, Ora } from "ora";
import ora, { Ora } from "ora";

import path from "node:path";
import { Readable } from "node:stream";
Expand All @@ -17,10 +17,18 @@ import { createWriteStream } from "node:fs";
import * as fs from "./fs.js";

export async function withSpinner<T>(text: string, fn: (spinner: Ora) => Promise<T>): Promise<T> {
return await oraPromise(fn, {
// NOTE: Ora comes with "oraPromise", but it doesn't clear the original text on completion.
// Thus, we use this custom async function to ensure the spinner is applied correctly.
const spinner = ora({
color: "white",
text: text,
});
}).start();

try {
return await fn(spinner);
} finally {
spinner.stop();
}
}

export async function downloadFile(url: string, dest: string): Promise<boolean> {
Expand Down
32 changes: 32 additions & 0 deletions cli/src/util/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import path from "node:path";
import * as fs from "./fs.js";
import * as vi from "./versioninfo.js";
import { extract } from "./tar.js";
import { execFile } from "./cp.js";
import { downloadFile, isOnline } from "./index.js";
import { GitHubOwner, GitHubRepo, SDK } from "../custom/globals.js";

Expand Down Expand Up @@ -60,3 +61,34 @@ export async function installRuntime(version: string) {
await extract(archivePath, installDir);
await fs.rm(archivePath);
}

export async function installBuildTools(sdk: SDK, sdkVersion: string) {
switch (sdk) {
case SDK.Go:
await installGoBuildTools(sdkVersion);
break;
}
}

async function installGoBuildTools(sdkVersion: string) {
const sdkPath = vi.getSdkPath(SDK.Go, sdkVersion);

const ext = os.platform() === "win32" ? ".exe" : "";
const buildTool = path.join(sdkPath, "modus-go-build" + ext);
if (await fs.exists(buildTool)) {
return;
}

if (!(await isOnline())) {
throw new Error("Could not find the Modus Go build tool. Please try again when you are online.");
}

const module = `github.com/${GitHubOwner}/${GitHubRepo}/sdk/go/tools/modus-go-build@${sdkVersion}`;
await execFile("go", ["install", module], {
shell: true,
env: {
...process.env,
GOBIN: sdkPath,
},
});
}

0 comments on commit 234d0ce

Please sign in to comment.