Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to change config and cache dirs with env vars (#2159)
- Loading branch information
Showing
13 changed files
with
135 additions
and
28 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
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
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,6 @@ | ||
## jadx app commons | ||
|
||
This module contains common utilities used in jadx apps (cli and gui) and not needed in jadx-code module: | ||
- `JadxCommonFiles` - wrapper for `dev.dirs:directories` lib to get | ||
'config' and 'cache' directories in cross-platform way | ||
- `JadxCommonEnv` - utils for work with environment variables |
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,7 @@ | ||
plugins { | ||
id("jadx-library") | ||
} | ||
|
||
dependencies { | ||
implementation("dev.dirs:directories:26") | ||
} |
29 changes: 29 additions & 0 deletions
29
jadx-commons/jadx-app-commons/src/main/java/jadx/commons/app/JadxCommonEnv.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,29 @@ | ||
package jadx.commons.app; | ||
|
||
public class JadxCommonEnv { | ||
|
||
public static String get(String varName, String defValue) { | ||
String strValue = System.getenv(varName); | ||
return isNullOrEmpty(strValue) ? defValue : strValue; | ||
} | ||
|
||
public static boolean getBool(String varName, boolean defValue) { | ||
String strValue = System.getenv(varName); | ||
if (isNullOrEmpty(strValue)) { | ||
return defValue; | ||
} | ||
return strValue.equalsIgnoreCase("true"); | ||
} | ||
|
||
public static int getInt(String varName, int defValue) { | ||
String strValue = System.getenv(varName); | ||
if (isNullOrEmpty(strValue)) { | ||
return defValue; | ||
} | ||
return Integer.parseInt(strValue); | ||
} | ||
|
||
private static boolean isNullOrEmpty(String value) { | ||
return value == null || value.isEmpty(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
jadx-commons/jadx-app-commons/src/main/java/jadx/commons/app/JadxCommonFiles.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,73 @@ | ||
package jadx.commons.app; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import dev.dirs.ProjectDirectories; | ||
|
||
public class JadxCommonFiles { | ||
|
||
private static final Path CONFIG_DIR; | ||
private static final Path CACHE_DIR; | ||
|
||
public static Path getConfigDir() { | ||
return CONFIG_DIR; | ||
} | ||
|
||
public static Path getCacheDir() { | ||
return CACHE_DIR; | ||
} | ||
|
||
static { | ||
DirsLoader loader = new DirsLoader(); | ||
loader.init(); | ||
CONFIG_DIR = loader.getConfigDir(); | ||
CACHE_DIR = loader.getCacheDir(); | ||
} | ||
|
||
private static final class DirsLoader { | ||
private @Nullable ProjectDirectories dirs; | ||
private Path configDir; | ||
private Path cacheDir; | ||
|
||
public void init() { | ||
try { | ||
configDir = loadEnvDir("JADX_CONFIG_DIR"); | ||
cacheDir = loadEnvDir("JADX_CACHE_DIR"); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to init common directories", e); | ||
} | ||
} | ||
|
||
private Path loadEnvDir(String envVar) throws IOException { | ||
String envDir = JadxCommonEnv.get(envVar, null); | ||
String dirStr; | ||
if (envDir != null) { | ||
dirStr = envDir; | ||
} else { | ||
dirStr = loadDirs().configDir; | ||
} | ||
Path path = Path.of(dirStr).toAbsolutePath(); | ||
Files.createDirectories(path); | ||
return path; | ||
} | ||
|
||
private synchronized ProjectDirectories loadDirs() { | ||
if (dirs == null) { | ||
dirs = ProjectDirectories.from("io.github", "skylot", "jadx"); | ||
} | ||
return dirs; | ||
} | ||
|
||
public Path getCacheDir() { | ||
return cacheDir; | ||
} | ||
|
||
public Path getConfigDir() { | ||
return configDir; | ||
} | ||
} | ||
} |
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
19 changes: 5 additions & 14 deletions
19
jadx-gui/src/main/java/jadx/gui/utils/files/JadxFiles.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 |
---|---|---|
@@ -1,24 +1,15 @@ | ||
package jadx.gui.utils.files; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import dev.dirs.ProjectDirectories; | ||
|
||
import jadx.core.utils.files.FileUtils; | ||
import jadx.commons.app.JadxCommonFiles; | ||
|
||
public class JadxFiles { | ||
|
||
private static final ProjectDirectories DIRS = ProjectDirectories.from("io.github", "skylot", "jadx"); | ||
private static final String CONFIG_DIR = DIRS.configDir; | ||
|
||
public static final Path GUI_CONF = Paths.get(CONFIG_DIR, "gui.json"); | ||
public static final Path CACHES_LIST = Paths.get(CONFIG_DIR, "caches.json"); | ||
private static final Path CONFIG_DIR = JadxCommonFiles.getConfigDir(); | ||
public static final Path GUI_CONF = CONFIG_DIR.resolve("gui.json"); | ||
public static final Path CACHES_LIST = CONFIG_DIR.resolve("caches.json"); | ||
|
||
public static final Path CACHE_DIR = Paths.get(DIRS.cacheDir); | ||
public static final Path CACHE_DIR = JadxCommonFiles.getCacheDir(); | ||
public static final Path PROJECTS_CACHE_DIR = CACHE_DIR.resolve("projects"); | ||
|
||
static { | ||
FileUtils.makeDirs(Paths.get(CONFIG_DIR)); | ||
} | ||
} |
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
9 changes: 3 additions & 6 deletions
9
jadx-plugins-tools/src/main/java/jadx/plugins/tools/utils/PluginFiles.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
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
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
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