1616
1717package  org .moe .gradle .remote ;
1818
19- import  com .jcraft .jsch .ChannelExec ;
2019import  com .jcraft .jsch .JSch ;
2120import  com .jcraft .jsch .JSchException ;
2221import  com .jcraft .jsch .Session ;
3534import  org .moe .gradle .remote .file .FileList ;
3635import  org .moe .gradle .utils .Require ;
3736
38- import  java .io .ByteArrayOutputStream ;
3937import  java .io .File ;
4038import  java .io .FileInputStream ;
4139import  java .io .IOException ;
@@ -225,45 +223,14 @@ private void fetchArchitecture() {
225223
226224    private  void  prepareServerMOE () {
227225        final  MoeSDK  sdk  = plugin .getSDK ();
228- 
229-         final  File  gradlewZip  = sdk .getGradlewZip ();
230-         final  FileList  list  = new  FileList (gradlewZip .getParentFile (), getBuildDir ());
231-         final  String  remoteGradlewZip  = list .add (gradlewZip );
232-         upload ("prepare - gradlew" , list );
233- 
234-         final  String  output  = exec ("install MOE SDK" , ""  +
235-                 "cd "  + getBuildDir ().getPath () + " && "  +
236- 
237-                 "unzip "  + remoteGradlewZip  + " && "  +
238- 
239-                 "cd gradlew && "  +
240- 
241-                 "echo 'distributionBase=GRADLE_USER_HOME' >> gradle/wrapper/gradle-wrapper.properties && "  +
242-                 "echo 'distributionPath=wrapper/dists' >> gradle/wrapper/gradle-wrapper.properties && "  +
243-                 "echo 'zipStoreBase=GRADLE_USER_HOME' >> gradle/wrapper/gradle-wrapper.properties && "  +
244-                 "echo 'zipStorePath=wrapper/dists' >> gradle/wrapper/gradle-wrapper.properties && "  +
245-                 "echo 'distributionUrl=https\\ ://services.gradle.org/distributions/gradle-"  + plugin .getRequiredGradleVersion () + "-bin.zip' >> gradle/wrapper/gradle-wrapper.properties && "  +
246- 
247-                 "echo 'buildscript {' >> build.gradle && "  +
248-                 "echo '    repositories {' >> build.gradle && "  +
249-                 "echo '        "  + settings .getGradleRepositories () + "' >> build.gradle && "  +
250-                 "echo '    }' >> build.gradle && "  +
251-                 "echo '    dependencies {' >> build.gradle && "  +
252-                 "echo '        classpath group: \" org.multi-os-engine\" , name: \" moe-gradle\" , version: \" "  + sdk .pluginVersion  + "\" ' >> build.gradle && "  +
253-                 "echo '    }' >> build.gradle && "  +
254-                 "echo '}' >> build.gradle && "  +
255-                 "echo '' >> build.gradle && "  +
256-                 "echo 'apply plugin: \" moe-sdk\" ' >> build.gradle && "  +
257-                 "echo 'task printSDKRoot << { print \" "  + SDK_ROOT_MARK  + ":${moe.sdk.root}\"  }' >> build.gradle && "  +
258- 
259-                 "./gradlew printSDKRoot -s && "  +
260-                 "cd .. && rm -rf gradlew && rm -f gradlew.zip" 
261-         );
262-         final  int  start  = output .indexOf (SDK_ROOT_MARK );
263-         Require .NE (start , -1 , "SDK_ROOT_MARK not found" );
264-         final  int  start2  = start  + SDK_ROOT_MARK .length () + 1 ;
265226        try  {
266-             sdkDir  = new  URI ("file://"  + output .substring (start2 , output .indexOf ('\n' , start2 )));
227+             final  FileList  list  = new  FileList (sdk .getRoot ().getParentFile (), new  URI ("file://"  + getUserHome () + "/" ).resolve (".moe-remote" ));
228+             final  String  remoteGradlewZip  = list .add (sdk .getRoot ());
229+             upload ("upload sdk" , list );
230+             // Since zip's can't hold executable info, we need to apply it afterwards. Maybe we can be more selective if we want 
231+             // Or do it in ServerFileUploader, just making the files executable that also are locally 
232+             exec ("make executable" , "chmod -R +x "  + list .getTarget ().getPath ());
233+             sdkDir  = new  URI ("file://"  + remoteGradlewZip );
267234        } catch  (URISyntaxException  e ) {
268235            throw  new  GradleException (e .getMessage (), e );
269236        }
@@ -272,77 +239,25 @@ private void prepareServerMOE() {
272239    }
273240
274241    private  void  setupUserHome () {
275-         final  ChannelExec  channel ;
276-         try  {
277-             channel  = (ChannelExec ) session .openChannel ("exec" );
278-         } catch  (JSchException  e ) {
279-             throw  new  GradleException (e .getMessage (), e );
280-         }
281- 
282-         channel .setCommand ("echo $HOME" );
283- 
284-         final  ByteArrayOutputStream  baos  = new  ByteArrayOutputStream ();
285-         channel .setOutputStream (baos );
286- 
287-         try  {
288-             channel .connect ();
289-         } catch  (JSchException  e ) {
290-             throw  new  GradleException (e .getMessage (), e );
291-         }
292- 
293-         while  (!channel .isClosed ()) {
294-             try  {
295-                 Thread .sleep (100 );
296-             } catch  (InterruptedException  e ) {
297-                 throw  new  GradleException (e .getMessage (), e );
298-             }
299-         }
300- 
301-         channel .disconnect ();
302-         if  (channel .getExitStatus () != 0 ) {
303-             throw  new  GradleException ("Failed to initialize connection with server" );
304-         }
305-         userHome  = baos .toString ().trim ();
242+         ServerCommandRunner  runner  = new  ServerCommandRunner (this , "get home" , "echo $HOME" );
243+         runner .run ();
244+         userHome  = runner .getOutput ().trim ();
306245        LOG .quiet ("MOE Remote Build - REMOTE_HOME="  + getUserHome ());
307246    }
308247
309248    private  void  setupBuildDir () {
310-         final  ChannelExec  channel ;
311-         try  {
312-             channel  = (ChannelExec ) session .openChannel ("exec" );
313-         } catch  (JSchException  e ) {
314-             throw  new  GradleException (e .getMessage (), e );
315-         }
316- 
317-         channel .setCommand ("mktemp -d" );
318- 
319-         final  ByteArrayOutputStream  baos  = new  ByteArrayOutputStream ();
320-         channel .setOutputStream (baos );
321- 
322-         try  {
323-             channel .connect ();
324-         } catch  (JSchException  e ) {
325-             throw  new  GradleException (e .getMessage (), e );
326-         }
327- 
328-         while  (!channel .isClosed ()) {
329-             try  {
330-                 Thread .sleep (100 );
331-             } catch  (InterruptedException  e ) {
332-                 throw  new  GradleException (e .getMessage (), e );
333-             }
334-         }
249+         buildDir  = getTempDir ();
250+         LOG .quiet ("MOE Remote Build - REMOTE_BUILD_DIR="  + buildDir .getPath ());
251+     }
335252
336-         channel .disconnect ();
337-         if  (channel .getExitStatus () != 0 ) {
338-             throw  new  GradleException ("Failed to initialize connection with server" );
339-         }
253+     public  URI  getTempDir () {
254+         ServerCommandRunner  runner  = new  ServerCommandRunner (this , "temp dir" , "mktemp -d" );
255+         runner .run ();
340256        try  {
341-             buildDir  =  new  URI ("file://"  + baos . toString ().trim ());
257+             return   new  URI ("file://"  + runner . getOutput ().trim ());
342258        } catch  (URISyntaxException  e ) {
343259            throw  new  GradleException (e .getMessage (), e );
344260        }
345-         LOG .quiet ("MOE Remote Build - REMOTE_BUILD_DIR="  + buildDir .getPath ());
346261    }
347262
348263    private  void  disconnect () {
0 commit comments