Skip to content

Create, prepare and build commands for iOS #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/nativescript-cli.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ export var DEFAULT_PROJECT_NAME = "HelloNativescript";
export var APP_RESOURCES_FOLDER_NAME = "App_Resources";
export var PROJECT_FRAMEWORK_FOLDER_NAME = "framework";

export class ReleaseType {
static MAJOR = "major";
static PREMAJOR = "premajor";
static MINOR = "minor";
static PREMINOR = "preminor";
static PATCH = "patch";
static PREPATCH = "prepatch";
static PRERELEASE = "prerelease";
}


5 changes: 3 additions & 2 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
interface INodePackageManager {
cache: string;
load(config?: any): IFuture<void>;
install(packageName: string, pathToSave?: string): IFuture<string>;
install(packageName: string, pathToSave?: string, version?: string): IFuture<string>;
}

interface IStaticConfig extends Config.IStaticConfig { }
interface IStaticConfig extends Config.IStaticConfig { }

7 changes: 4 additions & 3 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ interface IProjectTemplatesService {
}

interface IPlatformProjectService {
platformData: IPlatformData;
validate(): IFuture<void>;
createProject(projectRoot: string, frameworkDir: string): IFuture<void>;
interpolateData(projectRoot: string): void;
afterCreateProject(projectRoot: string): void;
prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void>;
interpolateData(projectRoot: string): IFuture<void>;
afterCreateProject(projectRoot: string): IFuture<void>;
prepareProject(platformData: IPlatformData): IFuture<string>;
buildProject(projectRoot: string): IFuture<void>;
}
14 changes: 14 additions & 0 deletions lib/definitions/semver.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

declare module "semver" {
function gt(version1: string, version2: string): boolean;
function lt(version1: string, version2: string): boolean;
function valid(version: string): boolean;
function inc(version: string, release: string): string;
function inc(version: string, release: "major"): string;
function inc(version: string, release: 'premajor'): string;
function inc(version: string, release: 'minor'): string;
function inc(version: string, release: 'preminor'): string;
function inc(version: string, release: 'patch'): string;
function inc(version: string, release: 'prepatch'): string;
function inc(version: string, release: 'prerelease'): string;
}
6 changes: 5 additions & 1 deletion lib/nativescript-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ errors.installUncaughtExceptionListener();

$injector.register("config", {
CI_LOGGER: false,
DEBUG: process.env.NATIVESCRIPT_DEBUG
PROJECT_FILE_NAME: ".tnsproject",
DEBUG: process.env.NATIVESCRIPT_DEBUG,
version: require("../package.json").version,
helpTextPath: path.join(__dirname, "../resources/help.txt"),
client: "tns"
});

var dispatcher = $injector.resolve("dispatcher");
Expand Down
66 changes: 48 additions & 18 deletions lib/node-package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
///<reference path=".d.ts"/>

import npm = require("npm");
import Future = require("fibers/future");
import shell = require("shelljs");
import npm = require("npm");
import path = require("path");
import semver = require("semver");
import shell = require("shelljs");
import helpers = require("./common/helpers");
import constants = require("./constants");

export class NodePackageManager implements INodePackageManager {
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
private static NPM_REGISTRY_URL = "http://registry.npmjs.org/";

constructor(private $logger: ILogger,
private $errors: IErrors) { }
private $errors: IErrors,
private $httpClient: Server.IHttpClient,
private $staticConfig: IStaticConfig) { }

