-
Notifications
You must be signed in to change notification settings - Fork 728
Fix 'npm run gulp updatePackageDependencies' #2766
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,33 +6,47 @@ | |
| import * as fs from 'fs'; | ||
| import * as os from 'os'; | ||
| import { Package } from '../packageManager/Package'; | ||
|
|
||
| import { DownloadFile } from '../packageManager/FileDownloader'; | ||
| import { EventStream } from '../EventStream'; | ||
| import * as Event from "../omnisharp/loggingEvents"; | ||
| import NetworkSettings, { NetworkSettingsProvider } from '../NetworkSettings'; | ||
| import { getBufferIntegrityHash } from '../packageManager/isValidDownload'; | ||
|
|
||
| interface PackageJSONFile | ||
| { | ||
| runtimeDependencies : Package[]; | ||
| } | ||
|
|
||
| export function updatePackageDependencies() { | ||
| export async function updatePackageDependencies() : Promise<void> { | ||
|
|
||
| const urlsIndex = process.argv.indexOf("--urls"); | ||
| const newPrimaryUrls = urlsIndex >= 0 ? process.argv[urlsIndex+1] : undefined; | ||
|
|
||
| const fallbackUrlsIndex = process.argv.indexOf("--fallbackUrls"); | ||
| const newFallbackUrls = fallbackUrlsIndex >= 0 ? process.argv[fallbackUrlsIndex+1] : undefined; | ||
| const newPrimaryUrls = process.env["NEW_DEPS_URLS"]; | ||
| const newVersion = process.env["NEW_DEPS_VERSION"]; | ||
|
|
||
| if (newPrimaryUrls === undefined || newPrimaryUrls === "-?" || newPrimaryUrls === "-h") { | ||
| console.log("This command will update the URLs for package dependencies in package.json"); | ||
| if (!newPrimaryUrls || !newVersion) { | ||
| console.log(); | ||
| console.log("'npm run gulp updatePackageDependencies' will update package.json with new URLs of dependencies."); | ||
| console.log(); | ||
| console.log("Syntax: updatePackageDependencies --urls \"<url1>,<url2>,...\" [--fallbackUrls \"<fallback-url-1>,<fallback-url-2>...\"]"); | ||
| console.log("To use:"); | ||
| const setEnvVarPrefix = os.platform() === 'win32' ? "set " : "export "; | ||
| const setEnvVarQuote = os.platform() === 'win32' ? "" : "\'"; | ||
| console.log(` ${setEnvVarPrefix}NEW_DEPS_URLS=${setEnvVarQuote}https://example1/foo-osx.zip,https://example1/foo-win.zip,https://example1/foo-linux.zip${setEnvVarQuote}`); | ||
| console.log(` ${setEnvVarPrefix}NEW_DEPS_VERSION=${setEnvVarQuote}1.2.3${setEnvVarQuote}`); | ||
| console.log(" npm run gulp updatePackageDependencies"); | ||
| console.log(); | ||
| return; | ||
| } | ||
|
|
||
| if (newPrimaryUrls.length === 0) { | ||
| throw new Error("Invalid first argument to updatePackageDependencies. URL string argument expected."); | ||
| const newPrimaryUrlArray = newPrimaryUrls.split(','); | ||
| for (let urlToUpdate of newPrimaryUrlArray) { | ||
| if (!urlToUpdate.startsWith("https://")) { | ||
| throw new Error("Unexpected 'NEW_DEPS_URLS' value. All URLs should start with 'https://'."); | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
|
|
@@ -54,23 +68,79 @@ export function updatePackageDependencies() { | |
| if (dependency === undefined) { | ||
| throw new Error(`Unable to update item for url '${url}'. No 'runtimeDependency' found with filename '${fileName}'.`); | ||
| } | ||
|
|
||
| console.log(`Updating ${url}`); | ||
| return dependency; | ||
| }; | ||
|
|
||
| newPrimaryUrls.split(',').forEach(urlToUpdate =>{ | ||
| console.log(`Trying to update ${urlToUpdate}`); | ||
| let dependency = findDependencyToUpdate(urlToUpdate); | ||
| dependency.url = urlToUpdate; | ||
| 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.`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Next take another pass to try and update to the URL | ||
| const eventStream = new EventStream(); | ||
| eventStream.subscribe((event: Event.BaseEvent) => { | ||
| switch (event.constructor.name) { | ||
| case Event.DownloadFailure.name: | ||
| console.log("Failed to download: " + (<Event.DownloadFailure>event).message); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like in the other places in the code, we could use a single eventStream that is passed to all these functions and in this file we could post the appropriate events to this stream and separately create a "ConsoleObserver" or "UpdatePackageDependencies" observer that subscribes to this event stream and does the logging accordingly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I think the eventStream pattern that we created in other places make a lot of sense for enabling unit tests, I don't really see a benefit in this case since unit tests don't seem to make sense for a gulp task that produces output that would be code reviewed. |
||
| break; | ||
| } | ||
| }); | ||
| const networkSettingsProvider : NetworkSettingsProvider = () => new NetworkSettings(/*proxy:*/ null, /*stringSSL:*/ true); | ||
|
|
||
| if (newFallbackUrls !== undefined) { | ||
| newFallbackUrls.split(',').forEach(urlToUpdate =>{ | ||
| console.log(`Trying to update ${urlToUpdate}`); | ||
| let dependency = findDependencyToUpdate(urlToUpdate); | ||
| dependency.fallbackUrl = urlToUpdate; | ||
| }); | ||
| const downloadAndGetHash = async (url:string) : Promise<string> => { | ||
| console.log(`Downlodaing 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, "-"); | ||
| } | ||
| dottedVersionRegExp.lastIndex = 0; | ||
|
|
||
| dependency.fallbackUrl = dependency.fallbackUrl.replace(regex, newValue); | ||
| 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}'.`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let content = JSON.stringify(packageJSON, null, 2); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also do a semver check here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is anything in 'semver' that supports searching for a semver in a string, and since we always want the input version to be subsequently searchable, I don't think it makes sense to use the semver library for this.