Change Git dart API

Use a class instead of a many functions. This way we avoid passing the
same arguments again and again.
This commit is contained in:
Vishesh Handa
2019-06-17 19:38:56 +02:00
parent b9cd8696c2
commit d653663a47
10 changed files with 158 additions and 176 deletions

View File

@ -17,11 +17,18 @@ Future<Directory> getGitBaseDirectory() async {
return Directory(path); return Directory(path);
} }
/// class GitRepo {
/// It will be clone in gitBaseDirectory/folderName String folderName;
/// String authorName;
Future<void> gitClone(String cloneUrl, String folderName) async { String authorEmail;
print("Going to git clone");
GitRepo({
@required this.folderName,
@required this.authorName,
@required this.authorEmail,
});
static Future<void> clone(String folderName, String cloneUrl) async {
try { try {
await _platform.invokeMethod('gitClone', { await _platform.invokeMethod('gitClone', {
'cloneUrl': cloneUrl, 'cloneUrl': cloneUrl,
@ -32,8 +39,93 @@ Future<void> gitClone(String cloneUrl, String folderName) async {
print("gitClone Failed: '${e.message}'."); print("gitClone Failed: '${e.message}'.");
throw createGitException(e.message); throw createGitException(e.message);
} }
}
return null; static Future<void> 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<void> 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<void> add(String filePattern) async {
try {
await _platform.invokeMethod('gitAdd', {
'folderName': folderName,
'filePattern': filePattern,
});
} on PlatformException catch (e) {
print("gitAdd Failed: '${e.message}'.");
}
}
Future<void> rm(String filePattern) async {
try {
await _platform.invokeMethod('gitRm', {
'folderName': folderName,
'filePattern': filePattern,
});
} on PlatformException catch (e) {
print("gitRm Failed: '${e.message}'.");
}
}
Future<void> 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<void> 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<void> 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<String> generateSSHKeys({@required String comment}) async { Future<String> generateSSHKeys({@required String comment}) async {
@ -97,109 +189,3 @@ GitException createGitException(String msg) {
} }
return GitException(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);
}
}

View File

@ -34,13 +34,14 @@ Future migrateGitRepo({
print("Migrating " + file.path + " --> " + toPath); print("Migrating " + file.path + " --> " + toPath);
await file.copy(toPath); await file.copy(toPath);
await gitAdd(toGitBaseFolder, fileName);
await gitCommit( var gitRepo = GitRepo(
gitFolder: toGitBaseFolder, folderName: toGitBaseFolder,
authorEmail: Settings.instance.gitAuthorEmail, authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor, authorName: Settings.instance.gitAuthor,
message: "Added Journal Entry",
); );
await gitRepo.add(fileName);
await gitRepo.commit(message: "Added Journal Entry");
} }
print("migrateGitRepo: Done"); print("migrateGitRepo: Done");
} }

View File

@ -9,9 +9,9 @@ abstract class GitHost {
Future launchOAuthScreen(); Future launchOAuthScreen();
Future<UserInfo> getUserInfo(); Future<UserInfo> getUserInfo();
Future<List<GitRepo>> listRepos(); Future<List<GitHostRepo>> listRepos();
Future<GitRepo> createRepo(String name); Future<GitHostRepo> createRepo(String name);
Future<GitRepo> getRepo(String name); Future<GitHostRepo> getRepo(String name);
Future addDeployKey(String sshPublicKey, String repo); Future addDeployKey(String sshPublicKey, String repo);
} }
@ -27,11 +27,11 @@ class UserInfo {
}); });
} }
class GitRepo { class GitHostRepo {
String fullName; String fullName;
String cloneUrl; String cloneUrl;
GitRepo({this.fullName, this.cloneUrl}); GitHostRepo({this.fullName, this.cloneUrl});
@override @override
String toString() { String toString() {

View File

@ -75,7 +75,7 @@ class GitHub implements GitHost {
} }
@override @override
Future<List<GitRepo>> listRepos() async { Future<List<GitHostRepo>> listRepos() async {
if (_accessCode.isEmpty) { if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode; throw GitHostException.MissingAccessCode;
} }
@ -93,7 +93,7 @@ class GitHub implements GitHost {
} }
List<dynamic> list = jsonDecode(response.body); List<dynamic> list = jsonDecode(response.body);
var repos = <GitRepo>[]; var repos = <GitHostRepo>[];
list.forEach((dynamic d) { list.forEach((dynamic d) {
var map = Map<String, dynamic>.from(d); var map = Map<String, dynamic>.from(d);
var repo = _repoFromJson(map); var repo = _repoFromJson(map);
@ -105,7 +105,7 @@ class GitHub implements GitHost {
} }
@override @override
Future<GitRepo> createRepo(String name) async { Future<GitHostRepo> createRepo(String name) async {
if (_accessCode.isEmpty) { if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode; throw GitHostException.MissingAccessCode;
} }
@ -143,7 +143,7 @@ class GitHub implements GitHost {
} }
@override @override
Future<GitRepo> getRepo(String name) async { Future<GitHostRepo> getRepo(String name) async {
if (_accessCode.isEmpty) { if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode; throw GitHostException.MissingAccessCode;
} }
@ -201,8 +201,8 @@ class GitHub implements GitHost {
return json.decode(response.body); return json.decode(response.body);
} }
GitRepo _repoFromJson(Map<String, dynamic> parsedJson) { GitHostRepo _repoFromJson(Map<String, dynamic> parsedJson) {
return GitRepo( return GitHostRepo(
fullName: parsedJson['full_name'], fullName: parsedJson['full_name'],
cloneUrl: parsedJson['ssh_url'], cloneUrl: parsedJson['ssh_url'],
); );

View File

@ -63,7 +63,7 @@ class GitLab implements GitHost {
} }
@override @override
Future<List<GitRepo>> listRepos() async { Future<List<GitHostRepo>> listRepos() async {
if (_accessCode.isEmpty) { if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode; throw GitHostException.MissingAccessCode;
} }
@ -82,7 +82,7 @@ class GitLab implements GitHost {
} }
List<dynamic> list = jsonDecode(response.body); List<dynamic> list = jsonDecode(response.body);
var repos = <GitRepo>[]; var repos = <GitHostRepo>[];
list.forEach((dynamic d) { list.forEach((dynamic d) {
var map = Map<String, dynamic>.from(d); var map = Map<String, dynamic>.from(d);
var repo = _repoFromJson(map); var repo = _repoFromJson(map);
@ -94,7 +94,7 @@ class GitLab implements GitHost {
} }
@override @override
Future<GitRepo> createRepo(String name) async { Future<GitHostRepo> createRepo(String name) async {
if (_accessCode.isEmpty) { if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode; throw GitHostException.MissingAccessCode;
} }
@ -132,7 +132,7 @@ class GitLab implements GitHost {
} }
@override @override
Future<GitRepo> getRepo(String name) async { Future<GitHostRepo> getRepo(String name) async {
if (_accessCode.isEmpty) { if (_accessCode.isEmpty) {
throw GitHostException.MissingAccessCode; throw GitHostException.MissingAccessCode;
} }
@ -191,8 +191,8 @@ class GitLab implements GitHost {
return json.decode(response.body); return json.decode(response.body);
} }
GitRepo _repoFromJson(Map<String, dynamic> parsedJson) { GitHostRepo _repoFromJson(Map<String, dynamic> parsedJson) {
return GitRepo( return GitHostRepo(
fullName: parsedJson['path_with_namespace'], fullName: parsedJson['path_with_namespace'],
cloneUrl: parsedJson['ssh_url_to_repo'], cloneUrl: parsedJson['ssh_url_to_repo'],
); );

View File

@ -43,7 +43,7 @@ class JournalApp extends StatelessWidget {
if (localGitRepoConfigured == false) { if (localGitRepoConfigured == false) {
// FIXME: What about exceptions! // FIXME: What about exceptions!
localGitRepoPath = "journal_local"; localGitRepoPath = "journal_local";
await gitInit(localGitRepoPath); await GitRepo.init(localGitRepoPath);
localGitRepoConfigured = true; localGitRepoConfigured = true;

View File

@ -40,6 +40,12 @@ class GitApp extends StatelessWidget {
} }
List<Widget> _buildGitButtons() { List<Widget> _buildGitButtons() {
var gitRepo = GitRepo(
folderName: basePath,
authorName: "Vishesh Handa",
authorEmail: "noemail@example.com",
);
return <Widget>[ return <Widget>[
RaisedButton( RaisedButton(
child: Text("Generate Keys"), child: Text("Generate Keys"),
@ -50,7 +56,7 @@ class GitApp extends StatelessWidget {
child: Text("Git Clone"), child: Text("Git Clone"),
onPressed: () async { onPressed: () async {
try { try {
await gitClone(cloneUrl, basePath); await GitRepo.clone(cloneUrl, basePath);
_sendSuccess(); _sendSuccess();
} on GitException catch (ex) { } on GitException catch (ex) {
print(ex); print(ex);
@ -61,32 +67,25 @@ class GitApp extends StatelessWidget {
RaisedButton( RaisedButton(
child: Text("Git Pull"), child: Text("Git Pull"),
onPressed: () async { onPressed: () async {
gitPull( gitRepo.pull();
folderName: basePath,
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
);
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Add"), child: Text("Git Add"),
onPressed: () async { onPressed: () async {
await gitAdd(basePath, "."); gitRepo.add(".");
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Push"), child: Text("Git Push"),
onPressed: () async { onPressed: () async {
gitPush(basePath); gitRepo.push();
}, },
), ),
RaisedButton( RaisedButton(
child: Text("Git Commit"), child: Text("Git Commit"),
onPressed: () async { onPressed: () async {
gitCommit( gitRepo.commit(
gitFolder: basePath,
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
message: "Default message from GitJournal", message: "Default message from GitJournal",
when: "2017-10-20T01:21:10+02:00", when: "2017-10-20T01:21:10+02:00",
); );
@ -95,7 +94,7 @@ class GitApp extends StatelessWidget {
RaisedButton( RaisedButton(
child: Text("Git Reset Last"), child: Text("Git Reset Last"),
onPressed: () async { onPressed: () async {
gitResetLast(basePath); gitRepo.resetLast();
}, },
), ),
RaisedButton( RaisedButton(

View File

@ -49,7 +49,7 @@ class GitHostSetupAutoConfigureState extends State<GitHostSetupAutoConfigure> {
} }
print("GitHost Initalized: " + widget.gitHostType.toString()); print("GitHost Initalized: " + widget.gitHostType.toString());
GitRepo repo; GitHostRepo repo;
try { try {
setState(() { setState(() {
_message = "Creating private repo"; _message = "Creating private repo";

View File

@ -380,7 +380,7 @@ class GitHostSetupScreenState extends State<GitHostSetupScreen> {
String error; String error;
try { try {
await gitClone(_gitCloneUrl, "journal"); await GitRepo.clone(_gitCloneUrl, "journal");
} on GitException catch (e) { } on GitException catch (e) {
error = e.cause; error = e.cause;
} }

View File

@ -13,6 +13,7 @@ class GitNoteRepository implements NoteRepository {
final FileStorage _fileStorage; final FileStorage _fileStorage;
final String dirName; final String dirName;
final String subDirName; final String subDirName;
final GitRepo _gitRepo;
bool cloned = false; bool cloned = false;
bool checkForCloned = false; bool checkForCloned = false;
@ -24,6 +25,11 @@ class GitNoteRepository implements NoteRepository {
}) : _fileStorage = FileStorage( }) : _fileStorage = FileStorage(
noteSerializer: MarkdownYAMLSerializer(), noteSerializer: MarkdownYAMLSerializer(),
baseDirectory: p.join(baseDirectory, dirName, subDirName), baseDirectory: p.join(baseDirectory, dirName, subDirName),
),
_gitRepo = GitRepo(
folderName: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
); );
@override @override
@ -37,11 +43,8 @@ class GitNoteRepository implements NoteRepository {
return result; return result;
} }
await gitAdd(dirName, '.'); await _gitRepo.add(".");
await gitCommit( await _gitRepo.commit(
gitFolder: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
message: commitMessage, message: commitMessage,
); );
@ -59,11 +62,8 @@ class GitNoteRepository implements NoteRepository {
var baseDir = _fileStorage.baseDirectory; var baseDir = _fileStorage.baseDirectory;
var filePath = result.noteFilePath.replaceFirst(baseDir + "/", ""); var filePath = result.noteFilePath.replaceFirst(baseDir + "/", "");
await gitRm(dirName, filePath); await _gitRepo.rm(filePath);
await gitCommit( await _gitRepo.commit(
gitFolder: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
message: "Removed Journal entry", message: "Removed Journal entry",
); );
@ -71,7 +71,7 @@ class GitNoteRepository implements NoteRepository {
} }
Future<NoteRepoResult> resetLastCommit() async { Future<NoteRepoResult> resetLastCommit() async {
await gitResetLast(dirName); await _gitRepo.resetLast();
return NoteRepoResult(error: false); return NoteRepoResult(error: false);
} }
@ -88,17 +88,13 @@ class GitNoteRepository implements NoteRepository {
@override @override
Future<bool> sync() async { Future<bool> sync() async {
try { try {
await gitPull( await _gitRepo.pull();
folderName: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
);
} on GitException catch (ex) { } on GitException catch (ex) {
print(ex); print(ex);
} }
try { try {
await gitPush(dirName); await _gitRepo.push();
} on GitException catch (ex) { } on GitException catch (ex) {
print(ex); print(ex);
rethrow; rethrow;