@@ -11,19 +11,32 @@ var Generator = module.exports = function Generator() {
1111util . inherits ( Generator , ScriptBase ) ;
1212
1313Generator . prototype . checkInstallation = function checkInstallation ( ) {
14- if ( this . name . toLowerCase ( ) != "heroku" ) return ;
1514 var done = this . async ( ) ;
16-
15+ this . rhcInstalled = false ;
1716 this . herokuInstalled = false ;
18- exec ( 'heroku --version' , function ( err ) {
19- if ( err ) {
20- this . log . error ( 'You don\'t have the Heroku Toolbelt installed. ' +
21- 'Grab it from https://toolbelt.heroku.com/' ) ;
22- } else {
23- this . herokuInstalled = true ;
24- }
17+ if ( this . name . toLowerCase ( ) == "openshift" ) {
18+ exec ( 'rhc --version' , function ( err ) {
19+ if ( err ) {
20+ this . log . error ( 'OpenShift\'s rhc command line interface is not available. ' +
21+ 'You can install it via RubyGems with: gem install rhc' ) ;
22+ } else {
23+ this . rhcInstalled = true ;
24+ }
25+ done ( ) ;
26+ } . bind ( this ) ) ;
27+ } else if ( this . name . toLowerCase ( ) == 'heroku' ) {
28+ exec ( 'heroku --version' , function ( err ) {
29+ if ( err ) {
30+ this . log . error ( 'You don\'t have the Heroku Toolbelt installed. ' +
31+ 'Grab it from https://toolbelt.heroku.com/' ) ;
32+ } else {
33+ this . herokuInstalled = true ;
34+ }
35+ done ( ) ;
36+ } . bind ( this ) ) ;
37+ } else {
2538 done ( ) ;
26- } . bind ( this ) ) ;
39+ }
2740} ;
2841
2942Generator . prototype . copyProcfile = function copyProcfile ( ) {
@@ -32,27 +45,167 @@ Generator.prototype.copyProcfile = function copyProcfile() {
3245} ;
3346
3447Generator . prototype . gruntBuild = function gruntBuild ( ) {
35- if ( this . name . toLowerCase ( ) != "heroku" ) return ;
48+ if ( this . name . toLowerCase ( ) != "heroku" &&
49+ this . name . toLowerCase ( ) != "openshift" ) return ;
3650 var done = this . async ( ) ;
3751
3852 console . log ( chalk . bold ( 'Building dist folder, please wait...' ) ) ;
3953 exec ( 'grunt build' , function ( err , stdout ) {
4054 console . log ( 'stdout: ' + stdout ) ;
41-
4255 if ( err ) {
4356 this . log . error ( err ) ;
4457 }
4558 done ( ) ;
4659 } . bind ( this ) ) ;
4760} ;
4861
62+ Generator . prototype . enableOpenShiftHotDeploy = function enableOpenshiftHotDeploy ( ) {
63+ if ( this . name . toLowerCase ( ) != "openshift" ) return ;
64+ this . log ( "enabling HotDeploy for OpenShift" )
65+ this . template ( '../deploy/openshift/hot_deploy' , 'dist/.openshift/markers/hot_deploy' ) ;
66+ } ;
67+
4968Generator . prototype . gitInit = function gitInit ( ) {
50- if ( this . name . toLowerCase ( ) != "heroku" ) return ;
5169 var done = this . async ( ) ;
70+ if ( this . name . toLowerCase ( ) == "heroku" ) {
71+ exec ( 'git init && git add -A && git commit -m "Initial commit"' , { cwd : 'dist' } , function ( err ) {
72+ if ( err ) {
73+ this . log . error ( err ) ;
74+ }
75+ done ( ) ;
76+ } . bind ( this ) ) ;
77+ } else if ( this . name . toLowerCase ( ) == "openshift" ) {
78+ exec ( 'git init && git add -A && git commit -m "Generating a fresh angular-fullstack application"' , { cwd : 'dist' } , function ( err , stdout , stderr ) {
79+ this . log ( stdout ) ;
80+ if ( stdout . search ( 'nothing to commit' ) >= 0 ) {
81+ this . log ( 'Re-pushing the existing "dist" build...' ) ;
82+ } else if ( err ) {
83+ this . log . error ( err ) ;
84+ }
85+ done ( ) ;
86+ } . bind ( this ) ) ;
87+ } else {
88+ done ( ) ;
89+ }
90+ } ;
5291
53- exec ( 'git init && git add -A && git commit -m "Initial commit"' , { cwd : 'dist' } , function ( err ) {
92+ Generator . prototype . gitRemoteCheck = function gitRemoteCheck ( ) {
93+ this . openshift_remote_exists = false ;
94+ if ( this . name . toLowerCase ( ) != "openshift" || typeof this . dist_repo_url != 'undefined' ) return ;
95+ var done = this . async ( ) ;
96+
97+ this . log ( "Checking for an existing git remote named 'openshift'..." )
98+ exec ( 'git remote -v' , { cwd : 'dist' } , function ( err , stdout , stderr ) {
99+ var lines = stdout . split ( '\n' )
100+ var dist_repo = ''
101+ this . log ( 'stdout: ' + stdout ) ;
54102 if ( err ) {
55103 this . log . error ( err ) ;
104+ } else {
105+ var repo_url_finder = / o p e n s h i f t [ ] * /
106+ lines . forEach ( function ( line ) {
107+ if ( line . search ( repo_url_finder ) == 0 && dist_repo == '' ) {
108+ var dist_repo_detailed = line . slice ( line . match ( repo_url_finder ) [ 0 ] . length )
109+ dist_repo = dist_repo_detailed . slice ( 0 , dist_repo_detailed . length - 7 )
110+ } } )
111+ if ( dist_repo != '' ) {
112+ console . log ( "Found an existing git remote for this app: " + dist_repo )
113+ this . dist_repo_url = dist_repo
114+ this . openshift_remote_exists = true ;
115+ }
116+ }
117+ done ( ) ;
118+ } . bind ( this ) ) ;
119+ } ;
120+
121+ Generator . prototype . rhcAppShow = function rhcAppShow ( ) {
122+ if ( this . name . toLowerCase ( ) != "openshift" || typeof this . dist_repo_url != 'undefined' ) return ;
123+ var done = this . async ( ) ;
124+
125+ this . log ( "Checking for an existing OpenShift hosting evironment..." )
126+ exec ( 'rhc app show ' + this . appname + ' --noprompt' , { cwd : 'dist' } , function ( err , stdout , stderr ) {
127+ var lines = stdout . split ( '\n' )
128+ var dist_repo = ''
129+ if ( stdout . search ( 'Not authenticated' ) >= 0 || stdout . search ( 'Invalid characters found in login' ) >= 0 ) {
130+ this . log . error ( 'Error: Not authenticated. Run "rhc setup" to login to your OpenShift account and try again.' ) ;
131+ } else if ( err ) {
132+ this . log . error ( err ) ;
133+ } else {
134+ this . log ( stdout ) ;
135+ var repo_url_finder = / * G i t U R L : * /
136+ lines . forEach ( function ( line ) {
137+ if ( line . search ( repo_url_finder ) == 0 ) {
138+ dist_repo = line . slice ( line . match ( repo_url_finder ) [ 0 ] . length )
139+ console . log ( "Found an existing git remote for this app: " + dist_repo )
140+ } } )
141+ if ( dist_repo != '' ) this . dist_repo_url = dist_repo
142+ }
143+ done ( ) ;
144+ } . bind ( this ) ) ;
145+ } ;
146+
147+ Generator . prototype . rhcAppCreate = function rhcAppCreate ( ) {
148+ if ( this . name . toLowerCase ( ) != "openshift" || typeof this . dist_repo_url != 'undefined' ) return ;
149+ var done = this . async ( ) ;
150+ this . log ( "Creating your OpenShift hosting evironment..." )
151+ exec ( 'rhc app create ' + this . appname + ' nodejs-0.10 mongodb-2.4 -s --noprompt --no-git NODE_ENV=production' , { cwd : 'dist' } , function ( err , stdout , stderr ) {
152+ var lines = stdout . split ( '\n' )
153+ this . log ( stdout ) ;
154+ if ( stdout . search ( 'Not authenticated' ) >= 0 || stdout . search ( 'Invalid characters found in login' ) >= 0 ) {
155+ this . log . error ( 'Error: Not authenticated. Run "rhc setup" to login to your OpenShift account and try again.' ) ;
156+ } else if ( err ) {
157+ this . log . error ( err ) ;
158+ } else {
159+ var dist_repo = ''
160+ var repo_url_finder = / * G i t r e m o t e : * /
161+ lines . forEach ( function ( line ) {
162+ if ( line . search ( repo_url_finder ) == 0 ) {
163+ dist_repo = line . slice ( line . match ( repo_url_finder ) [ 0 ] . length )
164+ } } )
165+
166+ if ( dist_repo != '' ) this . dist_repo_url = dist_repo
167+ this . log ( "New remote git repo at: " + this . dist_repo_url )
168+ }
169+ done ( ) ;
170+ } . bind ( this ) ) ;
171+ } ;
172+
173+ Generator . prototype . gitRemoteAdd = function gitRemoteAdd ( ) {
174+ if ( this . name . toLowerCase ( ) != "openshift" || typeof this . dist_repo_url == 'undefined' || this . openshift_remote_exists ) return ;
175+ var done = this . async ( ) ;
176+ this . log ( "Adding remote repo url: " + this . dist_repo_url )
177+
178+ exec ( 'git remote add openshift ' + this . dist_repo_url , { cwd : 'dist' } , function ( err , stdout , stderr ) {
179+ if ( err ) {
180+ this . log . error ( err ) ;
181+ } else {
182+ this . log ( 'stdout: ' + stdout ) ;
183+ this . openshift_remote_exists = true ;
184+ }
185+ done ( ) ;
186+ } . bind ( this ) ) ;
187+ } ;
188+
189+ Generator . prototype . gitForcePush = function gitForcePush ( ) {
190+ if ( this . name . toLowerCase ( ) != "openshift" || ! this . openshift_remote_exists ) return ;
191+ var done = this . async ( ) ;
192+ this . log ( chalk . green ( "Uploading your initial application code.\n This may take " + chalk . bold ( 'several minutes' ) + " depending on your connection speed..." ) )
193+
194+ exec ( 'git push -f openshift master' , { cwd : 'dist' } , function ( err , stdout , stderr ) {
195+ if ( err ) {
196+ this . log . error ( err ) ;
197+ } else {
198+ var host_url = '' ;
199+ this . log ( 'stdout: ' + stdout ) ;
200+ var before_hostname = 'ssh://xxxxxxxxxxxxxxxxxxxxxxxx@' . length ;
201+ var after_hostname = this . dist_repo_url . length - ( this . appname . length + 12 )
202+ host_url = 'http://' + this . dist_repo_url . slice ( before_hostname , after_hostname )
203+
204+ this . log ( chalk . green ( 'You\'re all set! Your app should now be live at \n\t' + chalk . bold ( host_url ) ) ) ;
205+ this . log ( chalk . yellow ( 'After app modification run\n\t' + chalk . bold ( 'grunt build' ) +
206+ '\nThen enter the dist folder to commit these updates:\n\t' + chalk . bold ( 'cd dist && git commit -am "describe your changes here"' ) ) ) ;
207+ this . log ( chalk . green ( 'Finally, deploy your updated build to OpenShift with\n\t' + chalk . bold ( 'git push openshift master' ) ) ) ;
208+ this . openshift_host_url = host_url ;
56209 }
57210 done ( ) ;
58211 } . bind ( this ) ) ;
0 commit comments