Skip to content

Commit

Permalink
Improve resource handling
Browse files Browse the repository at this point in the history
Improve resource handling for local files, fixing errors in some situations. Additionally fixes the version file not being accessible in distributions.

Include the git commit hash in the about dialog.

Normalize file endings for FXML files.
  • Loading branch information
AnonymousHacker1279 committed Jan 11, 2025
1 parent e913c4b commit d705ba4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.fxml text eol=lf
23 changes: 18 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ version '1.0.1'

apply from: 'optimization_utils.gradle'

def getGitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}

java.toolchain.languageVersion = JavaLanguageVersion.of(23)

repositories {
Expand Down Expand Up @@ -52,11 +61,15 @@ run {
environment 'RUNNING_IN_IDE', 'true'
}

processResources {
filesMatching('**/VERSION') {
filter {
it.replace('${version}', version)
}
tasks.named('processResources', ProcessResources).configure {
LinkedHashMap<String, String> replaceProperties = [
version: version, commit: getGitHash()
]

inputs.properties replaceProperties

filesMatching(['**/app.properties']) {
expand replaceProperties + [project: project]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import tech.anonymoushacker1279.marshallcodeampinterface.midi.IOMIDIDevice;

import javax.sound.midi.MidiUnavailableException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Properties;

public class CODEInterfaceApplication extends Application {

Expand All @@ -37,7 +38,8 @@ public class CODEInterfaceApplication extends Application {

public static final Logger LOGGER = LogManager.getLogger();

public static Semver APP_VERSION;
public static Semver APP_VERSION = Semver.ZERO;
public static String COMMIT = "Unknown";

public static Stage MAIN_STAGE;

Expand Down Expand Up @@ -114,12 +116,14 @@ private static void initializeDevices() {
}

public static void main(String[] args) {
// Set the application version from the VERSION file
try (FileInputStream fis = new FileInputStream(Objects.requireNonNull(CODEInterfaceApplication.class.getResource("VERSION")).getFile())) {
APP_VERSION = new Semver(new String(fis.readAllBytes()).trim());
} catch (IOException e) {
LOGGER.error("Failed to load application version", e);
APP_VERSION = null;
try {
InputStream url = CODEInterfaceApplication.class.getResourceAsStream("app.properties");
Properties properties = new Properties();
properties.load(url);
APP_VERSION = new Semver(properties.getProperty("version"));
COMMIT = properties.getProperty("commit");
} catch (IOException | NullPointerException e) {
LOGGER.error("Failed to load version information from app.properties file", e);
}

launch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import tech.anonymoushacker1279.marshallcodeampinterface.CODEInterfaceApplication;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public record AmpModel(String ampName, int familyId, int modelId, int deviceId, String ampImage) {
Expand All @@ -18,15 +16,15 @@ public record AmpModel(String ampName, int familyId, int modelId, int deviceId,

public static void load() {
Gson gson = new Gson();
URL url = CODEInterfaceApplication.class.getResource("amp_models.json");
InputStream inputStream = CODEInterfaceApplication.class.getResourceAsStream("amp_models.json");

if (url != null) {
if (inputStream != null) {
CODEInterfaceApplication.LOGGER.debug("Loading amp model information");

try (Reader reader = Files.newBufferedReader(Paths.get(url.toURI()))) {
try (Reader reader = new InputStreamReader(inputStream)) {
ALL_MODELS = gson.fromJson(reader, TypeToken.getParameterized(List.class, AmpModel.class).getType());
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
} catch (IOException e) {
CODEInterfaceApplication.LOGGER.error("Failed to load amp models", e);
}
} else {
throw new RuntimeException("Failed to load amp models");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class AboutDialogController implements Initializable {
@FXML
private Text versionText;
@FXML
private Text commitText;
@FXML
private Button githubButton;

private HostServices hostServices;
Expand All @@ -39,6 +41,7 @@ public void setHostServices(HostServices hostServices) {
@Override
public void initialize(URL location, ResourceBundle resources) {
versionText.setText("Version: " + CODEInterfaceApplication.APP_VERSION);
commitText.setText("Commit: " + CODEInterfaceApplication.COMMIT);
nameHyperlink.setOnAction(event -> hostServices.showDocument("https://github.com/AnonymousHacker1279"));
githubButton.setOnAction(event -> hostServices.showDocument("https://github.com/AnonymousHacker1279/MarshallCodeAmpInterface"));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version=${version}
commit=${commit}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
<Font name="System Bold Italic" size="16.0" />
</font>
</Text>
<Text fx:id="versionText" layoutX="32.0" layoutY="205.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Version: X" />
<Text fx:id="versionText" layoutX="32.0" layoutY="198.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Version: X" />
<Button fx:id="githubButton" layoutX="495.0" layoutY="183.0" mnemonicParsing="false" text="GitHub" />
<Text fx:id="commitText" layoutX="32.0" layoutY="220.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Commit: X" />
</children>
</AnchorPane>

0 comments on commit d705ba4

Please sign in to comment.