Skip to content

Commit

Permalink
Creates cache directory correctly for dev setups
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Nov 20, 2024
1 parent 78c1e6c commit 1380e79
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
.run/XeresApplication.run.xml
/scripts/bot/config.json
/scripts/bot/avatar.png

/cache/
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

package io.xeres.app.configuration;

import io.xeres.app.util.DevUtils;
import io.xeres.common.AppName;
import net.harawata.appdirs.AppDirsFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -61,7 +63,16 @@ public String getCacheDir()
return null;
}

cacheDir = getCacheDirFromPortableFileLocation();
if (environment.acceptsProfiles(Profiles.of("dev")))
{
cacheDir = DevUtils.getDirFromDevelopmentSetup("cache");
}

if (cacheDir == null)
{
cacheDir = getCacheDirFromPortableFileLocation();
}

if (cacheDir == null)
{
cacheDir = getCacheDirFromNativePlatform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.xeres.app.configuration;

import io.xeres.app.util.DevUtils;
import io.xeres.common.AppName;
import io.xeres.common.properties.StartupProperties;
import net.harawata.appdirs.AppDirsFactory;
Expand All @@ -30,7 +31,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import static io.xeres.common.properties.StartupProperties.Property.DATA_DIR;
Expand Down Expand Up @@ -74,7 +74,7 @@ public String getDataDir()
dataDir = getDataDirFromArgs();
if (dataDir == null && environment.acceptsProfiles(Profiles.of("dev")))
{
dataDir = getDataDirFromDevelopmentSetup();
dataDir = DevUtils.getDirFromDevelopmentSetup(LOCAL_DATA);
}

if (dataDir == null)
Expand Down Expand Up @@ -123,23 +123,4 @@ private static String getDataDirFromNativePlatform()
var appDirs = AppDirsFactory.getInstance();
return appDirs.getUserDataDir(AppName.NAME, null, null, true);
}

private static String getDataDirFromDevelopmentSetup()
{
// Find out if we're running from rootProject, which means
// we have an 'app' folder in there.
// We use a relative directory because currentDir is not supposed
// to change, and it looks clearer.
var appDir = Path.of("app");
if (Files.exists(appDir))
{
return Path.of(".", LOCAL_DATA).toString();
}
appDir = Path.of("..", "app");
if (Files.exists(appDir))
{
return Path.of("..", LOCAL_DATA).toString();
}
throw new IllegalStateException("Unable to find/create data directory. Current directory must be the project's root directory or 'app'. It is " + Paths.get("").toAbsolutePath());
}
}
51 changes: 51 additions & 0 deletions app/src/main/java/io/xeres/app/util/DevUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.util;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public final class DevUtils
{
private DevUtils()
{
throw new UnsupportedOperationException("Utility class");
}

public static String getDirFromDevelopmentSetup(String directory)
{
// Find out if we're running from rootProject, which means
// we have an 'app' folder in there.
// We use a relative directory because currentDir is not supposed
// to change, and it looks clearer.
var appDir = Path.of("app");
if (Files.exists(appDir))
{
return Path.of(".", directory).toString();
}
appDir = Path.of("..", "app");
if (Files.exists(appDir))
{
return Path.of("..", directory).toString();
}
throw new IllegalStateException("Unable to find/create directory. Current directory must be the project's root directory or 'app'. It is " + Paths.get("").toAbsolutePath());
}
}

0 comments on commit 1380e79

Please sign in to comment.