Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear nullability warnings in src #5236

Merged
merged 19 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 17 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
8 changes: 4 additions & 4 deletions src/DotnetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export interface DotnetPackExtensionExports {
getDotnetPath(version?: string): Promise<string | undefined>;
}

export async function getDotnetPackApi(): Promise<DotnetPackExtensionExports> {
export async function getDotnetPackApi(): Promise<DotnetPackExtensionExports | undefined> {
const dotnetExtension = vscode.extensions.getExtension<DotnetPackExtensionExports>(dotnetPackExtensionId);
if (!dotnetExtension) {
return null;
if (dotnetExtension === undefined) {
return undefined;
}

if (!dotnetExtension.isActive) {
await dotnetExtension.activate();
}

return dotnetExtension.exports;
}
}
4 changes: 2 additions & 2 deletions src/InstallRuntimeDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function installRuntimeDependencies(packageJSON: any, extensionPath
const packagesToInstall = await getAbsolutePathPackagesToInstall(runTimeDependencies, platformInfo, extensionPath);
const filteredPackages = filterOmniSharpPackage(packagesToInstall, useFramework);

if (filteredPackages && filteredPackages.length > 0) {
if (filteredPackages.length > 0) {
eventStream.post(new PackageInstallation("C# dependencies"));
// Display platform information and RID
eventStream.post(new LogPlatformInfo(platformInfo));
Expand All @@ -36,5 +36,5 @@ export async function installRuntimeDependencies(packageJSON: any, extensionPath
function filterOmniSharpPackage(packages: AbsolutePathPackage[], useFramework: boolean) {
// Since we will have more than one OmniSharp package defined for some platforms, we need
// to filter out the one that doesn't match which dotnet runtime is being used.
return packages.filter(pkg => pkg.id != "OmniSharp" || pkg.isFramework === useFramework);
return packages.filter(pkg => pkg.id !== "OmniSharp" || pkg.isFramework === useFramework);
}
4 changes: 2 additions & 2 deletions src/NestedError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

export class NestedError extends Error {
constructor(public message: string, public err: Error = null) {
constructor(public message: string, public err?: Error) {
super(message);
}
}
}
4 changes: 2 additions & 2 deletions src/NetworkSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export interface NetworkSettingsProvider {
export function vscodeNetworkSettingsProvider(vscode: vscode): NetworkSettingsProvider {
return () => {
const config = vscode.workspace.getConfiguration();
const proxy = config.get<string>('http.proxy');
const proxy = config.get<string>('http.proxy', '');
const strictSSL = config.get('http.proxyStrictSSL', true);
return new NetworkSettings(proxy, strictSSL);
};
}
}
145 changes: 63 additions & 82 deletions src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,114 +18,75 @@ import { OmniSharpServer } from './omnisharp/server';
import { tolerantParse } from './json';

export class AssetGenerator {
public workspaceFolder: vscode.WorkspaceFolder;
public vscodeFolder: string;
public tasksJsonPath: string;
public launchJsonPath: string;

private executeableProjects: protocol.MSBuildProject[];
private executableProjects: protocol.MSBuildProject[] = [];
private startupProject: protocol.MSBuildProject | undefined;
private fallbackBuildProject: protocol.MSBuildProject;

public constructor(workspaceInfo: protocol.WorkspaceInformationResponse, workspaceFolder: vscode.WorkspaceFolder = undefined) {
if (workspaceFolder) {
this.workspaceFolder = workspaceFolder;
}
else {
let resourcePath: string = undefined;

if (!resourcePath && workspaceInfo.Cake) {
resourcePath = workspaceInfo.Cake.Path;
}

if (!resourcePath && workspaceInfo.ScriptCs) {
resourcePath = workspaceInfo.ScriptCs.Path;
}

if (!resourcePath && workspaceInfo.DotNet && workspaceInfo.DotNet.Projects.length > 0) {
resourcePath = workspaceInfo.DotNet.Projects[0].Path;
}

if (!resourcePath && workspaceInfo.MsBuild) {
resourcePath = workspaceInfo.MsBuild.SolutionPath;
}

this.workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(resourcePath));
}
private fallbackBuildProject: protocol.MSBuildProject | undefined;

public constructor(workspaceInfo: protocol.WorkspaceInformationResponse, private workspaceFolder: vscode.WorkspaceFolder) {
this.vscodeFolder = path.join(this.workspaceFolder.uri.fsPath, '.vscode');
this.tasksJsonPath = path.join(this.vscodeFolder, 'tasks.json');
this.launchJsonPath = path.join(this.vscodeFolder, 'launch.json');

this.startupProject = undefined;
this.fallbackBuildProject = undefined;

if (workspaceInfo.MsBuild && workspaceInfo.MsBuild.Projects.length > 0) {
this.executeableProjects = protocol.findExecutableMSBuildProjects(workspaceInfo.MsBuild.Projects);
if (this.executeableProjects.length === 0) {
if (workspaceInfo.MsBuild !== undefined && workspaceInfo.MsBuild.Projects.length > 0) {
this.executableProjects = protocol.findExecutableMSBuildProjects(workspaceInfo.MsBuild.Projects);
if (this.executableProjects.length === 0) {
this.fallbackBuildProject = workspaceInfo.MsBuild.Projects[0];
}
} else {
this.executeableProjects = [];
}
}

public hasExecutableProjects(): boolean {
return this.executeableProjects.length > 0;
return this.executableProjects.length > 0;
}

public isStartupProjectSelected(): boolean {
if (this.startupProject) {
return true;
} else {
return false;
}
return this.startupProject !== undefined;
}

public async selectStartupProject(selectedIndex?: number): Promise<boolean> {
if (!this.hasExecutableProjects()) {
throw new Error("No executable projects");
}

if (this.executeableProjects.length === 1) {
this.startupProject = this.executeableProjects[0];
if (selectedIndex !== undefined) {
this.startupProject = this.executableProjects[selectedIndex];
return true;
} else {
const mapItemNameToProject: { [key: string]: protocol.MSBuildProject } = {};
const itemNames: string[] = [];
}

this.executeableProjects.forEach(project => {
const itemName = `${path.basename(project.Path, ".csproj")} (${project.Path})`;
itemNames.push(itemName);
mapItemNameToProject[itemName] = project;
if (this.executableProjects.length === 1) {
this.startupProject = this.executableProjects[0];
return true;
} else {
const items = this.executableProjects.map(project => ({
label: `${path.basename(project.Path, ".csproj")} (${project.Path})`,
project,
}));

const selectedItem = await vscode.window.showQuickPick(items, {
matchOnDescription: true,
placeHolder: "Select the project to launch"
});

let selectedItem: string;
if (selectedIndex != null) {
selectedItem = itemNames[selectedIndex];
}
else {
selectedItem = await vscode.window.showQuickPick(itemNames, {
matchOnDescription: true,
placeHolder: "Select the project to launch"
});
}
if (!selectedItem || !mapItemNameToProject[selectedItem]) {
if (selectedItem === undefined) {
return false;
}

this.startupProject = mapItemNameToProject[selectedItem];
this.startupProject = selectedItem.project;
return true;
}
}

// This method is used by the unit tests instead of selectStartupProject
public setStartupProject(index: number): void {
if (index >= this.executeableProjects.length) {
if (index >= this.executableProjects.length) {
throw new Error("Invalid project index");
}

this.startupProject = this.executeableProjects[index];
this.startupProject = this.executableProjects[index];
}

public hasWebServerDependency(): boolean {
Expand Down Expand Up @@ -465,9 +426,9 @@ async function getOperations(generator: AssetGenerator): Promise<AssetOperations
function getBuildTasks(tasksConfiguration: tasks.TaskConfiguration): tasks.TaskDescription[] {
let result: tasks.TaskDescription[] = [];

function findBuildTask(version: string, tasksDescriptions: tasks.TaskDescription[]) {
function findBuildTask(tasksDescriptions: tasks.TaskDescription[] | undefined) {
let buildTask = undefined;
if (tasksDescriptions) {
if (tasksDescriptions !== undefined) {
buildTask = tasksDescriptions.find(td => td.group === 'build');
}

Expand All @@ -476,18 +437,18 @@ function getBuildTasks(tasksConfiguration: tasks.TaskConfiguration): tasks.TaskD
}
}

findBuildTask(tasksConfiguration.version, tasksConfiguration.tasks);
findBuildTask(tasksConfiguration.tasks);

if (tasksConfiguration.windows) {
findBuildTask(tasksConfiguration.version, tasksConfiguration.windows.tasks);
findBuildTask(tasksConfiguration.windows.tasks);
}

if (tasksConfiguration.osx) {
findBuildTask(tasksConfiguration.version, tasksConfiguration.osx.tasks);
findBuildTask(tasksConfiguration.osx.tasks);
}

if (tasksConfiguration.linux) {
findBuildTask(tasksConfiguration.version, tasksConfiguration.linux.tasks);
findBuildTask(tasksConfiguration.linux.tasks);
}

return result;
Expand Down Expand Up @@ -618,13 +579,13 @@ export async function addTasksJsonIfNecessary(generator: AssetGenerator, operati
if (!fs.pathExistsSync(generator.tasksJsonPath)) {
// when tasks.json does not exist create it and write all the content directly
const tasksJsonText = JSON.stringify(tasksJson);
const tasksJsonTextFormatted = jsonc.applyEdits(tasksJsonText, jsonc.format(tasksJsonText, null, formattingOptions));
const tasksJsonTextFormatted = jsonc.applyEdits(tasksJsonText, jsonc.format(tasksJsonText, undefined, formattingOptions));
text = tasksJsonTextFormatted;
}
else {
// when tasks.json exists just update the tasks node
const ourConfigs = tasksJson.tasks;
const content = fs.readFileSync(generator.tasksJsonPath).toString();
const ourConfigs = tasksJson.tasks ?? [];
const content = fs.readFileSync(generator.tasksJsonPath, { encoding: 'utf8' });
const updatedJson = updateJsonWithComments(content, ourConfigs, 'tasks', 'label', formattingOptions);
text = updatedJson;
}
Expand Down Expand Up @@ -660,12 +621,12 @@ async function addLaunchJsonIfNecessary(generator: AssetGenerator, operations: A
"configurations": ${configurationsMassaged}
}`;

text = jsonc.applyEdits(launchJsonText, jsonc.format(launchJsonText, null, formattingOptions));
text = jsonc.applyEdits(launchJsonText, jsonc.format(launchJsonText, undefined, formattingOptions));
}
else {
// when launch.json exists replace or append our configurations
const ourConfigs = jsonc.parse(launchJsonConfigurations);
const content = fs.readFileSync(generator.launchJsonPath).toString();
const ourConfigs = jsonc.parse(launchJsonConfigurations) ?? [];
const content = fs.readFileSync(generator.launchJsonPath, { encoding: 'utf8' });
const updatedJson = updateJsonWithComments(content, ourConfigs, 'configurations', 'name', formattingOptions);
text = updatedJson;
}
Expand Down Expand Up @@ -711,15 +672,29 @@ export async function addAssetsIfNecessary(server: OmniSharpServer): Promise<Add
}

serverUtils.requestWorkspaceInformation(server).then(async info => {
const generator = new AssetGenerator(info);
const resourcePath = info.Cake?.Path ??
info.ScriptCs?.Path ??
info.DotNet?.Projects?.[0].Path ??
info.MsBuild?.SolutionPath;
if (resourcePath === undefined) {
// This shouldn't happen, but it's a cheap check.
return resolve(AddAssetResult.NotApplicable);
}

const workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(resourcePath));
if (workspaceFolder === undefined) {
return resolve(AddAssetResult.NotApplicable);
}

const generator = new AssetGenerator(info, workspaceFolder);
// If there aren't executable projects, we will not prompt
if (generator.hasExecutableProjects()) {
return getOperations(generator).then(operations => {
if (!hasAddOperations(operations)) {
return resolve(AddAssetResult.NotApplicable);
}

promptToAddAssets(generator.workspaceFolder).then(result => {
promptToAddAssets(workspaceFolder).then(result => {
if (result === PromptResult.Disable) {
return resolve(AddAssetResult.Disable);
}
Expand Down Expand Up @@ -775,7 +750,7 @@ async function getExistingAssets(generator: AssetGenerator) {
async function shouldGenerateAssets(generator: AssetGenerator): Promise<Boolean> {
return new Promise<Boolean>((resolve, reject) => {
getExistingAssets(generator).then(res => {
if (res && res.length) {
if (res.length > 0) {
const yesItem = { title: 'Yes' };
const cancelItem = { title: 'Cancel', isCloseAffordance: true };
vscode.window.showWarningMessage('Replace existing build and debug assets?', cancelItem, yesItem)
Expand All @@ -802,7 +777,13 @@ export async function generateAssets(server: OmniSharpServer, selectedIndex?: nu
try {
let workspaceInformation = await serverUtils.requestWorkspaceInformation(server);
if (workspaceInformation.MsBuild && workspaceInformation.MsBuild.Projects.length > 0) {
const generator = new AssetGenerator(workspaceInformation);
const resourcePath = workspaceInformation.MsBuild.SolutionPath;
const workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(resourcePath));
if (workspaceFolder === undefined) {
return;
}

const generator = new AssetGenerator(workspaceInformation, workspaceFolder);
let doGenerateAssets = await shouldGenerateAssets(generator);
if (!doGenerateAssets) {
return; // user cancelled
Expand Down
Loading