.csproj 파일에서 두 날짜의 차이를 얻으려면 어떻게 해야 합니까? (How do I get the difference between two dates in a .csproj file?)


문제 설명

.csproj 파일에서 두 날짜의 차이를 얻으려면 어떻게 해야 합니까? (How do I get the difference between two dates in a .csproj file?)

다음과 같은 코드를 보았습니다. , csproj 파일에서

$([System.DateTime]::UtcNow.ToString(mmff))

어셈블리 버전 자동 증가:

<VersionSuffix>2.0.0.$([System.DateTime]::UtcNow.ToString(mmff))</VersionSuffix>
<AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.0.1</AssemblyVersion>

어떤 종류의 언어/스크립트인가요? 두 날짜의 차이를 얻으려면 어떻게 사용합니까?

다음과 같이 하려고 했습니다.

<VersionMajor>2</VersionMajor>
<VersionMinor>1</VersionMinor>
<DaysFromLastRelease>$(([System.DateTime]::UtcNow ‑ new [System.DateTime](2021,1,1))::TotalDays)</DaysFromLastRelease>

하지만 작동하지 않습니다 :)


참조 솔루션

방법 1:

.csproj files are basically MSBuild files (XML). The embedded syntax you are referring to is called a Property Function.

It appears that subtraction using the minus () might not be supported. There is a Subtract() property function in Property Functions.

Perhaps this could be the base for a solution. I have not tried it!

<Now>$([System.DateTime]::UtcNow.DayOfYear)</Now>

<January>$([System.DateTime]::new(2021,1,1)).DayOfYear</January>
<!‑‑ or... (not sure about the below)
<January>$([System.DateTime]::Parse("1/1/2021").DayOfYear)</January>
 ‑‑>

<DaysFromLastRelease>$([MSBuild]::Subtract($(Now), $(January)))</DaysFromLastRelease>

Other possibilities

  • calculate the date difference by writing an MSBuild task
  • call out to a simple program you write
  • somehow use an external program to set an environment variable, and then reference that variable in your .csproj

방법 2:

That's what worked for me:

<PropertyGroup>
    <VersionMajor Condition="'$(VersionMajor)' == ''">0</VersionMajor>
    <VersionMinor Condition="'$(VersionMinor)' == ''">0</VersionMinor>
    <VersionPatch Condition="'$(VersionPatch)' == ''">$([System.DateTime]::UtcNow.Subtract($([System.DateTime]::new(2001,1,1))).TotalDays.ToString("0"))</VersionPatch>
    <VersionRevision Condition="'$(VersionRevision)' == ''">$([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes.ToString("0"))</VersionRevision>
    <Version>$(VersionMajor).$(VersionMinor).$(VersionPatch).$(VersionRevision)</Version>
</PropertyGroup>

Here I manually set VersionMajor and VersionMinor. Then I have autoincremented values for Patch and revision.

  • Patch : number of days since 2001/01/01 (first day of century XXI).
  • Revision : total minutes of the day

That's good enought untill June 2180 (Remember máx versión number is 65534).


Extra tip: I put all this lines into a Version.Build.props file into Properties folder. Then I import it from csproj file with this tag:

<Import Project="$([MSBuild]::GetPathOfFileAbove('Version.Build.props', '$(MSBuildThisFileDirectory)/Properties/'))" />

This way, I can manually set proyect versions manually in my csproj file, left them in auto or a mix of them by just setting VersionMajor and VersionMinor which is what I actually do.

(by sergeKitAntonio Rodríguez)

참조 문서

  1. How do I get the difference between two dates in a .csproj file? (CC BY‑SA 2.5/3.0/4.0)

#msbuild #csproj #C# #.net #visual-studio






관련 질문

빌드 오류 + Excel 2010용 VSTO 추가 기능 생성 (Build Error + Creating VSTO addin for excel 2010)

.sln 파일의 속성을 MSBuild의 프로젝트 파일로 전달할 수 있습니까? (Is it possible to pass properties from an .sln file to project file in MSBuild?)

ProjectInfo.xml 파일을 찾을 수 없습니다. 소나 속성 파일 생성 실패 (No ProjectInfo.xml files were found. Generation of the sonar-properties file failed)

CSC 컴파일러는 어디에서 PE 파일을 생성합니까? (Where CSC Compiler Generate PE file?)

error CNDL0005: Product 요소에 예기치 않은 자식 요소 'Dialog'가 포함되어 있습니다. (error CNDL0005 : The Product element contains an unexpected child element 'Dialog')

Visual Studio 빌드 중 PostSharp OutOfMemoryException (PostSharp OutOfMemoryException during Visual Studio build)

개체 배열을 설정하는 동안 FindBug 오류 '변경 가능한 개체에 대한 참조를 통합하여 내부 표현을 노출할 수 있음'을 수정하는 방법은 무엇입니까? (How to Fix FindBug error 'May expose internal representation by incorporating reference to mutable object' while setting Array of Objects?)

MSBuild에서 내 x86 프로그램을 빌드할 수 없습니다. (MSBuild cannot build my x86 program)

MSDeploy를 사용하여 Web.config 사용자 지정 섹션을 변환하는 방법은 무엇입니까? (How-to Transform Web.config Custom Sections With MSDeploy?)

오류: 프로젝트의 기본 XML 네임스페이스는 MSBuild XML 네임스페이스여야 합니다. (Error: The default XML namespace of the project must be the MSBuild XML namespace)

Azure Devops에서 msbuild를 사용하여 릴리스 및 디버그 구성 모두로 너겟 파일을 만드는 방법 (How to create a nuget file with both the Release and Debug configurations using msbuild in Azure Devops)

.csproj 파일에서 두 날짜의 차이를 얻으려면 어떻게 해야 합니까? (How do I get the difference between two dates in a .csproj file?)







코멘트