Skip to content

Commit d17afa9

Browse files
committed
Code review changes
1 parent 6cf93fd commit d17afa9

16 files changed

+109
-69
lines changed

lib/bootstrap.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ $injector.require("projectTemplatesService", "./services/project-templates-servi
99
$injector.require("platformService", "./services/platform-service");
1010

1111
$injector.requireCommand("create", "./commands/create-project-command");
12-
$injector.requireCommand("platform|*list", "./commands/platform-command");
13-
$injector.requireCommand("platform|add", "./commands/platform-command");
12+
$injector.requireCommand("platform|*list", "./commands/list-platforms-command");
13+
$injector.requireCommand("platform|add", "./commands/add-platform-command");
1414
$injector.requireCommand("run", "./commands/run-command");
15-
$injector.requireCommand("prepare", "./commands/run-command");
16-
$injector.requireCommand("build", "./commands/run-command");
15+
$injector.requireCommand("prepare", "./commands/prepare-command");
16+
$injector.requireCommand("build", "./commands/build-command");
1717

1818
$injector.require("npm", "./node-package-manager");
19+
$injector.require("propertiesParser", "./properties-parser");

lib/commands/add-platform-command.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference path="../.d.ts"/>
2+
3+
export class AddPlatformCommand implements ICommand {
4+
constructor(private $platformService: IPlatformService) { }
5+
6+
execute(args: string[]): IFuture<void> {
7+
return (() => {
8+
this.$platformService.addPlatforms(args).wait();
9+
}).future<void>()();
10+
}
11+
}
12+
$injector.registerCommand("platform|add", AddPlatformCommand);

lib/commands/build-command.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference path="../.d.ts"/>
2+
3+
export class BuildCommand implements ICommand {
4+
constructor(private $platformService: IPlatformService) { }
5+
6+
execute(args: string[]): IFuture<void> {
7+
return (() => {
8+
this.$platformService.buildPlatform(args[0]).wait();
9+
}).future<void>()();
10+
}
11+
}
12+
$injector.registerCommand("build", BuildCommand);

lib/commands/platform-command.ts renamed to lib/commands/list-platforms-command.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,4 @@ export class ListPlatformsCommand implements ICommand {
1717
}
1818
$injector.registerCommand("platform|*list", ListPlatformsCommand);
1919

20-
export class AddPlatformCommand implements ICommand {
21-
constructor(private $platformService: IPlatformService) { }
2220

23-
execute(args: string[]): IFuture<void> {
24-
return (() => {
25-
this.$platformService.addPlatforms(args).wait();
26-
}).future<void>()();
27-
}
28-
}
29-
$injector.registerCommand("platform|add", AddPlatformCommand);

lib/commands/prepare-command.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference path="../.d.ts"/>
2+
3+
export class PrepareCommand implements ICommand {
4+
constructor(private $platformService: IPlatformService) { }
5+
6+
execute(args: string[]): IFuture<void> {
7+
return (() => {
8+
this.$platformService.preparePlatform(args[0]).wait();
9+
}).future<void>()();
10+
}
11+
}
12+
$injector.registerCommand("prepare", PrepareCommand);

lib/commands/run-command.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,3 @@ export class RunCommand implements ICommand {
1010
}
1111
}
1212
$injector.registerCommand("run", RunCommand);
13-
14-
export class PrepareCommand implements ICommand {
15-
constructor(private $platformService: IPlatformService) { }
16-
17-
execute(args: string[]): IFuture<void> {
18-
return (() => {
19-
this.$platformService.preparePlatform(args[0]).wait();
20-
}).future<void>()();
21-
}
22-
}
23-
$injector.registerCommand("prepare", PrepareCommand);
24-
25-
export class BuildCommand implements ICommand {
26-
constructor(private $platformService: IPlatformService) { }
27-
28-
execute(args: string[]): IFuture<void> {
29-
return (() => {
30-
this.$platformService.buildPlatform(args[0]).wait();
31-
}).future<void>()();
32-
}
33-
}
34-
$injector.registerCommand("build", BuildCommand);

lib/common

lib/declarations.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
interface INodePackageManager {
2-
cache: string;
3-
load(config?: any): IFuture<void>;
4-
install(where: string, what: string): IFuture<any>;
2+
cache: string;
3+
load(config?: any): IFuture<void>;
4+
install(where: string, what: string): IFuture<any>;
5+
}
6+
7+
interface IPropertiesParser {
8+
createEditor(filePath: string): IFuture<any>;
59
}

lib/definitions/project.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ interface IProjectService {
77
projectData: IProjectData;
88
}
99

