Skip to content

Commit

Permalink
Add 'upgrade' command
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitemaeric committed Oct 17, 2024
1 parent 519f313 commit 9b20aaa
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 17 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@ Calling `drenv` without any arguments will also display this message.
```
Usage: drenv [options] [command]
CLI to manage DragonRuby environments.
CLI to manage DragonRuby environments
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
setup Setup your shell profile to use drenv.
new <name> Create a new DragonRuby project.
register <path> Register a DragonRuby installation. This moves the installation to the $HOME/.drenv directory.
global [version] Get or set the global version of DragonRuby.
local [version] Get or set the local version of DragonRuby.
versions List out all locally installed versions of DragonRuby.
setup Setup your shell profile to use drenv
new <name> Create a new DragonRuby project
register <path> Register a DragonRuby installation
global [version] Get or set the global version of DragonRuby
local [version] Get or set the local version of DragonRuby
versions List out all locally installed versions of DragonRuby
upgrade Upgrade the version of drenv
help [command] display help for command
```

Expand All @@ -60,11 +61,14 @@ into **drenv**'s home directory.
> You will need to register at least one DragonRuby installation before you can
> use **drenv**.
### `drenv global <version>`
### `drenv global [version]`

This command sets the version of DragonRuby that **drenv** will use when
creating new projects.

If you call `drenv global` without any arguments, it will display the current
global version.

### `drenv new <name>`

This command will create a new DragonRuby project with the specified name.
Expand All @@ -81,13 +85,19 @@ Under the hood, all **drenv** does is copy the contents of the global DragonRuby
installation into the current project directory, excluding the `mygame`
directory.

### `drenv upgrade`

This command will download and upgrade your **drenv** installation to the latest
version.

### `drenv versions`

This command will list out all registered versions of DragonRuby.

```
5.32
6.4
* 6.3
```

Tested on MacOS Macbook Pro M1 with DragonRuby 5.32 and 6.3.
Tested on MacOS Macbook Pro M1 with DragonRuby 5.32, 6.3, and 6.4.
62 changes: 62 additions & 0 deletions commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { greaterThan, parse } from "jsr:@std/semver";

import { program } from "../main.ts";
import { drenvBinPath, version } from "../constants.ts";

type Asset = {
url: string;
id: number;
node_id: string;
name: string;
label: string | null;
uploader: any;
content_type: string;
state: string;
size: number;
download_count: number;
created_at: string;
updated_at: string;
browser_download_url: string;
};

export default async function upgrade() {
console.log(`Current version is v${program.version()}`);
console.log("Checking for updates...");

const dataResponse = await fetch(
"https://api.github.com/repos/nitemaeric/drenv/releases/latest",
);
const data = await dataResponse.json();

const localVersion = parse(version);
const remoteVersion = parse(data.tag_name.slice(1));

if (greaterThan(remoteVersion, localVersion)) {
console.log(`New version found: ${data.tag_name}`);

const file = await Deno.open(drenvBinPath, { write: true, create: true });

const targetAsset = data.assets.find((asset: Asset) =>
asset.name.includes(Deno.build.target)
);

if (targetAsset) {
console.log(`Downloading new version...`);

const downloadResponse = await fetch(targetAsset.browser_download_url);

if (!downloadResponse.body) {
throw new Error("Could not download the new version");
}

await downloadResponse.body.pipeTo(file.writable);
await Deno.chmod(drenvBinPath, 0o755);

console.log(`Upgraded to ${data.tag_name}`);
} else {
console.log("No asset found for your platform");
}
} else {
console.log("Already up-to-date");
}
}
2 changes: 2 additions & 0 deletions constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const version = "0.3.0";
export const homePath = `${Deno.env.get("HOME")}/.drenv`;
export const versionsPath = `${homePath}/versions`;
export const binPath = `${homePath}/bin`;
export const drenvBinPath = `${binPath}/drenv`;
export const shell = Deno.env.get("SHELL");
4 changes: 4 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import local from "./commands/local.ts";
import newCommand from "./commands/new.ts";
import register from "./commands/register.ts";
import setup from "./commands/setup.ts";
import upgrade from "./commands/upgrade.ts";
import versions from "./commands/versions.ts";

const program = new Command();
import { version } from "./constants.ts";

export const program = new Command();

const actionRunner = (fn: (...args: any[]) => Promise<any>) => {
return (...args: any[]): any =>
Expand All @@ -24,37 +27,42 @@ const actionRunner = (fn: (...args: any[]) => Promise<any>) => {

program
.name("drenv")
.description("CLI to manage DragonRuby environments.")
.version("0.2.3");
.description("CLI to manage DragonRuby environments")
.version(version);

program.command("setup")
.description("Setup your shell profile to use drenv.")
.description("Setup your shell profile to use drenv")
.action(actionRunner(setup));

program.command("new")
.argument("<name>", "Name of the new project")
.description("Create a new DragonRuby project.")
.description("Create a new DragonRuby project")
.action(actionRunner(newCommand));

program.command("register")
.argument("<path>", "Path to a fresh DragonRuby directory")
.summary("Register a DragonRuby installation")
.description(
"Register a DragonRuby installation. This moves the installation to the $HOME/.drenv directory.",
)
.action(actionRunner(register));

program.command("global")
.argument("[version]", "Version of DragonRuby to use")
.description("Get or set the global version of DragonRuby.")
.description("Get or set the global version of DragonRuby")
.action(actionRunner(global));

program.command("local")
.argument("[version]", "Version of DragonRuby to use")
.description("Get or set the local version of DragonRuby.")
.description("Get or set the local version of DragonRuby")
.action(actionRunner(local));

program.command("versions")
.description("List out all locally installed versions of DragonRuby.")
.description("List out all locally installed versions of DragonRuby")
.action(actionRunner(versions));

program.command("upgrade")
.description("Upgrade the version of drenv")
.action(actionRunner(upgrade));

program.parse();

0 comments on commit 9b20aaa

Please sign in to comment.