Skip to content

Commit 1d12186

Browse files
authored
Merge pull request #1839 from NativeScript/clean-command
Clean command
2 parents cc36e47 + 60d1eb0 commit 1d12186

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
platform clean
2+
==========
3+
4+
Usage | Synopsis
5+
------|-------
6+
<% if((isConsole && isMacOS) || isHtml) { %>General | `$ tns platform clean <Platform>`<% } %>
7+
<% if(isConsole && (isLinux || isWindows)) { %>General | `$ tns platform clean android`<% } %>
8+
9+
Removes and adds the selected platform to the project currently targets. <% if(isMacOS) { %>You must specify the target platform that you want to remove.<% } %>
10+
11+
<% if(isMacOS) { %>### Attributes
12+
`<Platform>` is the target mobile platform that you want to clean in your project. You can set the following target platforms.
13+
* `android` - Removes configuration changes for Android development.
14+
* `ios` - Removes configuration changes for iOS development.<% } %>
15+
16+
<% if(isHtml) { %>
17+
### Command Limitations
18+
19+
* You can run `$ tns platform clean ios` only on OS X systems.
20+
* Clean command will not preserve your current installed platform version but will download and install latest platform version. If you are using clean with custom version of the runtime, specify the version in the command tns clean ios@2.1.0 or tns clean ios --frameworkPath <path-to-tgz>
21+
22+
### Related Commands
23+
24+
Command | Description
25+
----------|----------
26+
[install](install.html) | Installs all platforms and dependencies described in the `package.json` file in the current directory.
27+
[platform remove](platform-remove.html) | Removes the selected platform from the platforms that the project currently targets.
28+
[platform add](platform-add.html) | Configures the current project to target the selected platform.
29+
[platform update](platform-update.html) | Updates the NativeScript runtime for the specified platform.
30+
[platform](platform.html) | Lists all platforms that the project currently targets.
31+
[prepare](prepare.html) | Copies common and relevant platform-specific content from the app directory to the subdirectory for the selected target platform in the platforms directory.
32+
<% } %>

lib/bootstrap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ $injector.requireCommand("info", "./commands/info");
9797

9898
$injector.require("androidToolsInfo", "./android-tools-info");
9999

100+
$injector.requireCommand("platform|clean", "./commands/platform-clean");
101+
100102
$injector.requireCommand("livesync", "./commands/livesync");
101103
$injector.require("usbLiveSyncService", "./services/livesync/livesync-service"); // The name is used in https://github.com/NativeScript/nativescript-dev-typescript
102104
$injector.require("iosLiveSyncServiceLocator", "./services/livesync/ios-livesync-service");

lib/commands/platform-clean.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export class CleanCommand implements ICommand {
2+
constructor(private $platformService: IPlatformService,
3+
private $errors: IErrors) { }
4+
5+
public execute(args: string[]): IFuture<void> {
6+
return (() => {
7+
this.$platformService.removePlatforms(args).wait();
8+
this.$platformService.addPlatforms(args).wait();
9+
}).future<void>()();
10+
}
11+
12+
public canExecute(args: string[]): IFuture<boolean> {
13+
return (() => {
14+
if(!args || args.length === 0) {
15+
this.$errors.fail("No platform specified. Please specify a platform to clean");
16+
}
17+
18+
_.each(args, arg => this.$platformService.validatePlatform(arg));
19+
20+
return true;
21+
}).future<boolean>()();
22+
}
23+
24+
allowedParameters: ICommandParameter[] = [];
25+
}
26+
$injector.registerCommand("platform|clean", CleanCommand);

test/platform-commands.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {MobilePlatformsCapabilities} from "../lib/mobile-platforms-capabilities"
1919
import {DevicePlatformsConstants} from "../lib/common/mobile/device-platforms-constants";
2020
import { XmlValidator } from "../lib/xml-validator";
2121
import * as ChildProcessLib from "../lib/common/child-process";
22+
import {CleanCommand} from "../lib/commands/platform-clean";
2223

2324
let isCommandExecuted = true;
2425

@@ -309,6 +310,143 @@ describe('Platform Service Tests', () => {
309310
});
310311
});
311312

