Skip to content

Commit 64c9451

Browse files
author
Maddie Clayton
authored
Merge pull request Azure#6116 from MiYanni/NetCoreVersionUpdate
Allow version update for NetCore psd1 files
2 parents 663aa42 + 2734a4d commit 64c9451

File tree

3 files changed

+152
-15
lines changed

3 files changed

+152
-15
lines changed

tools/NetCorePsd1Sync/NetCorePsd1Sync/NetCoreDefinitionGenerator.cs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.IO;
2020
using System.Linq;
2121
using System.Management.Automation;
22+
using System.Reflection;
2223
using NetCorePsd1Sync.Model;
2324
using NetCorePsd1Sync.Utility;
2425
using static NetCorePsd1Sync.Model.PsDefinitionConstants;
@@ -87,18 +88,18 @@ public static Hashtable GetHashtable(PowerShell powershell, string path)
8788
return hashtable;
8889
}
8990

90-
public static IEnumerable<Hashtable> GetDesktopHashtables(IEnumerable<string> desktopFilePaths)
91+
public static IEnumerable<Hashtable> GetHashtables(IEnumerable<string> filePaths)
9192
{
9293
using (var powershell = PowerShell.Create())
9394
{
94-
foreach (var path in desktopFilePaths)
95+
foreach (var path in filePaths)
9596
{
9697
yield return GetHashtable(powershell, path);
9798
}
9899
}
99100
}
100101

