Skip to content

Commit b962f30

Browse files
committed
Remove platform-project-service
1 parent c0b11cf commit b962f30

10 files changed

+139
-162
lines changed

lib/bootstrap.ts

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ $injector.require("projectTemplatesService", "./services/project-templates-servi
1111

1212
$injector.require("platformsData", "./services/platform-service");
1313
$injector.require("platformService", "./services/platform-service");
14-
$injector.require("platformProjectService", "./services/platform-project-service");
1514

1615
$injector.requireCommand("create", "./commands/create-project");
1716
$injector.requireCommand("platform|*list", "./commands/list-platforms");

lib/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
export var APP_FOLDER_NAME = "app";
44
export var DEFAULT_PROJECT_ID = "com.telerik.tns.HelloWorld";
55
export var DEFAULT_PROJECT_NAME = "HelloNativescript";
6+
export var APP_RESOURCES_FOLDER_NAME = "App_Resources";
7+
export var PROJECT_FRAMEWORK_DIR = "framework";
8+

lib/definitions/platform.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface IPlatformService {
99

1010
interface IPlatformData {
1111
frameworkPackageName: string;
12-
platformProjectService: IPlatformSpecificProjectService;
12+
platformProjectService: IPlatformProjectService;
1313
projectRoot: string;
1414
normalizedPlatformName: string;
1515
targetedOS?: string[];

lib/definitions/project.d.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ interface IProjectTemplatesService {
1616
}
1717

1818
interface IPlatformProjectService {
19-
createProject(platform: string): IFuture<void>;
20-
buildProject(platform: string): IFuture<void>;
21-
prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void>;
22-
}
23-
24-
interface IPlatformSpecificProjectService {
2519
validate(): IFuture<void>;
2620
createProject(projectRoot: string, frameworkDir: string): IFuture<void>;
2721
interpolateData(projectRoot: string): void;
28-
executePlatformSpecificAction(projectRoot: string): void;
22+
afterCreateProject(projectRoot: string): void;
23+
prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void>;
2924
buildProject(projectRoot: string): IFuture<void>;
3025
}

lib/definitions/shelljs.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module "shelljs" {
2-
function cp(arg: string, sourcePath: string, destinationPath: string): void;
2+
function cp(arg: string, sourcePath: any, destinationPath: string): void;
33
function sed(arg: string, oldValue: any, newValue: string, filePath: string): void;
44
function mv(source: string[], destination: string);
55
function grep(what: any, where: string): any;

lib/node-package-manager.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ export class NodePackageManager implements INodePackageManager {
2727
return future;
2828
}
2929

30+
public install(packageName: string, pathToSave?: string): IFuture<string> {
31+
return (() => {
32+
var action = (packageName: string) => {
33+
this.installCore(pathToSave || npm.cache, packageName).wait();
34+
};
35+
36+
this.tryExecuteAction(action, packageName).wait();
37+
38+
return path.join(pathToSave || npm.cache, "node_modules", packageName);
39+
40+
}).future<string>()();
41+
}
42+
3043
private installCore(where: string, what: string): IFuture<any> {
3144
var future = new Future<any>();
3245
npm.commands["install"](where, what, (err, data) => {
@@ -50,18 +63,5 @@ export class NodePackageManager implements INodePackageManager {
5063
}
5164
}).future<void>()();
5265
}
53-
54-
public install(packageName: string, pathToSave?: string): IFuture<string> {
55-
return (() => {
56-
var action = (packageName: string) => {
57-
this.installCore(pathToSave || npm.cache, packageName).wait();
58-
};
59-
60-
this.tryExecuteAction(action, packageName).wait();
61-
62-
return path.join(pathToSave || npm.cache, "node_modules", packageName);
63-
64-
}).future<string>()();
65-
}
6666
}
6767
$injector.register("npm", NodePackageManager);

lib/services/android-project-service.ts

+59-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
///<reference path="../.d.ts"/>
22
import path = require("path");
33
import shell = require("shelljs");
4+
import util = require("util");
45
import options = require("./../options");
56
import helpers = require("./../common/helpers");
7+
import constants = require("./../constants");
68

7-
class AndroidProjectService implements IPlatformSpecificProjectService {
9+
class AndroidProjectService implements IPlatformProjectService {
810
constructor(private $fs: IFileSystem,
911
private $errors: IErrors,
1012
private $logger: ILogger,
@@ -23,26 +25,17 @@ class AndroidProjectService implements IPlatformSpecificProjectService {
2325

2426
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
2527
return (() => {
26-
var packageName = this.$projectData.projectId;
27-
var packageAsPath = packageName.replace(/\./g, path.sep);
28+
this.validateAndroidTarget(frameworkDir); // We need framework to be installed to validate android target so we can't call this method in validate()
2829

29-
var validTarget = this.getTarget(frameworkDir).wait();
30-
var output = this.$childProcess.exec('android list targets').wait();
31-
if (!output.match(validTarget)) {
32-
this.$errors.fail("Please install Android target %s the Android newest SDK). Make sure you have the latest Android tools installed as well. Run \"android\" from your command-line to install/update any missing SDKs or tools.",
33-
validTarget.split('-')[1]);
34-
}
30+
var paths = "assets gen libs res".split(' ').map(p => path.join(frameworkDir, p));
31+
shell.cp("-r", paths, projectRoot);
3532

36-
shell.cp("-r", path.join(frameworkDir, "assets"), projectRoot);
37-
shell.cp("-r", path.join(frameworkDir, "gen"), projectRoot);
38-
shell.cp("-r", path.join(frameworkDir, "libs"), projectRoot);
39-
shell.cp("-r", path.join(frameworkDir, "res"), projectRoot);
40-
41-
shell.cp("-f", path.join(frameworkDir, ".project"), projectRoot);
42-
shell.cp("-f", path.join(frameworkDir, "AndroidManifest.xml"), projectRoot);
43-
shell.cp("-f", path.join(frameworkDir, "project.properties"), projectRoot);
33+
paths = ".project AndroidManifest.xml project.properties".split(' ').map(p => path.join(frameworkDir, p));
34+
shell.cp("-f", paths, projectRoot);
4435

4536
// Create src folder
37+
var packageName = this.$projectData.projectId;
38+
var packageAsPath = packageName.replace(/\./g, path.sep);
4639
var activityDir = path.join(projectRoot, 'src', packageAsPath);
4740
this.$fs.createDirectory(activityDir).wait();
4841

@@ -58,12 +51,39 @@ class AndroidProjectService implements IPlatformSpecificProjectService {
5851
shell.sed('-i', /__PACKAGE__/, this.$projectData.projectId, path.join(projectRoot, "AndroidManifest.xml"));
5952
}
6053

61-
public executePlatformSpecificAction(projectRoot: string) {
54+
public afterCreateProject(projectRoot: string) {
6255
var targetApi = this.getTarget(projectRoot).wait();
6356
this.$logger.trace("Android target: %s", targetApi);
6457
this.runAndroidUpdate(projectRoot, targetApi).wait();
6558
}
6659

60+
public prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void> {
61+
return (() => {
62+
var platform = normalizedPlatformName.toLowerCase();
63+
var assetsDirectoryPath = path.join(this.$projectData.platformsDir, platform, "assets");
64+
var appResourcesDirectoryPath = path.join(assetsDirectoryPath, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
65+
shell.cp("-r", path.join(this.$projectData.projectDir, constants.APP_FOLDER_NAME), assetsDirectoryPath);
66+
67+
if (this.$fs.exists(appResourcesDirectoryPath).wait()) {
68+
shell.cp("-r", path.join(appResourcesDirectoryPath, normalizedPlatformName, "*"), path.join(this.$projectData.platformsDir, platform, "res"));
69+
this.$fs.deleteDirectory(appResourcesDirectoryPath).wait();
70+
}
71+
72+
var files = helpers.enumerateFilesInDirectorySync(path.join(assetsDirectoryPath, constants.APP_FOLDER_NAME));
73+
var platformsAsString = platforms.join("|");
74+
75+
_.each(files, fileName => {
76+
var platformInfo = AndroidProjectService.parsePlatformSpecificFileName(path.basename(fileName), platformsAsString);
77+
var shouldExcludeFile = platformInfo && platformInfo.platform !== platform;
78+
if (shouldExcludeFile) {
79+
this.$fs.deleteFile(fileName).wait();
80+
} else if (platformInfo && platformInfo.onDeviceName) {
81+
this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait();
82+
}
83+
});
84+
}).future<void>()();
85+
}
86+
6787
public buildProject(projectRoot: string): IFuture<void> {
6888
return (() => {
6989
var buildConfiguration = options.release ? "release" : "debug";
@@ -121,6 +141,15 @@ class AndroidProjectService implements IPlatformSpecificProjectService {
121141
}
122142
}
123143

144+
private validateAndroidTarget(frameworkDir: string) {
145+
var validTarget = this.getTarget(frameworkDir).wait();
146+
var output = this.$childProcess.exec('android list targets').wait();
147+
if (!output.match(validTarget)) {
148+
this.$errors.fail("Please install Android target %s the Android newest SDK). Make sure you have the latest Android tools installed as well. Run \"android\" from your command-line to install/update any missing SDKs or tools.",
149+
validTarget.split('-')[1]);
150+
}
151+
}
152+
124153
private getTarget(projectRoot: string): IFuture<string> {
125154
return (() => {
126155
var projectPropertiesFilePath = path.join(projectRoot, "project.properties");
@@ -167,5 +196,17 @@ class AndroidProjectService implements IPlatformSpecificProjectService {
167196
}
168197
}).future<void>()();
169198
}
199+
200+
private static parsePlatformSpecificFileName(fileName: string, platforms: string): any {
201+
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms);
202+
var parsed = fileName.toLowerCase().match(new RegExp(regex, "i"));
203+
if (parsed) {
204+
return {
205+
platform: parsed[2],
206+
onDeviceName: parsed[1] + parsed[3]
207+
};
208+
}
209+
return undefined;
210+
}
170211
}
171212
$injector.register("androidProjectService", AndroidProjectService);

lib/services/ios-project-service.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///<reference path="../.d.ts"/>
22

3-
class IOSProjectService implements IPlatformSpecificProjectService {
3+
class IOSProjectService implements IPlatformProjectService {
44
public validate(): IFuture<void> {
55
return (() => {
66
}).future<void>()();
@@ -10,14 +10,20 @@ class IOSProjectService implements IPlatformSpecificProjectService {
1010

1111
}
1212

13-
public executePlatformSpecificAction(): void {
13+
public afterCreateProject(): void {
1414

1515
}
1616

1717
public createProject(): IFuture<void> {
1818
return (() => {
1919

20-
}).future<any>()();
20+
}).future<void>()();
21+
}
22+
23+
public prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void> {
24+
return (() => {
25+
26+
}).future<void>()();
2127
}
2228

2329
public buildProject(): IFuture<void> {

lib/services/platform-project-service.ts

-100
This file was deleted.

0 commit comments

Comments
 (0)