-
Notifications
You must be signed in to change notification settings - Fork 51
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
Draft: Refactor installation state. #216
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,124 @@ | ||
import { app, ipcMain } from 'electron'; | ||
import log from 'electron-log/main'; | ||
import { IPC_CHANNELS } from '../constants'; | ||
import { createModelConfigFiles, getModelConfigPath } from '../config/extra_model_config'; | ||
import fs from 'fs'; | ||
import { AppWindow } from '../main-process/appWindow'; | ||
import { ComfyConfigManager } from '../config/comfyConfigManager'; | ||
|
||
type InstallationState = | ||
| { status: 'NOT_INSTALLED' } | ||
| { status: 'ASKING_FOR_DIRECTORY'; defaultLocation: string; validationErrorMessage?: string } | ||
| { | ||
status: 'INSTALLING'; | ||
location: string; | ||
} | ||
| { | ||
status: 'READY'; | ||
location: string; // Must exist! | ||
} | ||
| { | ||
status: 'ERROR'; | ||
error: Error; // Must exist! | ||
}; | ||
|
||
/** | ||
* This class is responsible for handling the installation of ComfyUI. | ||
* It registers an IPC handler for the current installation state. | ||
* It also sends any changes to the state to the renderer process. | ||
*/ | ||
export class ComfyUIInstall { | ||
private static instance: ComfyUIInstall; | ||
private installLocation: string; | ||
private defaultInstallLocation: string; | ||
private state: InstallationState = { status: 'NOT_INSTALLED' }; | ||
private appWindow: AppWindow; | ||
|
||
private constructor(appWindow: AppWindow) { | ||
const defaultInstallLocation = app.getPath('documents'); | ||
ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => defaultInstallLocation); // TODO: Remove after migration. | ||
ipcMain.handle(IPC_CHANNELS.GET_INSTALLATION_STATE, () => this.state); | ||
this.installLocation = defaultInstallLocation; | ||
this.defaultInstallLocation = defaultInstallLocation; | ||
this.appWindow = appWindow; | ||
} | ||
|
||
static get(appWindow: AppWindow): ComfyUIInstall { | ||
if (!ComfyUIInstall.instance) { | ||
ComfyUIInstall.instance = new ComfyUIInstall(appWindow); | ||
} | ||
return ComfyUIInstall.instance; | ||
} | ||
|
||
setState(state: InstallationState): void { | ||
log.info('Setting installation state:', state); | ||
this.state = state; | ||
this.appWindow.send(IPC_CHANNELS.INSTALLATION_STATE_CHANGED, state); | ||
} | ||
|
||
public async install(): Promise<void> { | ||
const firstTimeSetup = await this.isFirstTimeSetup(); | ||
log.info('First time setup:', firstTimeSetup); | ||
if (!firstTimeSetup) { | ||
this.appWindow.send(IPC_CHANNELS.FIRST_TIME_SETUP_COMPLETE, null); // TODO:Remove after migration. | ||
return; | ||
} | ||
this.setState({ status: 'ASKING_FOR_DIRECTORY', defaultLocation: this.defaultInstallLocation }); | ||
this.appWindow.send(IPC_CHANNELS.SHOW_SELECT_DIRECTORY, null); // TODO:Remove after migration. | ||
while (this.state.status === 'ASKING_FOR_DIRECTORY') { | ||
const selectedDirectory = await this.selectInstallDirectory(); | ||
const { valid, errorMessage } = await this.isValidComfyDirectory(selectedDirectory); | ||
if (!valid) { | ||
this.setState({ ...this.state, validationErrorMessage: errorMessage }); | ||
return; | ||
} else { | ||
this.setState({ status: 'INSTALLING', location: selectedDirectory }); | ||
this.installLocation = selectedDirectory; | ||
} | ||
} | ||
|
||
const actualComfyDirectory = ComfyConfigManager.setUpComfyUI(this.installLocation); | ||
const modelConfigPath = getModelConfigPath(); | ||
await createModelConfigFiles(modelConfigPath, actualComfyDirectory); | ||
|
||
this.setState({ status: 'READY', location: this.installLocation }); | ||
} | ||
|
||
/** | ||
* Check if the user has completed the first time setup wizard. | ||
* This means the extra_models_config.yaml file exists in the user's data directory. | ||
*/ | ||
private async isFirstTimeSetup(): Promise<boolean> { | ||
const extraModelsConfigPath = getModelConfigPath(); | ||
return !fs.existsSync(extraModelsConfigPath); | ||
} | ||
|
||
private async selectInstallDirectory(): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
ipcMain.on(IPC_CHANNELS.SELECTED_DIRECTORY, (_event, value: string) => { | ||
log.info('Directory selected:', value); | ||
resolve(value); | ||
}); | ||
}); | ||
} | ||
|
||
public async isValidComfyDirectory(selectedDirectory: string): Promise<{ valid: boolean; errorMessage?: string }> { | ||
try { | ||
const files = await fs.promises.readdir(selectedDirectory); | ||
|
||
if (files.includes('ComfyUI')) { | ||
return { | ||
valid: false, | ||
errorMessage: 'A ComfyUI installation already exists in this directory. Please choose another location.', | ||
}; | ||
} | ||
|
||
return { valid: true }; | ||
} catch (error) { | ||
return { | ||
valid: false, | ||
errorMessage: 'Unable to access the selected directory. Please ensure you have proper permissions.', | ||
}; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just keep this a simple enum matching
ProgressStatus
, and get the validation and location stuff in separate API.