10-
interface IAndroidProjectService {
10+
interface IPlatformProjectService {
1111
createProject(projectData: IProjectData): IFuture<void>;
1212
buildProject(projectData: IProjectData): IFuture<void>;
1313
}
1414

15-
interface IiOSProjectService {
16-
createProject(projectData: IProjectData): IFuture<void>;
17-
}
18-
1915
interface IProjectData {
2016
projectDir: string;
2117
platformsDir: string;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "properties-parser" {
2+
function createEditor(path: string, callback: (err: IErrors, data: any) => void);
3+
}

lib/nativescript-cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require("./options");
1010
import errors = require("./common/errors");
1111
errors.installUncaughtExceptionListener();
1212

13-
$injector.register("config", {"CI_LOGGER": false, PROJECT_FILE_NAME: ".tnsproject", "DEBUG": true});
13+
$injector.register("config", {"CI_LOGGER": false, PROJECT_FILE_NAME: ".tnsproject", "DEBUG": process.env.NATIVESCRIPT_DEBUG});
1414

1515
var dispatcher = $injector.resolve("dispatcher");
1616
dispatcher.runMainFiber();

lib/properties-parser.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
///<reference path=".d.ts"/>
2+
3+
import propertiesParser = require("properties-parser");
4+
import Future = require("fibers/future");
5+
6+
export class PropertiesParser implements IPropertiesParser {
7+
public createEditor(filePath: string) {
8+
var future = new Future<any>();
9+
propertiesParser.createEditor(filePath, (err, data) => {
10+
if(err) {
11+
future.throw(err);
12+
} else {
13+
future.return(data);
14+
}
15+
});
16+
17+
return future;
18+
}
19+
}
20+
$injector.register("propertiesParser", PropertiesParser);

lib/services/platform-service.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ export class PlatformService implements IPlatformService {
3434
}
3535

3636
var platformsDir = this.$projectService.projectData.platformsDir;
37-
if(!this.$fs.exists(platformsDir).wait()) {
38-
this.$fs.createDirectory(platformsDir).wait();
39-
}
37+
this.$fs.ensureDirectoryExists(platformsDir).wait();
4038

4139
_.each(platforms, platform => {
4240
this.addPlatform(platform.toLowerCase()).wait();
@@ -104,7 +102,7 @@ export class PlatformService implements IPlatformService {
104102
}).future<void>()();
105103
}
106104

107-
private validatePlatform(platform): void {
105+
private validatePlatform(platform: string): void {
108106
if (!this.isValidPlatform(platform)) {
109107
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(this.platformNames));
110108
}

lib/services/project-service.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ export class ProjectService implements IProjectService {
1313
public static APP_FOLDER_NAME = "app";
1414
public static PROJECT_FRAMEWORK_DIR = "framework";
1515

16-
private cachedProjectDir: string = "";
16+
private cachedProjectDir: string = null;
1717
public projectData: IProjectData = null;
1818

1919
constructor(private $logger: ILogger,
2020
private $errors: IErrors,
2121
private $fs: IFileSystem,
2222
private $projectTemplatesService: IProjectTemplatesService,
23-
private $androidProjectService: IAndroidProjectService,
24-
private $iOSProjectService: IiOSProjectService,
23+
private $androidProjectService: IPlatformProjectService,
24+
private $iOSProjectService: IPlatformProjectService,
2525
private $config) {
2626
this.projectData = this.getProjectData().wait();
2727
}
2828

2929
private get projectDir(): string {
30-
if(this.cachedProjectDir !== "") {
30+
if(this.cachedProjectDir) {
3131
return this.cachedProjectDir;
3232
}
3333

@@ -190,14 +190,17 @@ export class ProjectService implements IProjectService {
190190

191191
private executePlatformSpecificAction(platform, functionName: string): IFuture<void> {
192192
return (() => {
193+
var projectService = null;
193194
switch (platform) {
194195
case "android":
195-
this.executeFunctionByName(functionName, this.$androidProjectService, [this.projectData]).wait();
196+
projectService = this.$androidProjectService;
196197
break;
197198
case "ios":
198-
this.executeFunctionByName(functionName, this.$iOSProjectService, [this.projectData]).wait();
199+
projectService = this.$iOSProjectService;
199200
break;
200201
}
202+
203+
this.executeFunctionByName(functionName, projectService, [this.projectData]).wait();
201204
}).future<void>()();
202205
}
203206

@@ -235,25 +238,26 @@ export class ProjectService implements IProjectService {
235238
}
236239
$injector.register("projectService", ProjectService);
237240

238-
class AndroidProjectService implements IAndroidProjectService {
239-
private cachedFrameworkDir: string = "";
241+
class AndroidProjectService implements IPlatformProjectService {
242+
private cachedFrameworkDir: string = null;
240243

241244
constructor(private $fs: IFileSystem,
242245
private $errors: IErrors,
243246
private $logger: ILogger,
244247
private $childProcess: IChildProcess,
245-
private $projectTemplatesService: IProjectTemplatesService) { }
248+
private $projectTemplatesService: IProjectTemplatesService,
249+
private $propertiesParser: IPropertiesParser) { }
246250

247251
public createProject(projectData: IProjectData): IFuture<void> {
248252
return (() => {
249253
var packageName = projectData.projectId;
250254
var projectDir = path.join(projectData.projectDir, "platforms", "android");
251255

252-
var targetApi = this.getTarget().wait();
253-
254256
this.validatePackageName(packageName);
255257
this.validateProjectName(projectData.projectName);
256258

259+
var targetApi = this.getTarget().wait();
260+
257261
this.checkRequirements().wait();
258262

259263
// Log the values for project
@@ -274,8 +278,9 @@ class AndroidProjectService implements IAndroidProjectService {
274278
shell.cp("-f", path.join(this.frameworkDir.wait(), "AndroidManifest.xml"), projectDir);
275279

276280
// Interpolate the activity name and package
277-
shell.sed('-i', /__NAME__/, projectData.projectName, path.join(projectDir, 'res', 'values', 'strings.xml'));
278-
shell.sed('-i', /__TITLE_ACTIVITY__/, projectData.projectName, path.join(projectDir, 'res', 'values', 'strings.xml'));
281+
var stringsFilePath = path.join(projectDir, 'res', 'values', 'strings.xml');
282+
shell.sed('-i', /__NAME__/, projectData.projectName, stringsFilePath);
283+
shell.sed('-i', /__TITLE_ACTIVITY__/, projectData.projectName, stringsFilePath);
279284
shell.sed('-i', /__NAME__/, projectData.projectName, path.join(projectDir, '.project'));
280285
shell.sed('-i', /__PACKAGE__/, packageName, path.join(projectDir, "AndroidManifest.xml"));
281286

@@ -292,7 +297,7 @@ class AndroidProjectService implements IAndroidProjectService {
292297
}).future<void>()();
293298
}
294299

295-
private runAndroidUpdate(projectPath: string, targetApi): IFuture<void> {
300+
private runAndroidUpdate(projectPath: string, targetApi: string): IFuture<void> {
296301
return (() => {
297302
this.$childProcess.exec("android update project --subprojects --path " + projectPath + " --target " + targetApi).wait();
298303
}).future<void>()();
@@ -324,7 +329,7 @@ class AndroidProjectService implements IAndroidProjectService {
324329

325330
private get frameworkDir(): IFuture<string> {
326331
return(() => {
327-
if(this.cachedFrameworkDir === "") {
332+
if(!this.cachedFrameworkDir) {
328333
var androidFrameworkPath = this.$projectTemplatesService.androidFrameworkPath.wait();
329334
this.cachedFrameworkDir = path.join(androidFrameworkPath, ProjectService.PROJECT_FRAMEWORK_DIR);
330335
}
@@ -337,9 +342,10 @@ class AndroidProjectService implements IAndroidProjectService {
337342
private getTarget(): IFuture<string> {
338343
return (() => {
339344
var projectPropertiesFilePath = path.join(this.frameworkDir.wait(), "project.properties");
345+
340346
if (this.$fs.exists(projectPropertiesFilePath).wait()) {
341-
var target = shell.grep(/target=android-[\d+]/, projectPropertiesFilePath);
342-
return target.split('=')[1].replace('\n', '').replace('\r', '').replace(' ', ''); // Target should be in following format: target=android-XX
347+
var properties = this.$propertiesParser.createEditor(projectPropertiesFilePath).wait();
348+
return properties.get("target");
343349
}
344350

345351
return "";
@@ -398,11 +404,17 @@ class AndroidProjectService implements IAndroidProjectService {
398404
}
399405
$injector.register("androidProjectService", AndroidProjectService);
400406

401-
class iOSProjectService implements IiOSProjectService {
407+
class iOSProjectService implements IPlatformProjectService {
402408
public createProject(projectData: IProjectData): IFuture<void> {
403409
return (() => {
404410

405411
}).future<any>()();
406412
}
413+
414+
public buildProject(projectData: IProjectData): IFuture<void> {
415+
return (() => {
416+
417+
}).future<void>()();
418+
}
407419
}
408420
$injector.register("iOSProjectService", iOSProjectService);

lib/services/project-templates-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
2626
return this.downloadNpmPackage(ProjectTemplatesService.NPM_ANDROID_BRIDGE_NAME);
2727
}
2828

29-
private downloadNpmPackage(packageName): IFuture<string> {
29+
private downloadNpmPackage(packageName: string): IFuture<string> {
3030
return (() => {
3131
try {
3232
this.$npm.load().wait();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"underscore": "1.5.2",
3232
"unzip": "0.1.9",
3333
"yargs": "1.2.2",
34-
"npm": "1.4.10"
34+
"npm": "1.4.10",
35+
"properties-parser": "0.2.3"
3536
},
3637
"analyze": true,
3738
"devDependencies": {

0 commit comments

Comments
 (0)