1+ ///<reference path="../.d.ts"/>
2+ "use strict" ;
3+ import osenv = require( "osenv" ) ;
4+ import path = require( "path" ) ;
5+
6+ export class PostInstallCommand implements ICommand {
7+ private static CALL_PROFILE_SCRIPT =
8+ "if [ -f ~/.profile ]; then\n" +
9+ " . ~/.profile\n" +
10+ "fi\n" ;
11+
12+ constructor ( private $fs : IFileSystem ,
13+ private $childProcess : IChildProcess ,
14+ private $logger : ILogger ) { }
15+
16+ public disableAnalytics = true ;
17+
18+ public execute ( args : string [ ] ) : IFuture < void > {
19+ return ( ( ) => {
20+ var scriptsOk = true ;
21+
22+ try {
23+ this . updateShellScript ( ".profile" ) . wait ( ) ;
24+ this . updateShellScript ( ".bashrc" ) . wait ( ) ;
25+
26+ this . updateBashProfile ( ) . wait ( ) ;
27+
28+ // zsh - http://www.acm.uiuc.edu/workshops/zsh/startup_files.html
29+ this . updateShellScript ( ".zshrc" ) . wait ( ) ;
30+ } catch ( err ) {
31+ this . $logger . out ( "Failed to update all shell start-up scripts. Auto-completion may not work. " + err ) ;
32+ scriptsOk = false ;
33+ }
34+
35+ if ( scriptsOk ) {
36+ this . $logger . out ( "Restart your shell to enable command auto-completion." ) ;
37+ }
38+ } ) . future < void > ( ) ( ) ;
39+ }
40+
41+ private updateShellScript ( fileName : string ) : IFuture < void > {
42+ return ( ( ) => {
43+ var filePath = this . getHomePath ( fileName ) ;
44+
45+ var doUpdate = true ;
46+ if ( this . $fs . exists ( filePath ) . wait ( ) ) {
47+ var contents = this . $fs . readText ( filePath ) . wait ( ) ;
48+ if ( contents . match ( / n a t i v e s c r i p t \s + c o m p l e t i o n \s + - - \s + / ) || contents . match ( / t n s \s + c o m p l e t i o n \s + - - \s + / ) ) {
49+ doUpdate = false ;
50+ }
51+ }
52+
53+ if ( doUpdate ) {
54+ this . updateShellScriptCore ( filePath ) . wait ( ) ;
55+ }
56+
57+ } ) . future < void > ( ) ( ) ;
58+ }
59+
60+ private updateShellScriptCore ( filePath : string ) : IFuture < void > {
61+ return ( ( ) => {
62+ this . $childProcess . exec ( "nativescript completion >> " + filePath ) . wait ( ) ;
63+ this . $childProcess . exec ( "tns completion >> " + filePath ) . wait ( ) ;
64+ } ) . future < void > ( ) ( ) ;
65+ }
66+
67+ private getHomePath ( fileName : string ) : string {
68+ return path . join ( osenv . home ( ) , fileName ) ;
69+ }
70+
71+ private updateBashProfile ( ) : IFuture < void > {
72+ return ( ( ) => {
73+ var bashProfileFileName = this . getHomePath ( ".bash_profile" ) ;
74+ if ( this . $fs . exists ( bashProfileFileName ) . wait ( ) ) {
75+ var contens = this . $fs . readText ( bashProfileFileName ) . wait ( ) ;
76+ if ( contens . indexOf ( PostInstallCommand . CALL_PROFILE_SCRIPT ) < 0 ) {
77+ this . updateShellScript ( ".bash_profile" ) ;
78+ }
79+ } else {
80+ this . $fs . writeFile ( bashProfileFileName , PostInstallCommand . CALL_PROFILE_SCRIPT ) . wait ( ) ;
81+ this . updateShellScriptCore ( bashProfileFileName ) . wait ( ) ;
82+ }
83+ } ) . future < void > ( ) ( ) ;
84+ }
85+ }
86+ $injector . registerCommand ( "dev-post-install" , PostInstallCommand ) ;
0 commit comments