1 package com.neuronrobotics.bowlerstudio.assets;
4 import java.io.IOException;
5 import java.lang.reflect.Type;
6 import java.util.HashMap;
9 import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
10 import org.kohsuke.github.GHMyself;
11 import org.kohsuke.github.GHRepository;
13 import com.google.gson.Gson;
14 import com.google.gson.GsonBuilder;
15 import com.google.gson.reflect.TypeToken;
16 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
17 import com.neuronrobotics.bowlerstudio.scripting.IGithubLoginListener;
18 import com.neuronrobotics.bowlerstudio.scripting.PasswordManager;
19 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
23 private static final String
repo =
"BowlerStudioConfiguration";
28 private static String
dbFile =
"database.json";
30 private static HashMap<String, HashMap<String, Object>>
database =
null;
31 private static final Type
TT_mapStringString =
new TypeToken<HashMap<String, HashMap<String, Object>>>() {
34 private static Gson
gson =
new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
41 public static Object
getObject(String paramsKey, String objectKey, Object defaultValue) {
42 if (
getParamMap(paramsKey).
get(objectKey) ==
null) {
43 System.err.println(
"Cant find: " + paramsKey +
":" + objectKey);
44 setObject(paramsKey, objectKey, defaultValue);
49 public static HashMap<String, Object>
getParamMap(String paramsKey) {
51 getDatabase().put(paramsKey,
new HashMap<String, Object>());
56 public static Object
setObject(String paramsKey, String objectKey, Object value) {
57 return getParamMap(paramsKey).put(objectKey, value);
60 public static Object
removeObject(String paramsKey, String objectKey) {
64 public static void save() {
67 String writeOut =
null;
73 for (
int i = 0; i < 2; i++) {
76 System.out.println(
"Not pushing changes the default branch");
81 getDbFile(), writeOut,
"Saving database");
83 }
catch (Exception e) {
88 }
catch (Exception e1) {
97 @SuppressWarnings(
"unchecked")
98 public static HashMap<String, HashMap<String, Object>>
getDatabase() {
102 HashMap<String, HashMap<String, Object>> existing =
null;
109 }
catch (org.eclipse.jgit.api.errors.WrongRepositoryStateException e) {
114 }
catch (Exception e1) {
116 e1.printStackTrace();
118 }
catch (Exception ex) {
119 ex.printStackTrace();
122 database =
new HashMap<String, HashMap<String, Object>>();
136 if (username ==
null) {
142 System.out.println(
"\n\nLogout from "+loggedInAs2+
"\n\n");
150 HashMap<String, HashMap<String, Object>> existing =
database;
153 System.out.println(
"Setting Configurations repo to " +
getGitSource());
154 }
catch (Exception e) {
164 private static void syncOldDBToNew(HashMap<String, HashMap<String, Object>> existing) {
165 if (existing !=
null)
166 for (String pkey : existing.keySet()) {
167 HashMap<String, Object> val = existing.get(pkey);
169 for (String objectKey : val.keySet()) {
170 Object defaultValue = val.get(objectKey);
171 Object value =
getObject(pkey, objectKey, defaultValue);
172 System.err.println(
"Setting " + pkey +
":" + objectKey +
" to " + value +
" with previous being "+ defaultValue);
186 }
catch (Throwable t) {
188 GHRepository defaultRep = github.getRepository(
"CommonWealthRobotics/" +
repo);
189 GHRepository forkedRep = defaultRep.fork();
210 private static void setRepo(GHRepository forkedRep) {
211 String myAssets = forkedRep.getGitTransportUrl().replaceAll(
"git://",
"https://");
216 System.out.println(
"Using my version of configuration database: " + myAssets);
static HashMap< String, HashMap< String, Object > > database
static HashMap< String, Object > getParamMap(String paramsKey)
static final String HTTPS_GITHUB_COM_NEURON_ROBOTICS_BOWLER_STUDIO_CONFIGURATION_GIT
static HashMap< String, HashMap< String, Object > > getDatabase()
static void setDbFile(String dbFile)
static void loginEvent(String username)
static final Type TT_mapStringString
static Object removeObject(String paramsKey, String objectKey)
static IssueReportingExceptionHandler reporter
static void setRepo(GHRepository forkedRep)
static Object getObject(String paramsKey, String objectKey, Object defaultValue)
static String getDbFile()
static String getGitSource()
static void syncOldDBToNew(HashMap< String, HashMap< String, Object >> existing)
static void setGitSource(String myAssets)
static Object setObject(String paramsKey, String objectKey, Object value)
static String getUsername()
static String getLoginID()
static GitHub getGithub()
static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
static boolean isLoginSuccess()
static boolean hasNetwork()
static void deleteRepo(String remoteURI)
static Object inlineFileScriptRun(File f, ArrayList< Object > args)
static String getFullBranch(String remoteURI)
static File fileFromGit(String remoteURI, String fileInRepo)
static void pull(String remoteURI, String branch)