@@ -603,7 +603,6 @@ private Stream InstallVersion(
603
603
{
604
604
return null ;
605
605
}
606
-
607
606
string digest = GetDigestFromManifest ( manifest , out errRecord ) ;
608
607
if ( errRecord != null )
609
608
{
@@ -1018,7 +1017,7 @@ private static Collection<KeyValuePair<string, string>> GetDefaultHeaders(string
1018
1017
} ;
1019
1018
}
1020
1019
1021
- internal bool PushNupkgACR ( string outputNupkgDir , string pkgName , NuGetVersion pkgVersion , PSRepositoryInfo repository , out ErrorRecord errRecord )
1020
+ internal bool PushNupkgACR ( string psd1OrPs1File , string outputNupkgDir , string pkgName , NuGetVersion pkgVersion , PSRepositoryInfo repository , Hashtable parsedMetadataHash , out ErrorRecord errRecord )
1022
1021
{
1023
1022
errRecord = null ;
1024
1023
// Push the nupkg to the appropriate repository
@@ -1050,42 +1049,19 @@ internal bool PushNupkgACR(string outputNupkgDir, string pkgName, NuGetVersion p
1050
1049
_cmdletPassedIn . WriteVerbose ( "Getting acr access token" ) ;
1051
1050
var acrAccessToken = GetAcrAccessToken ( registry , acrRefreshToken , out errRecord ) ;
1052
1051
1052
+ /* Uploading .nupkg */
1053
1053
_cmdletPassedIn . WriteVerbose ( "Start uploading blob" ) ;
1054
1054
var moduleLocation = GetStartUploadBlobLocation ( registry , pkgName , acrAccessToken ) . Result ;
1055
1055
1056
1056
_cmdletPassedIn . WriteVerbose ( "Computing digest for .nupkg file" ) ;
1057
- bool digestCreated = CreateDigest ( fullNupkgFile , out string digest , out ErrorRecord digestError ) ;
1058
- if ( ! digestCreated )
1057
+ bool nupkgDigestCreated = CreateDigest ( fullNupkgFile , out string nupkgDigest , out ErrorRecord nupkgDigestError ) ;
1058
+ if ( ! nupkgDigestCreated )
1059
1059
{
1060
- _cmdletPassedIn . ThrowTerminatingError ( digestError ) ;
1060
+ _cmdletPassedIn . ThrowTerminatingError ( nupkgDigestError ) ;
1061
1061
}
1062
1062
1063
1063
_cmdletPassedIn . WriteVerbose ( "Finish uploading blob" ) ;
1064
- bool moduleUploadSuccess = EndUploadBlob ( registry , moduleLocation , fullNupkgFile , digest , false , acrAccessToken ) . Result ;
1065
-
1066
- _cmdletPassedIn . WriteVerbose ( "Create an empty file" ) ;
1067
- string emptyFileName = "empty.txt" ;
1068
- var emptyFilePath = System . IO . Path . Combine ( outputNupkgDir , emptyFileName ) ;
1069
- // Rename the empty file in case such a file already exists in the temp folder (although highly unlikely)
1070
- while ( File . Exists ( emptyFilePath ) )
1071
- {
1072
- emptyFilePath = Guid . NewGuid ( ) . ToString ( ) + ".txt" ;
1073
- }
1074
- FileStream emptyStream = File . Create ( emptyFilePath ) ;
1075
- emptyStream . Close ( ) ;
1076
-
1077
- _cmdletPassedIn . WriteVerbose ( "Start uploading an empty file" ) ;
1078
- var emptyLocation = GetStartUploadBlobLocation ( registry , pkgName , acrAccessToken ) . Result ;
1079
-
1080
- _cmdletPassedIn . WriteVerbose ( "Computing digest for empty file" ) ;
1081
- bool emptyDigestCreated = CreateDigest ( emptyFilePath , out string emptyDigest , out ErrorRecord emptyDigestError ) ;
1082
- if ( ! emptyDigestCreated )
1083
- {
1084
- _cmdletPassedIn . ThrowTerminatingError ( emptyDigestError ) ;
1085
- }
1086
-
1087
- _cmdletPassedIn . WriteVerbose ( "Finish uploading empty file" ) ;
1088
- bool emptyFileUploadSuccess = EndUploadBlob ( registry , emptyLocation , emptyFilePath , emptyDigest , false , acrAccessToken ) . Result ;
1064
+ bool moduleUploadSuccess = EndUploadBlob ( registry , moduleLocation , fullNupkgFile , nupkgDigest , false , acrAccessToken ) . Result ;
1089
1065
1090
1066
_cmdletPassedIn . WriteVerbose ( "Create the config file" ) ;
1091
1067
string configFileName = "config.json" ;
@@ -1096,11 +1072,25 @@ internal bool PushNupkgACR(string outputNupkgDir, string pkgName, NuGetVersion p
1096
1072
}
1097
1073
FileStream configStream = File . Create ( configFilePath ) ;
1098
1074
configStream . Close ( ) ;
1075
+ _cmdletPassedIn . WriteVerbose ( "Computing digest for config" ) ;
1076
+ bool configDigestCreated = CreateDigest ( configFilePath , out string configDigest , out ErrorRecord configDigestError ) ;
1077
+ if ( ! configDigestCreated )
1078
+ {
1079
+ _cmdletPassedIn . ThrowTerminatingError ( configDigestError ) ;
1080
+ }
1081
+
1082
+ /* Create manifest layer */
1083
+ _cmdletPassedIn . WriteVerbose ( "Create package version metadata as JSON string" ) ;
1084
+ string jsonString = CreateMetadataContent ( psd1OrPs1File , parsedMetadataHash , out ErrorRecord metadataCreationError ) ;
1085
+ if ( metadataCreationError != null )
1086
+ {
1087
+ _cmdletPassedIn . ThrowTerminatingError ( metadataCreationError ) ;
1088
+ }
1099
1089
1100
1090
FileInfo nupkgFile = new FileInfo ( fullNupkgFile ) ;
1101
1091
var fileSize = nupkgFile . Length ;
1102
1092
var fileName = System . IO . Path . GetFileName ( fullNupkgFile ) ;
1103
- string fileContent = CreateJsonContent ( digest , emptyDigest , fileSize , fileName ) ;
1093
+ string fileContent = CreateJsonContent ( nupkgDigest , configDigest , fileSize , fileName , jsonString ) ;
1104
1094
File . WriteAllText ( configFilePath , fileContent ) ;
1105
1095
1106
1096
_cmdletPassedIn . WriteVerbose ( "Create the manifest layer" ) ;
@@ -1113,7 +1103,12 @@ internal bool PushNupkgACR(string outputNupkgDir, string pkgName, NuGetVersion p
1113
1103
return false ;
1114
1104
}
1115
1105
1116
- private string CreateJsonContent ( string digest , string emptyDigest , long fileSize , string fileName )
1106
+ private string CreateJsonContent (
1107
+ string nupkgDigest ,
1108
+ string configDigest ,
1109
+ long nupkgFileSize ,
1110
+ string fileName ,
1111
+ string jsonString )
1117
1112
{
1118
1113
StringBuilder stringBuilder = new StringBuilder ( ) ;
1119
1114
StringWriter stringWriter = new StringWriter ( stringBuilder ) ;
@@ -1131,31 +1126,31 @@ private string CreateJsonContent(string digest, string emptyDigest, long fileSiz
1131
1126
jsonWriter . WritePropertyName ( "mediaType" ) ;
1132
1127
jsonWriter . WriteValue ( "application/vnd.unknown.config.v1+json" ) ;
1133
1128
jsonWriter . WritePropertyName ( "digest" ) ;
1134
- jsonWriter . WriteValue ( $ "sha256:{ emptyDigest } ") ;
1129
+ jsonWriter . WriteValue ( $ "sha256:{ configDigest } ") ;
1135
1130
jsonWriter . WritePropertyName ( "size" ) ;
1136
1131
jsonWriter . WriteValue ( 0 ) ;
1137
1132
jsonWriter . WriteEndObject ( ) ;
1138
1133
1139
1134
jsonWriter . WritePropertyName ( "layers" ) ;
1140
1135
jsonWriter . WriteStartArray ( ) ;
1141
- jsonWriter . WriteStartObject ( ) ;
1142
1136
1137
+ jsonWriter . WriteStartObject ( ) ;
1143
1138
jsonWriter . WritePropertyName ( "mediaType" ) ;
1144
1139
jsonWriter . WriteValue ( "application/vnd.oci.image.layer.nondistributable.v1.tar+gzip'" ) ;
1145
1140
jsonWriter . WritePropertyName ( "digest" ) ;
1146
- jsonWriter . WriteValue ( $ "sha256:{ digest } ") ;
1141
+ jsonWriter . WriteValue ( $ "sha256:{ nupkgDigest } ") ;
1147
1142
jsonWriter . WritePropertyName ( "size" ) ;
1148
- jsonWriter . WriteValue ( fileSize ) ;
1143
+ jsonWriter . WriteValue ( nupkgFileSize ) ;
1149
1144
jsonWriter . WritePropertyName ( "annotations" ) ;
1150
-
1151
1145
jsonWriter . WriteStartObject ( ) ;
1152
1146
jsonWriter . WritePropertyName ( "org.opencontainers.image.title" ) ;
1153
1147
jsonWriter . WriteValue ( fileName ) ;
1148
+ jsonWriter . WritePropertyName ( "metadata" ) ;
1149
+ jsonWriter . WriteValue ( jsonString ) ;
1154
1150
jsonWriter . WriteEndObject ( ) ;
1155
-
1156
1151
jsonWriter . WriteEndObject ( ) ;
1157
- jsonWriter . WriteEndArray ( ) ;
1158
1152
1153
+ jsonWriter . WriteEndArray ( ) ;
1159
1154
jsonWriter . WriteEndObject ( ) ;
1160
1155
1161
1156
return stringWriter . ToString ( ) ;
@@ -1205,6 +1200,49 @@ private bool CreateDigest(string fileName, out string digest, out ErrorRecord er
1205
1200
return true ;
1206
1201
}
1207
1202
1203
+ private string CreateMetadataContent ( string manifestFilePath , Hashtable parsedMetadata , out ErrorRecord metadataCreationError )
1204
+ {
1205
+ metadataCreationError = null ;
1206
+ Hashtable parsedMetadataHash = null ;
1207
+ string jsonString = string . Empty ;
1208
+
1209
+ // A script will already have the metadata parsed into the parsedMetadatahash,
1210
+ // a module will still need the module manifest to be parsed.
1211
+ if ( parsedMetadata == null || parsedMetadata . Count == 0 )
1212
+ {
1213
+ // Use the parsed module manifest data as 'parsedMetadataHash' instead of the passed-in data.
1214
+ if ( ! Utils . TryReadManifestFile (
1215
+ manifestFilePath : manifestFilePath ,
1216
+ manifestInfo : out parsedMetadataHash ,
1217
+ error : out Exception manifestReadError ) )
1218
+ {
1219
+ metadataCreationError = new ErrorRecord (
1220
+ manifestReadError ,
1221
+ "ManifestFileReadParseForACRPublishError" ,
1222
+ ErrorCategory . ReadError ,
1223
+ _cmdletPassedIn ) ;
1224
+
1225
+ return jsonString ;
1226
+ }
1227
+ }
1228
+
1229
+ if ( parsedMetadataHash == null )
1230
+ {
1231
+ metadataCreationError = new ErrorRecord (
1232
+ new InvalidOperationException ( "Error parsing package metadata into hashtable." ) ,
1233
+ "PackageMetadataHashEmptyError" ,
1234
+ ErrorCategory . InvalidData ,
1235
+ _cmdletPassedIn ) ;
1236
+
1237
+ return jsonString ;
1238
+ }
1239
+
1240
+ _cmdletPassedIn . WriteVerbose ( "Serialize JSON into string." ) ;
1241
+ jsonString = System . Text . Json . JsonSerializer . Serialize ( parsedMetadataHash ) ;
1242
+
1243
+ return jsonString ;
1244
+ }
1245
+
1208
1246
#endregion
1209
1247
}
1210
1248
}
0 commit comments