Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging input files (App) #2799

Merged
merged 27 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
246b9b6
refactor: move all menu text to shared/text.ts
PhilippMa Feb 17, 2025
a393b46
refactor: simplify handling of initially disabled menu items
PhilippMa Feb 17, 2025
8b2ebe2
feat: add menu entries for merge
PhilippMa Feb 17, 2025
947916b
feat: request opening merge dialog when menu item is clicked
PhilippMa Feb 18, 2025
1960bf3
refactor: change names related to file selection to uncouple it from …
PhilippMa Feb 18, 2025
7071b91
feat: prepare IPC channel for commencing merge and load in the backend
PhilippMa Feb 18, 2025
1d40e53
feat: add merge dialog
PhilippMa Feb 18, 2025
289a064
feat: fully integrate merge dialog with unsaved check mechanism
PhilippMa Feb 18, 2025
bc639d5
fix: update menu descriptions in MenuBar
PhilippMa Feb 18, 2025
e07822d
test: add e2e tests for merge dialog
PhilippMa Feb 18, 2025
e0534fb
fix: fix typo in MenuBar
PhilippMa Feb 18, 2025
acbed86
feat: update icon for merge menu item
PhilippMa Feb 19, 2025
3414ed3
refactor: use async/await instead of Promise.then in import and merge…
PhilippMa Feb 19, 2025
92b2045
feat: implement some review suggestions
PhilippMa Feb 19, 2025
475159a
fix: capitalization issues
PhilippMa Feb 19, 2025
101cdec
feat: improve error reporting during import/merge
PhilippMa Feb 19, 2025
9135423
feat: automatically create backup of current file before merging
PhilippMa Feb 19, 2025
f6396ff
refactor: variable renaming
PhilippMa Feb 20, 2025
7865c57
refactor: remove LogDisplayForDialog component
PhilippMa Feb 20, 2025
db6f6c4
refactor: collect error message strings in text.ts
PhilippMa Feb 20, 2025
9fc151b
fix: post-rebase cleanup
PhilippMa Feb 24, 2025
25b80d1
fix: log formatting in ProcessPopup
PhilippMa Feb 24, 2025
f28baf4
fix: linter issues
PhilippMa Feb 24, 2025
733970f
fix: update loading state handling in MergeDialog
PhilippMa Feb 24, 2025
3ebbd99
refactor: rename merge listener
PhilippMa Feb 25, 2025
6f312b3
refactor: move location of style definitions in ProcessPopup
PhilippMa Feb 25, 2025
d5647b8
refactor: move log related styling from sx into styled components
PhilippMa Feb 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/assets/icons/merge-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/icons/merge-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/ElectronBackend/main/__tests__/listeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import { setGlobalBackendState } from '../globalBackendState';
import {
exportFileListener,
importFileListener,
importFileSelectInputListener,
importFileSelectSaveLocationListener,
linkHasHttpSchema,
openLinkListener,
selectBaseURLListener,
selectFileListener,
} from '../listeners';
import { importFileFormats } from '../menu/fileMenu';

