Skip to content

Commit

Permalink
chore: add debug logs and dependabot
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseVSeb committed Feb 21, 2024
1 parent 4c3f477 commit 113637d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"
29 changes: 18 additions & 11 deletions src/getLatestReleaseOfGoogleJavaFormat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { LogOutputChannel } from "vscode";
import {
GoogleJavaFormatReleaseResponse,
parseGoogleJavaFormatReleaseResponse,
} from "./GoogleJavaFormatRelease";
import { logAsyncFunction } from "./logFunction";

export const getLatestReleaseOfGoogleJavaFormat = async () => {
const response = await fetch(
"https://api.github.com/repos/google/google-java-format/releases/latest",
);
if (!response.ok) {
throw new Error("Failed to get latest release of Google Java Format.");
}
export const getLatestReleaseOfGoogleJavaFormat = logAsyncFunction(
async function getLatestReleaseOfGoogleJavaFormat(log: LogOutputChannel) {
const url =
"https://api.github.com/repos/google/google-java-format/releases/latest";
log.debug("Fetching:", url);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
"Failed to get latest release of Google Java Format.",
);
}

return parseGoogleJavaFormatReleaseResponse(
(await response.json()) as GoogleJavaFormatReleaseResponse,
);
};
return parseGoogleJavaFormatReleaseResponse(
(await response.json()) as GoogleJavaFormatReleaseResponse,
);
},
);
31 changes: 18 additions & 13 deletions src/getReleaseOfGoogleJavaFormatByVersion.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { LogOutputChannel } from "vscode";
import { GoogleJavaFormatVersion } from "./ExtensionConfiguration";
import {
GoogleJavaFormatReleaseResponse,
parseGoogleJavaFormatReleaseResponse,
} from "./GoogleJavaFormatRelease";
import { logAsyncFunction } from "./logFunction";

export const getReleaseOfGoogleJavaFormatByVersion = async (
version: Exclude<GoogleJavaFormatVersion, "latest">,
) => {
const response = await fetch(
`https://api.github.com/repos/google/google-java-format/releases/tags/v${version}`,
);
if (!response.ok) {
throw new Error(`Failed to get v${version} of Google Java Format.`);
}
export const getReleaseOfGoogleJavaFormatByVersion = logAsyncFunction(
async function getReleaseOfGoogleJavaFormatByVersion(
log: LogOutputChannel,
version: Exclude<GoogleJavaFormatVersion, "latest">,
) {
const url = `https://api.github.com/repos/google/google-java-format/releases/tags/v${version}`;
log.debug("Fetching:", url);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to get v${version} of Google Java Format.`);
}

return parseGoogleJavaFormatReleaseResponse(
(await response.json()) as GoogleJavaFormatReleaseResponse,
);
};
return parseGoogleJavaFormatReleaseResponse(
(await response.json()) as GoogleJavaFormatReleaseResponse,
);
},
);
23 changes: 16 additions & 7 deletions src/getUriFromString.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Uri } from "vscode";
import { LogOutputChannel, Uri } from "vscode";
import { logFunction } from "./logFunction";

export function getUriFromString(value: string) {
try {
return Uri.parse(value, true);
} catch (e) {
return Uri.file(value);
}
function isRemote(value: string | null) {
return (
value !== null &&
(value.startsWith("http:/") ||
value.startsWith("https:/") ||
value.startsWith("file:/"))
);
}

export const getUriFromString = logFunction(function getUriFromString(
log: LogOutputChannel,
value: string,
) {
return isRemote(value) ? Uri.parse(value, true) : Uri.file(value);
});
51 changes: 51 additions & 0 deletions src/logFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { LogOutputChannel } from "vscode";

export function logFunction<Args extends unknown[], R>(
fn: (log: LogOutputChannel, ...args: Args) => R,
) {
return function (log: LogOutputChannel, ...args: Args) {
log.debug(
`Calling ${`"${fn.name}"` || "anonymous function"} with arguments:`,
args,
);
try {
const result = fn(log, ...args);
log.debug(
`${`"${fn.name}"` || "anonymous function"} returned:`,
result,
);
return result;
} catch (error) {
log.error(
`Error in ${`"${fn.name}"` || "anonymous function"}:`,
error,
);
throw error;
}
};
}

export function logAsyncFunction<Args extends unknown[], R>(
fn: (log: LogOutputChannel, ...args: Args) => Promise<R>,
) {
return async function (log: LogOutputChannel, ...args: Args) {
log.debug(
`Calling ${`"${fn.name}"` || "anonymous function"} with arguments:`,
args,
);
try {
const result = await fn(log, ...args);
log.debug(
`${`"${fn.name}"` || "anonymous function"} returned:`,
result,
);
return result;
} catch (error) {
log.error(
`Error in ${`"${fn.name}"` || "anonymous function"}:`,
error,
);
throw error;
}
};
}
6 changes: 3 additions & 3 deletions src/resolveExecutableFileFromConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function resolveExecutableFileFromConfig(
if (executable) {
log.debug(`Using config key 'executable': ${executable}`);

return getUriFromString(executable);
return getUriFromString(log, executable);
}

const shouldCheckNativeBinary = mode === "native-binary";
Expand All @@ -26,8 +26,8 @@ export async function resolveExecutableFileFromConfig(

const { assets } =
version && version !== "latest"
? await getReleaseOfGoogleJavaFormatByVersion(version)
: await getLatestReleaseOfGoogleJavaFormat();
? await getReleaseOfGoogleJavaFormatByVersion(log, version)
: await getLatestReleaseOfGoogleJavaFormat(log);

const url =
(shouldCheckNativeBinary && assets.get(system)) || assets.get("java")!;
Expand Down

0 comments on commit 113637d

Please sign in to comment.