diff --git a/lib/apis/git.dart b/lib/apis/git.dart index 03e20cd7..1469be76 100644 --- a/lib/apis/git.dart +++ b/lib/apis/git.dart @@ -17,23 +17,115 @@ Future getGitBaseDirectory() async { return Directory(path); } -/// -/// It will be clone in gitBaseDirectory/folderName -/// -Future gitClone(String cloneUrl, String folderName) async { - print("Going to git clone"); - try { - await _platform.invokeMethod('gitClone', { - 'cloneUrl': cloneUrl, - 'folderName': folderName, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitClone Failed: '${e.message}'."); - throw createGitException(e.message); +class GitRepo { + String folderName; + String authorName; + String authorEmail; + + GitRepo({ + @required this.folderName, + @required this.authorName, + @required this.authorEmail, + }); + + static Future clone(String folderName, String cloneUrl) async { + try { + await _platform.invokeMethod('gitClone', { + 'cloneUrl': cloneUrl, + 'folderName': folderName, + }); + print("Done"); + } on PlatformException catch (e) { + print("gitClone Failed: '${e.message}'."); + throw createGitException(e.message); + } } - return null; + static Future init(String folderName) async { + try { + await _platform.invokeMethod('gitInit', { + 'folderName': folderName, + }); + } on PlatformException catch (e) { + print("gitInit Failed: '${e.message}'."); + throw createGitException(e.message); + } + } + + Future pull() async { + try { + await _platform.invokeMethod('gitPull', { + 'folderName': folderName, + 'authorName': authorName, + 'authorEmail': authorEmail, + }); + print("Done"); + } on PlatformException catch (e) { + print("gitPull Failed: '${e.message}'."); + throw createGitException(e.message); + } + } + + Future add(String filePattern) async { + try { + await _platform.invokeMethod('gitAdd', { + 'folderName': folderName, + 'filePattern': filePattern, + }); + } on PlatformException catch (e) { + print("gitAdd Failed: '${e.message}'."); + } + } + + Future rm(String filePattern) async { + try { + await _platform.invokeMethod('gitRm', { + 'folderName': folderName, + 'filePattern': filePattern, + }); + } on PlatformException catch (e) { + print("gitRm Failed: '${e.message}'."); + } + } + + Future push() async { + try { + await _platform.invokeMethod('gitPush', { + 'folderName': folderName, + }); + } on PlatformException catch (e) { + print("gitPush Failed: '${e.message}'."); + throw createGitException(e.message); + } + } + + // FIXME: Change this method to just resetHard + Future resetLast() async { + try { + await _platform.invokeMethod('gitResetLast', { + 'folderName': folderName, + }); + } on PlatformException catch (e) { + print("gitResetLast Failed: '${e.message}'."); + throw createGitException(e.message); + } + } + + // FIXME: Change the datetime + // FIXME: Actually implement the 'when' + Future commit({@required String message, String when}) async { + try { + await _platform.invokeMethod('gitCommit', { + 'folderName': folderName, + 'authorName': authorName, + 'authorEmail': authorEmail, + 'message': message, + 'when': when, + }); + } on PlatformException catch (e) { + print("gitCommit Failed: '${e.message}'."); + } + } } Future generateSSHKeys({@required String comment}) async { @@ -97,109 +189,3 @@ GitException createGitException(String msg) { } return GitException(msg); } - -Future gitPull({ - String folderName, - String authorName, - String authorEmail, -}) async { - print("Going to git pull: $folderName"); - try { - await _platform.invokeMethod('gitPull', { - 'folderName': folderName, - 'authorName': authorName, - 'authorEmail': authorEmail, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitPull Failed: '${e.message}'."); - throw createGitException(e.message); - } -} - -Future gitAdd(String gitFolder, String filePattern) async { - print("Going to git add: " + filePattern); - try { - await _platform.invokeMethod('gitAdd', { - 'folderName': gitFolder, - 'filePattern': filePattern, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitAdd Failed: '${e.message}'."); - } -} - -Future gitRm(String gitFolder, String filePattern) async { - print("Going to git rm"); - try { - await _platform.invokeMethod('gitRm', { - 'folderName': gitFolder, - 'filePattern': filePattern, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitRm Failed: '${e.message}'."); - } -} - -Future gitPush(String folderName) async { - print("Going to git push"); - try { - await _platform.invokeMethod('gitPush', { - 'folderName': folderName, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitPush Failed: '${e.message}'."); - throw createGitException(e.message); - } -} - -Future gitResetLast(String folderName) async { - print("Going to git reset last"); - try { - await _platform.invokeMethod('gitResetLast', { - 'folderName': folderName, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitResetLast Failed: '${e.message}'."); - throw createGitException(e.message); - } -} - -Future gitCommit({ - @required String gitFolder, - @required String authorName, - @required String authorEmail, - @required String message, - String when, -}) async { - print("Going to git commit"); - try { - await _platform.invokeMethod('gitCommit', { - 'folderName': gitFolder, - 'authorName': authorName, - 'authorEmail': authorEmail, - 'message': message, - 'when': when, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitCommit Failed: '${e.message}'."); - } -} - -Future gitInit(String folderName) async { - print("Going to git init"); - try { - await _platform.invokeMethod('gitInit', { - 'folderName': folderName, - }); - print("Done"); - } on PlatformException catch (e) { - print("gitInit Failed: '${e.message}'."); - throw createGitException(e.message); - } -} diff --git a/lib/apis/git_migration.dart b/lib/apis/git_migration.dart index 12866442..6d79c0cf 100644 --- a/lib/apis/git_migration.dart +++ b/lib/apis/git_migration.dart @@ -34,13 +34,14 @@ Future migrateGitRepo({ print("Migrating " + file.path + " --> " + toPath); await file.copy(toPath); - await gitAdd(toGitBaseFolder, fileName); - await gitCommit( - gitFolder: toGitBaseFolder, + + var gitRepo = GitRepo( + folderName: toGitBaseFolder, authorEmail: Settings.instance.gitAuthorEmail, authorName: Settings.instance.gitAuthor, - message: "Added Journal Entry", ); + await gitRepo.add(fileName); + await gitRepo.commit(message: "Added Journal Entry"); } print("migrateGitRepo: Done"); } diff --git a/lib/apis/githost.dart b/lib/apis/githost.dart index 6e2ee4c2..84ea242c 100644 --- a/lib/apis/githost.dart +++ b/lib/apis/githost.dart @@ -9,9 +9,9 @@ abstract class GitHost { Future launchOAuthScreen(); Future getUserInfo(); - Future> listRepos(); - Future createRepo(String name); - Future getRepo(String name); + Future> listRepos(); + Future createRepo(String name); + Future getRepo(String name); Future addDeployKey(String sshPublicKey, String repo); } @@ -27,11 +27,11 @@ class UserInfo { }); } -class GitRepo { +class GitHostRepo { String fullName; String cloneUrl; - GitRepo({this.fullName, this.cloneUrl}); + GitHostRepo({this.fullName, this.cloneUrl}); @override String toString() { diff --git a/lib/apis/github.dart b/lib/apis/github.dart index cfedfbf9..8fcfcdf4 100644 --- a/lib/apis/github.dart +++ b/lib/apis/github.dart @@ -75,7 +75,7 @@ class GitHub implements GitHost { } @override - Future> listRepos() async { + Future> listRepos() async { if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -93,7 +93,7 @@ class GitHub implements GitHost { } List list = jsonDecode(response.body); - var repos = []; + var repos = []; list.forEach((dynamic d) { var map = Map.from(d); var repo = _repoFromJson(map); @@ -105,7 +105,7 @@ class GitHub implements GitHost { } @override - Future createRepo(String name) async { + Future createRepo(String name) async { if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -143,7 +143,7 @@ class GitHub implements GitHost { } @override - Future getRepo(String name) async { + Future getRepo(String name) async { if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -201,8 +201,8 @@ class GitHub implements GitHost { return json.decode(response.body); } - GitRepo _repoFromJson(Map parsedJson) { - return GitRepo( + GitHostRepo _repoFromJson(Map parsedJson) { + return GitHostRepo( fullName: parsedJson['full_name'], cloneUrl: parsedJson['ssh_url'], ); diff --git a/lib/apis/gitlab.dart b/lib/apis/gitlab.dart index 3f8eb375..b660fa86 100644 --- a/lib/apis/gitlab.dart +++ b/lib/apis/gitlab.dart @@ -63,7 +63,7 @@ class GitLab implements GitHost { } @override - Future> listRepos() async { + Future> listRepos() async { if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -82,7 +82,7 @@ class GitLab implements GitHost { } List list = jsonDecode(response.body); - var repos = []; + var repos = []; list.forEach((dynamic d) { var map = Map.from(d); var repo = _repoFromJson(map); @@ -94,7 +94,7 @@ class GitLab implements GitHost { } @override - Future createRepo(String name) async { + Future createRepo(String name) async { if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -132,7 +132,7 @@ class GitLab implements GitHost { } @override - Future getRepo(String name) async { + Future getRepo(String name) async { if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -191,8 +191,8 @@ class GitLab implements GitHost { return json.decode(response.body); } - GitRepo _repoFromJson(Map parsedJson) { - return GitRepo( + GitHostRepo _repoFromJson(Map parsedJson) { + return GitHostRepo( fullName: parsedJson['path_with_namespace'], cloneUrl: parsedJson['ssh_url_to_repo'], ); diff --git a/lib/app.dart b/lib/app.dart index 3f6f8729..9dc0f63f 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -43,7 +43,7 @@ class JournalApp extends StatelessWidget { if (localGitRepoConfigured == false) { // FIXME: What about exceptions! localGitRepoPath = "journal_local"; - await gitInit(localGitRepoPath); + await GitRepo.init(localGitRepoPath); localGitRepoConfigured = true; diff --git a/lib/gitapp.dart b/lib/gitapp.dart index 1bb717cf..051bb386 100644 --- a/lib/gitapp.dart +++ b/lib/gitapp.dart @@ -40,6 +40,12 @@ class GitApp extends StatelessWidget { } List _buildGitButtons() { + var gitRepo = GitRepo( + folderName: basePath, + authorName: "Vishesh Handa", + authorEmail: "noemail@example.com", + ); + return [ RaisedButton( child: Text("Generate Keys"), @@ -50,7 +56,7 @@ class GitApp extends StatelessWidget { child: Text("Git Clone"), onPressed: () async { try { - await gitClone(cloneUrl, basePath); + await GitRepo.clone(cloneUrl, basePath); _sendSuccess(); } on GitException catch (ex) { print(ex); @@ -61,32 +67,25 @@ class GitApp extends StatelessWidget { RaisedButton( child: Text("Git Pull"), onPressed: () async { - gitPull( - folderName: basePath, - authorEmail: "noemail@example.com", - authorName: "Vishesh Handa", - ); + gitRepo.pull(); }, ), RaisedButton( child: Text("Git Add"), onPressed: () async { - await gitAdd(basePath, "."); + gitRepo.add("."); }, ), RaisedButton( child: Text("Git Push"), onPressed: () async { - gitPush(basePath); + gitRepo.push(); }, ), RaisedButton( child: Text("Git Commit"), onPressed: () async { - gitCommit( - gitFolder: basePath, - authorEmail: "noemail@example.com", - authorName: "Vishesh Handa", + gitRepo.commit( message: "Default message from GitJournal", when: "2017-10-20T01:21:10+02:00", ); @@ -95,7 +94,7 @@ class GitApp extends StatelessWidget { RaisedButton( child: Text("Git Reset Last"), onPressed: () async { - gitResetLast(basePath); + gitRepo.resetLast(); }, ), RaisedButton( diff --git a/lib/screens/githostsetup_autoconfigure.dart b/lib/screens/githostsetup_autoconfigure.dart index b9ee2c00..2e94b011 100644 --- a/lib/screens/githostsetup_autoconfigure.dart +++ b/lib/screens/githostsetup_autoconfigure.dart @@ -49,7 +49,7 @@ class GitHostSetupAutoConfigureState extends State { } print("GitHost Initalized: " + widget.gitHostType.toString()); - GitRepo repo; + GitHostRepo repo; try { setState(() { _message = "Creating private repo"; diff --git a/lib/screens/githostsetup_screens.dart b/lib/screens/githostsetup_screens.dart index e84b32f7..a4f425ca 100644 --- a/lib/screens/githostsetup_screens.dart +++ b/lib/screens/githostsetup_screens.dart @@ -380,7 +380,7 @@ class GitHostSetupScreenState extends State { String error; try { - await gitClone(_gitCloneUrl, "journal"); + await GitRepo.clone(_gitCloneUrl, "journal"); } on GitException catch (e) { error = e.cause; } diff --git a/lib/storage/git_storage.dart b/lib/storage/git_storage.dart index d577ca0c..fc86bb0b 100644 --- a/lib/storage/git_storage.dart +++ b/lib/storage/git_storage.dart @@ -13,6 +13,7 @@ class GitNoteRepository implements NoteRepository { final FileStorage _fileStorage; final String dirName; final String subDirName; + final GitRepo _gitRepo; bool cloned = false; bool checkForCloned = false; @@ -21,9 +22,14 @@ class GitNoteRepository implements NoteRepository { @required this.dirName, @required this.subDirName, @required String baseDirectory, - }) : _fileStorage = FileStorage( + }) : _fileStorage = FileStorage( noteSerializer: MarkdownYAMLSerializer(), baseDirectory: p.join(baseDirectory, dirName, subDirName), + ), + _gitRepo = GitRepo( + folderName: dirName, + authorEmail: Settings.instance.gitAuthorEmail, + authorName: Settings.instance.gitAuthor, ); @override @@ -37,11 +43,8 @@ class GitNoteRepository implements NoteRepository { return result; } - await gitAdd(dirName, '.'); - await gitCommit( - gitFolder: dirName, - authorEmail: Settings.instance.gitAuthorEmail, - authorName: Settings.instance.gitAuthor, + await _gitRepo.add("."); + await _gitRepo.commit( message: commitMessage, ); @@ -59,11 +62,8 @@ class GitNoteRepository implements NoteRepository { var baseDir = _fileStorage.baseDirectory; var filePath = result.noteFilePath.replaceFirst(baseDir + "/", ""); - await gitRm(dirName, filePath); - await gitCommit( - gitFolder: dirName, - authorEmail: Settings.instance.gitAuthorEmail, - authorName: Settings.instance.gitAuthor, + await _gitRepo.rm(filePath); + await _gitRepo.commit( message: "Removed Journal entry", ); @@ -71,7 +71,7 @@ class GitNoteRepository implements NoteRepository { } Future resetLastCommit() async { - await gitResetLast(dirName); + await _gitRepo.resetLast(); return NoteRepoResult(error: false); } @@ -88,17 +88,13 @@ class GitNoteRepository implements NoteRepository { @override Future sync() async { try { - await gitPull( - folderName: dirName, - authorEmail: Settings.instance.gitAuthorEmail, - authorName: Settings.instance.gitAuthor, - ); + await _gitRepo.pull(); } on GitException catch (ex) { print(ex); } try { - await gitPush(dirName); + await _gitRepo.push(); } on GitException catch (ex) { print(ex); rethrow;