-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add generic TOML updater (#1833)
* feat: Add generic TOML updater * chore(docs): Add generic YAML & TOML updaters
- Loading branch information
Showing
9 changed files
with
292 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
exports['GenericToml updateContent updates deep entry in toml 1'] = ` | ||
[package] | ||
name = "rust-test-repo" | ||
version = "12.0.0" | ||
# To learn about other keys, check out Cargo's documentation | ||
[dependencies] | ||
normal-dep = "1.2.3" | ||
[dev-dependencies] | ||
dev-dep = { version = "2.3.4" } | ||
dev-dep-2 = { path = "../dev-dep-2" } | ||
[build-dependencies] | ||
# this is using a private registry | ||
build-dep = { version = "1.2.3", registry = "private", path = ".." } # trailing comment | ||
[target.'cfg(windows)'.dev-dependencies] | ||
windows-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(unix)'.dependencies] | ||
unix-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(target_arch = "x86")'.dependencies] | ||
x86-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(target_arch = "x86_64")'.dependencies] | ||
x86-64-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(foobar)'.dependencies] | ||
foobar-dep = "1.2.3" | ||
` | ||
|
||
exports['GenericToml updateContent updates matching entry 1'] = ` | ||
[package] | ||
name = "rust-test-repo" | ||
version = "2.3.4" | ||
# To learn about other keys, check out Cargo's documentation | ||
[dependencies] | ||
normal-dep = "1.2.3" | ||
[dev-dependencies] | ||
dev-dep = { version = "1.2.3" } | ||
dev-dep-2 = { path = "../dev-dep-2" } | ||
[build-dependencies] | ||
# this is using a private registry | ||
build-dep = { version = "1.2.3", registry = "private", path = ".." } # trailing comment | ||
[target.'cfg(windows)'.dev-dependencies] | ||
windows-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(unix)'.dependencies] | ||
unix-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(target_arch = "x86")'.dependencies] | ||
x86-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(target_arch = "x86_64")'.dependencies] | ||
x86-64-dep = { version = "1.2.3", registry = "private", path = ".." } | ||
[target.'cfg(foobar)'.dependencies] | ||
foobar-dep = "1.2.3" | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Updater} from '../update'; | ||
import {Version} from '../version'; | ||
import * as jp from 'jsonpath'; | ||
import {parseWith, replaceTomlValue} from '../util/toml-edit'; | ||
import * as toml from '@iarna/toml'; | ||
import {logger as defaultLogger, Logger} from '../util/logger'; | ||
|
||
/** | ||
* Updates TOML document according to given JSONPath. | ||
* | ||
* Note that used parser does reformat the document and removes all comments, | ||
* and converts everything to pure TOML. | ||
* If you want to retain formatting, use generic updater with comment hints. | ||
*/ | ||
export class GenericToml implements Updater { | ||
readonly jsonpath: string; | ||
readonly version: Version; | ||
|
||
constructor(jsonpath: string, version: Version) { | ||
this.jsonpath = jsonpath; | ||
this.version = version; | ||
} | ||
/** | ||
* Given initial file contents, return updated contents. | ||
* @param {string} content The initial content | ||
* @returns {string} The updated content | ||
*/ | ||
updateContent(content: string, logger: Logger = defaultLogger): string { | ||
let data: toml.JsonMap; | ||
try { | ||
data = parseWith(content); | ||
} catch (e) { | ||
logger.warn('Invalid toml, cannot be parsed', e); | ||
return content; | ||
} | ||
|
||
const paths = jp.paths(data, this.jsonpath); | ||
if (!paths || paths.length === 0) { | ||
logger.warn(`No entries modified in ${this.jsonpath}`); | ||
return content; | ||
} | ||
|
||
let processed = content; | ||
paths.forEach(path => { | ||
if (path[0] === '$') path = path.slice(1); | ||
processed = replaceTomlValue(processed, path, this.version.toString()); | ||
}); | ||
|
||
return processed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {readFileSync} from 'fs'; | ||
import {resolve} from 'path'; | ||
import * as snapshot from 'snap-shot-it'; | ||
import {describe, it} from 'mocha'; | ||
import {Version} from '../../src/version'; | ||
import {expect, assert} from 'chai'; | ||
import {GenericToml} from '../../src/updaters/generic-toml'; | ||
|
||
const fixturesPath = './test/updaters/fixtures'; | ||
|
||
describe('GenericToml', () => { | ||
describe('updateContent', () => { | ||
it('updates matching entry', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './Cargo.toml'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const updater = new GenericToml( | ||
'$.package.version', | ||
Version.parse('v2.3.4') | ||
); | ||
const newContent = updater.updateContent(oldContent); | ||
snapshot(newContent); | ||
}); | ||
it('updates deep entry in toml', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './Cargo.toml'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const updater = new GenericToml( | ||
"$['dev-dependencies']..version", | ||
Version.parse('v2.3.4') | ||
); | ||
const newContent = updater.updateContent(oldContent); | ||
snapshot(newContent); | ||
}); | ||
it('ignores non-matching entry', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './Cargo.toml'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const updater = new GenericToml('$.nonExistent', Version.parse('v2.3.4')); | ||
const newContent = updater.updateContent(oldContent); | ||
expect(newContent).to.eql(oldContent); | ||
}); | ||
it('warns on invalid jsonpath', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './Cargo.toml'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const updater = new GenericToml('bad jsonpath', Version.parse('v2.3.4')); | ||
assert.throws(() => { | ||
updater.updateContent(oldContent); | ||
}); | ||
}); | ||
it('ignores invalid file', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './toml/invalid.txt'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const updater = new GenericToml('$.boo', Version.parse('v2.3.4')); | ||
const newContent = updater.updateContent(oldContent); | ||
expect(newContent).to.eql(oldContent); | ||
}); | ||
}); | ||
}); |