public get cache(): string {
return npm.cache;
Expand All @@ -27,23 +33,41 @@ export class NodePackageManager implements INodePackageManager {
return future;
}

public install(packageName: string, pathToSave?: string): IFuture<string> {
public install(packageName: string, pathToSave?: string, version?: string): IFuture<string> {
return (() => {
var action = (packageName: string) => {
try {
this.load().wait(); // It's obligatory to execute load before whatever npm function
pathToSave = pathToSave || npm.cache;
this.installCore(pathToSave, packageName).wait();
};
var packageToInstall = packageName;

this.tryExecuteAction(action, packageName).wait();
if(version) {
this.validateVersion(packageName, version).wait();
packageToInstall = packageName + "@" + version;
}

this.installCore(pathToSave, packageToInstall).wait();
} catch(error) {
this.$logger.debug(error);
this.$errors.fail(NodePackageManager.NPM_LOAD_FAILED);
}

return path.join(pathToSave, "node_modules", packageName);

}).future<string>()();
}

private installCore(where: string, what: string): IFuture<any> {
var future = new Future<any>();
npm.commands["install"](where, what, (err, data) => {
private installCore(packageName: string, pathToSave: string): IFuture<void> {
var currentVersion = this.$staticConfig.version;
if(!semver.valid(currentVersion)) {
this.$errors.fail("Invalid version.");
}

var incrementedVersion = semver.inc(currentVersion, constants.ReleaseType.MINOR);
packageName = packageName + "@" + "<" + incrementedVersion;
this.$logger.trace("Installing", packageName);

var future = new Future<void>();
npm.commands["install"](pathToSave, packageName, (err, data) => {
if(err) {
future.throw(err);
} else {
Expand All @@ -53,14 +77,20 @@ export class NodePackageManager implements INodePackageManager {
return future;
}

private tryExecuteAction(action: (...args: any[]) => void, ...args: any[]): IFuture<void> {
private getAvailableVersions(packageName: string): IFuture<string[]> {
return (() => {
try {
this.load().wait(); // It's obligatory to execute load before whatever npm function
action.apply(null, args);
} catch(error) {
this.$logger.debug(error);
this.$errors.fail(NodePackageManager.NPM_LOAD_FAILED);
var url = NodePackageManager.NPM_REGISTRY_URL + packageName;
var response = this.$httpClient.httpRequest(url).wait().body;
var json = JSON.parse(response);
return _.keys(json.versions);
}).future<string[]>()();
}

private validateVersion(packageName: string, version: string): IFuture<void> {
return (() => {
var versions = this.getAvailableVersions(packageName).wait();
if(!_.contains(versions, version)) {
this.$errors.fail("Invalid version. Valid versions are: %s", helpers.formatListOfNames(versions, "and"));
}
}).future<void>()();
}
Expand Down
1 change: 1 addition & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var knownOpts:any = {
"copy-from": String,
"link-to": String,
"release": String,
"device": Boolean,
"version": Boolean,
"help": Boolean
},
Expand Down
70 changes: 31 additions & 39 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class AndroidProjectService implements IPlatformProjectService {
private $projectData: IProjectData,
private $propertiesParser: IPropertiesParser) { }

public get platformData(): IPlatformData {
return {
frameworkPackageName: "tns-android",
normalizedPlatformName: "Android",
platformProjectService: this,
projectRoot: path.join(this.$projectData.platformsDir, "android")
};
}

public validate(): IFuture<void> {
return (() => {
this.validatePackageName(this.$projectData.projectId);
Expand Down Expand Up @@ -61,54 +70,49 @@ class AndroidProjectService implements IPlatformProjectService {
}).future<void>()();
}

public afterCreateProject(projectRoot: string) {
var targetApi = this.getTarget(projectRoot).wait();
this.$logger.trace("Android target: %s", targetApi);
this.runAndroidUpdate(projectRoot, targetApi).wait();
public afterCreateProject(projectRoot: string): IFuture<void> {
return (() => {
var targetApi = this.getTarget(projectRoot).wait();
this.$logger.trace("Android target: %s", targetApi);
this.runAndroidUpdate(projectRoot, targetApi).wait();
}).future<void>()();
}

public prepareProject(normalizedPlatformName: string, platforms: string[]): IFuture<void> {
public prepareProject(platformData: IPlatformData): IFuture<string> {
return (() => {
var platform = normalizedPlatformName.toLowerCase();
var assetsDirectoryPath = path.join(this.$projectData.platformsDir, platform, "assets");
var appResourcesDirectoryPath = path.join(assetsDirectoryPath, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
shell.cp("-r", path.join(this.$projectData.projectDir, constants.APP_FOLDER_NAME), assetsDirectoryPath);
var appSourceDirectory = path.join(this.$projectData.projectDir, constants.APP_FOLDER_NAME);
var assetsDirectory = path.join(platformData.projectRoot, "assets");
var resDirectory = path.join(platformData.projectRoot, "res");

shell.cp("-r", appSourceDirectory, assetsDirectory);

var appResourcesDirectoryPath = path.join(assetsDirectory, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
if (this.$fs.exists(appResourcesDirectoryPath).wait()) {
shell.cp("-r", path.join(appResourcesDirectoryPath, normalizedPlatformName, "*"), path.join(this.$projectData.platformsDir, platform, "res"));
shell.cp("-r", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "*"), resDirectory);
this.$fs.deleteDirectory(appResourcesDirectoryPath).wait();
}

var files = helpers.enumerateFilesInDirectorySync(path.join(assetsDirectoryPath, constants.APP_FOLDER_NAME));
var platformsAsString = platforms.join("|");
return path.join(assetsDirectory, constants.APP_FOLDER_NAME);

_.each(files, fileName => {
var platformInfo = AndroidProjectService.parsePlatformSpecificFileName(path.basename(fileName), platformsAsString);
var shouldExcludeFile = platformInfo && platformInfo.platform !== platform;
if (shouldExcludeFile) {
this.$fs.deleteFile(fileName).wait();
} else if (platformInfo && platformInfo.onDeviceName) {
this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait();
}
});
}).future<void>()();
}).future<string>()();
}

public buildProject(projectRoot: string): IFuture<void> {
return (() => {
var buildConfiguration = options.release ? "release" : "debug";
var args = this.getAntArgs(buildConfiguration, projectRoot);
this.spawn('ant', args);
this.spawn('ant', args).wait();
}).future<void>()();
}

private spawn(command: string, args: string[], options?: any): void {
if(helpers.isWindows()) {
private spawn(command: string, args: string[]): IFuture<void> {
if (helpers.isWindows()) {
args.unshift('/s', '/c', command);
command = 'cmd';
}

this.$childProcess.spawn(command, args, {cwd: options, stdio: 'inherit'});
var child = this.$childProcess.spawn(command, args, {stdio: "inherit"});
return this.$fs.futureFromEvent(child, "close");
}

private getAntArgs(configuration: string, projectRoot: string): string[] {
Expand All @@ -123,7 +127,7 @@ class AndroidProjectService implements IPlatformProjectService {
"--target", targetApi
];

this.spawn("android update project", args);
this.spawn("android", ['update', 'project'].concat(args)).wait();
}).future<void>()();
}

Expand Down Expand Up @@ -208,17 +212,5 @@ class AndroidProjectService implements IPlatformProjectService {
}
}).future<void>()();
}

private static parsePlatformSpecificFileName(fileName: string, platforms: string): any {
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms);
var parsed = fileName.toLowerCase().match(new RegExp(regex, "i"));
if (parsed) {
return {
platform: parsed[2],
onDeviceName: parsed[1] + parsed[3]
};
}
return undefined;
}
}
$injector.register("androidProjectService", AndroidProjectService);
Loading