Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
{
"type": "node",
"request": "launch",
"name": "Launch gulp task",
"name": "Update package dependencies",
"preLaunchTask": "build",
"program": "${workspaceFolder}/node_modules/gulp/bin/gulp.js",
"args": [
Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"copyfiles": "2.0.0",
"cross-env": "5.1.4",
"del": "3.0.0",
"find-versions": "^3.0.0",
"get-port": "3.2.0",
"glob-promise": "3.4.0",
"gulp": "4.0.0",
Expand Down
147 changes: 95 additions & 52 deletions src/tools/UpdatePackageDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ import { EventStream } from '../EventStream';
import * as Event from "../omnisharp/loggingEvents";
import NetworkSettings, { NetworkSettingsProvider } from '../NetworkSettings';
import { getBufferIntegrityHash } from '../packageManager/isValidDownload';
const findVersions = require('find-versions');

interface PackageJSONFile
{
runtimeDependencies : Package[];
interface PackageJSONFile {
runtimeDependencies: Package[];
defaults: {
[key: string]: string;
};
}

export async function updatePackageDependencies() : Promise<void> {
const dottedVersionRegExp = /[0-9]+\.[0-9]+\.[0-9]+/g;
const dashedVersionRegExp = /[0-9]+-[0-9]+-[0-9]+/g;

export async function updatePackageDependencies(): Promise<void> {

const newPrimaryUrls = process.env["NEW_DEPS_URLS"];
const newVersion = process.env["NEW_DEPS_VERSION"];

if (!newPrimaryUrls || !newVersion) {
console.log();
console.log("'npm run gulp updatePackageDependencies' will update package.json with new URLs of dependencies.");
Expand All @@ -46,9 +52,9 @@ export async function updatePackageDependencies() : Promise<void> {
if (! /^[0-9]+\.[0-9]+\.[0-9]+$/.test(newVersion)) {
throw new Error("Unexpected 'NEW_DEPS_VERSION' value. Expected format similar to: 1.2.3.");
}

let packageJSON: PackageJSONFile = JSON.parse(fs.readFileSync('package.json').toString());

// map from lowercase filename to Package
const mapFileNameToDependency: { [key: string]: Package } = {};

Expand All @@ -61,8 +67,8 @@ export async function updatePackageDependencies() : Promise<void> {
}
mapFileNameToDependency[fileName] = dependency;
});
let findDependencyToUpdate = (url : string): Package => {

let findDependencyToUpdate = (url: string): Package => {
let fileName = getLowercaseFileNameFromUrl(url);
let dependency = mapFileNameToDependency[fileName];
if (dependency === undefined) {
Expand All @@ -71,34 +77,13 @@ export async function updatePackageDependencies() : Promise<void> {
return dependency;
};

const dottedVersionRegExp = /[0-9]+\.[0-9]+\.[0-9]+/g;
const dashedVersionRegExp = /[0-9]+-[0-9]+-[0-9]+/g;

const getMatchCount = (regexp: RegExp, searchString: string) : number => {
regexp.lastIndex = 0;
let retVal = 0;
while (regexp.test(searchString)) {
retVal++;
}
regexp.lastIndex = 0;
return retVal;
};

// First quickly make sure we could match up the URL to an existing item.
for (let urlToUpdate of newPrimaryUrlArray) {
const dependency = findDependencyToUpdate(urlToUpdate);
if (dependency.fallbackUrl) {
const dottedMatches : number = getMatchCount(dottedVersionRegExp, dependency.fallbackUrl);
const dashedMatches : number = getMatchCount(dashedVersionRegExp, dependency.fallbackUrl);
const matchCount : number = dottedMatches + dashedMatches;

if (matchCount == 0) {
throw new Error(`Version number not found in fallback URL '${dependency.fallbackUrl}'.`);
}
if (matchCount > 1) {
throw new Error(`Ambiguous version pattern found in fallback URL '${dependency.fallbackUrl}'. Multiple version strings found.`);
}
}
//Fallback url should contain a version
verifyVersionSubstringCount(dependency.fallbackUrl, true);
verifyVersionSubstringCount(dependency.installPath);
verifyVersionSubstringCount(dependency.installTestPath);
}

// Next take another pass to try and update to the URL
Expand All @@ -110,33 +95,30 @@ export async function updatePackageDependencies() : Promise<void> {
break;
}
});
const networkSettingsProvider : NetworkSettingsProvider = () => new NetworkSettings(/*proxy:*/ null, /*stringSSL:*/ true);
const networkSettingsProvider: NetworkSettingsProvider = () => new NetworkSettings(/*proxy:*/ null, /*stringSSL:*/ true);

const downloadAndGetHash = async (url:string) : Promise<string> => {
console.log(`Downlodaing from '${url}'`);
const buffer : Buffer = await DownloadFile(url, eventStream, networkSettingsProvider, url, null);
const downloadAndGetHash = async (url: string): Promise<string> => {
console.log(`Downloading from '${url}'`);
const buffer: Buffer = await DownloadFile(url, eventStream, networkSettingsProvider, url, null);
return getBufferIntegrityHash(buffer);
};

for (let urlToUpdate of newPrimaryUrlArray) {
let dependency = findDependencyToUpdate(urlToUpdate);
dependency.url = urlToUpdate;
dependency.integrity = await downloadAndGetHash(dependency.url);

if (dependency.fallbackUrl) {

// NOTE: We already verified in the first loop that one of these patterns will work, grab the one that does
let regex: RegExp = dottedVersionRegExp;
let newValue: string = newVersion;
if (!dottedVersionRegExp.test(dependency.fallbackUrl)) {
regex = dashedVersionRegExp;
newValue = newVersion.replace(/\./g, "-");
dependency.fallbackUrl = replaceVersion(dependency.fallbackUrl, newVersion);
dependency.installPath = replaceVersion(dependency.installPath, newVersion);
dependency.installTestPath = replaceVersion(dependency.installTestPath, newVersion);
Object.keys(packageJSON.defaults).forEach(key => {
//Update the version present in the defaults
if (key.toLowerCase() == dependency.id.toLowerCase()) {
packageJSON.defaults[key] = newVersion;
}
dottedVersionRegExp.lastIndex = 0;
});

dependency.fallbackUrl = dependency.fallbackUrl.replace(regex, newValue);
if (dependency.fallbackUrl) {
const fallbackUrlIntegrity = await downloadAndGetHash(dependency.fallbackUrl);

if (dependency.integrity !== fallbackUrlIntegrity) {
throw new Error(`File downloaded from primary URL '${dependency.url}' doesn't match '${dependency.fallbackUrl}'.`);
}
Expand All @@ -155,7 +137,55 @@ export async function updatePackageDependencies() : Promise<void> {
fs.writeFileSync('package.json', content);
}

function getLowercaseFileNameFromUrl(url : string) : string {

function replaceVersion(fileName: string, newVersion: string): string {
if (!fileName) {
return fileName; //if the value is null or undefined return the same one
}

let regex: RegExp = dottedVersionRegExp;
let newValue: string = newVersion;
if (!dottedVersionRegExp.test(fileName)) {
regex = dashedVersionRegExp;
newValue = newVersion.replace(/\./g, "-");
}
dottedVersionRegExp.lastIndex = 0;

if (!regex.test(fileName)) {
return fileName; //If the string doesn't contain any version return the same string
}

return fileName.replace(regex, newValue);
}

function verifyVersionSubstringCount(value: string, shouldContainVersion = false): void {
const getMatchCount = (regexp: RegExp, searchString: string): number => {
regexp.lastIndex = 0;
let retVal = 0;
while (regexp.test(searchString)) {
retVal++;
}
regexp.lastIndex = 0;
return retVal;
};

if (!value) {
return;
}

const dottedMatches: number = getMatchCount(dottedVersionRegExp, value);
const dashedMatches: number = getMatchCount(dashedVersionRegExp, value);
const matchCount: number = dottedMatches + dashedMatches;

if (shouldContainVersion && matchCount == 0) {
throw new Error(`Version number not found in '${value}'.`);
}
if (matchCount > 1) {
throw new Error(`Ambiguous version pattern found in '${value}'. Multiple version strings found.`);
}
}

function getLowercaseFileNameFromUrl(url: string): string {

if (!url.startsWith("https://")) {
throw new Error(`Unexpected URL '${url}'. URL expected to start with 'https://'.`);
Expand All @@ -166,5 +196,18 @@ function getLowercaseFileNameFromUrl(url : string) : string {
}

let index = url.lastIndexOf("/");
return url.substr(index + 1).toLowerCase();
let fileName = url.substr(index + 1).toLowerCase();
let versions = findVersions(fileName);
if (!versions || versions.length == 0) {
return fileName;
}

if (versions.length > 1) {
//we expect only one version string to be present in the last part of the url
throw new Error(`Ambiguous version pattern found URL '${url}'. Multiple version strings found.`);
}

let versionIndex = fileName.indexOf(versions[0]);
//remove the dash before the version number
return fileName.substr(0, versionIndex - 1).toLowerCase();
}