Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

`tsp init` adds `bin/` and `obj/` to the generated `.gitignore` when the C# server emitter is selected. Init templates can list `gitignorePatterns` for each emitter to add their own lines.

```json
"emitters": {
"@typespec/http-server-csharp": {
"label": "C# server stubs",
"gitignorePatterns": ["bin/", "obj/"]
}
}
```
1 change: 1 addition & 0 deletions packages/compiler/.scripts/build-init-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const builtInTemplates: Record<string, InitTemplate> = {
\tMore information on getting started:
\thttps://aka.ms/tsp/hscs/start
`,
gitignorePatterns: ["bin/", "obj/"],
},
"@typespec/http-server-js": {
label: "JavaScript server stubs",
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler/src/init/init-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface EmitterTemplate {
message?: string;
/** Optional specific emitter version. `latest` if not specified */
version?: string;
/** Optional lines to add to the generated .gitignore when this emitter is selected */
gitignorePatterns?: string[];
}

/**
Expand Down Expand Up @@ -151,6 +153,7 @@ export const InitTemplateSchema: JSONSchemaType<InitTemplate> = {
options: {} as any,
message: { type: "string", nullable: true },
version: { type: "string", nullable: true },
gitignorePatterns: { type: "array", items: { type: "string" }, nullable: true },
},
required: [],
},
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler/src/init/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,13 @@ async function writeGitignore(host: SystemHost, config: ScaffoldingConfig) {
return;
}

return host.writeFile(joinPaths(config.directory, ".gitignore"), defaultGitignore);
const extraLines = new Set<string>(
Object.values(config.emitters).flatMap((emitter) => emitter?.gitignorePatterns ?? []),
);
const content = extraLines.size
? [defaultGitignore, "", ...extraLines].join("\n")
: defaultGitignore;
return host.writeFile(joinPaths(config.directory, ".gitignore"), content);
}

async function writeFiles(host: SystemHost, config: ScaffoldingConfig) {
Expand Down
6 changes: 5 additions & 1 deletion packages/compiler/templates/scaffolding.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
"options": {
"emitter-output-dir": "{output-dir}/server/aspnet"
},
"message": "\n\tGenerated ASP.Net services require dotnet 9:\n\thttps://dotnet.microsoft.com/download \n\n\tCreate an ASP.Net service project for your TypeSpec:\n\t> npx hscs-scaffold . --use-swaggerui --overwrite\n\n\tMore information on getting started:\n\thttps://aka.ms/tsp/hscs/start\n "
"message": "\n\tGenerated ASP.Net services require dotnet 9:\n\thttps://dotnet.microsoft.com/download \n\n\tCreate an ASP.Net service project for your TypeSpec:\n\t> npx hscs-scaffold . --use-swaggerui --overwrite\n\n\tMore information on getting started:\n\thttps://aka.ms/tsp/hscs/start\n ",
"gitignorePatterns": [
"bin/",
"obj/"
]
},
"@typespec/http-server-js": {
"label": "JavaScript server stubs",
Expand Down
26 changes: 26 additions & 0 deletions packages/compiler/test/cli/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const TEST_SCAFFOLDING = {
options: {
"emitter-output-dir": "{output-dir}/clients/csharp",
},
gitignorePatterns: ["from-csharp-client/"],
},
},
files: [],
Expand Down Expand Up @@ -235,6 +236,31 @@ it("should support overriding emitters", async () => {
expect(tspConfig.emit).toEqual(["@typespec/openapi3", "@typespec/http-client-csharp"]);
});

it("leaves out gitignore patterns of emitters that are not selected", async () => {
const { fs, init } = await createTestFSWithCliCompilerHost();

await init("/tmp/test-project", {
template: "foo",
"no-prompt": true,
});

const gitignore = fs.get("/tmp/test-project/.gitignore");
expect(gitignore).toBeDefined();
expect(gitignore).not.toContain("from-csharp-client/");
});

it("adds gitignore patterns of emitters that are selected", async () => {
const { fs, init } = await createTestFSWithCliCompilerHost();

await init("/tmp/test-project", {
template: "foo",
"no-prompt": true,
emitters: ["@typespec/openapi3", "@typespec/http-client-csharp"],
});

expect(fs.get("/tmp/test-project/.gitignore")?.split("\n")).toContain("from-csharp-client/");
});

it("defaults to initialValue for parameters", async () => {
const { compilerHost, init } = await createTestFSWithCliCompilerHost();
await init("/tmp/test-project", {
Expand Down
23 changes: 23 additions & 0 deletions packages/compiler/test/init/init-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ it("can exclude .gitignore file", async () => {
strictEqual(typeof getOutputFile(".gitignore"), "undefined");
});

it("adds gitignore patterns from selected emitters to .gitignore file", async () => {
const emitters = {
foo: { selected: true, gitignorePatterns: ["bin/", "obj/"] },
};

await runTemplate({ emitters }, { emitters });
const output = getOutputFile(".gitignore");
ok(output);
expect(output.split("\n")).toEqual(expect.arrayContaining(["bin/", "obj/"]));
});

it("adds a gitignore pattern only once when multiple emitters specify it", async () => {
const emitters = {
foo: { selected: true, gitignorePatterns: ["bin/", "obj/"] },
bar: { selected: true, gitignorePatterns: ["bin/"] },
};

await runTemplate({ emitters }, { emitters });
const output = getOutputFile(".gitignore");
ok(output);
expect(output.split("\n").filter((line) => line === "bin/")).toHaveLength(1);
});

it("specifying both config and emitters merge the 2", async () => {
const emitters = {
foo: { selected: true, options: { opt1: "val-1" } },
Expand Down