Skip to content

Commit

Permalink
feat(core): add a generator for dotnet restore
Browse files Browse the repository at this point in the history
Add a generator to restore packages and tools for builders to use
  • Loading branch information
Ben Callaghan committed May 18, 2021
1 parent 6ab5d4a commit 96082a1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"factory": "./src/generators/nuget-reference/generator",
"schema": "./src/generators/nuget-reference/schema.json",
"description": "nuget-reference generator"
},
"restore": {
"factory": "./src/generators/restore/generator",
"schema": "./src/generators/restore/schema.json",
"description": "Restores NuGet packages and .NET tools used by the workspace"
}
}
}
38 changes: 38 additions & 0 deletions packages/core/src/generators/restore/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

import { prompt } from 'inquirer';

import { updateConfig, getNxDotnetProjects } from '@nx-dotnet/utils';

import generator from './generator';

import PromptUI = require('inquirer/lib/ui/prompt');

jest.mock('../../../../dotnet/src/lib/core/dotnet.client');
jest.mock('../../../../utils/src/lib/utility-functions/workspace');
jest.mock('inquirer');

describe('restore generator', () => {
let appTree: Tree;

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
updateConfig(appTree, { nugetPackages: {} });

(prompt as jest.MockedFunction<typeof prompt>)
.mockReset()
.mockImplementation((async () => {
return {};
}) as () => Promise<unknown> & { ui: PromptUI });

(getNxDotnetProjects as jest.MockedFunction<typeof getNxDotnetProjects>)
.mockReset()
.mockImplementation(() => new Map());
});

it('should run successfully', async () => {
await generator(appTree);
expect(true).toBeTruthy();
});
});
21 changes: 21 additions & 0 deletions packages/core/src/generators/restore/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Tree } from '@nrwl/devkit';
import { DotNetClient, dotnetFactory } from '@nx-dotnet/dotnet';
import {
getNxDotnetProjects,
getProjectFilesForProject,
} from '@nx-dotnet/utils';

export default async function (
host: Tree,
dotnetClient = new DotNetClient(dotnetFactory()),
) {
const projects = getNxDotnetProjects(host);
for (const project of projects.values()) {
const projectFiles = getProjectFilesForProject(host, project);
for (const file of projectFiles) {
dotnetClient.restorePackages(file);
}
}

dotnetClient.restoreTools();
}
9 changes: 9 additions & 0 deletions packages/core/src/generators/restore/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"id": "Restore",
"title": "",
"type": "object",
"description": "Restores NuGet packages and .NET tools used by the workspace.",
"properties": {}
}
10 changes: 10 additions & 0 deletions packages/dotnet/src/lib/core/dotnet.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ export class DotNetClient {
return this.logAndExecute(cmd);
}

restorePackages(project: string): Buffer {
const cmd = `${this.cliCommand.command} restore ${project}`;
return this.logAndExecute(cmd);
}

restoreTools(): Buffer {
const cmd = `${this.cliCommand.command} tool restore`;
return this.logAndExecute(cmd);
}

private logAndExecute(cmd: string): Buffer {
console.log(`Executing Command: ${cmd}`);
return execSync(cmd, { stdio: 'inherit' });
Expand Down

0 comments on commit 96082a1

Please sign in to comment.