diff --git a/package-lock.json b/package-lock.json index 513ea2cf..1fd08018 100644 --- a/package-lock.json +++ b/package-lock.json @@ -261,9 +261,9 @@ "dev": true }, "adm-zip": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.0.tgz", - "integrity": "sha512-X6W8Pb1LFdDOQGMsO6OLGmRmyTzIxg5zjXitKjf2fQbqva4iAjcxWS4c01DH+HJ19n3qoQ9TstIRCxJmPOtDow==" + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.1.tgz", + "integrity": "sha512-a5ABmIFUJ9OxHV5zrXM9Q41JzpRIflFtdgpL4UQM9DsTHHxQzPRaeyAdnMW7kxL0NRWm/NHafJdj6pO+ty7L2g==" }, "agent-base": { "version": "4.3.0", diff --git a/package.json b/package.json index 2ff1c80c..2df7e61b 100644 --- a/package.json +++ b/package.json @@ -700,7 +700,7 @@ "depcheck": "depcheck" }, "dependencies": { - "adm-zip": "^0.5.0", + "adm-zip": "^0.5.1", "byline": "^5.0.0", "https-proxy-agent": "^5.0.0", "ini": "^1.3.4", diff --git a/recipe.cake b/recipe.cake index a8aec88d..06ad519e 100644 --- a/recipe.cake +++ b/recipe.cake @@ -1,10 +1,9 @@ -#load "nuget:https://www.nuget.org/api/v2?package=Cake.VsCode.Recipe&version=0.3.0" +#load "nuget:https://www.nuget.org/api/v2?package=Cake.VsCode.Recipe&version=0.4.0" if(BuildSystem.IsLocalBuild) { Environment.SetVariableNames( - githubUserNameVariable: "CAKE_GITHUB_USERNAME", - githubPasswordVariable: "CAKE_GITHUB_PASSWORD" + githubTokenVariable: "CAKE_GITHUB_PAT" ); } else diff --git a/src/addPackage/actions/fetchCakePackages.ts b/src/addPackage/actions/fetchCakePackages.ts index 2a7cd058..fea877bd 100644 --- a/src/addPackage/actions/fetchCakePackages.ts +++ b/src/addPackage/actions/fetchCakePackages.ts @@ -5,23 +5,25 @@ import { getFetchOptions } from '../../shared/utils'; import { window, workspace } from 'vscode'; import { CAKE_SEARCH_PAGE_SIZE, - NUGET_SEARCH_URL, CANCEL } from '../../constants'; -import { - NUGET_SEARCHING_PACKAGES -} from '../../shared/messages'; +import { NUGET_SEARCHING_PACKAGES } from '../../shared/messages'; +import { getNugetServiceUrl, NuGetServiceType } from '../../shared/nugetServiceUrl'; -export default function fetchCakePackages( +export default async function fetchCakePackages( value: string | undefined, - searchUrl: string = NUGET_SEARCH_URL, + searchUrl?: string, take: string = CAKE_SEARCH_PAGE_SIZE -): Promise | Promise { +): Promise { if (!value) { // User has canceled the process. return Promise.reject(CANCEL); } + if(!searchUrl){ + searchUrl = await getNugetServiceUrl(NuGetServiceType.SearchAutocompleteService); + } + window.setStatusBarMessage(NUGET_SEARCHING_PACKAGES); const queryParams = qs.stringify({ @@ -30,7 +32,7 @@ export default function fetchCakePackages( take: take }); - return fetch( + return await fetch( `${searchUrl}?${queryParams}`, getFetchOptions(workspace.getConfiguration('http')) ); diff --git a/src/addPackage/actions/fetchPackageVersions.ts b/src/addPackage/actions/fetchPackageVersions.ts index 442f4d38..1cd39e64 100644 --- a/src/addPackage/actions/fetchPackageVersions.ts +++ b/src/addPackage/actions/fetchPackageVersions.ts @@ -1,28 +1,29 @@ import { window, workspace } from 'vscode'; import fetch from 'node-fetch'; -import { Response } from 'node-fetch'; import { getFetchOptions } from '../../shared/utils'; -import { NUGET_VERSIONS_URL, CANCEL } from '../../constants'; +import { CANCEL } from '../../constants'; import { NUGET_LOADING_VERSIONS } from '../../shared/messages'; +import { getNugetServiceUrl, NuGetServiceType } from '../../shared/nugetServiceUrl'; -export default function fetchPackageVersions( +export default async function fetchPackageVersions( selectedPackageName: string | undefined, - versionsUrl: string = NUGET_VERSIONS_URL -): Promise | Promise { - return new Promise((resolve, reject) => { - if (!selectedPackageName) { - // User has canceled the process. - return reject(CANCEL); - } + versionsUrl?: string +): Promise { + if (!selectedPackageName) { + // User has canceled the process. + return Promise.reject(CANCEL); + } - window.setStatusBarMessage(NUGET_LOADING_VERSIONS); + window.setStatusBarMessage(NUGET_LOADING_VERSIONS); + if(!versionsUrl) { + versionsUrl = await getNugetServiceUrl(NuGetServiceType.FlatContainer3); + } - fetch( - `${versionsUrl}${selectedPackageName}/index.json`, - getFetchOptions(workspace.getConfiguration('http')) - ).then((response: Response) => { - window.setStatusBarMessage(''); - resolve({ response, selectedPackageName }); - }); - }); + const response = await fetch( + versionsUrl.replace(/\/?$/,`/${selectedPackageName}/index.json`), + getFetchOptions(workspace.getConfiguration('http')) + ); + + window.setStatusBarMessage(''); + return { response, selectedPackageName }; } diff --git a/src/constants.ts b/src/constants.ts index 2b9295e7..a3d06098 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,8 +5,7 @@ export const DEFAULT_SCRIPT_NAME = 'build.cake'; export const OUTPUT_CHANNEL_NAME = 'Cake Workspace'; export const ERROR_INVALID_SETTINGS = 'Invalid installation options! Please try again.'; export const ERROR_NO_WORKSPACE = 'You have not yet opened a folder.'; -export const NUGET_SEARCH_URL = 'https://api-v2v3search-0.nuget.org/autocomplete'; -export const NUGET_VERSIONS_URL = "https://api.nuget.org/v3-flatcontainer/"; +export const NUGET_SEVICE_INDEX_URL = 'https://api.nuget.org/v3/index.json'; export const DEFAULT_RESPONSE_TIMEOUT = 10000; export const CAKE_DEFAULT_NAME = "Cake"; export const CAKE_BAKERY_PACKAGE_URL = 'http://nuget.org/api/v2/package/Cake.Bakery/'; diff --git a/src/shared/cakeTool.ts b/src/shared/cakeTool.ts index a22ab490..0ae152aa 100644 --- a/src/shared/cakeTool.ts +++ b/src/shared/cakeTool.ts @@ -1,6 +1,10 @@ import { ChildProcessWithoutNullStreams, spawn } from 'child_process'; -import { window, Memento, ExtensionContext } from 'vscode'; +import { window, Memento, ExtensionContext, workspace } from 'vscode'; +import fetch from 'node-fetch'; +import { getFetchOptions } from './utils'; import { Version } from './version'; +import { logError, logToOutput } from './log'; +import { getNugetServiceUrl, NuGetServiceType } from './nugetServiceUrl'; export class CakeTool { @@ -36,10 +40,28 @@ export class CakeTool { }); } + /** + * returns the latest available version of Cake.Tool on nuget + */ public async getAvailableVersion(): Promise { - const proc = spawn('dotnet', ['tool', 'search', 'cake.tool']); - const ver = await this.getCakeVersionFromProc(proc); - return ver; + const url = (await getNugetServiceUrl(NuGetServiceType.SearchQueryService)) + '?q=Cake.Tool&prerelease=false'; + try { + const search = await fetch( + url, + getFetchOptions(workspace.getConfiguration('http'))); + const searchResult: any = await search.json(); + const cakeTool = (searchResult?.data || []).find((x:any) => x.id === "Cake.Tool"); + const version = cakeTool?.version; + if(!version) { + logToOutput("Could not find a latest version for Cake.Tool from: "+url); + return null; + } + return Version.parse(version); + } + catch (ex: any) { + logError(ex, true); + } + return null } /** diff --git a/src/shared/nugetServiceUrl.ts b/src/shared/nugetServiceUrl.ts new file mode 100644 index 00000000..b2a11c20 --- /dev/null +++ b/src/shared/nugetServiceUrl.ts @@ -0,0 +1,26 @@ +import fetch from 'node-fetch'; +import { workspace } from 'vscode'; +import { NUGET_SEVICE_INDEX_URL } from '../constants'; +import { getFetchOptions } from './utils'; + +export enum NuGetServiceType { + SearchAutocompleteService = 'SearchAutocompleteService', + FlatContainer3 = 'PackageBaseAddress/3.0.0', + SearchQueryService = 'SearchQueryService' +} +export async function getNugetServiceUrl(type: NuGetServiceType) : Promise { + // TODO: the url's won't change every 5 min. - should we cache the call to the services? + const response = await fetch(NUGET_SEVICE_INDEX_URL, getFetchOptions(workspace.getConfiguration('http'))); + const json: any = await response.json(); + const resources = (json.resources as any[] || []).filter((x:any) => x['@type'] === type); + let resource = resources.find((x: any) => (x.comment as string).toLowerCase().indexOf('primary') >= 0); + if(!resource && resources.length > 0) { + resource = resources[0]; + } + + if(!resource){ + throw new Error("Service endpoint not Found: "+type); + } + + return resource['@id'] as string; +} \ No newline at end of file