Skip to content

Commit

Permalink
Merge pull request #73 from RyotaUshio/feat/auto-enable
Browse files Browse the repository at this point in the history
Feat: add an option to auto-enable beta plugins after installation
  • Loading branch information
TfTHacker authored Dec 20, 2023
2 parents 49b00dd + 6cbb0de commit 4f0f8e1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/features/BetaPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class BetaPlugins {
'manifest.json',
this.plugin.settings.debuggingMode
)
: '',
: '',
styles: await grabReleaseFileFromRepository(
repositoryPath,
version,
Expand Down Expand Up @@ -197,7 +197,8 @@ export default class BetaPlugins {
seeIfUpdatedOnly = false,
reportIfNotUpdted = false,
specifyVersion = '',
forceReinstall = false
forceReinstall = false,
enableAfterInstall = this.plugin.settings.enableAfterInstall
): Promise<boolean> {
if (this.plugin.settings.debuggingMode)
console.log(
Expand All @@ -207,7 +208,8 @@ export default class BetaPlugins {
seeIfUpdatedOnly,
reportIfNotUpdted,
specifyVersion,
forceReinstall
forceReinstall,
enableAfterInstall
);

const noticeTimeout = 10;
Expand All @@ -226,9 +228,8 @@ export default class BetaPlugins {
}

if (!Object.hasOwn(primaryManifest, 'version')) {
const msg = `${repositoryPath}\nThe manifest${
usingBetaManifest ? '-beta' : ''
}.json file in the root directory of the repository does not have a version number in the file. This plugin cannot be installed.`;
const msg = `${repositoryPath}\nThe manifest${usingBetaManifest ? '-beta' : ''
}.json file in the root directory of the repository does not have a version number in the file. This plugin cannot be installed.`;
await this.plugin.log(msg, true);
toastMessage(this.plugin, `${msg}`, noticeTimeout);
return false;
Expand All @@ -239,8 +240,7 @@ export default class BetaPlugins {
if (!requireApiVersion(primaryManifest.minAppVersion)) {
const msg =
`Plugin: ${repositoryPath}\n\n` +
`The manifest${
usingBetaManifest ? '-beta' : ''
`The manifest${usingBetaManifest ? '-beta' : ''
}.json for this plugin indicates that the Obsidian ` +
`version of the app needs to be ${primaryManifest.minAppVersion}, ` +
`but this installation of Obsidian is ${apiVersion}. \n\nYou will need to update your ` +
Expand Down Expand Up @@ -284,8 +284,13 @@ export default class BetaPlugins {
if (releaseFiles === null) return false;
await this.writeReleaseFilesToPluginFolder(primaryManifest.id, releaseFiles);
if (!forceReinstall)
// only add to list if not a force reinstall
addBetaPluginToList(this.plugin, repositoryPath, specifyVersion);
if (enableAfterInstall) {
const { plugins } = this.plugin.app;
const pluginTargetFolderPath = normalizePath(plugins.getPluginFolder() + '/' + primaryManifest.id);
await plugins.loadManifest(pluginTargetFolderPath);
await plugins.enablePlugin(primaryManifest.id);
}
await this.plugin.app.plugins.loadManifests();
if (forceReinstall) {
// reload if enabled
Expand All @@ -298,7 +303,10 @@ export default class BetaPlugins {
);
} else {
const versionText = specifyVersion === '' ? '' : ` (version: ${specifyVersion})`;
const msg = `${repositoryPath}${versionText}\nThe plugin has been registered with BRAT. You may still need to enable it the Community Plugin List.`;
let msg = `${repositoryPath}${versionText}\nThe plugin has been registered with BRAT.`;
if (!enableAfterInstall) {
msg += " You may still need to enable it the Community Plugin List.";
}
await this.plugin.log(msg, true);
toastMessage(this.plugin, msg, noticeTimeout);
}
Expand Down Expand Up @@ -360,7 +368,7 @@ export default class BetaPlugins {
const msg = `There is an update available for ${primaryManifest.id} from version ${localManifestJson.version} to ${primaryManifest.version}. `;
await this.plugin.log(
msg +
`[Release Info](https://github.com/${repositoryPath}/releases/tag/${primaryManifest.version})`,
`[Release Info](https://github.com/${repositoryPath}/releases/tag/${primaryManifest.version})`,
true
);
toastMessage(this.plugin, msg, 30, () => {
Expand All @@ -377,7 +385,7 @@ export default class BetaPlugins {
const msg = `${primaryManifest.id}\nPlugin has been updated from version ${localManifestJson.version} to ${primaryManifest.version}. `;
await this.plugin.log(
msg +
`[Release Info](https://github.com/${repositoryPath}/releases/tag/${primaryManifest.version})`,
`[Release Info](https://github.com/${repositoryPath}/releases/tag/${primaryManifest.version})`,
true
);
toastMessage(this.plugin, msg, 30, () => {
Expand Down Expand Up @@ -508,12 +516,12 @@ export default class BetaPlugins {
(p) => p.manifest
);
return enabled ?
manifests.filter((manifest) =>
enabledPlugins.find((pluginName) => manifest.id === pluginName.id)
)
manifests.filter((manifest) =>
enabledPlugins.find((pluginName) => manifest.id === pluginName.id)
)
: manifests.filter(
(manifest) =>
!enabledPlugins.find((pluginName) => manifest.id === pluginName.id)
);
(manifest) =>
!enabledPlugins.find((pluginName) => manifest.id === pluginName.id)
);
}
}
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Settings {
themesList: ThemeInforamtion[];
updateAtStartup: boolean;
updateThemesAtStartup: boolean;
enableAfterInstall: boolean;
loggingEnabled: boolean;
loggingPath: string;
loggingVerboseEnabled: boolean;
Expand All @@ -31,6 +32,7 @@ export const DEFAULT_SETTINGS: Settings = {
themesList: [],
updateAtStartup: true,
updateThemesAtStartup: true,
enableAfterInstall: true,
loggingEnabled: false,
loggingPath: 'BRAT-log',
loggingVerboseEnabled: false,
Expand Down
16 changes: 16 additions & 0 deletions src/ui/AddNewPluginModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class AddNewPluginModal extends Modal {
address: string;
openSettingsTabAfterwards: boolean;
readonly useFrozenVersion: boolean;
enableAfterInstall: boolean;
version: string;

constructor(
Expand All @@ -28,6 +29,7 @@ export default class AddNewPluginModal extends Modal {
this.address = '';
this.openSettingsTabAfterwards = openSettingsTabAfterwards;
this.useFrozenVersion = useFrozenVersion;
this.enableAfterInstall = plugin.settings.enableAfterInstall;
this.version = '';
}

Expand Down Expand Up @@ -92,6 +94,20 @@ export default class AddNewPluginModal extends Modal {
}

formEl.createDiv('modal-button-container', (buttonContainerEl) => {
buttonContainerEl.createEl("label", {
cls: "mod-checkbox"
}, (labelEl) => {
const checkboxEl = labelEl.createEl("input", {
attr: { tabindex: -1 },
type: "checkbox"
});
checkboxEl.checked = this.enableAfterInstall;
checkboxEl.addEventListener("click", () => {
this.enableAfterInstall = checkboxEl.checked;
});
labelEl.appendText('Enable after installing the plugin');
});

buttonContainerEl
.createEl('button', { attr: { type: 'button' }, text: 'Never mind' })
.addEventListener('click', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/ui/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ export class BratSettingsTab extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName('Auto-enable plugins after installation')
.setDesc(
'If enabled beta plugins will be automatically enabled after installtion by default. Note: you can toggle this on and off for each plugin in the "Add Plugin" form.'
)
.addToggle((cb: ToggleComponent) => {
cb.setValue(this.plugin.settings.enableAfterInstall);
cb.onChange(async (value: boolean) => {
this.plugin.settings.enableAfterInstall = value;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName('Auto-update themes at startup')
.setDesc(
Expand Down

0 comments on commit 4f0f8e1

Please sign in to comment.