Skip to content

Commit

Permalink
fix: csproj setup could not find version property
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed May 29, 2022
1 parent 7b00fdf commit 0ba462c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ function getCurrentVersionCsproj(csprojDocument) {
const propertyGroupNodes = Object.values(rootNode.childNodes).filter((node) => {
return node.nodeName === 'PropertyGroup';
});
if (propertyGroupNodes.length === 1) {
const propertyGroupNode = propertyGroupNodes[0];

//find version property within one of the property groups
for (const propertyGroupNode of propertyGroupNodes) {
const versionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'Version';
});
Expand All @@ -63,22 +62,17 @@ function getNewProjectContentCsproj(newVersion, csprojDocument) {
const propertyGroupNodes = Object.values(rootNode.childNodes).filter((node) => {
return node.nodeName === 'PropertyGroup';
});
if (propertyGroupNodes.length === 1) {
const propertyGroupNode = propertyGroupNodes[0];

let coreVersionSat = false;
//find version property within one of the property groups
for (const propertyGroupNode of propertyGroupNodes) {
const versionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'Version';
});
if (versionNodes.length === 1) {
const versionNode = versionNodes[0];
versionNode.childNodes.item(0).data = newVersion;
} else {
const versionNode = new DOMParser().parseFromString(
`<Version>${newVersion}</Version>`,
'text/xml'
);
propertyGroupNode.appendChild(versionNode);
}
coreVersionSat = true;
}

const packageVersionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'PackageVersion';
Expand All @@ -104,6 +98,15 @@ function getNewProjectContentCsproj(newVersion, csprojDocument) {
versionNode.childNodes.item(0).data = `${newVersion}.0`;
}
}
if (coreVersionSat === false && propertyGroupNodes.length > 0) {
//Greedy, set the version in the first encountered property group
const propertyGroupNode = propertyGroupNodes[0];
const versionNode = new DOMParser().parseFromString(
`<Version>${newVersion}</Version>`,
'text/xml'
);
propertyGroupNode.appendChild(versionNode);
}
}
return new XMLSerializer().serializeToString(rootNode);
}
Expand Down
50 changes: 50 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,56 @@ describe('Utils', () => {
expect(version).toEqual('1.0.0');
});

test('should return version 3', () => {
const csproj = `
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E7ECDD31-5969-43AF-A7E1-31E04631124C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Oxide.Ext.GamingApi</RootNamespace>
<AssemblyName>Oxide.Ext.GamingApi</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<Version>0.0.0</Version>
<PackageVersion>0.0.0</PackageVersion>
<AssemblyVersion>0.0.0.0</AssemblyVersion>
<FileVersion>0.0.0.0</FileVersion>
<RepositoryUrl>https://github.com/GamingAPI/umod-rust-server-extension.git</RepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>6</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
</Project>
`;
const version = getCurrentVersionCsproj(csproj);
expect(version).toEqual('0.0.0');
});
test('should return version 2', () => {
const csproj = `
<Project Sdk="Microsoft.NET.Sdk">
Expand Down

0 comments on commit 0ba462c

Please sign in to comment.