diff --git a/lib/manager/pub/__snapshots__/update.spec.ts.snap b/lib/manager/pub/__snapshots__/update.spec.ts.snap deleted file mode 100644 index 426313103ff291e3a92db9e34fe256e62e42c77b..0000000000000000000000000000000000000000 --- a/lib/manager/pub/__snapshots__/update.spec.ts.snap +++ /dev/null @@ -1,37 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`manager/pub/update updateDependency replaces nested value 1`] = ` -"dev_dependencies: - test: 0.1 - build: - version: 0.1 - -dependencies: - bar: - sdk: flatter - foo: 1 - bar: - version: 1.2.3 - baz: - non-sense: true - qux: false -" -`; - -exports[`manager/pub/update updateDependency replaces one-line value 1`] = ` -"dev_dependencies: - test: 0.1 - build: - version: 0.1 - -dependencies: - bar: - sdk: flatter - foo: 1.2.3 - bar: - version: 1 - baz: - non-sense: true - qux: false -" -`; diff --git a/lib/manager/pub/index.ts b/lib/manager/pub/index.ts index 3034fc824f688d10e8beee4d890736d758284efe..245129a5d6ea8dc2922c8c9d5fb96de8dbd40849 100644 --- a/lib/manager/pub/index.ts +++ b/lib/manager/pub/index.ts @@ -1,7 +1,8 @@ import * as npmVersioning from '../../versioning/npm'; export { extractPackageFile } from './extract'; -export { updateDependency } from './update'; + +export const autoReplace = true; export const defaultConfig = { fileMatch: ['(^|/)pubspec\\.ya?ml$'], diff --git a/lib/manager/pub/update.spec.ts b/lib/manager/pub/update.spec.ts deleted file mode 100644 index 0949e38da580b3cfe4b60e5fdc09f6cf3cb6ef5f..0000000000000000000000000000000000000000 --- a/lib/manager/pub/update.spec.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { readFileSync } from 'fs'; -import { updateDependency } from '.'; - -const fileContent = readFileSync( - 'lib/manager/pub/__fixtures__/update.yaml', - 'utf8' -); - -describe('manager/pub/update', () => { - describe('updateDependency', () => { - it('returns content untouched if versions are same', () => { - const upgrade = { - depName: 'foo', - currentValue: '1', - newValue: '1', - depType: 'dependencies', - }; - const res = updateDependency({ fileContent, upgrade }); - expect(res).toEqual(fileContent); - }); - it('returns null if content was updated', () => { - expect( - updateDependency({ - fileContent, - upgrade: { - depName: 'test', - currentValue: '0.0.1', - newValue: '1', - depType: 'dev_dependencies', - }, - }) - ).toBe(null); - expect( - updateDependency({ - fileContent, - upgrade: { - depName: 'build', - currentValue: '0.0.1', - newValue: '1', - depType: 'dev_dependencies', - }, - }) - ).toBe(null); - }); - it('replaces one-line value', () => { - const upgrade = { - depName: 'foo', - currentValue: '1', - newValue: '1.2.3', - depType: 'dependencies', - }; - const res = updateDependency({ fileContent, upgrade }); - expect(res).not.toEqual(fileContent); - expect(res.includes(upgrade.newValue)).toBe(true); - expect(res).toMatchSnapshot(); - }); - it('replaces nested value', () => { - const upgrade = { - depName: 'bar', - currentValue: '1', - newValue: '1.2.3', - depType: 'dependencies', - }; - const res = updateDependency({ fileContent, upgrade }); - expect(res).not.toEqual(fileContent); - expect(res.includes(upgrade.newValue)).toBe(true); - expect(res).toMatchSnapshot(); - }); - }); -}); diff --git a/lib/manager/pub/update.ts b/lib/manager/pub/update.ts deleted file mode 100644 index 8732f2fa0ab2812fa9e4e1eab148796cf9834e82..0000000000000000000000000000000000000000 --- a/lib/manager/pub/update.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { load } from 'js-yaml'; -import { UpdateDependencyConfig } from '../common'; - -export function updateDependency({ - fileContent, - upgrade, -}: UpdateDependencyConfig): string { - const { depName, depType, currentValue, newValue } = upgrade; - - if (currentValue === newValue) { - return fileContent; - } - - const sectionBeginRegExp = new RegExp(`^${depType}:`); - const isSectionBegin = (line: string): boolean => - sectionBeginRegExp.test(line); - const isSectionEnd = (line: string): boolean => /^[^\s]/.test(line); - - const simpleDepRegExp = new RegExp(`^\\s+${depName}:\\s*[^\\s]+\\s*$`); - const isOneLineDep = (line: string): boolean => simpleDepRegExp.test(line); - - const multilineDepRegExp = new RegExp(`^\\s+${depName}:\\s*$`); - const isMultilineDepRegExp = (line: string): boolean => - multilineDepRegExp.test(line); - - const versionRegExp = new RegExp('^\\s+version:\\s*[^\\s]+\\s*$'); - const isVersionLine = (line: string): boolean => versionRegExp.test(line); - - const isValidVersion = (line: string): boolean => { - const version = load(line.replace(/^.*:\s*/, '')).toString(); - return version === currentValue; - }; - - let isSection = false; - let indent = null; - - const lines = fileContent.split('\n'); - const len = lines.length; - for (let idx = 0; idx < len; idx += 1) { - const line = lines[idx]; - - if (isSectionBegin(line)) { - isSection = true; - } else if (isSectionEnd(line)) { - isSection = false; - } else if (isSection) { - if (isOneLineDep(line)) { - if (!isValidVersion(line)) { - return null; - } - lines[idx] = line.replace(currentValue, newValue); - break; - } else if (isMultilineDepRegExp(line)) { - indent = line.search(/[^\s]/); - } else if (indent) { - const currentIndent = line.search(/[^\s]/); - if (currentIndent <= indent) { - indent = null; - } else if (isVersionLine(line)) { - if (!isValidVersion(line)) { - return null; - } - lines[idx] = line.replace(currentValue, newValue); - break; - } - } - } - } - - return lines.join('\n'); -}