Skip to content

Commit

Permalink
fix(#41): update xml file write to use same logic as html (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbarton-andovercos authored Jan 20, 2025
1 parent 78e2070 commit a3d6725
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
26 changes: 11 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Bumper extends Plugin {
case 'xml':
case 'html':
const element = parsed(path);
if (!element) {
if (!element.length) {
throw new Error(`Failed to find the element with the provided selector: ${path}`);
}
version = element.text();
Expand Down Expand Up @@ -179,24 +179,20 @@ class Bumper extends Plugin {
case 'ini':
return writeFileSync(file, ini.encode(parsed));
case 'xml':
const xmlElement = parsed(path);
if (!xmlElement) {
throw new Error(`Failed to find the element with the provided selector: ${path}`);
}

xmlElement.text(version);
return writeFileSync(file, parsed.xml());
case 'html':
const htmlElement = parsed(path);
if (!htmlElement) {
const element = parsed(path);
if (!element.length) {
throw new Error(`Failed to find the element with the provided selector: ${path}`);
}

// We are taking this approach as cheerio will modify the original html doc type and head formatting if we just used the parsed.html() function.
const previousContents = htmlElement.prop('outerHTML');
htmlElement.text(version);
const html = data.replace(previousContents.trim(), htmlElement.prop('outerHTML').trim());
return writeFileSync(file, html);
// If we just used the parsed.html() or parsed.xml() function, cheerio will modify:
// - html doctype
// - html head
// - encode special characters in strings too eagerly (https://github.com/cheeriojs/cheerio/issues/4045)
const previousContents = element.prop('outerHTML');
element.text(version);
const contents = data.replace(previousContents.trim(), element.prop('outerHTML').trim());
return writeFileSync(file, contents);
default:
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = parsed && !consumeWholeFile ? parsed.replace(versionMatch, version) : version + EOL;
Expand Down
15 changes: 14 additions & 1 deletion test/xml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { NAMESPACE, CURRENT_VERSION, NEW_VERSION } from './globals/constants.js'
import { readFile } from './globals/file-utils.js';

mock({
'./foo.xml': `<project>${EOL}\t<modelVersion>4.0.0</modelVersion>${EOL}\t<groupId>com.mycompany.app</groupId>${EOL}\t<artifactId>my-app</artifactId>${EOL}\t<version>${CURRENT_VERSION}</version>${EOL}\t<dependencies>${EOL}\t\t<dependency>${EOL}\t\t\t<groupId>group-a</groupId>${EOL}\t\t\t<artifactId>artifact-a</artifactId>${EOL}\t\t\t<version>${CURRENT_VERSION}</version>${EOL}\t\t</dependency>${EOL}\t</dependencies>${EOL}</project>${EOL}`
'./foo.xml': `<project>${EOL}\t<modelVersion>4.0.0</modelVersion>${EOL}\t<groupId>com.mycompany.app</groupId>${EOL}\t<artifactId>my-app</artifactId>${EOL}\t<version>${CURRENT_VERSION}</version>${EOL}\t<dependencies>${EOL}\t\t<dependency>${EOL}\t\t\t<groupId>group-a</groupId>${EOL}\t\t\t<artifactId>artifact-a</artifactId>${EOL}\t\t\t<version>${CURRENT_VERSION}</version>${EOL}\t\t</dependency>${EOL}\t</dependencies>${EOL}</project>${EOL}`,
'./foo.csproj': `<Project>${EOL}\t<PropertyGroup>${EOL}\t\t<AssemblyName>HelloWorld</AssemblyName>${EOL}\t\t<OutputPath>Bin\</OutputPath>${EOL}\t\t<Version>${CURRENT_VERSION}</Version>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">26.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.1.2.1.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>${EOL}\t</PropertyGroup>${EOL}\t<ItemGroup>${EOL}\t\t<Compile Include="helloworld.cs"/>${EOL}\t</ItemGroup>${EOL}\t<Target Name="Build">${EOL}\t\t<Csc Sources="@(Compile)"/>${EOL}\t</Target>${EOL}</Project>${EOL}`
});

describe('xml file', { concurrency: true }, () => {
Expand Down Expand Up @@ -56,6 +57,18 @@ describe('xml file', { concurrency: true }, () => {
assert.equal(readFile('./foo.xml'), `<project>${EOL}\t<modelVersion>4.0.0</modelVersion>${EOL}\t<groupId>com.mycompany.app</groupId>${EOL}\t<artifactId>my-app</artifactId>${EOL}\t<version>${NEW_VERSION}</version>${EOL}\t<dependencies>${EOL}\t\t<dependency>${EOL}\t\t\t<groupId>group-a</groupId>${EOL}\t\t\t<artifactId>artifact-a</artifactId>${EOL}\t\t\t<version>${CURRENT_VERSION}</version>${EOL}\t\t</dependency>${EOL}\t</dependencies>${EOL}</project>${EOL}`);
});

it('should read/write with file with special characters', async () => {
const options = {
[NAMESPACE]: {
in: { file: './foo.csproj', type: 'application/xml', path: 'Project > PropertyGroup > Version' },
out: { file: './foo.csproj', type: 'application/xml', path: 'Project > PropertyGroup > Version' }
}
};
const plugin = factory(Bumper, { NAMESPACE, options });
await runTasks(plugin);
assert.equal(readFile('./foo.csproj'), `<Project>${EOL}\t<PropertyGroup>${EOL}\t\t<AssemblyName>HelloWorld</AssemblyName>${EOL}\t\t<OutputPath>Bin\</OutputPath>${EOL}\t\t<Version>${NEW_VERSION}</Version>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">26.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.1.2.1.0</SupportedOSPlatformVersion>${EOL}\t\t<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>${EOL}\t</PropertyGroup>${EOL}\t<ItemGroup>${EOL}\t\t<Compile Include="helloworld.cs"/>${EOL}\t</ItemGroup>${EOL}\t<Target Name="Build">${EOL}\t\t<Csc Sources="@(Compile)"/>${EOL}\t</Target>${EOL}</Project>${EOL}`);
});

it('should read/write without defining the type', async () => {
const options = {
[NAMESPACE]: {
Expand Down

0 comments on commit a3d6725

Please # to comment.