101-
public static PsDefinition CreateNetCoreDefinition(Hashtable desktopData)
102+
public static PsDefinition CreateNewNetCoreDefinition(Hashtable desktopData)
102103
{
103104
var psData = new PsData();
104105
var desktopPsData = desktopData.GetValueAsHashtable("PrivateData").GetValueAsHashtable("PSData");
@@ -138,5 +139,79 @@ public static PsDefinition CreateNetCoreDefinition(Hashtable desktopData)
138139
PrivateData = new PrivateData { PsData = psData }
139140
};
140141
}
142+
143+
private static ModuleReference CreateModuleReferenceFromHashtable(Hashtable data)
144+
{
145+
var guid = data.GetValueAsStringOrDefault("GUID");
146+
return new ModuleReference
147+
{
148+
ModuleName = data.GetValueAsStringOrDefault("ModuleName"),
149+
ModuleVersion = data.GetValueAsVersionOrDefault("ModuleVersion"),
150+
Guid = String.IsNullOrEmpty(guid) ? (Guid?)null : Guid.Parse(guid)
151+
};
152+
}
153+
154+
private static List<ModuleReference> CreateModuleReferenceList(Hashtable data, string key) =>
155+
data.GetValueAsArrayOrDefault(key)?.OfType<Hashtable>().Select(CreateModuleReferenceFromHashtable).ToList();
156+
157+
public static PsDefinition CreateDefinitionFromExisting(Hashtable existingDefinition, PsDefinitionHeader existingHeader)
158+
{
159+
var psData = new PsData();
160+
var existingPsData = existingDefinition.GetValueAsHashtable("PrivateData").GetValueAsHashtable("PSData");
161+
if (existingPsData.Any())
162+
{
163+
var licenseUri = existingPsData.GetValueAsStringOrDefault("LicenseUri");
164+
var projectUri = existingPsData.GetValueAsStringOrDefault("ProjectUri");
165+
var iconUri = existingPsData.GetValueAsStringOrDefault("IconUri");
166+
var requireLicenseAcceptance = existingPsData.GetValueAsStringOrDefault("RequireLicenseAcceptance");
167+
psData = new PsData
168+
{
169+
Tags = existingPsData.GetValueAsStringListOrDefault("Tags"),
170+
LicenseUri = String.IsNullOrEmpty(licenseUri) ? null : new Uri(licenseUri),
171+
ProjectUri = String.IsNullOrEmpty(projectUri) ? null : new Uri(projectUri),
172+
IconUri = String.IsNullOrEmpty(iconUri) ? null : new Uri(iconUri),
173+
ReleaseNotes = existingPsData.GetValueAsStringOrDefault("ReleaseNotes"),
174+
Prerelease = existingPsData.GetValueAsStringOrDefault("Prerelease"),
175+
RequireLicenseAcceptance = String.IsNullOrEmpty(requireLicenseAcceptance) ? (bool?)null : Boolean.Parse(requireLicenseAcceptance),
176+
ExternalModuleDependencies = CreateModuleReferenceList(existingPsData, "ExternalModuleDependencies")
177+
};
178+
}
179+
180+
var processorArchitecture = existingDefinition.GetValueAsStringOrDefault("ProcessorArchitecture");
181+
return new PsDefinition
182+
{
183+
ManifestHeader = existingHeader,
184+
RootModule = existingDefinition.GetValueAsStringOrDefault("RootModule"),
185+
ModuleVersion = existingDefinition.GetValueAsVersionOrDefault("ModuleVersion"),
186+
CompatiblePsEditions = existingDefinition.GetValueAsStringListOrDefault("CompatiblePSEditions"),
187+
Guid = Guid.Parse(existingDefinition.GetValueAsStringOrDefault("GUID")),
188+
Author = existingDefinition.GetValueAsStringOrDefault("Author"),
189+
CompanyName = existingDefinition.GetValueAsStringOrDefault("CompanyName"),
190+
Copyright = existingDefinition.GetValueAsStringOrDefault("Copyright"),
191+
Description = existingDefinition.GetValueAsStringOrDefault("Description"),
192+
PowerShellVersion = existingDefinition.GetValueAsVersionOrDefault("PowerShellVersion"),
193+
PowerShellHostName = existingDefinition.GetValueAsStringOrDefault("PowerShellHostName"),
194+
PowerShellHostVersion = existingDefinition.GetValueAsVersionOrDefault("PowerShellHostVersion"),
195+
DotNetFrameworkVersion = existingDefinition.GetValueAsVersionOrDefault("DotNetFrameworkVersion"),
196+
ClrVersion = existingDefinition.GetValueAsVersionOrDefault("CLRVersion"),
197+
ProcessorArchitecture = processorArchitecture != null ? Enum.Parse<ProcessorArchitecture>(processorArchitecture) : (ProcessorArchitecture?)null,
198+
RequiredModules = CreateModuleReferenceList(existingDefinition, "RequiredModules"),
199+
RequiredAssemblies = existingDefinition.GetValueAsStringListOrDefault("RequiredAssemblies"),
200+
ScriptsToProcess = existingDefinition.GetValueAsStringListOrDefault("ScriptsToProcess"),
201+
TypesToProcess = existingDefinition.GetValueAsStringListOrDefault("TypesToProcess"),
202+
FormatsToProcess = existingDefinition.GetValueAsStringListOrDefault("FormatsToProcess"),
203+
NestedModules = existingDefinition.GetValueAsStringListOrDefault("NestedModules")?.Select(m => new ModuleReference { ModuleName = m }).ToList(),
204+
FunctionsToExport = existingDefinition.GetValueAsStringListOrDefault("FunctionsToExport"),
205+
CmdletsToExport = existingDefinition.GetValueAsStringListOrDefault("CmdletsToExport"),
206+
VariablesToExport = existingDefinition.GetValueAsStringListOrDefault("VariablesToExport"),
207+
AliasesToExport = existingDefinition.GetValueAsStringListOrDefault("AliasesToExport"),
208+
DscResourcesToExport = existingDefinition.GetValueAsStringListOrDefault("DscResourcesToExport"),
209+
ModuleList = CreateModuleReferenceList(existingDefinition, "ModuleList"),
210+
FileList = existingDefinition.GetValueAsStringListOrDefault("FileList"),
211+
PrivateData = new PrivateData { PsData = psData },
212+
HelpInfoUri = existingDefinition.GetValueAsStringOrDefault("HelpInfoURI"),
213+
DefaultCommandPrefix = existingDefinition.GetValueAsStringOrDefault("DefaultCommandPrefix")
214+
};
215+
}
141216
}
142217
}

tools/NetCorePsd1Sync/NetCorePsd1Sync/Program.cs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.IO;
1919
using System.Linq;
2020
using System.Management.Automation;
21+
using NetCorePsd1Sync.Model;
2122
using NetCorePsd1Sync.Utility;
2223
using static NetCorePsd1Sync.NetCoreDefinitionGenerator;
2324

