Skip to content

Commit

Permalink
Reorganize version and platform into Deno.build and Deno.version (den…
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Mar 6, 2019
1 parent 91364ca commit de1a10e
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 83 deletions.
2 changes: 1 addition & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ ts_sources = [
"js/assets.ts",
"js/blob.ts",
"js/buffer.ts",
"js/build.ts",
"js/chmod.ts",
"js/console_table.ts",
"js/compiler.ts",
Expand Down Expand Up @@ -92,7 +93,6 @@ ts_sources = [
"js/net.ts",
"js/os.ts",
"js/permissions.ts",
"js/platform.ts",
"js/plugins.d.ts",
"js/process.ts",
"js/read_dir.ts",
Expand Down
27 changes: 27 additions & 0 deletions js/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

// Do not add unsupported platforms.
/** Build related information */
export interface BuildInfo {
/** The operating system CPU architecture. */
arch: "x64";

/** The operating system platform. */
os: "mac" | "win" | "linux";

/** The GN build arguments */
gnArgs: string;
}

// 'build' is injected by rollup.config.js at compile time.
export const build: BuildInfo = {
// tslint:disable:no-any
// These string will be replaced by rollup
arch: `ROLLUP_REPLACE_ARCH` as any,
os: `ROLLUP_REPLACE_OS` as any,
gnArgs: `ROLLUP_REPLACE_GN_ARGS`
// tslint:disable:any
};

// TODO(kevinkassimo): deprecate Deno.platform
export const platform = build;
10 changes: 7 additions & 3 deletions js/platform_test.ts → js/build_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert } from "./test_util.ts";

test(function platformTransform() {
// deno.platform is injected by rollup at compile time. Here
test(function buildInfo() {
// Deno.build is injected by rollup at compile time. Here
// we check it has been properly transformed.
const { arch, os } = Deno.platform;
const { arch, os } = Deno.build;
assert(arch === "x64");
assert(os === "mac" || os === "win" || os === "linux");
});

test(function buildGnArgs() {
assert(Deno.build.gnArgs.length > 100);
});
2 changes: 1 addition & 1 deletion js/chmod_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assertEqual } from "./test_util.ts";

const isNotWindows = Deno.platform.os !== "win";
const isNotWindows = Deno.build.os !== "win";

testPerm({ read: true, write: true }, function chmodSyncSuccess() {
const enc = new TextEncoder();
Expand Down
2 changes: 1 addition & 1 deletion js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export {
Permission,
Permissions
} from "./permissions";
export { platform } from "./platform";
export { truncateSync, truncate } from "./truncate";
export { FileInfo } from "./file_info";
export { connect, dial, listen, Listener, Conn } from "./net";
export { metrics, Metrics } from "./metrics";
export { resources } from "./resources";
export { run, RunOptions, Process, ProcessStatus } from "./process";
export { inspect } from "./console";
export { build, platform } from "./build";
export { version } from "./version";
export const args: string[] = [];

Expand Down
4 changes: 2 additions & 2 deletions js/dir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ testPerm({ write: true }, function dirCwdChdirSuccess() {
const path = Deno.makeTempDirSync();
Deno.chdir(path);
const current = Deno.cwd();
if (Deno.platform.os === "mac") {
if (Deno.build.os === "mac") {
assertEqual(current, "/private" + path);
} else {
assertEqual(current, path);
Expand All @@ -20,7 +20,7 @@ testPerm({ write: true }, function dirCwdChdirSuccess() {

testPerm({ write: true }, function dirCwdError() {
// excluding windows since it throws resource busy, while removeSync
if (["linux", "mac"].includes(Deno.platform.os)) {
if (["linux", "mac"].includes(Deno.build.os)) {
const initialdir = Deno.cwd();
const path = Deno.makeTempDirSync();
Deno.chdir(path);
Expand Down
18 changes: 0 additions & 18 deletions js/platform.ts

This file was deleted.

2 changes: 1 addition & 1 deletion js/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ testPerm({ run: true }, async function runCommandFailedWithCode() {
});

testPerm({ run: true }, async function runCommandFailedWithSignal() {
if (Deno.platform.os === "win") {
if (Deno.build.os === "win") {
return; // No signals on windows.
}
const p = run({
Expand Down
4 changes: 2 additions & 2 deletions js/read_link_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ testPerm({ write: true, read: true }, function readlinkSyncSuccess() {
Deno.mkdirSync(target);
// TODO Add test for Windows once symlink is implemented for Windows.
// See https://github.com/denoland/deno/issues/815.
if (Deno.platform.os !== "win") {
if (Deno.build.os !== "win") {
Deno.symlinkSync(target, symlink);
const targetPath = Deno.readlinkSync(symlink);
assertEqual(targetPath, target);
Expand Down Expand Up @@ -47,7 +47,7 @@ testPerm({ write: true, read: true }, async function readlinkSuccess() {
Deno.mkdirSync(target);
// TODO Add test for Windows once symlink is implemented for Windows.
// See https://github.com/denoland/deno/issues/815.
if (Deno.platform.os !== "win") {
if (Deno.build.os !== "win") {
Deno.symlinkSync(target, symlink);
const targetPath = await Deno.readlink(symlink);
assertEqual(targetPath, target);
Expand Down
2 changes: 1 addition & 1 deletion js/unit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import "./blob_test.ts";
import "./buffer_test.ts";
import "./build_test.ts";
import "./chmod_test.ts";
// TODO find a way to test the compiler with split snapshots
// import "./compiler_test.ts";
Expand All @@ -29,7 +30,6 @@ import "./mixins/dom_iterable_test.ts";
import "./mkdir_test.ts";
import "./net_test.ts";
import "./os_test.ts";
import "./platform_test.ts";
import "./process_test.ts";
import "./read_dir_test.ts";
import "./read_file_test.ts";
Expand Down
5 changes: 2 additions & 3 deletions js/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ interface Version {
deno: string;
v8: string;
typescript: string;
gnArgs: string;
}

export const version: Version = {
deno: "",
v8: "",
typescript: "TS_VERSION", // This string will be replaced by rollup
gnArgs: `GN_ARGS` // This string will be replaced by rollup
// This string will be replaced by rollup
typescript: `ROLLUP_REPLACE_TS_VERSION`
};

/**
Expand Down
4 changes: 0 additions & 4 deletions js/version_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ test(function version() {
assert(pattern.test(Deno.version.v8));
assert(pattern.test(Deno.version.typescript));
});

test(function versionGnArgs() {
assert(Deno.version.gnArgs.length > 100);
});
4 changes: 2 additions & 2 deletions js/write_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ testPerm({ write: false }, function writeFileSyncPerm() {
});

testPerm({ read: true, write: true }, function writeFileSyncUpdatePerm() {
if (Deno.platform.os !== "win") {
if (Deno.build.os !== "win") {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
Expand Down Expand Up @@ -146,7 +146,7 @@ testPerm({ read: true, write: false }, async function writeFilePerm() {
});

testPerm({ read: true, write: true }, async function writeFileUpdatePerm() {
if (Deno.platform.os !== "win") {
if (Deno.build.os !== "win") {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
Expand Down
49 changes: 5 additions & 44 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import typescriptPlugin from "rollup-plugin-typescript2";
import { createFilter } from "rollup-pluginutils";
import replace from "rollup-plugin-replace";
import typescript from "typescript";
import MagicString from "magic-string";

const mockPath = path.resolve(__dirname, "js/mock_builtin.js");
const platformPath = path.resolve(__dirname, "js/platform.ts");
const tsconfig = path.resolve(__dirname, "tsconfig.json");
const typescriptPath = path.resolve(
__dirname,
Expand Down Expand Up @@ -91,40 +89,6 @@ const osNodeToDeno = {
linux: "linux"
};

/** Inject deno.platform.arch and deno.platform.os
* @param {any} param0
*/
function platform({ include, exclude } = {}) {
if (!include) {
throw new Error("include option must be passed");
}

const filter = createFilter(include, exclude);

return {
name: "platform",
/**
* @param {any} _code
* @param {string} id
*/
transform(_code, id) {
if (filter(id)) {
// Adapted from https://github.com/rollup/rollup-plugin-inject/blob/master/src/index.js
const arch = archNodeToDeno[process.arch];
const os = osNodeToDeno[process.platform];
// We do not have to worry about the interface here, because this is just to generate
// the actual runtime code, not any type information integrated into Deno
const magicString = new MagicString(`
export const platform = { arch: "${arch}", os:"${os}" };`);
return {
code: magicString.toString(),
map: magicString.generateMap()
};
}
}
};
}

// This plugin resolves at bundle time any generated resources that are
// in the build path under `gen` and specified with a MID starting with `gen/`.
// The plugin assumes that the MID needs to have the `.ts` extension appended.
Expand Down Expand Up @@ -222,15 +186,12 @@ export default function makeConfig(commandOptions) {
},

plugins: [
// inject platform and arch from Node
platform({
include: [platformPath]
}),

// replace strings
// inject build and version info
replace({
TS_VERSION: typescript.version,
GN_ARGS: gnArgs
ROLLUP_REPLACE_TS_VERSION: typescript.version,
ROLLUP_REPLACE_ARCH: archNodeToDeno[process.arch],
ROLLUP_REPLACE_OS: osNodeToDeno[process.platform],
ROLLUP_REPLACE_GN_ARGS: gnArgs
}),

// would prefer to use `rollup-plugin-virtual` to inject the empty module, but there
Expand Down

0 comments on commit de1a10e

Please sign in to comment.