diff --git a/android/app/src/main/java/com/example/journal/GitAddTask.java b/android/app/src/main/java/com/example/journal/GitAddTask.java new file mode 100644 index 00000000..096d1152 --- /dev/null +++ b/android/app/src/main/java/com/example/journal/GitAddTask.java @@ -0,0 +1,55 @@ +package com.example.journal; + +import android.os.AsyncTask; +import android.util.Log; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; + +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.errors.TransportException; + +import java.io.File; + +import io.flutter.plugin.common.MethodChannel.Result; + +public class GitAddTask extends AsyncTask { + private final static String TAG = "GitAdd"; + private Result result; + + public GitAddTask(Result _result) { + result = _result; + } + + protected Void doInBackground(String... params) { + final String cloneDirPath = params[0]; + final String filePattern = params[1]; + + File cloneDir = new File(cloneDirPath); + Log.d("GitClone Directory", cloneDirPath); + + try { + Git git = Git.open(cloneDir); + + AddCommand addCommand = git.add(); + addCommand.addFilepattern(filePattern); + addCommand.call(); + + } catch (TransportException e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } catch (GitAPIException e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } catch (Exception e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } + + result.success(null); + return null; + } +} diff --git a/android/app/src/main/java/com/example/journal/GitCloneTask.java b/android/app/src/main/java/com/example/journal/GitCloneTask.java index 37fc18e2..dc70ea9b 100644 --- a/android/app/src/main/java/com/example/journal/GitCloneTask.java +++ b/android/app/src/main/java/com/example/journal/GitCloneTask.java @@ -19,6 +19,7 @@ import java.io.File; import io.flutter.plugin.common.MethodChannel.Result; public class GitCloneTask extends AsyncTask { + private final static String TAG = "GitClone"; private Result result; public GitCloneTask(Result _result) { @@ -49,15 +50,15 @@ public class GitCloneTask extends AsyncTask { cloneCommand.call(); } catch (TransportException e) { - Log.d("gitClone", e.toString()); + Log.d(TAG, e.toString()); result.error("FAILED", e.toString(), null); return null; } catch (GitAPIException e) { - Log.d("gitClone", e.toString()); + Log.d(TAG, e.toString()); result.error("FAILED", e.toString(), null); return null; } catch (Exception e) { - Log.d("gitClone", e.toString()); + Log.d(TAG, e.toString()); result.error("FAILED", e.toString(), null); return null; } diff --git a/android/app/src/main/java/com/example/journal/GitCommitTask.java b/android/app/src/main/java/com/example/journal/GitCommitTask.java new file mode 100644 index 00000000..ab31cb52 --- /dev/null +++ b/android/app/src/main/java/com/example/journal/GitCommitTask.java @@ -0,0 +1,58 @@ +package com.example.journal; + +import android.os.AsyncTask; +import android.util.Log; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; + +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.errors.TransportException; + +import java.io.File; + +import io.flutter.plugin.common.MethodChannel.Result; + +public class GitCommitTask extends AsyncTask { + private final static String TAG = "GitCommit"; + private Result result; + + public GitCommitTask(Result _result) { + result = _result; + } + + protected Void doInBackground(String... params) { + final String cloneDirPath = params[0]; + final String authorName = params[1]; + final String authorEmail = params[2]; + final String message = params[3]; + + File cloneDir = new File(cloneDirPath); + Log.d("GitClone Directory", cloneDirPath); + + try { + Git git = Git.open(cloneDir); + + CommitCommand commitCommand = git.commit(); + commitCommand.setAuthor(authorName, authorEmail); + commitCommand.setMessage(message); + commitCommand.call(); + + } catch (TransportException e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } catch (GitAPIException e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } catch (Exception e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } + + result.success(null); + return null; + } +} diff --git a/android/app/src/main/java/com/example/journal/GitPushTask.java b/android/app/src/main/java/com/example/journal/GitPushTask.java new file mode 100644 index 00000000..c0f6a743 --- /dev/null +++ b/android/app/src/main/java/com/example/journal/GitPushTask.java @@ -0,0 +1,64 @@ +package com.example.journal; + +import android.os.AsyncTask; +import android.util.Log; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; + +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.TransportConfigCallback; +import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.transport.Transport; +import org.eclipse.jgit.transport.SshTransport; + +import java.io.File; + +import io.flutter.plugin.common.MethodChannel.Result; + +public class GitPushTask extends AsyncTask { + private final static String TAG = "GitPush"; + private Result result; + + public GitPushTask(Result _result) { + result = _result; + } + + protected Void doInBackground(String... params) { + String cloneDirPath = params[0]; + final String privateKeyPath = params[1]; + + File cloneDir = new File(cloneDirPath); + Log.d("GitClone Directory", cloneDirPath); + + try { + Git git = Git.open(cloneDir); + + PushCommand pushCommand = git.push(); + pushCommand.setTransportConfigCallback(new TransportConfigCallback() { + @Override + public void configure(Transport transport) { + SshTransport sshTransport = (SshTransport) transport; + sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath)); + } + }); + + pushCommand.call(); + } catch (TransportException e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } catch (GitAPIException e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } catch (Exception e) { + Log.d(TAG, e.toString()); + result.error("FAILED", e.toString(), null); + return null; + } + + result.success(null); + return null; + } +} diff --git a/android/app/src/main/java/com/example/journal/MainActivity.java b/android/app/src/main/java/com/example/journal/MainActivity.java index c7754ef4..d0fd5443 100644 --- a/android/app/src/main/java/com/example/journal/MainActivity.java +++ b/android/app/src/main/java/com/example/journal/MainActivity.java @@ -34,6 +34,10 @@ public class MainActivity extends FlutterActivity { new MethodCallHandler() { @Override public void onMethodCall(MethodCall call, Result result) { + final String filesDir = PathUtils.getFilesDir(getApplicationContext()); + final String sshKeysLocation = filesDir + "/ssh"; + final String privateKeyPath = sshKeysLocation + "/id_rsa"; + if (call.method.equals("gitClone")) { String cloneUrl = call.argument("cloneUrl"); String folderName = call.argument("folderName"); @@ -43,15 +47,11 @@ public class MainActivity extends FlutterActivity { return; } - String filesDir = PathUtils.getFilesDir(getApplicationContext()); String cloneLocation = filesDir + "/" + folderName; - final String privateKeyPath = filesDir + "/ssh/id_rsa"; new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath); return; - } - - if (call.method.equals("gitPull")) { + } else if (call.method.equals("gitPull")) { String folderName = call.argument("folderName"); if (folderName.isEmpty()) { @@ -59,18 +59,51 @@ public class MainActivity extends FlutterActivity { return; } - String filesDir = PathUtils.getFilesDir(getApplicationContext()); String cloneLocation = filesDir + "/" + folderName; - final String privateKeyPath = filesDir + "/ssh/id_rsa"; new GitPullTask(result).execute(cloneLocation, privateKeyPath); return; - } + } else if (call.method.equals("gitPush")) { + String folderName = call.argument("folderName"); - if (call.method.equals("generateSSHKeys")) { - String appFilesDir = PathUtils.getFilesDir(getApplicationContext()); - String sshKeysLocation = appFilesDir + "/ssh"; + if (folderName.isEmpty()) { + result.error("Invalid Parameters", "Arguments Invalid", null); + return; + } + String cloneLocation = filesDir + "/" + folderName; + + new GitPushTask(result).execute(cloneLocation, privateKeyPath); + return; + } else if (call.method.equals("gitAdd")) { + String folderName = call.argument("folderName"); + String filePattern = call.argument("filePattern"); + + if (folderName.isEmpty() || filePattern.isEmpty()) { + result.error("Invalid Parameters", "Arguments Invalid", null); + return; + } + + String cloneLocation = filesDir + "/" + folderName; + + new GitAddTask(result).execute(cloneLocation, filePattern); + return; + } else if (call.method.equals("gitCommit")) { + String folderName = call.argument("folderName"); + String authorName = call.argument("authorName"); + String authorEmail = call.argument("authorEmail"); + String message = call.argument("message"); + + if (folderName.isEmpty() || authorName.isEmpty() || authorEmail.isEmpty() || message.isEmpty()) { + result.error("Invalid Parameters", "Arguments Invalid", null); + return; + } + + String cloneLocation = filesDir + "/" + folderName; + + new GitCommitTask(result).execute(cloneLocation, authorName, authorEmail, message); + return; + } else if (call.method.equals("generateSSHKeys")) { new GenerateSSHKeysTask(result).execute(sshKeysLocation); return; } diff --git a/lib/gitapp.dart b/lib/gitapp.dart index e3716b37..443ac03c 100644 --- a/lib/gitapp.dart +++ b/lib/gitapp.dart @@ -22,27 +22,27 @@ buildGitButtons() { return [ RaisedButton( child: Text("Generate Keys"), - onPressed: () async { - await generateSSHKeys(); - }, + onPressed: generateSSHKeys, ), RaisedButton( child: Text("Git Clone"), - onPressed: () async { - await gitClone(); - }, + onPressed: gitClone, ), RaisedButton( child: Text("Git Pull"), - onPressed: () async { - await gitPull(); - }, + onPressed: gitPull, ), RaisedButton( - child: Text("New File"), - onPressed: () { - print("FOO"); - }, + child: Text("Git Add"), + onPressed: gitAdd, + ), + RaisedButton( + child: Text("Git Push"), + onPressed: gitPush, + ), + RaisedButton( + child: Text("Git Commit"), + onPressed: gitCommit, ), ]; } @@ -86,3 +86,49 @@ Future gitPull() async { print("gitPull Failed: '${e.message}'."); } } + +Future gitAdd() async { + const platform = const MethodChannel('gitjournal.io/git'); + + print("Going to git add"); + try { + await platform.invokeMethod('gitAdd', { + 'folderName': "journal", + 'filePattern': ".", + }); + print("Done"); + } on PlatformException catch (e) { + print("gitAdd Failed: '${e.message}'."); + } +} + +Future gitPush() async { + const platform = const MethodChannel('gitjournal.io/git'); + + print("Going to git push"); + try { + await platform.invokeMethod('gitPush', { + 'folderName': "journal", + }); + print("Done"); + } on PlatformException catch (e) { + print("gitPush Failed: '${e.message}'."); + } +} + +Future gitCommit() async { + const platform = const MethodChannel('gitjournal.io/git'); + + print("Going to git commit"); + try { + await platform.invokeMethod('gitCommit', { + 'folderName': "journal", + 'authorName': "Vishesh Handa", + 'authorEmail': "noemail@example.com", + 'message': "Default message from GitJournal", + }); + print("Done"); + } on PlatformException catch (e) { + print("gitCommit Failed: '${e.message}'."); + } +}