-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e902f33
Showing
6 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>me.blueysh</groupId> | ||
<artifactId>appman</artifactId> | ||
<version>1.0</version> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>jitpack.io</id> | ||
<url>https://jitpack.io</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.blueysh</groupId> | ||
<artifactId>lineman</artifactId> | ||
<version>release</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.blueysh</groupId> | ||
<artifactId>sap4j</artifactId> | ||
<version>stable-1.1b</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20230618</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.3.0</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<mainClass>me.blueysh.appman.Main</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.4.1</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<relocations> | ||
<relocation> | ||
<pattern>com.seailz.discordjar</pattern> | ||
<shadedPattern>me.blueysh</shadedPattern> | ||
</relocation> | ||
</relocations> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.blueysh.appman; | ||
|
||
import lineman.Lineman; | ||
import lineman.Logger; | ||
import me.blueysh.appman.command.InstallCommand; | ||
import me.blueysh.appman.command.UninstallCommand; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
if (args.length < 1) { | ||
System.out.println(Logger.Color.RED + "Usage: appman [i(nstall) | u(ninstall)] [appId]"); | ||
return; | ||
} | ||
|
||
Lineman app = Lineman.create(); | ||
app.addStartupHook(new Thread(() -> app.getLogger().println(Logger.Color.WHITE + "appman v1.0"))); | ||
app.addCommand("install", new InstallCommand()); | ||
app.addCommand("i", new InstallCommand()); | ||
app.addCommand("uninstall", new UninstallCommand()); | ||
app.addCommand("u", new UninstallCommand()); | ||
app.run(args); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/me/blueysh/appman/command/InstallCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package me.blueysh.appman.command; | ||
|
||
import lineman.Command; | ||
import lineman.Lineman; | ||
import lineman.Logger; | ||
import me.blueysh.appman.util.DataGetter; | ||
import org.json.JSONObject; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Scanner; | ||
|
||
public class InstallCommand implements Command { | ||
@Override | ||
public void run(Lineman lineman, String[] strings) { | ||
if (strings.length < 1) { | ||
lineman.getLogger().err("No app id provided. Usage: appman install [app id]"); | ||
return; | ||
} | ||
|
||
String appId = strings[0]; | ||
Logger logger = lineman.getLogger(); | ||
logger.println(Logger.Color.GREEN + "INSTALL " + Logger.Color.RESET + "'" + appId + "'"); | ||
logger.info("Getting info for '%s'...".formatted(appId)); | ||
|
||
JSONObject data = DataGetter.getData(lineman, appId); | ||
|
||
if (data == null) { | ||
logger.err("Data for '%s' could not be downloaded, or none exists.".formatted(appId)); | ||
return; | ||
} | ||
|
||
String appName = data.getString("appName"); | ||
String developerName = data.getString("developerName"); | ||
JSONObject versions = data.getJSONObject("versions"); | ||
String intelLink = getLink(versions, "intel"); | ||
String siliconLink = getLink(versions, "silicon"); | ||
|
||
logger.success("Found '%s' by '%s'.".formatted(appName, developerName)); | ||
logger.info("Versions available:"); | ||
|
||
if (intelLink != null) { | ||
logger.info(" - intel"); | ||
} | ||
if (siliconLink != null) { | ||
logger.info(" - silicon"); | ||
} | ||
|
||
logger.println("Select version: "); | ||
Scanner input = new Scanner(System.in); | ||
String targetVersion = input.nextLine().toLowerCase(); | ||
|
||
String targetVersionLink; | ||
|
||
switch (targetVersion) { | ||
case "intel" -> { | ||
if (intelLink == null) { | ||
logger.err("'%s' has no Intel version.".formatted(appId)); | ||
return; | ||
} | ||
logger.info("Installing Intel version..."); | ||
targetVersionLink = intelLink; | ||
} | ||
case "silicon" -> { | ||
if (siliconLink == null) { | ||
logger.err("'%s' has no Apple Silicon version.".formatted(appId)); | ||
return; | ||
} | ||
logger.info("Installing Apple Silicon version..."); | ||
targetVersionLink = siliconLink; | ||
} | ||
default -> { | ||
logger.err("Invalid version '%s'.".formatted(targetVersion)); | ||
return; | ||
} | ||
} | ||
|
||
try { | ||
HttpsURLConnection fileDownloadConn = (HttpsURLConnection) new URL(targetVersionLink).openConnection(); | ||
byte[] fileData = fileDownloadConn.getInputStream().readAllBytes(); | ||
String fileName = "/Applications/appman_install-%s.%s".formatted(appId, data.getString("downloadsAs")); | ||
Files.write(Path.of(fileName), fileData); | ||
|
||
new Thread(() -> { | ||
try { | ||
Process process = Runtime.getRuntime().exec("open " + fileName); | ||
process.waitFor(); | ||
Files.deleteIfExists(Path.of(fileName)); | ||
logger.success("'%s' has been installed.".formatted(appName)); | ||
} catch (Exception e) { | ||
logger.severe("An unexpected error occurred when downloading the target file."); | ||
} | ||
}).start(); | ||
} catch (Exception e) { | ||
logger.severe("An unexpected error occurred when downloading the target file."); | ||
} | ||
} | ||
|
||
private String getLink(JSONObject versions, String version) { | ||
return versions.has(version) && !versions.get(version).toString().equalsIgnoreCase("null") ? versions.getString(version) : null; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/me/blueysh/appman/command/UninstallCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.blueysh.appman.command; | ||
|
||
import lineman.Command; | ||
import lineman.Lineman; | ||
import lineman.Logger; | ||
import me.blueysh.appman.util.DataGetter; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.io.File; | ||
|
||
public class UninstallCommand implements Command { | ||
@Override | ||
public void run(Lineman lineman, String[] strings) { | ||
if (strings.length < 1) { | ||
lineman.getLogger().err("No app id provided. Usage: appman uninstall [app id]"); | ||
return; | ||
} | ||
String appId = strings[0]; | ||
Logger logger = lineman.getLogger(); | ||
logger.println(Logger.Color.RED + "UNINSTALL " + Logger.Color.RESET + "'" + appId + "'"); | ||
logger.info("Getting info for '%s'...".formatted(appId)); | ||
|
||
JSONObject data = DataGetter.getData(lineman, appId); | ||
|
||
if (data == null) { | ||
logger.err("Data for '%s' could not be downloaded, or none exists.".formatted(appId)); | ||
return; | ||
} | ||
|
||
String appName = data.getString("appName"); | ||
String appSupportDirName = data.getString("appSupportDirName"); | ||
|
||
logger.success("Found '%s' by '%s'.".formatted(appName, data.getString("developerName"))); | ||
|
||
try { | ||
String appPath = "/Applications/%s.app".formatted(appName); | ||
String appSupportPath = System.getProperty("user.home") + "/Library/Application Support/%s".formatted(appSupportDirName); | ||
|
||
// Delete the app directory | ||
deleteDirectory(appPath); | ||
// Delete the app support directory | ||
deleteDirectory(appSupportPath); | ||
|
||
logger.success("'%s' has been uninstalled.".formatted(appName)); | ||
} catch (IOException e) { | ||
logger.severe("An unexpected exception was encountered: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private void deleteDirectory(String path) throws IOException { | ||
Path directoryPath = Path.of(path); | ||
if (Files.exists(directoryPath)) { | ||
Files.walk(directoryPath) | ||
.map(Path::toFile) | ||
.sorted((f1, f2) -> -f1.compareTo(f2)) | ||
.forEach(File::delete); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.blueysh.appman.util; | ||
|
||
import lineman.Lineman; | ||
import org.json.JSONObject; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
|
||
public class DataGetter { | ||
public static JSONObject getData(Lineman lineman, String appId) { | ||
try { | ||
HttpsURLConnection conn = (HttpsURLConnection) new URL("https://assets.blueysh.me/lib/appman/app/%s.json".formatted(appId)).openConnection(); | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { | ||
StringBuilder rawData = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
rawData.append(line); | ||
} | ||
return new JSONObject(rawData.toString()); | ||
} | ||
} catch (IOException e) { | ||
// May handle the exception appropriately later, e.g., log it or throw it | ||
// lineman.getLogger().severe("An unexpected error was encountered. No changes made."); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import me.blueysh.appman.Main; | ||
|
||
import java.util.Scanner; | ||
|
||
public class TestingInterface { | ||
public static void main(String[] args) { | ||
|
||
System.out.print("Input args: "); | ||
Scanner scan = new Scanner(System.in); | ||
|
||
Main.main(scan.nextLine().split(" ")); | ||
} | ||
} |