]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java
a3b409027a0c314354a3e172eac62163094b8be0
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / BaselineCreatorApplication.java
1 package org.simantics;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.time.LocalDateTime;
9 import java.time.format.DateTimeFormatter;
10
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.equinox.app.IApplication;
18 import org.eclipse.equinox.app.IApplicationContext;
19 import org.eclipse.osgi.service.datalocation.Location;
20 import org.simantics.application.arguments.Arguments;
21 import org.simantics.application.arguments.IArgumentFactory;
22 import org.simantics.application.arguments.IArgumentFactory.StringArgumentFactory;
23 import org.simantics.application.arguments.IArgumentFactory.NoValueArgumentFactory;
24 import org.simantics.application.arguments.IArguments;
25 import org.simantics.application.arguments.SimanticsArguments;
26 import org.simantics.internal.Activator;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Tuukka Lehtonen
32  * @since 1.34.0
33  */
34 public class BaselineCreatorApplication implements IApplication {
35
36         private static final Logger LOGGER = LoggerFactory.getLogger(BaselineCreatorApplication.class);
37
38         private static final IArgumentFactory<String> OUTPUT = new StringArgumentFactory("-o");
39         private static final IArgumentFactory<Boolean> VERBOSE = new NoValueArgumentFactory("-v");
40
41         IArgumentFactory<?>[] accepted = {
42                         SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS,
43                         SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL,
44                         SimanticsArguments.DISABLE_INDEX,
45                         SimanticsArguments.DATABASE_ID,
46                         OUTPUT,
47                         VERBOSE,
48         };
49
50         private static String currentLocalDateTimeStamp() {
51                 return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
52         }
53
54         private static Path constructOutputPath(Path workspace, IArguments parsedArgs) {
55                 if (parsedArgs.contains(OUTPUT)) {
56                         return workspace.resolve(parsedArgs.get(OUTPUT));
57                 } else {
58                         return workspace.resolve(workspace.getFileName().toString() + "-" + currentLocalDateTimeStamp() + ".zip");
59                 }
60         }
61
62         private static Path getInstanceLocation() throws CoreException, IOException {
63                 Location l = Platform.getInstanceLocation();
64                 if (l == null || l.isReadOnly())
65                         throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
66                                         "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
67
68                 URL workspaceUrl = l.getURL();
69                 Path workspacePath = new File(workspaceUrl.getPath()).toPath();
70                 Files.createDirectories(workspacePath);
71                 return workspacePath;
72         }
73
74         @Override
75         public Object start(IApplicationContext context) throws Exception {
76                 try {
77                         Path workspace = getInstanceLocation();
78
79                         String[] args = (String[]) context.getArguments().get("application.args");
80                         IArguments parsedArgs = Arguments.parse(args, accepted);
81
82                         Path output = constructOutputPath(workspace, parsedArgs);
83
84                         // Create database and indexes
85                         IProgressMonitor progress = parsedArgs.contains(VERBOSE)
86                                         ? new TimingProgressMonitor()
87                                         : new NullProgressMonitor();
88                         Simantics.startUpHeadless(parsedArgs, progress);
89                         Simantics.shutdown(progress);
90
91                         // Create the baseline package file
92                         Path actualOutput = DatabaseBaselines.packageBaseline(workspace, output);
93                         System.out.println("OK " + actualOutput.toAbsolutePath());
94
95                         return IApplication.EXIT_OK;
96                 } catch (Exception e) {
97                         LOGGER.error("Baseline creation failed.", e);
98                         throw (Exception) e;
99                 } finally {
100                         System.exit(0);
101                 }
102         }
103
104         @Override
105         public void stop() {
106         }
107
108 }