@@ -27,29 +28,75 @@ public static class Program
2728
{
2829
private const string Validate = "-v";
2930
private const string Create = "-c";
31+
private const string UpdateVersion = "-u";
3032

3133
private static readonly Dictionary<string, Action<string>> ModeMap = new Dictionary<string, Action<string>>
3234
{
3335
{ Validate, ValidateDefinitionFiles },
34-
{ Create, CreateDefinitionFiles }
36+
{ Create, CreateDefinitionFiles },
37+
{ UpdateVersion, p => UpdateModuleVersions(p, _newVersion) }
3538
};
39+
40+
private static Version _newVersion;
41+
3642
public static void Main(string[] args)
3743
{
38-
var rmPath = args.FirstOrDefault(a => !ModeMap.ContainsKey(a)) ?? @"..\..\..\src\ResourceManager";
44+
var rmPath = args.FirstOrDefault(a => !ModeMap.ContainsKey(a.ToLower()) && !Version.TryParse(a, out var _)) ?? @"..\..\..\src\ResourceManager";
3945
if (!Directory.Exists(rmPath))
4046
{
4147
throw new ArgumentException($"Directory [{rmPath}] does not exist");
4248
}
4349
//https://stackoverflow.com/a/17563994/294804
44-
var mode = args.Any(a => a.IndexOf(Create, StringComparison.InvariantCultureIgnoreCase) >= 0) ? Create : Validate;
50+
var mode = ModeMap.Keys.FirstOrDefault(k => args.Any(a => a.IndexOf(k, StringComparison.InvariantCultureIgnoreCase) >= 0)) ?? Validate;
51+
if(mode == UpdateVersion)
52+
{
53+
var newVersion = args.FirstOrDefault(a => Version.TryParse(a, out var _));
54+
if (newVersion == null)
55+
{
56+
throw new ArgumentException($"Module update version must be provided");
57+
}
58+
_newVersion = Version.Parse(newVersion);
59+
}
4560
ModeMap[mode](rmPath);
4661
}
4762

63+
private static void UpdateModuleVersions(string rmPath, Version newVersion)
64+
{
65+
var modulePaths = GetModulePaths(rmPath, true);
66+
var desktopFilePaths = GetDesktopFilePaths(modulePaths);
67+
var netCoreFilePaths = desktopFilePaths.Select(ConvertDesktopToNetCorePath).Where(File.Exists).ToList();
68+
netCoreFilePaths.Add(Path.Combine(rmPath, @"..\Storage\Azure.Storage.Netcore.psd1"));
69+
netCoreFilePaths.Add(Path.Combine(rmPath, @"..\..\tools\AzureRM.Netcore\AzureRM.Netcore.psd1"));
70+
netCoreFilePaths.Add(Path.Combine(rmPath, @"AnalysisServices\Commands.AnalysisServices.Dataplane\Azure.AnalysisServices.Netcore.psd1"));
71+
var netCoreHashTables = GetHashtables(netCoreFilePaths);
72+
73+
foreach (var netCoreHashtable in netCoreHashTables)
74+
{
75+
var netCoreFilePath = netCoreHashtable.GetValueAsString("FilePath");
76+
var definitionContent = File.ReadAllLines(netCoreFilePath);
77+
const string moduleNameLeader = "# Module manifest for module ";
78+
var headerModuleName = definitionContent.First(c => c.StartsWith(moduleNameLeader)).Replace(moduleNameLeader, String.Empty).Replace("'", String.Empty);
79+
const string authorLeader = "# Generated by: ";
80+
var headerAuthor = definitionContent.First(c => c.StartsWith(authorLeader)).Replace(authorLeader, String.Empty);
81+
const string dateLeader = "# Generated on: ";
82+
var headerDate = DateTime.Parse(definitionContent.First(c => c.StartsWith(dateLeader)).Replace(dateLeader, String.Empty));
83+
Console.WriteLine($"Updating {netCoreFilePath} to version {newVersion}");
84+
var netCoreDefinition = CreateDefinitionFromExisting(netCoreHashtable, new PsDefinitionHeader { ModuleName = headerModuleName, Author = headerAuthor, Date = headerDate });
85+
netCoreDefinition.ModuleVersion = newVersion;
86+
foreach (var requiredModule in netCoreDefinition.RequiredModules ?? Enumerable.Empty<ModuleReference>())
87+
{
88+
requiredModule.ModuleVersion = newVersion;
89+
}
90+
91+
File.WriteAllLines(netCoreFilePath, netCoreDefinition.ToDefinitionEntry());
92+
}
93+
}
94+
4895
private static void ValidateDefinitionFiles(string rmPath)
4996
{
5097
var modulePaths = GetModulePaths(rmPath, true);
5198
var desktopFilePaths = GetDesktopFilePaths(modulePaths);
52-
var desktopHashtables = GetDesktopHashtables(desktopFilePaths);
99+
var desktopHashtables = GetHashtables(desktopFilePaths);
53100
foreach (var desktopHashtable in desktopHashtables)
54101
{
55102
var netCorePath = ConvertDesktopToNetCorePath(desktopHashtable.GetValueAsString("FilePath"));
@@ -109,18 +156,18 @@ private static void CreateDefinitionFiles(string rmPath)
109156
{
110157
var modulePaths = GetModulePaths(rmPath);
111158
var desktopFilePaths = GetDesktopFilePaths(modulePaths);
112-
var desktopHashtables = GetDesktopHashtables(desktopFilePaths);
159+
var desktopHashtables = GetHashtables(desktopFilePaths);
113160
foreach (var desktopHashtable in desktopHashtables)
114161
{
115162
var netCoreFilePath = ConvertDesktopToNetCorePath(desktopHashtable.GetValueAsString("FilePath"));
116163
Console.WriteLine($"Creating {netCoreFilePath}");
117-
var netCoreDefinition = CreateNetCoreDefinition(desktopHashtable);
164+
var netCoreDefinition = CreateNewNetCoreDefinition(desktopHashtable);
118165
if (File.Exists(netCoreFilePath))
119166
{
120167
using (var powershell = PowerShell.Create())
121168
{
122169
var netCoreHashtable = GetHashtable(powershell, netCoreFilePath);
123-
netCoreDefinition = CreateNetCoreDefinition(netCoreHashtable);
170+
netCoreDefinition = CreateNewNetCoreDefinition(netCoreHashtable);
124171
var rootModule = netCoreHashtable.GetValueAsString("RootModule");
125172
netCoreDefinition.RootModule = rootModule == String.Empty ? null : rootModule;
126173
netCoreDefinition.Guid = new Guid(netCoreHashtable.GetValueAsString("GUID"));

tools/NetCorePsd1Sync/NetCorePsd1Sync/Utility/HashtableExtensions.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,34 @@ namespace NetCorePsd1Sync.Utility
2121
{
2222
internal static class HashtableExtensions
2323
{
24-
public static string GetValueAsString(this Hashtable hashtable, string key) => hashtable[key]?.ToString() ?? String.Empty;
24+
public static string GetValueAsString(this Hashtable hashtable, string key) => GetValueAsStringOrDefault(hashtable, key) ?? String.Empty;
2525

26-
public static object[] GetValueAsArray(this Hashtable hashtable, string key) => hashtable[key] as object[] ?? new object[]{};
26+
public static string GetValueAsStringOrDefault(this Hashtable hashtable, string key) => hashtable[key]?.ToString();
27+
28+
public static Version GetValueAsVersion(this Hashtable hashtable, string key) => GetValueAsVersionOrDefault(hashtable, key) ?? new Version();
29+
30+
public static Version GetValueAsVersionOrDefault(this Hashtable hashtable, string key)
31+
{
32+
var valueAsString = GetValueAsStringOrDefault(hashtable, key);
33+
if (valueAsString == null || !Version.TryParse(valueAsString, out var version)) return null;
34+
return version;
35+
}
36+
37+
public static object[] GetValueAsArray(this Hashtable hashtable, string key) => GetValueAsArrayOrDefault(hashtable, key) ?? new object[]{};
38+
39+
public static object[] GetValueAsArrayOrDefault(this Hashtable hashtable, string key) => hashtable[key] as object[];
2740

2841
public static Hashtable GetValueAsHashtable(this Hashtable hashtable, string key) => hashtable[key] as Hashtable ?? new Hashtable();
2942

30-
public static List<string> GetValueAsStringList(this Hashtable hashtable, string key)
43+
public static List<string> GetValueAsStringList(this Hashtable hashtable, string key) => GetValueAsStringListOrDefault(hashtable, key) ?? new List<string>();
44+
45+
public static List<string> GetValueAsStringListOrDefault(this Hashtable hashtable, string key)
3146
{
3247
if (hashtable[key] is string stringValue)
3348
{
34-
return new List<string>{stringValue};
49+
return new List<string> { stringValue };
3550
}
36-
return hashtable.GetValueAsArray(key).OfType<string>().ToList();
51+
return hashtable.GetValueAsArrayOrDefault(key)?.OfType<string>().ToList();
3752
}
3853

3954
public static bool Any(this Hashtable hashtable) => hashtable.Count > 0;

0 commit comments

Comments
 (0)