Skip to content

Commit

Permalink
Merge pull request #292 from ebkr/feature/275/custom-data-folder-loca…
Browse files Browse the repository at this point in the history
…tion

Feature/275/custom data folder location
  • Loading branch information
ebkr authored Sep 8, 2020
2 parents bb8ac4a + c50e64b commit 94aa4bc
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src-electron/main-process/ipcListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ ipcMain.on('get-is-portable', ()=>{
browserWindow.webContents.send('receive-is-portable', process.execPath.startsWith(os.tmpdir()));
})

ipcMain.on('restart', ()=>{
app.relaunch();
app.exit();
})

ipcMain.on('get-assets-path', ()=>{
if (process.env.PROD) {
browserWindow.webContents.send('receive-assets-path', global.__statics);
Expand Down
12 changes: 11 additions & 1 deletion src/components/settings-components/SettingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,23 @@
new SettingsRow(
'Locations',
'Browse profile folder',
'Open the directory where mods are stored for the current profile.',
'Change the directory where mods and profiles are stored.',
() => {
return Profile.getActiveProfile().getPathOfProfile();
},
'fa-door-open',
() => this.emitInvoke('BrowseProfileFolder')
),
new SettingsRow(
'Locations',
'Change data folder directory',
'Open the directory where mods are stored for the current profile. The folder will not be deleted, and existing profiles will not carry across.',
() => {
return PathResolver.ROOT;
},
'fa-folder-open',
() => this.emitInvoke('ChangeDataFolder')
),
new SettingsRow(
'Debugging',
'Copy LogOutput contents to clipboard',
Expand Down
25 changes: 25 additions & 0 deletions src/pages/Manager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,28 @@
this.view = 'installed';
}
changeDataFolder() {
const dir: string = PathResolver.ROOT;
ipcRenderer.once('receive-selection', (_sender: any, files: string[] | null) => {
if (files !== null && files.length === 1) {
const filesInDirectory = fs.readdirSync(files[0]);
if (filesInDirectory.length > 0 && files[0] !== PathResolver.APPDATA_DIR) {
this.showError(new R2Error("Selected directory is not empty", `Directory is not empty: ${files[0]}. Contains ${filesInDirectory.length} files.`, "Select an empty directory or create a new one."));
return;
} else {
ManagerSettings.getSingleton().setDataDirectory(files[0]);
ipcRenderer.send('restart');
}
}
});
ipcRenderer.send('open-dialog', {
title: 'Select a new folder to store r2modman data',
defaultPath: dir,
properties: ['openDirectory'],
buttonLabel: 'Select Data Folder'
});
}
handleSettingsCallbacks(invokedSetting: any) {
switch(invokedSetting) {
case "BrowseDataFolder":
Expand Down Expand Up @@ -1107,6 +1129,9 @@
case "ShowDependencyStrings":
this.showDependencyStrings = true;
break;
case "ChangeDataFolder":
this.changeDataFolder();
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Splash.vue
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ export default class Splash extends Vue {
async created() {
ipcRenderer.once('receive-appData-directory', (_sender: any, appData: string) => {
PathResolver.ROOT = path.join(appData, 'r2modmanPlus-local');
fs.ensureDirSync(PathResolver.ROOT);
PathResolver.APPDATA_DIR = path.join(appData, 'r2modmanPlus-local');
fs.ensureDirSync(PathResolver.APPDATA_DIR);
ThemeManager.apply();
Logger.Log(LogSeverity.INFO, `Starting manager on version ${ManagerInformation.VERSION.toString()}`);
ipcRenderer.once('receive-is-portable', (_sender: any, isPortable: boolean) => {
Expand Down
9 changes: 8 additions & 1 deletion src/r2mm/manager/ManagerSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export default class ManagerSettings {
public darkTheme: boolean = false;
public launchParameters: string = '';
public ignoreCache: boolean = false;
public dataDirectory: string = PathResolver.APPDATA_DIR;

public load(): R2Error | void {
configPath = path.join(PathResolver.ROOT, 'config');
configPath = path.join(PathResolver.APPDATA_DIR, 'config');
configFile = path.join(configPath, 'conf.yml');
fs.ensureDirSync(configPath);
if (fs.existsSync(configFile)) {
Expand All @@ -49,6 +50,7 @@ export default class ManagerSettings {
this.darkTheme = parsedYaml.darkTheme;
this.launchParameters = parsedYaml.launchParameters || '';
this.ignoreCache = parsedYaml.ignoreCache || false;
this.dataDirectory = parsedYaml.dataDirectory || PathResolver.APPDATA_DIR;
} catch(e) {
const err: Error = e;
return new YamlParseError(
Expand Down Expand Up @@ -139,4 +141,9 @@ export default class ManagerSettings {
this.ignoreCache = ignore;
return this.save();
}

public setDataDirectory(dataDirectory: string): R2Error | void {
this.dataDirectory = dataDirectory;
return this.save();
}
}
20 changes: 15 additions & 5 deletions src/r2mm/manager/PathResolver.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import ManagerSettings from './ManagerSettings';

export default class PathResolver {

private static _APPDATA_DIR: string = '';
private static _ROOT: string = '';
private static _MOD_ROOT: string = '';

static get ROOT(): string {
return PathResolver._ROOT;
static set APPDATA_DIR(appDataDir: string) {
PathResolver._APPDATA_DIR = appDataDir;
ManagerSettings.getSingleton().load();
PathResolver._ROOT = ManagerSettings.getSingleton().dataDirectory;
fs.ensureDirSync(PathResolver._ROOT);
PathResolver._MOD_ROOT = path.join(PathResolver._ROOT, 'mods');
}

static set ROOT(root: string) {
PathResolver._ROOT = root;
PathResolver._MOD_ROOT = path.join(root, 'mods');
static get ROOT(): string {
return PathResolver._ROOT;
}

static get MOD_ROOT(): string {
return PathResolver._MOD_ROOT;
}

static get APPDATA_DIR(): string {
return PathResolver._APPDATA_DIR;
}
}
6 changes: 3 additions & 3 deletions tests/test-setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import Profile from '../src/model/Profile';
export default class TestSetup {

public static setUp() {
PathResolver.ROOT = '__test_data__';
PathResolver.APPDATA_DIR = '__test_data__';
new Profile('Default');
}

public static tearDown() {
fs.emptyDirSync(PathResolver.ROOT);
fs.removeSync(PathResolver.ROOT);
fs.emptyDirSync(PathResolver.APPDATA_DIR);
fs.removeSync(PathResolver.APPDATA_DIR);
}

}
6 changes: 0 additions & 6 deletions tests/unit/manager/ManagerSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ describe('ManagerSettings', () => {
});
});

it('Ensure file is created on construction', () => {
expect(fs.existsSync(path.join(PathResolver.ROOT, 'config', 'conf.yml'))).equals(false);
ManagerSettings.getSingleton().load();
expect(fs.existsSync(path.join(PathResolver.ROOT, 'config', 'conf.yml'))).equals(true);
});

context('Ensure values saved', () => {
it('Expand/collapse cards', () => {
booleanSettingTestHelper(
Expand Down

0 comments on commit 94aa4bc

Please sign in to comment.