-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathPqSdkNugetPackageService.ts
More file actions
165 lines (143 loc) · 7.02 KB
/
Copy pathPqSdkNugetPackageService.ts
File metadata and controls
165 lines (143 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the MIT license found in the
* LICENSE file in the root of this projects source tree.
*/
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { GlobalEventBus, GlobalEvents } from "../GlobalEventBus";
import { debounce } from "../utils/debounce";
import { ExtensionConfigurations } from "../constants/PowerQuerySdkConfiguration";
import { ExtensionConstants } from "../constants/PowerQuerySdkExtension";
import { NugetCommandService } from "./nuget/NugetCommandService";
import { NugetHttpService } from "./nuget/NugetHttpService";
import { NugetVersions } from "../utils/NugetVersions";
import { PqSdkOutputChannel } from "../features/PqSdkOutputChannel";
export class PqSdkNugetPackageService {
private readonly nugetHttpService: NugetHttpService;
private readonly nugetCommandService: NugetCommandService;
private readonly nullableMaximumPqTestNugetVersion?: NugetVersions = ExtensionConstants.MaximumPqTestNugetVersion
? NugetVersions.createFromFuzzyVersionString(ExtensionConstants.MaximumPqTestNugetVersion)
: undefined;
private readonly nullableMinimumPqTestNugetVersion?: NugetVersions = ExtensionConstants.MinimumPqTestNugetVersion
? NugetVersions.createFromFuzzyVersionString(ExtensionConstants.MinimumPqTestNugetVersion)
: undefined;
constructor(
readonly vscExtCtx: vscode.ExtensionContext,
readonly globalEventBus?: GlobalEventBus,
readonly outputChannel?: PqSdkOutputChannel,
) {
this.nugetHttpService = new NugetHttpService(outputChannel);
this.nugetCommandService = new NugetCommandService(vscExtCtx.extensionPath, outputChannel);
this.globalEventBus?.on(
GlobalEvents.VSCodeEvents.onProxySettingsChanged,
debounce(() => {
this.nugetHttpService.updateAxiosInstance(
NugetHttpService.DefaultBaseUrl,
ExtensionConfigurations.httpProxy,
ExtensionConfigurations.httpProxyAuthorization,
);
}, 750).bind(this.nugetHttpService),
);
}
/**
* Return a list of nuget versions less or equal to `options.maximumNugetVersion` if it got specified
* Otherwise,
* return the latest version in `Latest` version tag mode
* return the closest version in `Customized` version tag mode
* @param options
*/
public async findNullableNewPqSdkVersion(
options: {
maximumNugetVersion?: NugetVersions;
minimumNugetVersion?: NugetVersions;
} = {},
): Promise<string | undefined> {
let sortedNugetVersions: NugetVersions[];
if (ExtensionConfigurations.externalsVersionTag === "Recommended") {
// force limiting the maximum versions of the nuget list result
if (!options.maximumNugetVersion) {
// always restrain the version found beneath the MaximumPqTestNugetVersion
options.maximumNugetVersion = this.nullableMaximumPqTestNugetVersion;
}
if (!options.minimumNugetVersion) {
// always restrain the version found above the MinimumPqTestNugetVersion
options.minimumNugetVersion = this.nullableMinimumPqTestNugetVersion;
}
} else {
// in other cases, always force returning the whole list of the nuget versions
options = {};
}
if (ExtensionConfigurations.nugetPath) {
sortedNugetVersions = await this.nugetCommandService.getSortedPackageReleasedVersions(
ExtensionConfigurations.nugetPath,
ExtensionConfigurations.nugetFeed,
ExtensionConstants.PublicMsftPqSdkToolsNugetName,
options,
);
} else {
// we gonna use http endpoint to query the public feed
sortedNugetVersions = await this.nugetHttpService.getSortedPackageReleasedVersions(
ExtensionConstants.PublicMsftPqSdkToolsNugetName,
options,
);
}
if (ExtensionConfigurations.externalsVersionTag === "Custom") {
// need to find the closest version among the result list:
const expectedNugetVersion: NugetVersions = NugetVersions.createFromFuzzyVersionString(
ExtensionConfigurations.PQTestVersion || ExtensionConstants.SuggestedPqTestNugetVersion,
);
const closestVersion: NugetVersions = NugetVersions.findClosetAmong(
sortedNugetVersions,
expectedNugetVersion,
);
return closestVersion === NugetVersions.ZERO_VERSION
? ExtensionConstants.SuggestedPqTestNugetVersion
: closestVersion.toString();
} else if (sortedNugetVersions.length && !sortedNugetVersions[sortedNugetVersions.length - 1].isZero()) {
return sortedNugetVersions[sortedNugetVersions.length - 1].toString();
} else {
return undefined;
}
}
public expectedPqSdkPath(maybeNextVersion: string | undefined): string {
return this.nugetCommandService.expectedPackagePath(
ExtensionConstants.InternalMsftPqSdkToolsNugetName,
maybeNextVersion || ExtensionConstants.SuggestedPqTestNugetVersion,
);
}
public nugetPqSdkExistsSync(maybeNextVersion?: string): boolean {
const expectedPqTestPath: string = this.expectedPqSdkPath(maybeNextVersion);
return fs.existsSync(expectedPqTestPath);
}
public async updatePqSdkFromNuget(maybeNextVersion: string | undefined): Promise<string | undefined> {
const baseNugetFolder: string = path.resolve(this.vscExtCtx.extensionPath, ExtensionConstants.NugetBaseFolder);
if (!fs.existsSync(baseNugetFolder)) {
fs.mkdirSync(baseNugetFolder);
}
if (ExtensionConfigurations.nugetPath) {
return this.nugetCommandService.downloadAndExtractNugetPackage(
ExtensionConfigurations.nugetPath,
ExtensionConfigurations.nugetFeed,
ExtensionConstants.InternalMsftPqSdkToolsNugetName,
maybeNextVersion || ExtensionConstants.SuggestedPqTestNugetVersion,
);
} else {
const pqTestFullPath: string = this.expectedPqSdkPath(maybeNextVersion);
if (fs.existsSync(pqTestFullPath)) return pqTestFullPath;
await this.nugetHttpService.downloadAndExtractNugetPackage(
ExtensionConstants.PublicMsftPqSdkToolsNugetName,
maybeNextVersion ?? ExtensionConstants.SuggestedPqTestNugetVersion,
path.join(
baseNugetFolder,
`${ExtensionConstants.PublicMsftPqSdkToolsNugetName}.${
maybeNextVersion || ExtensionConstants.SuggestedPqTestNugetVersion
}`,
),
);
return fs.existsSync(pqTestFullPath) ? pqTestFullPath : undefined;
}
}
}