Skip to content

Commit

Permalink
[tsp-client] Add support for private repo specs (#7525)
Browse files Browse the repository at this point in the history
* assume by default the tspconfig is a url

* check if config file exists in local file system

* remove unused fetch function

* get tspconfig.yaml through git to account for private repo links

* changelog
  • Loading branch information
catalinaperalta authored Jan 23, 2024
1 parent 9868d72 commit adc439e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
6 changes: 6 additions & 0 deletions tools/tsp-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release

## Unreleased - 0.4.0

- Added support for initializing a project from a private repository specification.
- Changed `doesFileExist()` function to check local file system.
- Removed `fetch()` function.

## 2023-12-21 - 0.3.0

- Fix TypeSpec compilation issue with module-resolver.
Expand Down
17 changes: 13 additions & 4 deletions tools/tsp-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TspLocation, compileTsp, discoverMainFile, resolveTspConfigUrl } from "
import { getOptions } from "./options.js";
import { mkdir, writeFile, cp, readFile } from "node:fs/promises";
import { addSpecFiles, checkoutCommit, cloneRepo, getRepoRoot, sparseCheckout } from "./git.js";
import { fetch } from "./network.js";
import { parse as parseYaml } from "yaml";
import { joinPaths, resolvePath } from "@typespec/compiler";
import { formatAdditionalDirectories, getAdditionalDirectoryName } from "./utils.js";
Expand All @@ -29,10 +28,18 @@ async function sdkInit(
}): Promise<string> {
if (isUrl) {
// URL scenario
const repoRoot = await getRepoRoot(outputDir);
const resolvedConfigUrl = resolveTspConfigUrl(config);
Logger.debug(`Resolved config url: ${resolvedConfigUrl.resolvedUrl}`)
const tspConfig = await fetch(resolvedConfigUrl.resolvedUrl);
const configYaml = parseYaml(tspConfig);
const cloneDir = joinPaths(repoRoot, "..", "sparse-spec");
await mkdir(cloneDir, { recursive: true });
Logger.debug(`Created temporary sparse-checkout directory ${cloneDir}`);
Logger.debug(`Cloning repo to ${cloneDir}`);
await cloneRepo(outputDir, cloneDir, `https://github.com/${resolvedConfigUrl.repo}.git`);
await sparseCheckout(cloneDir);
const tspConfigPath = joinPaths(resolvedConfigUrl.path, "tspconfig.yaml");
await addSpecFiles(cloneDir, tspConfigPath)
await checkoutCommit(cloneDir, resolvedConfigUrl.commit);
const configYaml = parseYaml(joinPaths(cloneDir, tspConfigPath));
const serviceDir = configYaml?.parameters?.["service-dir"]?.default;
if (!serviceDir) {
throw new Error(`Parameter service-dir is not defined correctly in tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`)
Expand All @@ -48,6 +55,8 @@ async function sdkInit(
await writeFile(
joinPaths(newPackageDir, "tsp-location.yaml"),
`directory: ${resolvedConfigUrl.path}\ncommit: ${resolvedConfigUrl.commit}\nrepo: ${resolvedConfigUrl.repo}\nadditionalDirectories:${additionalDirOutput}\n`);
Logger.debug(`Removing sparse-checkout directory ${cloneDir}`);
await removeDirectory(cloneDir);
return newPackageDir;
} else {
// Local directory scenario
Expand Down
19 changes: 4 additions & 15 deletions tools/tsp-client/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import { createDefaultHttpClient, createPipelineRequest } from "@azure/core-rest-pipeline";

const httpClient = createDefaultHttpClient();

export async function fetch(url: string, method: "GET" | "HEAD" = "GET"): Promise<string> {
const result = await httpClient.sendRequest(createPipelineRequest({ url, method }));
if (result.status !== 200) {
throw new Error(`failed to fetch ${url}: ${result.status}`);
}
return String(result.bodyAsText);
}
import { stat } from "fs/promises";

export function isValidUrl(url: string) {
try {
Expand All @@ -19,8 +9,7 @@ export function isValidUrl(url: string) {
}
}

export function doesFileExist(url: string): Promise<boolean> {
return fetch(url, "HEAD")
.then(() => true)
.catch(() => false);
// Checks if a file exists locally
export function doesFileExist(path: string): Promise<boolean> {
return stat(path).then(() => true).catch(() => false);
}
4 changes: 2 additions & 2 deletions tools/tsp-client/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ export async function getOptions(): Promise<Options> {
process.exit(1);
}

let isUrl = false;
let isUrl = true;
if (positionals[0] === "init") {
if (!values["tsp-config"]) {
Logger.error("tspConfig is required");
printUsage();
process.exit(1);
}
if (await doesFileExist(values["tsp-config"])) {
isUrl = true;
isUrl = false;
}
if (!isUrl) {
if (!values.commit || !values.repo) {
Expand Down

0 comments on commit adc439e

Please sign in to comment.