66import joptsimple .OptionSet ;
77import joptsimple .OptionSpec ;
88import joptsimple .util .PathConverter ;
9+ import org .mangorage .installer .core .UpdateChecker ;
910import org .mangorage .installer .core .data .*;
1011import java .io .*;
1112import java .lang .reflect .Method ;
@@ -29,23 +30,31 @@ public class Installer {
2930 private static final Path LIBRARIES_PATH = Path .of ("libraries/" ).toAbsolutePath ();
3031
3132 public static void main (String [] args ) {
32- System . out .println ("Starting Installer..." );
33- System . out .println ("Arguments Supplied: " + Arrays .toString (args ));
33+ org . mangorage . installer . core . LogUtil .println ("Starting Installer..." );
34+ org . mangorage . installer . core . LogUtil .println ("Arguments Supplied: " + Arrays .toString (args ));
3435
3536 OptionParser parser = new OptionParser ();
3637
37- OptionSpec <Void > launchArg = parser
38+ final OptionSpec <Void > launchArg = parser
3839 .accepts ("launch" , "Whether or not to launch the program that will be installed/updated" );
3940
40- OptionSpec <Path > manualJar = parser
41+ final OptionSpec <Path > manualJar = parser
4142 .accepts ("manualJar" , "Provide a path to the jar" )
4243 .withRequiredArg ()
4344 .withValuesSeparatedBy (";" )
4445 .withValuesConvertedBy (new PathConverter ());
4546
47+ final OptionSpec <Integer > checkUpdates = parser
48+ .accepts ("checkUpdates" , "Automatically check for updates for packages which want to be checked" )
49+ .withRequiredArg ()
50+ .ofType (Integer .TYPE );
51+
4652 var options = parser .parse (args );
4753
48- List <File > jars = options .has ("manualJar" ) ? getManualJars (options , manualJar ) : processPackages ();
54+ List <File > jars = options .has ("manualJar" ) ? getManualJars (options , manualJar ) : processPackages (
55+ options .has (checkUpdates ) && options .has ("launch" ),
56+ options .has (checkUpdates ) ? options .valueOf (checkUpdates ) : 0
57+ );
4958
5059 if (jars .isEmpty ()) {
5160 throw new IllegalStateException ("No JARs found to process!" );
@@ -55,10 +64,10 @@ public static void main(String[] args) {
5564 handleDependencies (dependencies );
5665
5766 if (options .has ("launch" )) {
58- System . out .println ("Finished running installer..." );
67+ org . mangorage . installer . core . LogUtil .println ("Finished running installer..." );
5968 launchJar (jars , args );
6069 } else {
61- System . out .println ("Finished running installer..." );
70+ org . mangorage . installer . core . LogUtil .println ("Finished running installer..." );
6271 System .exit (0 );
6372 }
6473 }
@@ -70,8 +79,8 @@ private static List<File> getManualJars(OptionSet options, OptionSpec<Path> manu
7079 .toList ();
7180 }
7281
73- private static List <File > processPackages () {
74- System . out .println ("Processing installer/packages.json" );
82+ private static List <File > processPackages (final boolean checkUpdates , final int updateFreq ) {
83+ org . mangorage . installer . core . LogUtil .println ("Processing installer/packages.json" );
7584 File file = new File ("installer/packages.json" );
7685 if (!file .exists ()) throw new IllegalStateException ("packages.json not found!" );
7786
@@ -80,6 +89,7 @@ private static List<File> processPackages() {
8089
8190 try (var reader = new FileReader (file )) {
8291 Packages packages = GSON .fromJson (reader , Packages .class );
92+ if (checkUpdates ) UpdateChecker .startChecker (packages , updateFreq );
8393 Map <String , String > newVersions = new HashMap <>();
8494
8595 for (Dependency dependency : packages .packages ()) {
@@ -125,7 +135,7 @@ private static File handleDependency(Dependency dependency, Map<String, String>
125135 return new File (destination , dependency .target ());
126136 }
127137
128- System . out .println ("Installing/updating " + dependency .target ());
138+ org . mangorage . installer . core . LogUtil .println ("Installing/updating " + dependency .target ());
129139 return Util .downloadTo (maven , latestVersion , destination + "/" + dependency .target ());
130140 }
131141
@@ -134,13 +144,13 @@ private static String fetchLatestVersion(Maven maven, String defaultVersion) {
134144 try {
135145 return Util .parseLatestVersion (future .get (10 , TimeUnit .SECONDS ), defaultVersion );
136146 } catch (Exception e ) {
137- System . out .println ("Failed to get metadata, using default version: " + defaultVersion );
147+ org . mangorage . installer . core . LogUtil .println ("Failed to get metadata, using default version: " + defaultVersion );
138148 return defaultVersion ;
139149 }
140150 }
141151
142152 private static List <Dependency > extractDependencies (List <File > jars ) {
143- System . out .println ("Extracting dependencies from JARs" );
153+ org . mangorage . installer . core . LogUtil .println ("Extracting dependencies from JARs" );
144154 List <Dependency > dependencies = new ArrayList <>();
145155
146156 for (File jar : jars ) {
@@ -150,7 +160,7 @@ private static List<Dependency> extractDependencies(List<File> jars) {
150160 try (var reader = new InputStreamReader (jarFile .getInputStream (entry ))) {
151161 List <Dependency > extracted = GSON .fromJson (reader , Dependencies .class ).dependencies ();
152162 dependencies .addAll (extracted );
153- extracted .forEach (dep -> System . out .println ("Found dependency: " + dep ));
163+ extracted .forEach (dep -> org . mangorage . installer . core . LogUtil .println ("Found dependency: " + dep ));
154164 }
155165 }
156166 } catch (IOException e ) {
@@ -161,8 +171,8 @@ private static List<Dependency> extractDependencies(List<File> jars) {
161171 }
162172
163173 private static void handleDependencies (List <Dependency > dependencies ) {
164- System . out .println ("Handling dependencies..." );
165- System . out .println ("Skipping dependencies already present..." );
174+ org . mangorage . installer . core . LogUtil .println ("Handling dependencies..." );
175+ org . mangorage . installer . core . LogUtil .println ("Skipping dependencies already present..." );
166176 Set <String > installedJars = new HashSet <>();
167177 List <File > existingJars = getExistingLibraryJars ();
168178
@@ -171,7 +181,7 @@ private static void handleDependencies(List<Dependency> dependencies) {
171181 if (!existingJars .contains (new File (LIBRARIES_PATH .toString (), dep .target ()))) {
172182 Util .installUrl (dep .getDownloadURL (), LIBRARIES_PATH .toString (), true );
173183 } else {
174- System . out .println ("Skipped -> " + dep .target ());
184+ org . mangorage . installer . core . LogUtil .println ("Skipped -> " + dep .target ());
175185 }
176186 }
177187
@@ -186,7 +196,7 @@ private static List<File> getExistingLibraryJars() {
186196 private static void deleteUnusedDependencies (List <File > existingJars , Set <String > installedJars ) {
187197 existingJars .forEach (file -> {
188198 if (!installedJars .contains (file .getName ())) {
189- System . out .println ("Deleting unused dependency: " + file .getName ());
199+ org . mangorage . installer . core . LogUtil .println ("Deleting unused dependency: " + file .getName ());
190200 file .delete ();
191201 }
192202 });
@@ -201,7 +211,7 @@ public static String findMainClass(List<File> files) {
201211 ZipEntry entry ;
202212 while ((entry = zis .getNextEntry ()) != null ) {
203213 if (SERVICE_PATH .equals (entry .getName ())) {
204- System . out .println ("Found " + SERVICE_PATH + " in " + file .getName ());
214+ org . mangorage . installer . core . LogUtil .println ("Found " + SERVICE_PATH + " in " + file .getName ());
205215 return reader .lines ().collect (Collectors .joining ("\n " ));
206216 }
207217 }
@@ -213,10 +223,10 @@ public static String findMainClass(List<File> files) {
213223 }
214224
215225 public static void launchJar (List <File > jars , String [] args ) {
216- System . out .println ("Attempting to launch...." );
226+ org . mangorage . installer . core . LogUtil .println ("Attempting to launch...." );
217227 String mainClass = findMainClass (jars ).strip ();
218228 if (mainClass .isEmpty ()) {
219- System . out .println ("Could not find Valid Launch File from List of Jars..." );
229+ org . mangorage . installer . core . LogUtil .println ("Could not find Valid Launch File from List of Jars..." );
220230 return ;
221231 }
222232
0 commit comments