Skip to content

Commit a473706

Browse files
committed
Merge pull request #103 from NativeScript/fatme/platform-update-command
Update platform command
2 parents 834662d + 6fd5acb commit a473706

19 files changed

+344
-89
lines changed

lib/bootstrap.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ require("./common/bootstrap");
22

33
$injector.require("nativescript-cli", "./nativescript-cli");
44

5-
$injector.require("projectData", "./services/project-service");
5+
$injector.require("projectData", "./project-data");
6+
$injector.require("projectDataService", "./services/project-data-service");
67
$injector.require("projectService", "./services/project-service");
78
$injector.require("androidProjectService", "./services/android-project-service");
89
$injector.require("iOSProjectService", "./services/ios-project-service");
910

1011
$injector.require("projectTemplatesService", "./services/project-templates-service");
1112

12-
$injector.require("platformsData", "./services/platform-service");
13+
$injector.require("platformsData", "./platforms-data");
1314
$injector.require("platformService", "./services/platform-service");
1415

1516
$injector.require("userSettingsService", "./services/user-settings-service");
@@ -21,6 +22,7 @@ $injector.requireCommand("create", "./commands/create-project");
2122
$injector.requireCommand("platform|*list", "./commands/list-platforms");
2223
$injector.requireCommand("platform|add", "./commands/add-platform");
2324
$injector.requireCommand("platform|remove", "./commands/remove-platform");
25+
$injector.requireCommand("platform|update", "./commands/update-platform");
2426
$injector.requireCommand("run", "./commands/run");
2527
$injector.requireCommand("prepare", "./commands/prepare");
2628
$injector.requireCommand("build", "./commands/build");

lib/commands/update-platform.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 UpdatePlatformCommand implements ICommand {
4+
constructor(private $platformService: IPlatformService) { }
5+
6+
execute(args: string[]): IFuture<void> {
7+
return (() => {
8+
this.$platformService.updatePlatforms(args).wait();
9+
}).future<void>()();
10+
}
11+
}
12+
$injector.registerCommand("platform|update", UpdatePlatformCommand);

lib/common

Submodule common updated 1 file

lib/declarations.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
interface INodePackageManager {
2-
cache: string;
2+
getCacheRootPath(): IFuture<string>;
3+
addToCache(packageName: string): IFuture<void>;
34
load(config?: any): IFuture<void>;
45
install(packageName: string, options?: INpmInstallOptions): IFuture<string>;
6+
getLatestVersion(packageName: string): IFuture<string>;
57
}
68

