11package com .axone_io .ignition .git .managers ;
22
3- import java .io .IOException ;
4- import java .nio .file .Files ;
5- import java .nio .file .Path ;
6- import java .util .ArrayList ;
7- import java .util .Arrays ;
8- import java .util .List ;
9- import java .util .Set ;
10- import java .util .concurrent .CompletableFuture ;
11-
123import com .axone_io .ignition .git .SshTransportConfigCallback ;
134import com .axone_io .ignition .git .records .GitProjectsConfigRecord ;
145import com .axone_io .ignition .git .records .GitReposUsersRecord ;
156import com .inductiveautomation .ignition .common .JsonUtilities ;
167import com .inductiveautomation .ignition .common .gson .Gson ;
17- import com .inductiveautomation .ignition .common .gson .GsonBuilder ;
188import com .inductiveautomation .ignition .common .gson .JsonElement ;
199import com .inductiveautomation .ignition .common .gson .JsonObject ;
2010import com .inductiveautomation .ignition .common .project .RuntimeProject ;
21- import com .inductiveautomation .ignition .common .project .resource .*;
11+ import com .inductiveautomation .ignition .common .project .resource .LastModification ;
12+ import com .inductiveautomation .ignition .common .project .resource .ProjectResource ;
13+ import com .inductiveautomation .ignition .common .project .resource .ResourcePath ;
14+ import com .inductiveautomation .ignition .common .project .resource .ResourceType ;
2215import com .inductiveautomation .ignition .common .tags .TagUtilities ;
2316import com .inductiveautomation .ignition .common .tags .config .TagConfigurationModel ;
2417import com .inductiveautomation .ignition .common .tags .model .TagPath ;
3629import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
3730import simpleorm .dataset .SQuery ;
3831
32+ import java .io .IOException ;
33+ import java .nio .file .Files ;
34+ import java .nio .file .Path ;
35+ import java .util .ArrayList ;
36+ import java .util .Arrays ;
37+ import java .util .List ;
38+ import java .util .Set ;
39+ import java .util .concurrent .CompletableFuture ;
40+
3941import static com .axone_io .ignition .git .GatewayHook .context ;
4042
4143public class GitManager {
4244 private final static LoggerEx logger = LoggerEx .newBuilder ().build (GitManager .class );
4345
44- private static final Gson PROJECT_GSON = new GsonBuilder ()
45- .registerTypeAdapter (ImmutableProjectResource .class , new ImmutableProjectResource .GsonAdapter ())
46- .registerTypeAdapter (byte [].class , new JsonUtilities .ByteArrayAdapter ())
47- .setPrettyPrinting ()
48- .create ();
49-
5046 public static Git getGit (Path projectFolderPath ) {
5147 Git git ;
5248 try {
5349 git = Git .open (projectFolderPath .resolve (".git" ).toFile ());
54- StoredConfig config = git .getRepository ().getConfig ();
55- config .setBoolean ("http" , null , "sslVerify" , false );
56- config .save ();
50+ disableSsl (git );
5751 } catch (IOException e ) {
5852 logger .error ("Unable to retrieve Git repository" , e );
5953 throw new RuntimeException (e );
@@ -71,7 +65,9 @@ public static void clearDirectory(Path folderPath) {
7165
7266 public static void exportTag (Path projectFolderPath ) {
7367 Path tagFolderPath = projectFolderPath .resolve ("tags" );
74- clearDirectory (tagFolderPath );
68+ if (tagFolderPath .toFile ().exists ()) {
69+ clearDirectory (tagFolderPath );
70+ }
7571
7672 try {
7773 Files .createDirectories (tagFolderPath );
@@ -82,7 +78,7 @@ public static void exportTag(Path projectFolderPath) {
8278 tagPaths .add (typesPath );
8379
8480 CompletableFuture <List <TagConfigurationModel >> cfTagModels =
85- tagProvider .getTagConfigsAsync (tagPaths , true , true );
81+ tagProvider .getTagConfigsAsync (tagPaths , true , true );
8682 List <TagConfigurationModel > tModels = cfTagModels .get ();
8783
8884 JsonObject json = TagUtilities .toJsonObject (tModels .get (0 ));
@@ -99,36 +95,45 @@ public static void exportTag(Path projectFolderPath) {
9995 }
10096
10197 public static void exportTheme (Path projectFolderPath ) {
102- try {
103- Path sessionPropsPath = projectFolderPath .resolve ("com.inductiveautomation.perspective" )
104- .resolve ("session-props" )
105- .resolve ("props.json" );
106- String content = Files .readString (sessionPropsPath );
107- Gson g = new Gson ();
108- JsonObject json = g .fromJson (content , JsonObject .class );
109- String theme = JsonUtilities .readString (json , "props.theme" , "light" );
110-
111- Path themesDir = context .getSystemManager ().getDataDir ().toPath ()
112- .resolve ("modules" )
113- .resolve ("com.inductiveautomation.perspective" )
114- .resolve ("themes" );
115-
116- Path themeFolder = themesDir .resolve (theme );
117- Path themeFile = themesDir .resolve (theme + ".css" );
118-
119- Path themeFolderPath = projectFolderPath .resolve ("themes" );
98+ Path themeFolderPath = projectFolderPath .resolve ("themes" );
99+ if (themeFolderPath .toFile ().exists ()) {
120100 clearDirectory (themeFolderPath );
121- Files .createDirectories (themeFolderPath );
122- FileUtils .copyDirectoryToDirectory (themeFolder .toFile (), themeFolderPath .toFile ());
123- Files .copy (themeFile , themeFolderPath .resolve (themeFile .getFileName ()));
101+ }
102+ try {
103+ Path perspectiveFolderPath = projectFolderPath .resolve ("com.inductiveautomation.perspective" );
104+
105+ if (perspectiveFolderPath .toFile ().exists ()) {
106+ Path sessionPropsPath = perspectiveFolderPath
107+ .resolve ("session-props" )
108+ .resolve ("props.json" );
109+ String content = Files .readString (sessionPropsPath );
110+ Gson g = new Gson ();
111+ JsonObject json = g .fromJson (content , JsonObject .class );
112+ String theme = JsonUtilities .readString (json , "props.theme" , "light" );
113+
114+ Path themesDir = context .getSystemManager ().getDataDir ().toPath ()
115+ .resolve ("modules" )
116+ .resolve ("com.inductiveautomation.perspective" )
117+ .resolve ("themes" );
118+
119+ Path themeFolder = themesDir .resolve (theme );
120+ Path themeFile = themesDir .resolve (theme + ".css" );
121+
122+ Files .createDirectories (themeFolderPath );
123+ FileUtils .copyDirectoryToDirectory (themeFolder .toFile (), themeFolderPath .toFile ());
124+ Files .copy (themeFile , themeFolderPath .resolve (themeFile .getFileName ()));
125+ }
124126 } catch (IOException e ) {
125127 logger .error (e .toString (), e );
126128 }
127129 }
128130
129131 public static void exportImages (Path projectFolderPath ) {
130132 Path imageFolderPath = projectFolderPath .resolve ("images" );
131- clearDirectory (imageFolderPath );
133+ if (imageFolderPath .toFile ().exists ()) {
134+ clearDirectory (imageFolderPath );
135+ }
136+
132137 try {
133138 Files .createDirectories (imageFolderPath );
134139 } catch (IOException e ) {
@@ -151,7 +156,7 @@ public static void saveFolderImage(Path folderPath, String directory) {
151156 } else {
152157 byte [] data = imageManager .getImage (path ).getBytes (ImageRecord .Data );
153158 try {
154- Files .write (folderPath .resolve (path ), data );
159+ Files .write (folderPath .resolve (path ), data );
155160 } catch (IOException e ) {
156161 logger .error (e .toString (), e );
157162 }
@@ -160,7 +165,7 @@ public static void saveFolderImage(Path folderPath, String directory) {
160165 }
161166
162167 public static void setAuthentication (TransportCommand <?, ?> command , String projectName , String userName )
163- throws Exception {
168+ throws Exception {
164169 GitProjectsConfigRecord gitProjectsConfigRecord = getGitProjectConfigRecord (projectName );
165170 GitReposUsersRecord user = getGitReposUserRecord (gitProjectsConfigRecord , userName );
166171
@@ -173,7 +178,7 @@ public static void setAuthentication(TransportCommand<?, ?> command, String proj
173178
174179 public static GitProjectsConfigRecord getGitProjectConfigRecord (String projectName ) throws Exception {
175180 SQuery <GitProjectsConfigRecord > projectQuery = new SQuery <>(GitProjectsConfigRecord .META )
176- .eq (GitProjectsConfigRecord .ProjectName , projectName );
181+ .eq (GitProjectsConfigRecord .ProjectName , projectName );
177182 GitProjectsConfigRecord gitProjectsConfigRecord = context .getPersistenceInterface ().queryOne (projectQuery );
178183
179184 if (gitProjectsConfigRecord == null ) {
@@ -186,8 +191,8 @@ public static GitProjectsConfigRecord getGitProjectConfigRecord(String projectNa
186191 public static GitReposUsersRecord getGitReposUserRecord (GitProjectsConfigRecord gitProjectsConfigRecord ,
187192 String userName ) throws Exception {
188193 SQuery <GitReposUsersRecord > userQuery = new SQuery <>(GitReposUsersRecord .META )
189- .eq (GitReposUsersRecord .ProjectId , gitProjectsConfigRecord .getId ())
190- .eq (GitReposUsersRecord .IgnitionUser , userName );
194+ .eq (GitReposUsersRecord .ProjectId , gitProjectsConfigRecord .getId ())
195+ .eq (GitReposUsersRecord .IgnitionUser , userName );
191196 GitReposUsersRecord user = context .getPersistenceInterface ().queryOne (userQuery );
192197
193198 if (user == null ) {
@@ -244,25 +249,32 @@ public static boolean hasActor(String resource) {
244249
245250 public static String getActor (String projectName , String path ) {
246251 ProjectManager projectManager = context .getProjectManager ();
247- RuntimeProject project =projectManager .getProject (projectName ).get ();
252+ RuntimeProject project = projectManager .getProject (projectName ).get ();
248253
249254 ProjectResource projectResource = project .getResource (getResourcePath (path )).get ();
250- String actor = LastModification .of (projectResource ).map (LastModification ::getActor ).orElse ("unknown" );;
255+ String actor = LastModification .of (projectResource ).map (LastModification ::getActor ).orElse ("unknown" );
256+ ;
251257
252258 return actor ;
253259 }
254260
255261
256- public static ResourcePath getResourcePath (String resourcePath ){
262+ public static ResourcePath getResourcePath (String resourcePath ) {
257263 String moduleId = "" ;
258264 String typeId = "" ;
259265 String resource = "" ;
260266 String [] paths = resourcePath .split ("/" );
261267
262- if (paths .length >0 ) moduleId = paths [0 ];
263- if (paths .length >1 ) typeId = paths [1 ];
264- if (paths .length >2 ) resource = resourcePath .replace (moduleId + "/" + typeId + "/" , "" );
268+ if (paths .length > 0 ) moduleId = paths [0 ];
269+ if (paths .length > 1 ) typeId = paths [1 ];
270+ if (paths .length > 2 ) resource = resourcePath .replace (moduleId + "/" + typeId + "/" , "" );
265271
266272 return new ResourcePath (new ResourceType (moduleId , typeId ), resource );
267273 }
274+
275+ public static void disableSsl (Git git ) throws IOException {
276+ StoredConfig config = git .getRepository ().getConfig ();
277+ config .setBoolean ("http" , null , "sslVerify" , false );
278+ config .save ();
279+ }
268280}
0 commit comments