@@ -28,7 +28,6 @@ function executeConanTask(commandArgs) {
2828
2929 let conanTaskId = generateConanTaskUUId ( ) ;
3030 tl . debug ( 'Conan Task Id: ' + conanTaskId ) ;
31- let buildTimestamp = Date . now ( ) ;
3231
3332 let conanPath = null ;
3433 try {
@@ -58,7 +57,7 @@ function executeConanTask(commandArgs) {
5857 let buildName = tl . getInput ( 'buildName' , true ) ;
5958 let buildNumber = tl . getInput ( 'buildNumber' , true ) ;
6059 try {
61- initCliPartialsBuildDir ( buildName , buildNumber ) ;
60+ let buildTimestamp = initCliPartialsBuildDir ( buildName , buildNumber ) ;
6261 setConanTraceFileLocation ( conanUserHome , conanTaskId ) ;
6362 setArtifactsBuildInfoProperties ( conanUserHome , buildName , buildNumber , buildTimestamp ) ;
6463 } catch ( err ) {
@@ -387,14 +386,56 @@ function purgeConanRemotes() {
387386 * Creates the path of for partials build info and initializing the details file with Timestamp.
388387 * @param buildName (string) - The build name
389388 * @param buildNumber (string) - The build number
389+ * @returns {number } - The timestamp of the build in milliseconds.
390390 */
391391function initCliPartialsBuildDir ( buildName , buildNumber ) {
392392 let partialsBuildDir = join ( getCliPartialsBuildDir ( buildName , buildNumber ) , 'partials' ) ;
393+ let partialBuildDetailsFile = join ( partialsBuildDir , 'details' ) ;
394+
395+ // If a build was initialized before this task, read the build timestamp from the existing build details.
396+ if ( fs . pathExistsSync ( partialBuildDetailsFile ) ) {
397+ let buildTimestamp = readTimestampFromBuildPartialDetailsFile ( partialBuildDetailsFile ) ;
398+ if ( buildTimestamp ) {
399+ tl . debug ( 'Read timestamp "' + buildTimestamp + '" from partial build details at: ' + partialBuildDetailsFile ) ;
400+ return buildTimestamp ;
401+ }
402+ }
403+ // If a build was not initialized, create a new build details file.
404+ return createBuildDetailsPartial ( partialsBuildDir , partialBuildDetailsFile ) ;
405+ }
406+
407+ /**
408+ * Creates the partial details file with the current timestamp.
409+ * @param partialsBuildDir (string) - path to the build's partials directory.
410+ * @param buildDetailsFile (string) - path to the build's partial build details file.
411+ * @returns {number } - The timestamp of the build in milliseconds.
412+ */
413+ function createBuildDetailsPartial ( partialsBuildDir , buildDetailsFile ) {
393414 if ( ! fs . pathExistsSync ( partialsBuildDir ) ) {
394415 fs . ensureDirSync ( partialsBuildDir ) ;
395416 }
396- fs . writeJsonSync ( join ( partialsBuildDir , 'details' ) , { Timestamp : new Date ( ) . toISOString ( ) } ) ;
397- tl . debug ( 'Created partial details at: ' + join ( partialsBuildDir , 'details' ) ) ;
417+
418+ // The start time is saved as ISO, but the artifacts property is saved as a timestamp.
419+ let buildStartTime = new Date ( ) ;
420+ fs . writeJsonSync ( buildDetailsFile , { Timestamp : buildStartTime . toISOString ( ) } ) ;
421+ tl . debug ( 'Created partial build details at: ' + buildDetailsFile ) ;
422+ return buildStartTime . getTime ( ) ;
423+ }
424+
425+ /**
426+ * Reads the build start time from the build's partial build details file, and returns it as timestamp.
427+ * @param buildDetailsFile (string) - path to the build's partial build details file.
428+ * @returns {number } - The timestamp of the build in milliseconds.
429+ */
430+ function readTimestampFromBuildPartialDetailsFile ( buildDetailsFile ) {
431+ try {
432+ const data = fs . readFileSync ( buildDetailsFile , 'utf8' ) ;
433+ const jsonData = JSON . parse ( data ) ;
434+ return Date . parse ( jsonData . Timestamp ) ;
435+ } catch ( err ) {
436+ console . error ( 'Error reading or parsing the build details partials file:' , err ) ;
437+ return undefined ;
438+ }
398439}
399440
400441function getCliPartialsBuildDir ( buildName , buildNumber ) {
@@ -405,6 +446,8 @@ function getCliPartialsBuildDir(buildName, buildNumber) {
405446
406447module . exports = {
407448 executeConanTask : executeConanTask ,
408- getCliPartialsBuildDir : getCliPartialsBuildDir , // Exported for tests
409449 purgeConanRemotes : purgeConanRemotes ,
450+ // Exported for tests:
451+ getCliPartialsBuildDir : getCliPartialsBuildDir ,
452+ initCliPartialsBuildDir : initCliPartialsBuildDir ,
410453} ;
0 commit comments