79
interface INpmInstallOptions {

lib/definitions/npm.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module "npm" {
2-
var cache: string;
2+
var cache: any;
33
var commands: { [index: string]: any };
44
function load(config: Object, callback: (err: any, data: any) => void): void;
55
}

lib/definitions/platform.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface IPlatformService {
44
getAvailablePlatforms(): IFuture<string[]>;
55
getPreparedPlatforms(): IFuture<string[]>;
66
removePlatforms(platforms: string[]): IFuture<void>;
7+
updatePlatforms(platforms: string[]): IFuture<void>;
78
runPlatform(platform: string): IFuture<void>;
89
preparePlatform(platform: string): IFuture<void>;
910
buildPlatform(platform: string): IFuture<void>;
@@ -21,6 +22,7 @@ interface IPlatformData {
2122
emulatorBuildOutputPath?: string;
2223
validPackageNamesForDevice: string[];
2324
validPackageNamesForEmulator?: string[];
25+
frameworkFilesExtensions: string[];
2426
targetedOS?: string[];
2527
}
2628

lib/definitions/project.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ interface IProjectData {
1010
projectId?: string;
1111
}
1212

13+
interface IProjectDataService {
14+
initialize(projectDir: string): void;
15+
getValue(propertyName: string): IFuture<any>;
16+
setValue(key: string, value: any): IFuture<void>;
17+
}
18+
1319
interface IProjectTemplatesService {
1420
defaultTemplatePath: IFuture<string>;
1521
}
@@ -23,4 +29,4 @@ interface IPlatformProjectService {
2329
prepareProject(platformData: IPlatformData): IFuture<string>;
2430
buildProject(projectRoot: string): IFuture<void>;
2531
isPlatformPrepared(projectRoot: string): IFuture<boolean>;
26-
}
32+
}

lib/definitions/semver.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare module "semver" {
33
function gt(version1: string, version2: string): boolean;
44
function lt(version1: string, version2: string): boolean;
5+
function eq(version1: string, version2: string): boolean;
56
function valid(version: string): boolean;
67
function inc(version: string, release: string): string;
78
function inc(version: string, release: "major"): string;

lib/node-package-manager.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,29 @@ export class NodePackageManager implements INodePackageManager {
1313
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
1414
private static NPM_REGISTRY_URL = "http://registry.npmjs.org/";
1515

16+
private versionsCache: IDictionary<string[]>;
17+
private isLoaded: boolean;
18+
1619
constructor(private $logger: ILogger,
1720
private $errors: IErrors,
1821
private $httpClient: Server.IHttpClient,
1922
private $staticConfig: IStaticConfig,
20-
private $fs: IFileSystem) { }
23+
private $fs: IFileSystem) {
24+
this.versionsCache = {};
25+
}
2126

22-
public get cache(): string {
23-
return npm.cache;
27+
public getCacheRootPath(): IFuture<string> {
28+
return (() => {
29+
this.load().wait();
30+
return npm.cache;
31+
}).future<string>()();
32+
}
33+
34+
public addToCache(packageName: string): IFuture<void> {
35+
return (() => {
36+
this.load().wait();
37+
this.addToCacheCore(packageName).wait();
38+
}).future<void>()();
2439
}
2540

2641
public load(config?: any): IFuture<void> {
@@ -39,6 +54,7 @@ export class NodePackageManager implements INodePackageManager {
3954
return (() => {
4055
try {
4156
this.load().wait(); // It's obligatory to execute load before whatever npm function
57+
4258
var packageToInstall = packageName;
4359
var pathToSave = (opts && opts.pathToSave) || npm.cache;
4460
var version = (opts && opts.version) || null;
@@ -62,6 +78,14 @@ export class NodePackageManager implements INodePackageManager {
6278
}).future<string>()();
6379
}
6480

81+
public getLatestVersion(packageName: string): IFuture<string> {
82+
return (() => {
83+
var versions = this.getAvailableVersions(packageName).wait();
84+
versions = _.sortBy(versions, (ver: string) => { return ver; });
85+
return versions.reverse()[0];
86+
}).future<string>()();
87+
}
88+
6589
private installCore(packageName: string, pathToSave: string, isSemanticVersioningDisabled: boolean): IFuture<void> {
6690
var currentVersion = this.$staticConfig.version;
6791
if(!semver.valid(currentVersion)) {
@@ -89,12 +113,28 @@ export class NodePackageManager implements INodePackageManager {
89113
return future;
90114
}
91115

116+
private addToCacheCore(packageName: string): IFuture<void> {
117+
var future = new Future<void>();
118+
npm.commands["cache"].add(packageName, (err: Error, data: any) => {
119+
if(err) {
120+
future.throw(err);
121+
} else {
122+
future.return();
123+
}
124+
});
125+
return future;
126+
}
127+
92128
private getAvailableVersions(packageName: string): IFuture<string[]> {
93129
return (() => {
94-
var url = NodePackageManager.NPM_REGISTRY_URL + packageName;
95-
var response = this.$httpClient.httpRequest(url).wait().body;
96-
var json = JSON.parse(response);
97-
return _.keys(json.versions);
130+
if(!this.versionsCache[packageName]) {
131+
var url = NodePackageManager.NPM_REGISTRY_URL + packageName;
132+
var response = this.$httpClient.httpRequest(url).wait().body;
133+
var json = JSON.parse(response);
134+
this.versionsCache[packageName] = _.keys(json.versions);
135+
}
136+
137+
return this.versionsCache[packageName];
98138
}).future<string[]>()();
99139
}
100140

lib/platforms-data.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
///<reference path=".d.ts"/>
2+
"use strict";
3+
4+
export class PlatformsData implements IPlatformsData {
5+
private platformsData : { [index: string]: any } = {};
6+
7+
constructor($androidProjectService: IPlatformProjectService,
8+
$iOSProjectService: IPlatformProjectService) {
9+
10+
this.platformsData = {
11+
ios: $iOSProjectService.platformData,
12+
android: $androidProjectService.platformData
13+
}
14+
}
15+
16+
public get platformsNames() {
17+
return Object.keys(this.platformsData);
18+
}
19+
20+
public getPlatformData(platform: string): IPlatformData {
21+
return this.platformsData[platform];
22+
}
23+
}
24+
$injector.register("platformsData", PlatformsData);

0 commit comments

Comments
 (0)