Skip to content

Commit

Permalink
fix: installQuartoExtensionSource to keep "source" for install/upda…
Browse files Browse the repository at this point in the history
…te (#23)
  • Loading branch information
mcanouil authored Jan 19, 2025
1 parent bf0ffd5 commit 20475a1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## 0.5.1 (2025-01-20)
## 0.5.2 (2025-01-19)

- fix: add source after updating an extension.
- refactor: add `installQuartoExtensionSource` to contain the logic to install an extension and add the source.

## 0.5.1 (2025-01-19)

- fix(.vscodeignore): remove wrong entry.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "quarto-wizard",
"displayName": "Quarto Wizard",
"description": "A Visual Studio Code extension that helps you manage Quarto projects.",
"version": "0.5.1",
"version": "0.5.2",
"publisher": "mcanouil",
"author": {
"name": "Mickaël CANOUIL"
Expand Down
10 changes: 4 additions & 6 deletions src/ui/extensionsInstalled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
import * as fs from "fs";
import { findQuartoExtensions } from "../utils/extensions";
import { ExtensionData, readExtensions } from "../utils/extensions";
import { installQuartoExtension, removeQuartoExtension } from "../utils/quarto";
import { removeQuartoExtension, installQuartoExtensionSource } from "../utils/quarto";

class ExtensionTreeItem extends vscode.TreeItem {
constructor(
Expand Down Expand Up @@ -124,11 +124,9 @@ export class ExtensionsInstalled {
);
context.subscriptions.push(
vscode.commands.registerCommand("quartoWizard.extensionsInstalled.update", (item: ExtensionTreeItem) => {
if (item.data?.source) {
installQuartoExtension(item.data?.source, log);
} else {
installQuartoExtension(item.label, log);
}
installQuartoExtensionSource(item.data?.source ?? item.label, log, workspaceFolder);
// Once source is supported in _extension.yml, the above line can be replaced with the following line
// installQuartoExtension(item.data?.source ?? item.label, log);
})
);
context.subscriptions.push(
Expand Down
30 changes: 6 additions & 24 deletions src/utils/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as crypto from "crypto";
import * as fs from "fs";
import * as path from "path";
import * as yaml from "js-yaml";
import { installQuartoExtension } from "./quarto";
import { installQuartoExtension, installQuartoExtensionSource } from "./quarto";
import { askTrustAuthors, askConfirmInstall } from "./ask";

interface ExtensionQuickPickItem extends vscode.QuickPickItem {
Expand Down Expand Up @@ -115,33 +115,15 @@ export async function installQuartoExtensions(
increment: (1 / (totalExtensions + 1)) * 100,
});

const extensionsDirectory = path.join(workspaceFolder, "_extensions");
const existingExtensions = getMtimeExtensions(extensionsDirectory);

const success = await installQuartoExtension(selectedExtension.description, log);
const success = await installQuartoExtensionSource(selectedExtension.description, log, workspaceFolder);
// Once source is supported in _extension.yml, the above line can be replaced with the following line
// const success = await installQuartoExtension(extension, log);
if (success) {
installedExtensions.push(selectedExtension.description);
} else {
failedExtensions.push(selectedExtension.description);
}

// Update _extension.yml file with source, i.e., GitHub username/repo
// This is needed for the extension to be updated in the future
// To be removed when Quarto supports source records in the _extension.yml file or elsewhere
// See https://github.com/quarto-dev/quarto-cli/issues/11468
const newExtension = findModifiedExtensions(existingExtensions, extensionsDirectory);
const fileNames = ["_extension.yml", "_extension.yaml"];
const filePath = fileNames
.map((name) => path.join(extensionsDirectory, ...newExtension, name))
.find((fullPath) => fs.existsSync(fullPath));
if (filePath) {
const fileContent = fs.readFileSync(filePath, "utf-8");
const updatedContent = fileContent.includes("source: ")
? fileContent.replace(/source: .*/, `source: ${selectedExtension.description}`)
: `${fileContent.trim()}\nsource: ${selectedExtension.description}`;
fs.writeFileSync(filePath, updatedContent);
}

installedCount++;
}
progress.report({
Expand Down Expand Up @@ -201,7 +183,7 @@ export function findQuartoExtensions(dir: string): string[] {
return findQuartoExtensionsRecurse(dir).map((filePath) => path.relative(dir, path.dirname(filePath)));
}

function getMtimeExtensions(dir: string): { [key: string]: Date } {
export function getMtimeExtensions(dir: string): { [key: string]: Date } {
if (!fs.existsSync(dir)) {
return {};
}
Expand All @@ -213,7 +195,7 @@ function getMtimeExtensions(dir: string): { [key: string]: Date } {
return extensionsMtimeDict;
}

function findModifiedExtensions(extensions: { [key: string]: Date }, dir: string): string[] {
export function findModifiedExtensions(extensions: { [key: string]: Date }, dir: string): string[] {
if (!fs.existsSync(dir)) {
return [];
}
Expand Down
33 changes: 33 additions & 0 deletions src/utils/quarto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as vscode from "vscode";
import { exec } from "child_process";
import * as path from "path";
import * as fs from "fs";
import { findModifiedExtensions, getMtimeExtensions } from "./extensions";

let cachedQuartoPath: string | undefined;

Expand Down Expand Up @@ -81,6 +84,36 @@ export async function installQuartoExtension(extension: string, log: vscode.Outp
});
}

// Update _extension.yml file with source, i.e., GitHub username/repo
// This is needed for the extension to be updated in the future
// To be removed when Quarto supports source records in the _extension.yml file or elsewhere
// See https://github.com/quarto-dev/quarto-cli/issues/11468
export async function installQuartoExtensionSource(
extension: string,
log: any,
workspaceFolder: string
): Promise<boolean> {
const extensionsDirectory = path.join(workspaceFolder, "_extensions");
const existingExtensions = getMtimeExtensions(extensionsDirectory);

const success = await installQuartoExtension(extension, log);

const newExtension = findModifiedExtensions(existingExtensions, extensionsDirectory);
const fileNames = ["_extension.yml", "_extension.yaml"];
const filePath = fileNames
.map((name) => path.join(extensionsDirectory, ...newExtension, name))
.find((fullPath) => fs.existsSync(fullPath));
if (filePath) {
const fileContent = fs.readFileSync(filePath, "utf-8");
const updatedContent = fileContent.includes("source: ")
? fileContent.replace(/source: .*/, `source: ${extension}`)
: `${fileContent.trim()}\nsource: ${extension}`;
fs.writeFileSync(filePath, updatedContent);
}
vscode.commands.executeCommand("quartoWizard.extensionsInstalled.refresh");
return success;
}

export async function removeQuartoExtension(extension: string, log: vscode.OutputChannel): Promise<boolean> {
log.appendLine(`\n\nRemoving ${extension} ...`);

Expand Down

0 comments on commit 20475a1

Please sign in to comment.