Skip to content

Commit

Permalink
0.4.2 GitHub CP open
Browse files Browse the repository at this point in the history
  • Loading branch information
TfTHacker committed Oct 18, 2021
1 parent 274cba8 commit 9959515
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian42-brat",
"name": "Obsidian42 - BRAT",
"version": "0.4.1",
"version": "0.4.2",
"minAppVersion": "0.9.12",
"description": "Easily install a beta version of a plugin for testing.",
"author": "TfTHacker",
Expand Down
30 changes: 16 additions & 14 deletions src/BetaPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ThePlugin from "./main";
import AddNewPluginModal from "./AddNewPluginModal";
import { grabManifestJsonFromRepository, grabReleaseFileFromRepository } from "./githubUtils";
import { Notice, PluginManifest } from "obsidian";
import { normalizePath, Notice, PluginManifest } from "obsidian";
import { addBetaPluginToList } from "./settings";

/**
Expand Down Expand Up @@ -88,15 +88,16 @@ export default class BetaPlugins {
* @return {Promise<void>}
*/
async writeReleaseFilesToPluginFolder(betaPluginID: string, relFiles: ReleaseFiles): Promise<void> {
const pluginTargetFolderPath = this.plugin.app.vault.configDir + "/plugins/" + betaPluginID + "/";
if (await this.plugin.app.vault.adapter.exists(pluginTargetFolderPath) === false ||
!(await this.plugin.app.vault.adapter.exists(pluginTargetFolderPath + "manifest.json"))) {
const pluginTargetFolderPath = normalizePath(this.plugin.app.vault.configDir + "/plugins/" + betaPluginID) + "/";
const adapter = this.plugin.app.vault.adapter;
if (await adapter.exists(pluginTargetFolderPath) === false ||
!(await adapter.exists(pluginTargetFolderPath + "manifest.json"))) {
// if plugin folder doesnt exist or manifest.json doesn't exist, create it and save the plugin files
await this.plugin.app.vault.adapter.mkdir(pluginTargetFolderPath);
await adapter.mkdir(pluginTargetFolderPath);
}
await this.plugin.app.vault.adapter.write(pluginTargetFolderPath + "main.js", relFiles.mainJs);
await this.plugin.app.vault.adapter.write(pluginTargetFolderPath + "manifest.json", relFiles.manifest);
if (relFiles.styles) await this.plugin.app.vault.adapter.write(pluginTargetFolderPath + "styles.css", relFiles.styles);
await adapter.write(pluginTargetFolderPath + "main.js", relFiles.mainJs);
await adapter.write(pluginTargetFolderPath + "manifest.json", relFiles.manifest);
if (relFiles.styles) await adapter.write(pluginTargetFolderPath + "styles.css", relFiles.styles);
}

/**
Expand Down Expand Up @@ -145,10 +146,8 @@ export default class BetaPlugins {
await this.writeReleaseFilesToPluginFolder(primaryManifest.id, releaseFiles);
await addBetaPluginToList(this.plugin, repositoryPath);
//@ts-ignore
const appPlugins = this.plugin.app.plugins;
await appPlugins.loadManifests();
await appPlugins.enablePlugin(primaryManifest.id);
new Notice(`BRAT\n${repositoryPath}\nThe plugin has been installed.`, noticeTimeout);
await this.plugin.app.plugins.loadManifests();
new Notice(`BRAT\n${repositoryPath}\nThe plugin has been registered with BRAT. You may still need to enable it the Community Plugin List.`, noticeTimeout);
} else {
// test if the plugin needs to be updated
const pluginTargetFolderPath = this.plugin.app.vault.configDir + "/plugins/" + primaryManifest.id + "/";
Expand All @@ -172,8 +171,11 @@ export default class BetaPlugins {
new Notice(`BRAT\nThere is an update available for ${primaryManifest.id}`);
} else {
await this.writeReleaseFilesToPluginFolder(primaryManifest.id, releaseFiles);
await this.reloadPlugin(primaryManifest.id)
new Notice(`BRAT\n${primaryManifest.id}\nplugin has been updated and reloaded`, noticeTimeout);
//@ts-ignore
await this.plugin.app.plugins.loadManifests();
//@ts-ignore
if(!this.plugin.app.plugins.plugins[primaryManifest.id]===undefined) await this.reloadPlugin(primaryManifest.id); //reload if enabled
new Notice(`BRAT\n${primaryManifest.id}\nPlugin has been updated.`, noticeTimeout);
}
} else
if (reportIfNotUpdted) new Notice(`BRAT\nNo update available for ${repositoryPath}`, 3000);
Expand Down
11 changes: 11 additions & 0 deletions src/githubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ export const grabManifestJsonFromRepository = async (repositoryPath: string, roo
console.log("error in grabManifestJsonFromRepository", error)
}
}


export const grabCommmunityPluginList = async(): Promise<JSON> => {
const pluginListURL = `https://raw.githubusercontent.com/obsidianmd/obsidian-releases/HEAD/community-plugins.json`;
try {
const response = await request({ url: pluginListURL });
return (response === "404: Not Found" ? null : await JSON.parse(response));
} catch (error) {
console.log("error in grabCommmunityPluginList", error)
}
}
20 changes: 19 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SettingsTab } from "./SettingsTab";
import { Settings, DEFAULT_SETTINGS } from "./settings";
import BetaPlugins from "./BetaPlugins";
import { GenericFuzzySuggester, SuggesterItem } from "./GenericFuzzySuggester";
import { grabCommmunityPluginList } from "./githubUtils";

export default class ThePlugin extends Plugin {
appName = "Obsidian42 - Beta Reviewer's Auto-update Tool (BRAT)";
Expand Down Expand Up @@ -43,7 +44,7 @@ export default class ThePlugin extends Plugin {
const gfs = new GenericFuzzySuggester(this);
gfs.setSuggesterData(pluginList);
await gfs.display(async (results) => {
new Notice(`BRAT\nChecking for updates for ${results.info}`,3000);
new Notice(`BRAT\nChecking for updates for ${results.info}`, 3000);
await this.betaPlugins.updatePlugin(results.info, false, true);
});
}
Expand All @@ -64,6 +65,23 @@ export default class ThePlugin extends Plugin {
}
});

this.addCommand({
id: "BRAT-openGitHubRepository",
name: "Open the GitHub repository for a plugin",
callback: async () => {
let communityPlugins = await grabCommmunityPluginList();
let communityPluginList: SuggesterItem[] = Object.values(communityPlugins).map((p) => { return { display: `${p.name} (${p.repo})`, info: p.repo } });
const bratList: SuggesterItem[] = Object.values(this.settings.pluginList).map((p) => { return { display: "BRAT: " + p, info: p } });
bratList.push({ display: "-------------------------- Community Plugins --------------------------", info: null });
communityPluginList.forEach(si => bratList.push(si));
const gfs = new GenericFuzzySuggester(this);
gfs.setSuggesterData(bratList);
await gfs.display(async (results) => {
if (results.info) window.open(`https://github.com/${results.info}`)
});
}
});

this.app.workspace.onLayoutReady((): void => {
if (this.settings.updateAtStartup) // let obsidian load and calm down before check
setTimeout(async () => { await this.betaPlugins.checkForUpdatesAndInstallUpdates(false) }, 60000);
Expand Down

0 comments on commit 9959515

Please sign in to comment.