Expand Down Expand Up @@ -412,7 +412,7 @@ describe('getImportFileListener', () => {
listener();

expect(mainWindow.webContents.send).toHaveBeenCalledWith(
AllowedFrontendChannels.ImportFileShowDialog,
AllowedFrontendChannels.ShowImportDialog,
fileFormat,
);
});
Expand All @@ -424,7 +424,7 @@ describe('getImportFileSelectInputListener', () => {
const fileFormat = importFileFormats[0];
const selectedFilePath = '/home/input.json';

const listener = importFileSelectInputListener(mainWindow);
const listener = selectFileListener(mainWindow);

jest.mocked(openNonOpossumFileDialog).mockReturnValue([selectedFilePath]);

Expand All @@ -440,7 +440,7 @@ describe('getImportFileSelectInputListener', () => {
const mainWindow = await initWindowAndBackendState();
const fileFormat = importFileFormats[0];

const listener = importFileSelectInputListener(mainWindow);
const listener = selectFileListener(mainWindow);

jest
.mocked(openNonOpossumFileDialog)
Expand Down
2 changes: 1 addition & 1 deletion src/ElectronBackend/main/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function openNonOpossumFileDialog(
): Array<string> | undefined {
return openFileDialog([
{
name: `${fileFormat.name}s (${fileFormat.extensions.map((ext) => `.${ext}`).join('/')})`,
name: `${fileFormat.name} Files (${fileFormat.extensions.map((ext) => `.${ext}`).join('/')})`,
extensions: fileFormat.extensions,
},
]);
Expand Down
91 changes: 84 additions & 7 deletions src/ElectronBackend/main/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
PackageInfo,
SaveFileArgs,
} from '../../shared/shared-types';
import { text } from '../../shared/text';
import { writeFile, writeOpossumFile } from '../../shared/write-file';
import { LoadedFileFormat } from '../enums/enums';
import {
Expand All @@ -32,7 +33,10 @@ import {
} from '../errorHandling/errorHandling';
import { loadInputAndOutputFromFilePath } from '../input/importFromFile';
import { serializeAttributions } from '../input/parseInputData';
import { convertToOpossum } from '../opossum-file/opossum-file';
import {
convertToOpossum,
mergeFileIntoOpossum,
} from '../opossum-file/opossum-file';
import { writeCsvToFile } from '../output/writeCsvToFile';
import { writeSpdxFile } from '../output/writeSpdxFile';
import { GlobalBackendState, OpossumOutputFile } from '../types/types';
Expand Down Expand Up @@ -132,12 +136,20 @@ export async function handleOpeningFile(
export const importFileListener =
(mainWindow: BrowserWindow, fileFormat: FileFormatInfo) => (): void => {
mainWindow.webContents.send(
AllowedFrontendChannels.ImportFileShowDialog,
AllowedFrontendChannels.ShowImportDialog,
fileFormat,
);
};

export const getMergeListener =
(mainWindow: BrowserWindow, fileFormat: FileFormatInfo) => (): void => {
mainWindow.webContents.send(
AllowedFrontendChannels.ShowMergeDialog,
fileFormat,
);
};

export const importFileSelectInputListener =
export const selectFileListener =
(mainWindow: BrowserWindow) =>
async (
_: Electron.IpcMainInvokeEvent,
Expand Down Expand Up @@ -183,19 +195,31 @@ export const importFileConvertAndLoadListener =

try {
if (!resourceFilePath.trim() || !fs.existsSync(resourceFilePath)) {
throw new Error('Input file does not exist');
throw new Error(text.backendError.inputFileDoesNotExist);
}

try {
fs.accessSync(resourceFilePath, fs.constants.R_OK);
} catch (error) {
throw new Error(text.backendError.inputFilePermissionError);
}

if (!opossumFilePath.trim()) {
throw new Error('No .opossum save location selected');
throw new Error(text.backendError.opossumFileNotSelected);
}

if (!opossumFilePath.endsWith('.opossum')) {
throw new Error('Output file name must have .opossum extension');
throw new Error(text.backendError.opossumFileWrongExtension);
}

if (!fs.existsSync(path.dirname(opossumFilePath))) {
throw new Error('Output directory does not exist');
throw new Error(text.backendError.opossumFileDirectoryDoesNotExist);
}

try {
fs.accessSync(path.dirname(opossumFilePath), fs.constants.W_OK);
} catch (error) {
throw new Error(text.backendError.opossumFilePermissionError);
}

logger.info('Converting input file to .opossum format');
Expand All @@ -215,6 +239,59 @@ export const importFileConvertAndLoadListener =
}
};

export const mergeFileAndLoadListener =
(mainWindow: BrowserWindow) =>
async (
_: Electron.IpcMainInvokeEvent,
inputFilePath: string,
fileType: FileType,
): Promise<boolean> => {
setLoadingState(mainWindow.webContents, true);

try {
if (!inputFilePath.trim() || !fs.existsSync(inputFilePath)) {
throw new Error(text.backendError.inputFileDoesNotExist);
}

try {
fs.accessSync(inputFilePath, fs.constants.R_OK);
} catch (error) {
throw new Error(text.backendError.inputFilePermissionError);
}

const currentOpossumFilePath = getGlobalBackendState().opossumFilePath;

if (!currentOpossumFilePath) {
throw new Error(text.backendError.noOpenFileToMergeInto);
}

try {
fs.copyFileSync(
currentOpossumFilePath,
`${currentOpossumFilePath}.backup`,
);
} catch (error) {
throw new Error(text.backendError.cantCreateBackup);
}

logger.info('Merging input file into current .opossum file');
await mergeFileIntoOpossum(
inputFilePath,
currentOpossumFilePath,
fileType,
);

await openFile(mainWindow, currentOpossumFilePath, () => {});

return true;
} catch (error) {
sendListenerErrorToFrontend(mainWindow, error);
return false;
} finally {
setLoadingState(mainWindow.webContents, false);
}
};

function initializeGlobalBackendState(
filePath: string,
isOpossumFormat: boolean,
Expand Down
21 changes: 13 additions & 8 deletions src/ElectronBackend/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import { createWindow } from './createWindow';
import {
exportFileListener,
importFileConvertAndLoadListener,
importFileSelectInputListener,
importFileSelectSaveLocationListener,
mergeFileAndLoadListener,
openFileListener,
openLinkListener,
saveFileListener,
selectFileListener,
} from './listeners';
import { createMenu } from './menu';
import { activateMenuItems } from './menu/initiallyDisabledMenuItems';
import { DisabledMenuItemHandler } from './menu/DisabledMenuItemHandler';
import { openFileFromCliOrEnvVariableIfProvided } from './openFileFromCliOrEnvVariableIfProvided';
import { UserSettings } from './user-settings';

Expand Down Expand Up @@ -65,19 +66,23 @@ export async function main(): Promise<void> {
});
ipcMain.handle(
IpcChannel.OpenFile,
openFileListener(mainWindow, activateMenuItems),
);
ipcMain.handle(
IpcChannel.ImportFileSelectInput,
importFileSelectInputListener(mainWindow),
openFileListener(mainWindow, DisabledMenuItemHandler.activateMenuItems),
);
ipcMain.handle(IpcChannel.SelectFile, selectFileListener(mainWindow));
ipcMain.handle(
IpcChannel.ImportFileSelectSaveLocation,
importFileSelectSaveLocationListener(mainWindow),
);
ipcMain.handle(
IpcChannel.ImportFileConvertAndLoad,
importFileConvertAndLoadListener(mainWindow, activateMenuItems),
importFileConvertAndLoadListener(
mainWindow,
DisabledMenuItemHandler.activateMenuItems,
),
);
ipcMain.handle(
IpcChannel.MergeFileAndLoad,
mergeFileAndLoadListener(mainWindow),
);
ipcMain.handle(IpcChannel.SaveFile, saveFileListener(mainWindow));
ipcMain.handle(IpcChannel.ExportFile, exportFileListener(mainWindow));
Expand Down
32 changes: 32 additions & 0 deletions src/ElectronBackend/main/menu/DisabledMenuItemHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import { Menu } from 'electron';

export class DisabledMenuItemHandler {
private static nextId: number = 0;

static registerDisabledMenuItem(): string {
const idString = DisabledMenuItemHandler.nextId.toString();
DisabledMenuItemHandler.nextId++;
return idString;
}

private static disabledIds(): Array<string> {
return Array(DisabledMenuItemHandler.nextId)
.keys()
.map((id) => id.toString())
.toArray();
}

static activateMenuItems(): void {
const menu = Menu.getApplicationMenu();
DisabledMenuItemHandler.disabledIds().forEach((id) => {
const menuItem = menu?.getMenuItemById(id);
if (menuItem) {
menuItem.enabled = true;
}
});
}
}
9 changes: 5 additions & 4 deletions src/ElectronBackend/main/menu/aboutMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
import { shell } from 'electron';

import { text } from '../../../shared/text';
import { getIconBasedOnTheme } from '../iconHelpers';
import {
getPathOfChromiumNoticeDocument,
Expand All @@ -17,7 +18,7 @@ function getOpenOnGithub() {
'icons/github-white.png',
'icons/github-black.png',
),
label: 'Open on GitHub',
label: text.menu.aboutSubmenu.openOnGithub,
click: () =>
shell.openExternal('https://github.com/opossum-tool/opossumUI'),
};
Expand All @@ -29,7 +30,7 @@ function getOpossumUiNotices() {
'icons/notice-white.png',
'icons/notice-black.png',
),
label: 'OpossumUI Notices',
label: text.menu.aboutSubmenu.opossumUINotices,
click: () => shell.openPath(getPathOfNoticeDocument()),
};
}
Expand All @@ -40,14 +41,14 @@ function getChromiumNotices() {
'icons/chromium-white.png',
'icons/chromium-black.png',
),
label: 'Chromium Notices',
label: text.menu.aboutSubmenu.chromiumNotices,
click: () => shell.openPath(getPathOfChromiumNoticeDocument()),
};
}

export function getAboutMenu() {
return {
label: 'About',
label: text.menu.about,
submenu: [getOpenOnGithub(), getOpossumUiNotices(), getChromiumNotices()],
};
}
Loading
Loading