313+
describe("#CleanPlatformCommand", () => {
314+
it("is not executed when platform is not passed", () => {
315+
isCommandExecuted = false;
316+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
317+
return (() => {
318+
if (commandName !== "help") {
319+
isCommandExecuted = true;
320+
}
321+
return false;
322+
}).future<boolean>()();
323+
};
324+
325+
commandsService.tryExecuteCommand("platform|clean", []).wait();
326+
assert.isFalse(isCommandExecuted);
327+
});
328+
329+
it("is not executed when platform is not valid", () => {
330+
isCommandExecuted = false;
331+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
332+
return (() => {
333+
if (commandName !== "help") {
334+
isCommandExecuted = true;
335+
}
336+
return false;
337+
}).future<boolean>()();
338+
};
339+
340+
commandsService.tryExecuteCommand("platform|clean", ["invalidPlatform"]).wait();
341+
assert.isFalse(isCommandExecuted);
342+
});
343+
344+
it("is executed when platform is valid", () => {
345+
isCommandExecuted = false;
346+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
347+
return (() => {
348+
if (commandName !== "help") {
349+
isCommandExecuted = true;
350+
}
351+
return false;
352+
}).future<boolean>()();
353+
};
354+
355+
commandsService.tryExecuteCommand("platform|add", ["android"]).wait();
356+
commandsService.tryExecuteCommand("platform|clean", ["android"]).wait();
357+
assert.isTrue(isCommandExecuted);
358+
});
359+
360+
it("is not executed when platform is not added", () => {
361+
isCommandExecuted = false;
362+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
363+
return (() => {
364+
if (commandName !== "help") {
365+
isCommandExecuted = true;
366+
}
367+
return false;
368+
}).future<boolean>()();
369+
};
370+
371+
commandsService.tryExecuteCommand("platform|clean", ["android"]).wait();
372+
assert.isFalse(isCommandExecuted);
373+
});
374+
375+
it("is executed when all platforms are valid", () => {
376+
isCommandExecuted = false;
377+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
378+
return (() => {
379+
if (commandName !== "help") {
380+
isCommandExecuted = true;
381+
}
382+
return false;
383+
}).future<boolean>()();
384+
};
385+
386+
commandsService.tryExecuteCommand("platform|add", ["android"]).wait();
387+
commandsService.tryExecuteCommand("platform|add", ["ios"]).wait();
388+
commandsService.tryExecuteCommand("platform|clean", ["android", "ios"]).wait();
389+
assert.isTrue(isCommandExecuted);
390+
});
391+
392+
it("is not executed when at least on platform is not added", () => {
393+
isCommandExecuted = false;
394+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
395+
return (() => {
396+
if (commandName !== "help") {
397+
isCommandExecuted = true;
398+
}
399+
return false;
400+
}).future<boolean>()();
401+
};
402+
403+
commandsService.tryExecuteCommand("platform|clean", ["android", "ios"]).wait();
404+
assert.isFalse(isCommandExecuted);
405+
});
406+
407+
it("is not executed when at least one platform is not valid", () => {
408+
isCommandExecuted = false;
409+
commandsService.executeCommandUnchecked = (commandName: string): IFuture<boolean> => {
410+
return (() => {
411+
if (commandName !== "help") {
412+
isCommandExecuted = true;
413+
}
414+
return false;
415+
}).future<boolean>()();
416+
};
417+
418+
commandsService.tryExecuteCommand("platform|clean", ["ios", "invalid"]).wait();
419+
assert.isFalse(isCommandExecuted);
420+
});
421+
422+
it("will call removePlatform and addPlatform on the platformService passing the provided platforms", () => {
423+
let platformActions: { action: string, platforms: string[] }[] = [];
424+
testInjector.registerCommand("platform|clean", CleanCommand);
425+
let cleanCommand = testInjector.resolveCommand("platform|clean");
426+
427+
platformService.removePlatforms = (platforms: string[]) => {
428+
return (() => {
429+
platformActions.push({ action: "removePlatforms", platforms });
430+
}).future<void>()();
431+
};
432+
433+
platformService.addPlatforms = (platforms: string[]) => {
434+
return (() => {
435+
platformActions.push({ action: "addPlatforms", platforms });
436+
}).future<void>()();
437+
};
438+
439+
cleanCommand.execute(["ios"]).wait();
440+
441+
let expectedPlatformActions = [
442+
{ action: "removePlatforms", platforms: ["ios"] },
443+
{ action: "addPlatforms", platforms: ["ios"] },
444+
];
445+
446+
assert.deepEqual(platformActions, expectedPlatformActions, "Expected `remove ios`, `add ios` calls to the platformService.");
447+
});
448+
});
449+
312450
describe("#UpdatePlatformCommand", () => {
313451
it("is not executed when platform is not passed", () => {
314452
isCommandExecuted = false;

0 commit comments

Comments
 (0)