Skip to content

Commit 717b513

Browse files
committed
Add isIOS, isAndroid in platform, and fast ts watcher and transpiler
Image should not requestLayout when sized with 'exactly' spec Update image tests Tests will run in ios only
1 parent 44abf94 commit 717b513

File tree

9 files changed

+272
-41
lines changed

9 files changed

+272
-41
lines changed

build/tsc-dev.js

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,88 @@ var ts = require("typescript");
33
var fs = require("fs");
44
var path = require("path");
55
var arg1 = process.argv.length > 2 ? process.argv[2] : "";
6+
var isTranspile = arg1.indexOf("t") >= 0;
67
var isIncremental = arg1.indexOf("i") >= 0;
8+
var isWatching = arg1.indexOf("w") >= 0;
9+
var opts = [];
10+
if (isTranspile) {
11+
opts.push("transpile");
12+
}
713
if (isIncremental) {
8-
console.log("incremental");
14+
opts.push("incremental");
15+
}
16+
if (isWatching) {
17+
opts.push("watch");
18+
}
19+
if (opts.length > 0) {
20+
console.log("Options: " + opts.join(", "));
21+
}
22+
function isTS(file) {
23+
return file.lastIndexOf(".ts") === file.length - 3;
24+
}
25+
function isDTS(file) {
26+
return file.lastIndexOf(".d.ts") === file.length - 5;
27+
}
28+
function getJsPath(tsPath) {
29+
return path.join(path.dirname(tsPath), path.basename(tsPath, ".ts")) + ".js";
30+
}
31+
function hasChanged(tsName) {
32+
try {
33+
var jsName = getJsPath(tsName);
34+
var tsTime = fs.statSync(tsName).mtime.getTime();
35+
var jsTime = fs.statSync(jsName).mtime.getTime();
36+
return jsTime < tsTime;
37+
}
38+
catch (e) {
39+
return true;
40+
}
41+
}
42+
function transpile(fileNames, options) {
43+
console.time("transpile");
44+
var files = fileNames.filter(function (f) { return !isDTS(f); });
45+
if (isIncremental) {
46+
files = files.filter(hasChanged);
47+
}
48+
files.forEach(function (tsPath) {
49+
var tsSource = fs.readFileSync(tsPath, { encoding: "utf8" });
50+
var jsSource = ts.transpile(tsSource, options);
51+
var jsPath = getJsPath(tsPath);
52+
fs.writeFileSync(jsPath, jsSource, { flag: "w" }, function (err) { console.log(err); });
53+
if (isIncremental) {
54+
console.log(" - " + tsPath);
55+
}
56+
});
57+
console.timeEnd("transpile");
58+
if (isWatching) {
59+
console.log("Watching for changes...");
60+
fs.watch(".", { persistent: true, recursive: true, encoding: "utf8" }, function (event, file) {
61+
try {
62+
if (isTS(file) && !isDTS(file)) {
63+
var tsPath = file;
64+
var label = " - " + tsPath;
65+
console.time(label);
66+
var tsSource = fs.readFileSync(tsPath, { encoding: "utf8" });
67+
var jsSource = ts.transpile(tsSource, options);
68+
var jsPath = getJsPath(tsPath);
69+
fs.writeFileSync(jsPath, jsSource, { flag: "w" }, function (err) { console.log(err); });
70+
console.timeEnd(label);
71+
}
72+
}
73+
catch (e) {
74+
}
75+
});
76+
}
977
}
1078
function compile(fileNames, options) {
1179
console.time("program");
1280
var program = ts.createProgram(fileNames, options);
1381
console.timeEnd("program");
14-
var sourceFiles = program.getSourceFiles().filter(function (f) { return f.fileName.lastIndexOf(".d.ts") !== f.fileName.length - 5; });
82+
var sourceFiles = program.getSourceFiles().filter(function (f) { return !isDTS(f.fileName); });
1583
var emitResults = [];
1684
var allDiagnostics = [];
1785
console.time("transpile");
1886
if (isIncremental) {
19-
sourceFiles = sourceFiles.filter(function (srcFile) {
20-
try {
21-
var tsName = srcFile.fileName;
22-
var jsName = path.join(path.dirname(tsName), path.basename(tsName, ".ts")) + ".js";
23-
var tsTime = fs.statSync(tsName).mtime.getTime();
24-
var jsTime = fs.statSync(jsName).mtime.getTime();
25-
return jsTime < tsTime;
26-
}
27-
catch (e) {
28-
return true;
29-
}
30-
});
87+
sourceFiles = sourceFiles.filter(function (srcFile) { return hasChanged(srcFile.fileName); });
3188
sourceFiles.forEach(function (srcFile) {
3289
console.log(" - " + srcFile.fileName);
3390
emitResults.push(program.emit(srcFile));
@@ -54,13 +111,19 @@ function compile(fileNames, options) {
54111
process.exit(exitCode);
55112
}
56113
var files = JSON.parse(fs.readFileSync("./tsconfig.json")).files;
57-
compile(files, {
114+
var options = {
58115
noEmitOnError: true,
59116
noEmitHelpers: true,
60-
target: ts.ScriptTarget.ES5,
61-
module: ts.ModuleKind.CommonJS,
117+
target: 1 /* ES5 */,
118+
module: 1 /* CommonJS */,
62119
declaration: false,
63120
noImplicitAny: false,
64121
noImplicitUseStrict: true,
65122
experimentalDecorators: true
66-
});
123+
};
124+
if (isTranspile) {
125+
transpile(files, { module: 1 /* CommonJS */ });
126+
}
127+
else {
128+
compile(files, options);
129+
}

build/tsc-dev.ts

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,104 @@ var path = require("path");
55

66
var arg1 = process.argv.length > 2 ? process.argv[2] : "";
77

8+
var isTranspile = arg1.indexOf("t") >= 0;
89
var isIncremental = arg1.indexOf("i") >= 0;
10+
var isWatching = arg1.indexOf("w") >= 0;
11+
12+
var opts = [];
13+
14+
if (isTranspile) {
15+
opts.push("transpile");
16+
}
17+
918
if (isIncremental) {
10-
console.log("incremental");
19+
opts.push("incremental");
20+
}
21+
22+
if (isWatching) {
23+
opts.push("watch");
24+
}
25+
26+
if (opts.length > 0) {
27+
console.log("Options: " + opts.join(", "));
28+
}
29+
30+
function isTS(file: string): boolean {
31+
return file.lastIndexOf(".ts") === file.length - 3;
32+
}
33+
34+
function isDTS(file: string): boolean {
35+
return file.lastIndexOf(".d.ts") === file.length - 5;
36+
}
37+
38+
function getJsPath(tsPath: string): string {
39+
return path.join(path.dirname(tsPath), path.basename(tsPath, ".ts")) + ".js";
40+
}
41+
42+
function hasChanged(tsName: string): boolean {
43+
try {
44+
var jsName = getJsPath(tsName);
45+
46+
var tsTime = fs.statSync(tsName).mtime.getTime();
47+
var jsTime = fs.statSync(jsName).mtime.getTime();
48+
49+
return jsTime < tsTime;
50+
} catch(e) {
51+
return true;
52+
}
53+
}
54+
55+
function transpile(fileNames: string[], options: ts.CompilerOptions) {
56+
console.time("transpile");
57+
var files = fileNames.filter(f => !isDTS(f));
58+
if (isIncremental) {
59+
files = files.filter(hasChanged);
60+
}
61+
files.forEach(tsPath => {
62+
var tsSource = fs.readFileSync(tsPath, { encoding: "utf8" });
63+
var jsSource = ts.transpile(tsSource, options);
64+
var jsPath = getJsPath(tsPath);
65+
fs.writeFileSync(jsPath, jsSource, { flag: "w" }, function(err) { console.log(err); });
66+
if (isIncremental) {
67+
console.log(" - " + tsPath);
68+
}
69+
});
70+
console.timeEnd("transpile");
71+
72+
if (isWatching) {
73+
console.log("Watching for changes...");
74+
fs.watch(".", { persistent: true, recursive: true, encoding: "utf8" }, (event, file) => {
75+
try {
76+
if (isTS(file) && !isDTS(file)) {
77+
var tsPath = file;
78+
var label = " - " + tsPath;
79+
console.time(label);
80+
var tsSource = fs.readFileSync(tsPath, { encoding: "utf8" });
81+
var jsSource = ts.transpile(tsSource, options);
82+
var jsPath = getJsPath(tsPath);
83+
fs.writeFileSync(jsPath, jsSource, { flag: "w" }, function(err) { console.log(err); });
84+
console.timeEnd(label);
85+
}
86+
} catch(e) {
87+
// console.log(e);
88+
}
89+
});
90+
}
1191
}
1292

1393
function compile(fileNames: string[], options: ts.CompilerOptions) {
1494
console.time("program");
1595
var program = ts.createProgram(fileNames, options);
1696

1797
console.timeEnd("program");
18-
var sourceFiles = program.getSourceFiles().filter(f => f.fileName.lastIndexOf(".d.ts") !== f.fileName.length - 5);
98+
var sourceFiles = program.getSourceFiles().filter(f => !isDTS(f.fileName));
1999

20100
var emitResults = [];
21101
var allDiagnostics = [];
22102

23103
console.time("transpile");
24104
if (isIncremental) {
25-
sourceFiles = sourceFiles.filter(srcFile => {
26-
try {
27-
var tsName = srcFile.fileName;
28-
var jsName = path.join(path.dirname(tsName), path.basename(tsName, ".ts")) + ".js";
29-
30-
var tsTime = fs.statSync(tsName).mtime.getTime();
31-
var jsTime = fs.statSync(jsName).mtime.getTime();
32-
33-
return jsTime < tsTime;
34-
} catch(e) {
35-
return true;
36-
}
37-
});
38-
105+
sourceFiles = sourceFiles.filter(srcFile => hasChanged(srcFile.fileName));
39106
sourceFiles.forEach(srcFile => {
40107
console.log(" - " + srcFile.fileName);
41108
emitResults.push(program.emit(srcFile));
@@ -66,8 +133,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions) {
66133
}
67134

68135
var files = JSON.parse(fs.readFileSync("./tsconfig.json")).files;
69-
compile(files,
70-
{
136+
var options: ts.CompilerOptions = {
71137
noEmitOnError: true,
72138
noEmitHelpers: true,
73139
target: ts.ScriptTarget.ES5,
@@ -76,5 +142,9 @@ compile(files,
76142
noImplicitAny: false,
77143
noImplicitUseStrict: true,
78144
experimentalDecorators: true
79-
});
80-
145+
};
146+
if (isTranspile) {
147+
transpile(files, { module: ts.ModuleKind.CommonJS });
148+
} else {
149+
compile(files, options);
150+
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@
2121
"time-grunt": "1.3.0",
2222
"tslint": "3.4.0",
2323
"typescript": "1.8.2"
24+
},
25+
"scripts": {
26+
"tsc-tiw": "node build/tsc-dev.js tiw",
27+
"tsc": "tsc",
28+
"link-tests": "cd tns-core-modules && npm link && cd ../tests && npm link tns-core-modules"
2429
}
2530
}

tests/app/platform-tests.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import TKUnit = require("./TKUnit");
22
import app = require("application");
3+
import { isIOS, isAndroid } from "platform";
34

45
// >> platform-require
56
import platformModule = require("platform");
@@ -29,3 +30,12 @@ export function snippet_print_all() {
2930
console.log("Screen scale: " + platformModule.screen.mainScreen.scale);
3031
// << platform-current
3132
};
33+
34+
export function testIsIOSandIsAndroid() {
35+
if (isIOS) {
36+
TKUnit.assertTrue(!!NSObject, "isIOS is true-ish but common iOS APIs are not available.");
37+
} else if (isAndroid) {
38+
TKUnit.assertTrue(!!android, "isAndroid is true but common 'android' package is not available.");
39+
}
40+
}
41+

tests/app/ui/image/image-tests.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import TKUnit = require("../../TKUnit");
1+
import {Image} from "ui/image";
2+
import {StackLayout} from "ui/layouts/stack-layout";
3+
import {GridLayout} from "ui/layouts/grid-layout";
4+
import {isIOS} from "platform";
5+
// import {target} from "../../TKUnit";
6+
7+
import TKUnit = require("../../TKUnit");
8+
29
// >> img-require
310
import ImageModule = require("ui/image");
411
// << img-require
@@ -244,3 +251,62 @@ export var test_SettingStretch_none = function () {
244251

245252
helper.buildUIAndRunTest(image, testFunc);
246253
}
254+
255+
function ios<T>(func: T): T {
256+
return isIOS ? func : undefined;
257+
}
258+
259+
export var test_SettingImageSourceWhenSizedToParentDoesNotRequestLayout = ios(() => {
260+
let host = new GridLayout();
261+
262+
let image = new Image();
263+
264+
host.width = 300;
265+
host.height = 300;
266+
host.addChild(image);
267+
268+
let mainPage = helper.getCurrentPage();
269+
mainPage.content = host;
270+
TKUnit.waitUntilReady(() => host.isLoaded);
271+
272+
let called = false;
273+
image.requestLayout = () => called = true;
274+
image.src = "~/logo.png";
275+
276+
TKUnit.assertFalse(called, "image.requestLayout should not be called.");
277+
});
278+
279+
export var test_SettingImageSourceWhenFixedWidthAndHeightDoesNotRequestLayout = ios(() => {
280+
let host = new StackLayout();
281+
let image = new Image();
282+
image.width = 100;
283+
image.height = 100;
284+
host.addChild(image);
285+
286+
let mainPage = helper.getCurrentPage();
287+
mainPage.content = host;
288+
TKUnit.waitUntilReady(() => host.isLoaded);
289+
290+
let called = false;
291+
image.requestLayout = () => called = true;
292+
image.src = "~/logo.png";
293+
294+
TKUnit.assertFalse(called, "image.requestLayout should not be called.");
295+
});
296+
297+
export var test_SettingImageSourceWhenSizedToContentShouldInvalidate = ios(() => {
298+
let host = new StackLayout();
299+
let image = new Image();
300+
host.addChild(image);
301+
302+
let mainPage = helper.getCurrentPage();
303+
mainPage.content = host;
304+
TKUnit.waitUntilReady(() => host.isLoaded);
305+
306+
let called = false;
307+
image.requestLayout = () => called = true;
308+
image.src = "~/logo.png";
309+
310+
TKUnit.assertTrue(called, "image.requestLayout should be called.");
311+
});
312+

tns-core-modules/platform/platform.android.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ export var device: definition.Device = new Device();
133133
export module screen {
134134
export var mainScreen = new MainScreen();
135135
}
136+
137+
export var isAndroid = true;

0 commit comments

Comments
 (0)