-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathUpdateModuleManifest.cs
More file actions
1214 lines (1009 loc) · 44.7 KB
/
UpdateModuleManifest.cs
File metadata and controls
1214 lines (1009 loc) · 44.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
/// <summary>
/// Updates the module manifest (.psd1) for a resource.
/// </summary>
[Cmdlet(VerbsData.Update, "PSModuleManifest")]
public sealed class UpdateModuleManifest : PSCmdlet
{
#region Parameters
/// <summary>
/// Specifies the path and file name of the module manifest.
/// </summary>
[Parameter (Position = 0, Mandatory = true, HelpMessage = "Path (including file name) to the module manifest (.psd1 file) to update.")]
[ValidateNotNullOrEmpty]
public string Path { get; set; }
/// <summary>
/// Specifies script modules (.psm1) and binary modules (.dll) that are imported into the module's session state.
/// </summary>
[Parameter]
public object[] NestedModules { get; set; }
/// <summary>
/// Specifies a unique identifier for the module, can be used to distinguish among modules with the same name.
/// </summary>
[Parameter]
public Guid Guid { get; set; }
/// <summary>
/// Specifies the module author.
/// </summary>
[Parameter]
public string Author { get; set; }
/// <summary>
/// Specifies the company or vendor who created the module.
/// </summary>
[Parameter]
public string CompanyName { get; set; }
/// <summary>
/// Specifies a copyright statement for the module.
/// </summary>
[Parameter]
public string Copyright { get; set; }
/// <summary>
/// Specifies the primary or root file of the module.
/// </summary>
[Parameter]
public string RootModule { get; set; }
/// <summary>
/// Specifies the version of the module.
/// </summary>
[Parameter]
public Version ModuleVersion { get; set; }
/// <summary>
/// Specifies a description of the module.
/// </summary>
[Parameter]
public string Description { get; set; }
/// <summary>
/// Specifies the processor architecture that the module requires.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public ProcessorArchitecture ProcessorArchitecture { get; set; }
/// <summary>
/// Specifies the compatible PSEditions of the module.
/// </summary>
[Parameter]
public string[] CompatiblePSEditions { get; set; }
/// <summary>
/// Specifies the minimum version of PowerShell that will work with this module.
/// </summary>
[Parameter]
public Version PowerShellVersion { get; set; }
/// <summary>
/// Specifies the minimum version of the Common Language Runtime (CLR) of the Microsoft .NET Framework that the module requires.
/// </summary>
[Parameter]
public Version ClrVersion { get; set; }
/// <summary>
/// Specifies the minimum version of the Microsoft .NET Framework that the module requires.
/// </summary>
[Parameter]
public Version DotNetFrameworkVersion { get; set; }
/// <summary>
/// Specifies the name of the PowerShell host program that the module requires.
/// </summary>
[Parameter]
public string PowerShellHostName { get; set; }
/// <summary>
/// Specifies the minimum version of the PowerShell host program that works with the module.
/// </summary>
[Parameter]
public Version PowerShellHostVersion { get; set; }
/// <summary>
/// Specifies modules that must be in the global session state.
/// </summary>
[Parameter]
public Object[] RequiredModules { get; set; }
/// <summary>
/// Specifies the type files (.ps1xml) that run when the module is imported.
/// </summary>
[Parameter]
public string[] TypesToProcess { get; set; }
/// <summary>
/// Specifies the formatting files (.ps1xml) that run when the module is imported.
/// </summary>
[Parameter]
public string[] FormatsToProcess { get; set; }
/// <summary>
/// Specifies script (.ps1) files that run in the caller's session state when the module is imported.
/// </summary>
[Parameter]
public string[] ScriptsToProcess { get; set; }
/// <summary>
/// Specifies the assembly (.dll) files that the module requires.
/// </summary>
[Parameter]
public string[] RequiredAssemblies { get; set; }
/// <summary>
/// Specifies all items that are included in the module.
/// </summary>
[Parameter]
public string[] FileList { get; set; }
/// <summary>
/// Specifies an array of modules that are included in the module.
/// </summary>
[Parameter]
public Object[] ModuleList { get; set; }
/// <summary>
/// Specifies the functions that the module exports.
/// </summary>
[SupportsWildcards]
[Parameter]
public string[] FunctionsToExport { get; set; }
/// <summary>
/// Specifies the aliases that the module exports.
/// </summary>
[SupportsWildcards]
[Parameter]
public string[] AliasesToExport { get; set; }
/// <summary>
/// Specifies the variables that the module exports.
/// </summary>
[SupportsWildcards]
[Parameter]
public string[] VariablesToExport { get; set; }
/// <summary>
/// Specifies the cmdlets that the module exports.
/// </summary>
[SupportsWildcards]
[Parameter]
public string[] CmdletsToExport { get; set; }
/// <summary>
/// Specifies the Desired State Configuration (DSC) resources that the module exports.
/// </summary>
[SupportsWildcards]
[Parameter]
public string[] DscResourcesToExport { get; set; }
/// <summary>
/// Specifies an array of tags.
/// </summary>
[Parameter]
[Alias("Tag")]
public string[] Tags { get; set; }
/// <summary>
/// Specifies the URL of a web page about this project.
/// </summary>
[Parameter]
public Uri ProjectUri { get; set; }
/// <summary>
/// Specifies the URL of licensing terms for the module.
/// </summary>
[Parameter]
public Uri LicenseUri { get; set; }
/// <summary>
/// Specifies the URL of an icon for the module.
/// </summary>
[Parameter]
public Uri IconUri { get; set; }
/// <summary>
/// Specifies a string that contains release notes or comments that you want available for this version of the script.
/// </summary>
[Parameter]
public string ReleaseNotes { get; set; }
/// <summary>
/// Indicates the prerelease label of the module.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string Prerelease { get; set; }
/// <summary>
/// Specifies the internet address of the module's HelpInfo XML file.
/// </summary>
[Parameter]
public Uri HelpInfoUri { get; set; }
/// <summary>
/// Specifies the default command prefix.
/// </summary>
[Parameter]
public string DefaultCommandPrefix { get; set; }
/// <summary>
/// Specifies an array of external module dependencies.
/// </summary>
[Parameter]
public string[] ExternalModuleDependencies { get; set; }
/// <summary>
/// Specifies that a license acceptance is required for the module.
/// </summary>
[Parameter]
public SwitchParameter RequireLicenseAcceptance { get; set; }
/// <summary>
/// Specifies data that is passed to the module when it's imported.
/// </summary>
[Parameter]
public Hashtable PrivateData { get; set; }
#endregion
#region Methods
protected override void EndProcessing()
{
if (MyInvocation.BoundParameters.ContainsKey(nameof(Prerelease)))
{
// get rid of any whitespace on prerelease label string.
Prerelease = Prerelease.Trim();
if (string.IsNullOrWhiteSpace(Prerelease))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException("Prerelease value cannot be empty or whitespace. Please re-run cmdlet with valid value."),
"PrereleaseValueCannotBeWhiteSpace",
ErrorCategory.InvalidArgument,
this));
}
}
string resolvedManifestPath = GetResolvedProviderPathFromPSPath(Path, out ProviderInfo provider).First();
// Test the path of the module manifest to see if the file exists
if (!File.Exists(resolvedManifestPath) || !resolvedManifestPath.EndsWith(".psd1"))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException($"The provided file path was not found: '{resolvedManifestPath}'. Please specify a valid module manifest (.psd1) file path."),
"moduleManifestPathNotFound",
ErrorCategory.ObjectNotFound,
this));
}
// Parse the module manifest
if(!Utils.TryReadManifestFile(
manifestFilePath: resolvedManifestPath,
manifestInfo: out Hashtable parsedMetadata,
error: out Exception manifestReadError))
{
ThrowTerminatingError(new ErrorRecord(
manifestReadError,
"ModuleManifestParseFailure",
ErrorCategory.ParserError,
this));
}
// Due to a PowerShell New-ModuleManifest bug with the PrivateData entry when it's a nested hashtable (https://github.com/PowerShell/PowerShell/issues/5922)
// we have to handle PrivateData entry, and thus module manifest creation, differently on PSCore than on WindowsPowerShell.
ErrorRecord errorRecord = null;
if (Utils.GetIsWindowsPowerShell(this))
{
CreateModuleManifestForWinPSHelper(parsedMetadata, resolvedManifestPath, out errorRecord);
}
else
{
CreateModuleManifestHelper(parsedMetadata, resolvedManifestPath, out errorRecord);
}
if (errorRecord != null)
{
ThrowTerminatingError(errorRecord);
}
}
/// <summary>
/// Handles module manifest creation for non-WindowsPowerShell platforms.
/// </summary>
private void CreateModuleManifestHelper(Hashtable parsedMetadata, string resolvedManifestPath, out ErrorRecord errorRecord)
{
errorRecord = null;
// Prerelease, ReleaseNotes, Tags, ProjectUri, LicenseUri, IconUri, RequireLicenseAcceptance,
// and ExternalModuleDependencies are all properties within a hashtable property called 'PSData'
// which is within another hashtable property called 'PrivateData'
// All of the properties mentioned above have their own parameter in 'New-ModuleManifest', so
// we will parse out these values from the parsedMetadata and create entries for each one in individually.
// This way any values that were previously specified here will get transferred over to the new manifest.
// Example of the contents of PSData:
// PrivateData = @{
// PSData = @{
// # Tags applied to this module. These help with module discovery in online galleries.
// Tags = @('Tag1', 'Tag2')
//
// # A URL to the license for this module.
// LicenseUri = 'https://www.licenseurl.com/'
//
// # A URL to the main website for this project.
// ProjectUri = 'https://www.projecturi.com/'
//
// # A URL to an icon representing this module.
// IconUri = 'https://iconuri.com/'
//
// # ReleaseNotes of this module.
// ReleaseNotes = 'These are the release notes of this module.'
//
// # Prerelease string of this module.
// Prerelease = 'preview'
//
// # Flag to indicate whether the module requires explicit user acceptance for install/update/save.
// RequireLicenseAcceptance = $false
//
// # External dependent modules of this module
// ExternalModuleDependencies = @('ModuleDep1, 'ModuleDep2')
//
// } # End of PSData hashtable
//
// } # End of PrivateData hashtable
Hashtable privateData = new Hashtable();
if (PrivateData != null && PrivateData.Count != 0)
{
privateData = PrivateData;
}
else
{
privateData = parsedMetadata["PrivateData"] as Hashtable;
}
var psData = privateData["PSData"] as Hashtable;
if (psData.ContainsKey("Prerelease"))
{
parsedMetadata["Prerelease"] = psData["Prerelease"];
}
if (psData.ContainsKey("ReleaseNotes"))
{
parsedMetadata["ReleaseNotes"] = psData["ReleaseNotes"];
}
if (psData.ContainsKey("Tags"))
{
parsedMetadata["Tags"] = psData["Tags"];
}
if (psData.ContainsKey("ProjectUri"))
{
parsedMetadata["ProjectUri"] = psData["ProjectUri"];
}
if (psData.ContainsKey("LicenseUri"))
{
parsedMetadata["LicenseUri"] = psData["LicenseUri"];
}
if (psData.ContainsKey("IconUri"))
{
parsedMetadata["IconUri"] = psData["IconUri"];
}
if (psData.ContainsKey("RequireLicenseAcceptance"))
{
parsedMetadata["RequireLicenseAcceptance"] = psData["RequireLicenseAcceptance"];
}
if (psData.ContainsKey("ExternalModuleDependencies"))
{
parsedMetadata["ExternalModuleDependencies"] = psData["ExternalModuleDependencies"];
}
// Now we need to remove 'PSData' because if we leave this value in the hashtable,
// New-ModuleManifest will keep this value and also attempt to create a new value for 'PSData'
// and then complain that there's two keys within the PrivateData hashtable.
// This is due to the issue of New-ModuleManifest when the PrivateData entry is a nested hashtable (https://github.com/PowerShell/PowerShell/issues/5922).
privateData.Remove("PSData");
// After getting the original module manifest contents, migrate all the fields to the parsedMetadata hashtable which will be provided as params for New-ModuleManifest.
if (NestedModules != null)
{
parsedMetadata["NestedModules"] = NestedModules;
}
if (Guid != Guid.Empty)
{
parsedMetadata["Guid"] = Guid;
}
if (!string.IsNullOrWhiteSpace(Author))
{
parsedMetadata["Author"] = Author;
}
if (CompanyName != null)
{
parsedMetadata["CompanyName"] = CompanyName;
}
if (Copyright != null)
{
parsedMetadata["Copyright"] = Copyright;
}
if (RootModule != null)
{
parsedMetadata["RootModule"] = RootModule;
}
if (ModuleVersion != null)
{
parsedMetadata["ModuleVersion"] = ModuleVersion;
}
if (Description != null)
{
parsedMetadata["Description"] = Description;
}
if (ProcessorArchitecture != ProcessorArchitecture.None)
{
parsedMetadata["ProcessorArchitecture"] = ProcessorArchitecture;
}
if (PowerShellVersion != null)
{
parsedMetadata["PowerShellVersion"] = PowerShellVersion;
}
if (ClrVersion != null)
{
parsedMetadata["ClrVersion"] = ClrVersion;
}
if (DotNetFrameworkVersion != null)
{
parsedMetadata["DotNetFrameworkVersion"] = DotNetFrameworkVersion;
}
if (PowerShellHostName != null)
{
parsedMetadata["PowerShellHostName"] = PowerShellHostName;
}
if (PowerShellHostVersion != null)
{
parsedMetadata["PowerShellHostVersion"] = PowerShellHostVersion;
}
if (RequiredModules != null)
{
parsedMetadata["RequiredModules"] = RequiredModules;
}
if (TypesToProcess != null)
{
parsedMetadata["TypesToProcess"] = TypesToProcess;
}
if (FormatsToProcess != null)
{
parsedMetadata["FormatsToProcess"] = FormatsToProcess;
}
if (ScriptsToProcess != null)
{
parsedMetadata["ScriptsToProcess"] = ScriptsToProcess;
}
if (RequiredAssemblies != null)
{
parsedMetadata["RequiredAssemblies"] = RequiredAssemblies;
}
if (FileList != null)
{
parsedMetadata["FileList"] = FileList;
}
if (ModuleList != null)
{
parsedMetadata["ModuleList"] = ModuleList;
}
if (FunctionsToExport != null)
{
parsedMetadata["FunctionsToExport"] = FunctionsToExport;
}
if (AliasesToExport != null)
{
parsedMetadata["AliasesToExport"] = AliasesToExport;
}
if (VariablesToExport != null)
{
parsedMetadata["VariablesToExport"] = VariablesToExport;
}
if (CmdletsToExport != null)
{
parsedMetadata["CmdletsToExport"] = CmdletsToExport;
}
if (DscResourcesToExport != null)
{
parsedMetadata["DscResourcesToExport"] = DscResourcesToExport;
}
if (CompatiblePSEditions != null)
{
parsedMetadata["CompatiblePSEditions"] = CompatiblePSEditions;
}
if (HelpInfoUri != null)
{
parsedMetadata["HelpInfoUri"] = HelpInfoUri;
}
if (DefaultCommandPrefix != null)
{
parsedMetadata["DefaultCommandPrefix"] = DefaultCommandPrefix;
}
if (Tags != null)
{
parsedMetadata["Tags"] = Tags;
}
if (LicenseUri != null)
{
parsedMetadata["LicenseUri"] = LicenseUri;
}
if (ProjectUri != null)
{
parsedMetadata["ProjectUri"] = ProjectUri;
}
if (IconUri != null)
{
parsedMetadata["IconUri"] = IconUri;
}
if (ReleaseNotes != null)
{
parsedMetadata["ReleaseNotes"] = ReleaseNotes;
}
if (Prerelease != null)
{
parsedMetadata["Prerelease"] = Prerelease;
}
if (RequireLicenseAcceptance != null && RequireLicenseAcceptance.IsPresent)
{
parsedMetadata["RequireLicenseAcceptance"] = RequireLicenseAcceptance;
}
if (ExternalModuleDependencies != null)
{
parsedMetadata["ExternalModuleDependencies"] = ExternalModuleDependencies;
}
// create a tmp path to create the module manifest
string tmpParentPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
try
{
Directory.CreateDirectory(tmpParentPath);
}
catch (Exception e)
{
Utils.DeleteDirectory(tmpParentPath);
errorRecord = new ErrorRecord(
new ArgumentException(e.Message),
"ErrorCreatingTempDir",
ErrorCategory.InvalidData,
this);
return;
}
string tmpModuleManifestPath = System.IO.Path.Combine(tmpParentPath, System.IO.Path.GetFileName(resolvedManifestPath));
parsedMetadata["Path"] = tmpModuleManifestPath;
WriteVerbose($"Temp path created for new module manifest is: {tmpModuleManifestPath}");
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
{
try
{
var results = pwsh.AddCommand("Microsoft.PowerShell.Core\\New-ModuleManifest").AddParameters(parsedMetadata).Invoke<Object>();
if (pwsh.HadErrors || pwsh.Streams.Error.Count > 0)
{
foreach (var err in pwsh.Streams.Error)
{
WriteError(err);
}
}
}
catch (Exception e)
{
errorRecord = new ErrorRecord(
new ArgumentException($"Error occurred while running 'New-ModuleManifest': {e.Message}"),
"ErrorExecutingNewModuleManifest",
ErrorCategory.InvalidArgument,
this);
return;
}
}
try
{
// Move to the new module manifest back to the original location
WriteVerbose($"Moving '{tmpModuleManifestPath}' to '{resolvedManifestPath}'");
Utils.MoveFiles(tmpModuleManifestPath, resolvedManifestPath, overwrite: true);
}
catch (Exception e)
{
errorRecord = new ErrorRecord(
e,
"CreateModuleManifestFailed",
ErrorCategory.InvalidOperation,
this);
}
finally {
// Clean up temp file if move fails
if (File.Exists(tmpModuleManifestPath))
{
File.Delete(tmpModuleManifestPath);
}
Utils.DeleteDirectory(tmpParentPath);
}
}
/// <summary>
/// Handles module manifest creation for Windows PowerShell platform.
/// Since the code calls New-ModuleManifest and the Windows PowerShell version of the cmdlet did not have Prerelease, ExternalModuleDependencies and RequireLicenseAcceptance parameters,
/// we can't simply call New-ModuleManifest with all parameters. Instead, create the manifest without PrivateData parameter (and the keys usually inside it) and then update the lines for PrivateData later.
/// </summary>
private void CreateModuleManifestForWinPSHelper(Hashtable parsedMetadata, string resolvedManifestPath, out ErrorRecord errorRecord)
{
// Note on priority of values:
// If -PrivateData parameter was provided with the cmdlet & .psd1 file PrivateData already had values, the passed in -PrivateData values replace those previously there.
// any direct parameters supplied by the user (i.e ProjectUri) [takes priority over but in mix-and-match fashion] over -> -PrivateData parameter [takes priority over but in replacement fashion] over -> original .psd1 file's PrivateData values (complete replacement)
errorRecord = null;
string[] tags = Utils.EmptyStrArray;
Uri licenseUri = null;
Uri iconUri = null;
Uri projectUri = null;
string prerelease = String.Empty;
string releaseNotes = String.Empty;
bool? requireLicenseAcceptance = null;
string[] externalModuleDependencies = Utils.EmptyStrArray;
Hashtable privateData = new Hashtable();
if (PrivateData != null && PrivateData.Count != 0)
{
privateData = PrivateData;
}
else
{
privateData = parsedMetadata["PrivateData"] as Hashtable;
}
var psData = privateData["PSData"] as Hashtable;
if (psData.ContainsKey("Prerelease"))
{
prerelease = psData["Prerelease"] as string;
}
if (psData.ContainsKey("ReleaseNotes"))
{
releaseNotes = psData["ReleaseNotes"] as string;
}
if (psData.ContainsKey("Tags"))
{
tags = psData["Tags"] as string[];
}
if (psData.ContainsKey("ProjectUri") && psData["ProjectUri"] is string projectUriString)
{
if (!Uri.TryCreate(projectUriString, UriKind.Absolute, out projectUri))
{
projectUri = null;
}
}
if (psData.ContainsKey("LicenseUri") && psData["LicenseUri"] is string licenseUriString)
{
if (!Uri.TryCreate(licenseUriString, UriKind.Absolute, out licenseUri))
{
licenseUri = null;
}
}
if (psData.ContainsKey("IconUri") && psData["IconUri"] is string iconUriString)
{
if (!Uri.TryCreate(iconUriString, UriKind.Absolute, out iconUri))
{
iconUri = null;
}
}
if (psData.ContainsKey("RequireLicenseAcceptance"))
{
requireLicenseAcceptance = psData["RequireLicenseAcceptance"] as bool?;
}
if (psData.ContainsKey("ExternalModuleDependencies"))
{
externalModuleDependencies = psData["ExternalModuleDependencies"] as string[];
}
// the rest of the parameters can be directly provided to New-ModuleManifest, so add it parsedMetadata hashtable used for cmdlet parameters.
if (NestedModules != null)
{
parsedMetadata["NestedModules"] = NestedModules;
}
if (Guid != Guid.Empty)
{
parsedMetadata["Guid"] = Guid;
}
if (!string.IsNullOrWhiteSpace(Author))
{
parsedMetadata["Author"] = Author;
}
if (CompanyName != null)
{
parsedMetadata["CompanyName"] = CompanyName;
}
if (Copyright != null)
{
parsedMetadata["Copyright"] = Copyright;
}
if (RootModule != null)
{
parsedMetadata["RootModule"] = RootModule;
}
if (ModuleVersion != null)
{
parsedMetadata["ModuleVersion"] = ModuleVersion;
}
if (Description != null)
{
parsedMetadata["Description"] = Description;
}
if (ProcessorArchitecture != ProcessorArchitecture.None)
{
parsedMetadata["ProcessorArchitecture"] = ProcessorArchitecture;
}
if (PowerShellVersion != null)
{
parsedMetadata["PowerShellVersion"] = PowerShellVersion;
}
if (ClrVersion != null)
{
parsedMetadata["ClrVersion"] = ClrVersion;
}
if (DotNetFrameworkVersion != null)
{
parsedMetadata["DotNetFrameworkVersion"] = DotNetFrameworkVersion;
}
if (PowerShellHostName != null)
{
parsedMetadata["PowerShellHostName"] = PowerShellHostName;
}
if (PowerShellHostVersion != null)
{
parsedMetadata["PowerShellHostVersion"] = PowerShellHostVersion;
}
if (RequiredModules != null)
{
parsedMetadata["RequiredModules"] = RequiredModules;
}
if (TypesToProcess != null)
{
parsedMetadata["TypesToProcess"] = TypesToProcess;
}
if (FormatsToProcess != null)
{
parsedMetadata["FormatsToProcess"] = FormatsToProcess;
}
if (ScriptsToProcess != null)
{
parsedMetadata["ScriptsToProcess"] = ScriptsToProcess;
}
if (RequiredAssemblies != null)
{
parsedMetadata["RequiredAssemblies"] = RequiredAssemblies;
}
if (FileList != null)
{
parsedMetadata["FileList"] = FileList;
}
if (ModuleList != null)
{
parsedMetadata["ModuleList"] = ModuleList;
}
if (FunctionsToExport != null)
{
parsedMetadata["FunctionsToExport"] = FunctionsToExport;
}
if (AliasesToExport != null)
{
parsedMetadata["AliasesToExport"] = AliasesToExport;
}
if (VariablesToExport != null)
{
parsedMetadata["VariablesToExport"] = VariablesToExport;
}
if (CmdletsToExport != null)
{
parsedMetadata["CmdletsToExport"] = CmdletsToExport;
}
if (DscResourcesToExport != null)
{
parsedMetadata["DscResourcesToExport"] = DscResourcesToExport;
}
if (CompatiblePSEditions != null)
{
parsedMetadata["CompatiblePSEditions"] = CompatiblePSEditions;
}
if (HelpInfoUri != null)
{
parsedMetadata["HelpInfoUri"] = HelpInfoUri;
}
if (DefaultCommandPrefix != null)
{
parsedMetadata["DefaultCommandPrefix"] = DefaultCommandPrefix;
}
// if values were passed in for these parameters, they will be prioritized over values retrieved from PrivateData
// we need to populate the local variables with their values to use for PrivateData entry creation later.
// and parameters that can be passed to New-ModuleManifest are added to the parsedMetadata hashtable.
if (Tags != null)
{
tags = Tags;
parsedMetadata["Tags"] = tags;
}
if (LicenseUri != null)
{
licenseUri = LicenseUri;
parsedMetadata["LicenseUri"] = licenseUri;
}
if (ProjectUri != null)
{
projectUri = ProjectUri;
parsedMetadata["ProjectUri"] = projectUri;
}
if (IconUri != null)
{
iconUri = IconUri;
parsedMetadata["IconUri"] = iconUri;
}
if (ReleaseNotes != null)
{
releaseNotes = ReleaseNotes;
parsedMetadata["ReleaseNotes"] = releaseNotes;
}
// New-ModuleManifest on WinPS doesn't support parameters: Prerelease, RequireLicenseAcceptance, and ExternalModuleDependencies so we don't add those to parsedMetadata hashtable.
if (Prerelease != null)
{
prerelease = Prerelease;
}
if (RequireLicenseAcceptance != null && RequireLicenseAcceptance.IsPresent)
{
requireLicenseAcceptance = RequireLicenseAcceptance;
}
if (ExternalModuleDependencies != null)
{
externalModuleDependencies = ExternalModuleDependencies;
}
// create a tmp path to create the module manifest
string tmpParentPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
try
{
Directory.CreateDirectory(tmpParentPath);
}
catch (Exception e)
{
Utils.DeleteDirectory(tmpParentPath);
errorRecord = new ErrorRecord(
new ArgumentException(e.Message),
"ErrorCreatingTempDir",
ErrorCategory.InvalidData,
this);
return;
}
string tmpModuleManifestPath = System.IO.Path.Combine(tmpParentPath, System.IO.Path.GetFileName(resolvedManifestPath));
parsedMetadata["Path"] = tmpModuleManifestPath;
WriteVerbose($"Temp path created for new module manifest is: {tmpModuleManifestPath}");
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
{
try
{
var results = pwsh.AddCommand("Microsoft.PowerShell.Core\\New-ModuleManifest").AddParameters(parsedMetadata).Invoke<Object>();
if (pwsh.HadErrors || pwsh.Streams.Error.Count > 0)
{
foreach (var err in pwsh.Streams.Error)
{
WriteError(err);
}
}