Skip to content

Commit

Permalink
feat(core): allow tool installation to be skipped for update-swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jun 8, 2022
1 parent c189540 commit d1044f6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
4 changes: 2 additions & 2 deletions e2e/core-e2e/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ public class UnitTest1
});
});

describe('swagger integration', () => {
it('should generate swagger project for webapi', async () => {
fdescribe('swagger integration', () => {
fit('should generate swagger project for webapi', async () => {
const api = uniq('api');
await runNxCommandAsync(
`generate @nx-dotnet/core:app ${api} --language="C#" --template="webapi"`,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"kill-port": "^1.6.1",
"lint-staged": "^12.3.4",
"nx": "13.10.2",
"openapi-types": "^11.0.1",
"prettier": "2.5.1",
"run-p": "*",
"semantic-release": "^19.0.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"factory": "./src/generators/add-swagger-target/add-swagger-target",
"schema": "./src/generators/add-swagger-target/schema.json",
"description": "Generate a target to extract the swagger.json file from a .NET webapi"
},
"swagger-typescript": {
"factory": "./src/generators/swagger-typescript/generator",
"schema": "./src/generators/swagger-typescript/schema.json",
"description": "swagger-typescript generator"
}
}
}
23 changes: 22 additions & 1 deletion packages/core/src/executors/update-swagger/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const options: UpdateSwaggerJsonExecutorSchema = {
output: '',
startupAssembly: '',
swaggerDoc: '',
skipInstall: false,
};

const root = '/virtual';
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('Update-Swagger Executor', () => {
expect(res.success).toBeTruthy();
});

it(`doesnt install ${SWAGGER_CLI_TOOL} if already installed`, async () => {
it(`doesn't install ${SWAGGER_CLI_TOOL} if already installed`, async () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'readFileSync').mockImplementation((p): string => {
if (p === '1.csproj') {
Expand All @@ -134,4 +135,24 @@ describe('Update-Swagger Executor', () => {
).not.toHaveBeenCalled();
expect(res.success).toBeTruthy();
});

it(`skips installation when skipInstall is true`, async () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
jest.spyOn(fs, 'readFileSync').mockImplementation((p): string => {
if (p === '1.csproj') {
return mockCSProj;
}
throw new Error('Attempted to read unexpected file');
});
jest.spyOn(devkit, 'readJsonFile').mockReturnValue({});
const res = await executor(
{ ...options, skipInstall: true },
context,
dotnetClient,
);
expect(
(dotnetClient as jest.Mocked<DotNetClient>).installTool,
).not.toHaveBeenCalled();
expect(res.success).toBeTruthy();
});
});
14 changes: 9 additions & 5 deletions packages/core/src/executors/update-swagger/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function normalizeOptions(
opts.startupAssembly ??
buildStartupAssemblyPath(projectName, project, csProjFilePath),
swaggerDoc: opts.swaggerDoc ?? 'v1',
skipInstall: opts.skipInstall ?? false,
};
}

Expand Down Expand Up @@ -85,11 +86,14 @@ export default async function runExecutor(
);
ensureDirSync(dirname(options.output));

ensureSwaggerToolInstalled(
context,
dotnetClient,
await readSwashbuckleVersion(csProjFilePath),
);
if (!options.skipInstall) {
ensureSwaggerToolInstalled(
context,
dotnetClient,
await readSwashbuckleVersion(csProjFilePath),
);
}

dotnetClient.runTool('swagger', [
'tofile',
'--output',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/executors/update-swagger/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface UpdateSwaggerJsonExecutorSchema {
output: string;
swaggerDoc: string;
startupAssembly: string;
skipInstall: boolean;
}
5 changes: 5 additions & 0 deletions packages/core/src/executors/update-swagger/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"startupAssembly": {
"type": "string",
"description": "Path from workspace root to the built api's startup dll file"
},
"skipInstall": {
"type": "boolean",
"description": "Skips installing Swashbuckle.AspNetCore.Cli. This option should be used if you are managing the installation on your own.",
"default": false
}
},
"required": []
Expand Down
5 changes: 4 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10297,7 +10297,10 @@ open@^8.0.9, open@^8.4.0:
is-docker "^2.1.1"
is-wsl "^2.2.0"

opener@^1.5.2:
openapi-types@^11.0.1:
version "11.0.1"
resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-11.0.1.tgz#7e1cee2c9b8cb16787d14f2b0f433c5ba0e32e96"
integrity sha512-P2pGRlHFXgP8z6vrp5P/MtftOXYtlIY1A+V0VmioOoo85NN6RSPgGbEprRAUNMIsbfRjnCPdx/r8mi8QRR7grQ==
version "1.5.2"
resolved "https://registry.yarnpkg.com/opener/-/opener-1.5.2.tgz#5d37e1f35077b9dcac4301372271afdeb2a13598"
integrity sha512-ur5UIdyw5Y7yEj9wLzhqXiy6GZ3Mwx0yGI+5sMn2r0N0v3cKJvUmFH5yPP+WXh9e0xfyzyJX95D8l088DNFj7A==
Expand Down

0 comments on commit d1044f6

Please sign in to comment.