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

Installer: supports windows #499

Merged
merged 28 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 8 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
68 changes: 50 additions & 18 deletions installer/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const {
stdin,
stat,
readAll,
run,
chmod,
remove
} = Deno;
import * as path from "../fs/path.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder("utf-8");
const isWindows = Deno.platform.os === "win";

enum Permission {
Read,
Expand Down Expand Up @@ -89,7 +90,7 @@ function createDirIfNotExists(path: string): void {
function checkIfExistsInPath(path: string): boolean {
const { PATH } = env();

const paths = (PATH as string).split(":");
const paths = (PATH as string).split(isWindows ? ";" : ":");

return paths.includes(path);
}
Expand Down Expand Up @@ -140,17 +141,58 @@ async function fetchModule(url: string): Promise<any> {
function showHelp(): void {
console.log(`deno installer
Install remote or local script as executables.

USAGE:
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...]
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...]

ARGS:
EXE_NAME Name for executable
EXE_NAME Name for executable
SCRIPT_URL Local or remote URL of script to install
[FLAGS...] List of flags for script, both Deno permission and script specific flag can be used.
`);
}

async function genereateExecutable(
axetroy marked this conversation as resolved.
Show resolved Hide resolved
filePath: string,
commands: string[]
): Promise<void> {
// genereate Batch script
axetroy marked this conversation as resolved.
Show resolved Hide resolved
if (isWindows) {
const template = `
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" ${commands.slice(1).join(" ")} %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
${commands.join(" ")} %*
)
`;
const cmdFile = filePath + ".cmd";
axetroy marked this conversation as resolved.
Show resolved Hide resolved
await writeFile(cmdFile, encoder.encode(template));
await chmod(cmdFile, 0o755);
axetroy marked this conversation as resolved.
Show resolved Hide resolved
}

// generate Shell script
const template = `#/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")

case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac

if [ -x "$basedir/deno" ]; then
"$basedir/deno" ${commands.slice(1).join(" ")} "$@"
ret=$?
else
${commands.join(" ")} "$@"
ret=$?
fi
exit $ret
`;
await writeFile(filePath, encoder.encode(template));
await chmod(filePath, 0o755);
axetroy marked this conversation as resolved.
Show resolved Hide resolved
}

export async function install(
moduleName: string,
moduleUrl: string,
Expand Down Expand Up @@ -201,23 +243,13 @@ export async function install(

const commands = [
"deno",
"run",
...grantedPermissions.map(getFlagFromPermission),
moduleUrl,
...scriptArgs,
"$@"
...scriptArgs
];

// TODO: add windows Version
const template = `#/bin/sh\n${commands.join(" ")}`;
await writeFile(filePath, encoder.encode(template));

const makeExecutable = run({ args: ["chmod", "+x", filePath] });
const { code } = await makeExecutable.status();
makeExecutable.close();

if (code !== 0) {
throw new Error("Failed to make file executable");
}
await genereateExecutable(filePath, commands);

console.log(`✅ Successfully installed ${moduleName}`);
console.log(filePath);
Expand Down
77 changes: 69 additions & 8 deletions installer/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { install, uninstall } from "./mod.ts";
import * as path from "../fs/path.ts";

let fileServer: Deno.Process;
const isWindows = Deno.platform.os === "win";

// copied from `http/file_server_test.ts`
async function startFileServer(): Promise<void> {
Expand Down Expand Up @@ -65,10 +66,40 @@ installerTest(async function installBasic(): Promise<void> {

const fileBytes = await readFile(filePath);
const fileContents = new TextDecoder().decode(fileBytes);
assertEquals(
fileContents,
"#/bin/sh\ndeno http://localhost:4500/http/file_server.ts $@"
);
if (isWindows) {
assertEquals(
fileContents,
`
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" run http://localhost:4500/http/file_server.ts %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
deno run http://localhost:4500/http/file_server.ts %*
)
`
);
} else {
assertEquals(
fileContents,
`#/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")

case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac

if [ -x "$basedir/deno" ]; then
"$basedir/deno" run http://localhost:4500/http/file_server.ts "$@"
ret=$?
else
deno run http://localhost:4500/http/file_server.ts "$@"
ret=$?
fi
exit $ret
`
);
}
});

installerTest(async function installWithFlags(): Promise<void> {
Expand All @@ -83,10 +114,40 @@ installerTest(async function installWithFlags(): Promise<void> {

const fileBytes = await readFile(filePath);
const fileContents = new TextDecoder().decode(fileBytes);
assertEquals(
fileContents,
"#/bin/sh\ndeno --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar $@"
);
if (isWindows) {
assertEquals(
fileContents,
`
@IF EXIST "%~dp0\deno.exe" (
"%~dp0\deno.exe" run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
deno run http://localhost:4500/http/file_server.ts %*
)
`
);
} else {
assertEquals(
fileContents,
`#/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")

case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac

if [ -x "$basedir/deno" ]; then
"$basedir/deno" run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar "$@"
ret=$?
else
deno run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar "$@"
ret=$?
fi
exit $ret
`
);
}
});

installerTest(async function uninstallBasic(): Promise<void> {
Expand Down