Skip to content

Commit

Permalink
Merge pull request #35 from 0x4007/fix/manifest-name-requirement-v
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Dec 11, 2024
2 parents f3e6f62 + 640f492 commit 0603a6a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
6 changes: 3 additions & 3 deletions static/scripts/fetch-manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Octokit } from "@octokit/rest";
import { CONFIG_FULL_PATH, CONFIG_ORG_REPO, DEV_CONFIG_FULL_PATH } from "@ubiquity-os/plugin-sdk/constants";
import { Manifest, ManifestPreDecode } from "../types/plugins";
import { DEV_CONFIG_FULL_PATH, CONFIG_FULL_PATH, CONFIG_ORG_REPO } from "@ubiquity-os/plugin-sdk/constants";
import { getOfficialPluginConfig } from "../utils/storage";

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export class ManifestFetcher {
for (const repo of repos.data) {
const manifestUrl = this.createGithubRawEndpoint(org, repo.name, "development", "manifest.json");
const manifest = await this.fetchPluginManifest(manifestUrl);
const decoded = this.decodeManifestFromFetch(manifest);
const decoded = this.decodeManifestFromFetch(manifest, repo.name);
const readme = await this.fetchPluginReadme(this.createGithubRawEndpoint(org, repo.name, "development", "README.md"));

if (decoded) {
Expand Down Expand Up @@ -190,7 +190,7 @@ export class ManifestFetcher {
}
}

decodeManifestFromFetch(manifest: ManifestPreDecode) {
decodeManifestFromFetch(manifest: ManifestPreDecode, repoName: string) {
if (manifest.error) {
return null;
}
Expand Down
50 changes: 35 additions & 15 deletions static/scripts/rendering/config-editor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import MarkdownIt from "markdown-it";
import { Manifest, Plugin } from "../../types/plugins";
import { controlButtons } from "./control-buttons";
import { getManifestCache } from "../../utils/storage";
import { extractPluginIdentifier } from "../../utils/strings";
import { ManifestRenderer } from "../render-manifest";
import { controlButtons } from "./control-buttons";
import { processProperties } from "./input-parsing";
import { addTrackedEventListener, getTrackedEventListeners, normalizePluginName, removeTrackedEventListener, updateGuiTitle } from "./utils";
import { addTrackedEventListener, getTrackedEventListeners, removeTrackedEventListener, updateGuiTitle } from "./utils";
import { handleResetToDefault, writeNewConfig } from "./write-add-remove";
import MarkdownIt from "markdown-it";
import { getManifestCache } from "../../utils/storage";
const md = new MarkdownIt();

/**
Expand Down Expand Up @@ -84,8 +85,36 @@ export function renderConfigEditor(renderer: ManifestRenderer, pluginManifest: M
}

const parsedConfig = renderer.configParser.parseConfig(renderer.configParser.repoConfig || localStorage.getItem("config"));
// for when `resetToDefault` is called and no plugin gets passed in, we still want to show the remove button
const isInstalled = parsedConfig.plugins?.find((p) => p.uses[0].plugin.includes(normalizePluginName(pluginManifest?.name || "")));

// Get the repository URL for the current plugin from the manifest cache
const manifestCache = getManifestCache();
const pluginUrls = Object.keys(manifestCache);
const pluginUrl = pluginUrls.find((url) => {
return manifestCache[url].name === pluginManifest?.name;
});

if (!pluginUrl) {
throw new Error("Plugin URL not found");
}

const manifestPluginId = extractPluginIdentifier(pluginUrl);

// Check if plugin is installed by looking for any URL that matches
const isInstalled = parsedConfig.plugins?.find((p) => {
const installedUrl = p.uses[0].plugin;

// If the installed plugin is a GitHub URL, extract its identifier
const installedPluginId = extractPluginIdentifier(installedUrl);

// If both are GitHub URLs, compare the repo names
const isBothGithubUrls = pluginUrl.includes("github") && installedUrl.includes("github");
if (isBothGithubUrls) {
return manifestPluginId === installedPluginId;
}

// Otherwise check if the installed URL contains the repo name
return installedUrl.toLowerCase().includes(manifestPluginId.toLowerCase());
});

loadListeners({
renderer,
Expand All @@ -105,15 +134,6 @@ export function renderConfigEditor(renderer: ManifestRenderer, pluginManifest: M
}

resetToDefaultButton.hidden = !!(plugin || isInstalled);
const manifestCache = getManifestCache();
const pluginUrls = Object.keys(manifestCache);
const pluginUrl = pluginUrls.find((url) => {
return manifestCache[url].name === pluginManifest?.name;
});

if (!pluginUrl) {
throw new Error("Plugin URL not found");
}
const readme = manifestCache[pluginUrl].readme;

if (readme) {
Expand Down
27 changes: 22 additions & 5 deletions static/scripts/rendering/plugin-select.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ManifestCache, ManifestPreDecode, Plugin } from "../../types/plugins";
import { createElement } from "../../utils/element-helpers";
import { getManifestCache } from "../../utils/storage";
import { STRINGS } from "../../utils/strings";
import { STRINGS, extractPluginIdentifier } from "../../utils/strings";
import { ManifestRenderer } from "../render-manifest";
import { renderConfigEditor } from "./config-editor";
import { controlButtons } from "./control-buttons";
import { closeAllSelect, normalizePluginName, updateGuiTitle } from "./utils";
import { closeAllSelect, updateGuiTitle } from "./utils";

/**
* Renders a dropdown of plugins taken from the marketplace with an installed indicator.
Expand Down Expand Up @@ -62,9 +62,26 @@ export function renderPluginSelector(renderer: ManifestRenderer): void {
if (!cleanManifestCache[url]?.name) {
return;
}
const normalizedName = normalizePluginName(cleanManifestCache[url].name);
const reg = new RegExp(normalizedName, "i");
const installedPlugin: Plugin | undefined = installedPlugins.find((plugin) => plugin.uses[0].plugin.match(reg));

const manifestPluginId = extractPluginIdentifier(url);

// Check if plugin is installed by looking for any URL that matches
const installedPlugin: Plugin | undefined = installedPlugins.find((plugin) => {
const installedUrl = plugin.uses[0].plugin;

// If the installed plugin is a GitHub URL, extract its identifier
const installedPluginId = extractPluginIdentifier(installedUrl);

// If both are GitHub URLs, compare the repo names
const isBothGithubUrls = url.includes("github") && installedUrl.includes("github");
if (isBothGithubUrls) {
return manifestPluginId === installedPluginId;
}

// Otherwise check if the installed URL contains the repo name
return installedUrl.toLowerCase().includes(manifestPluginId.toLowerCase());
});

const defaultForInstalled: ManifestPreDecode | null = cleanManifestCache[url];
const optionText = defaultForInstalled.name;
const indicator = installedPlugin ? "🟢" : "🔴";
Expand Down
18 changes: 18 additions & 0 deletions static/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ export const STRINGS = {
SELECT_ARROW_ACTIVE: "select-arrow-active",
PICKER_SELECT: "picker-select",
};

/**
* For manifest URLs from GitHub, extracts just the repo name.
* For all other URLs (workers, etc), returns the full URL.
* This allows for flexible matching without assuming URL formats.
*/
export function extractPluginIdentifier(url: string): string {
// For GitHub manifest URLs, extract just the repo name
if (url.includes("github.com/") || url.includes("githubusercontent.com/")) {
const parts = url.split("/");
if (parts.length >= 5) {
return parts[4].split("@")[0].split("?")[0]; // Get repo name without branch or query params
}
}

// For all other URLs (workers, etc), use the full URL
return url;
}

0 comments on commit 0603a6a

Please sign in to comment.