private static RequirePackageMetadataState GetDefaultRuleSet() { var subscription = new MicrosoftTeamSubscription(); var policies = subscription.Policies; var state = RequirePackageMetadataComplianceUtility.DeserializeState(policies); return(state); }
private async Task <int> ExecuteAsync() { if (_helpOption.HasValue()) { ShowHelp(); return(-1); } if (_versionOption.HasValue()) { _console.WriteLine(ShortVersionGetter()); var commitId = _thisAssembly .GetCustomAttributes <AssemblyMetadataAttribute>() .Where(x => x.Key == "CommitId") .FirstOrDefault(); if (commitId != null) { _console.WriteLine($"Commit ID: {commitId.Value}"); } return(-1); } var packageService = GetPackageService(); var state = GetDefaultRuleSet(); if (_ruleSetOption.HasValue() && !string.IsNullOrWhiteSpace(_ruleSetOption.Value())) { var path = Path.GetFullPath(_ruleSetOption.Value()); if (_outputRuleSetOption.HasValue()) { // Just write out the default rule set. _console.WriteLine("Writing the default rule set to this path:"); _console.WriteLine(path); if (File.Exists(path)) { OutputColor( ConsoleColor.Yellow, () => { _console.WriteLine("A rule set already exists at this path. It will be replaced."); }); } var directory = Path.GetDirectoryName(path); if (!string.IsNullOrWhiteSpace(directory)) { Directory.CreateDirectory(directory); } var stateJson = SerializeState(state); var stateBytes = Encoding.UTF8.GetBytes(stateJson); File.WriteAllBytes(path, stateBytes); _console.WriteLine("Done."); return(0); } else { _console.WriteLine("Using rule set from this path:"); _console.WriteLine(path); var json = File.ReadAllText(path); state = RequirePackageMetadataComplianceUtility.DeserializeState(new[] { new UserSecurityPolicy(string.Empty, string.Empty, json), }); } } else { if (_outputRuleSetOption.HasValue()) { OutputColor( ConsoleColor.Red, () => { _console.WriteLine($"The {RuleSetOptionTemplate} option is required when specifying {WriteDefaultRuleSetOptionTemplate}."); }); ShowHelp(); return(-1); } } if (_pathsArgument.Values.Count == 0) { OutputColor( ConsoleColor.Red, () => { _console.WriteLine("At least one package path argument is required."); }); ShowHelp(); return(-1); } // Iterate over each argument. var validCount = 0; var invalidCount = 0; foreach (var path in _pathsArgument.Values) { if (string.IsNullOrWhiteSpace(path)) { continue; } _console.WriteLine("Using the following package path argument:"); _console.WriteLine(path); _console.WriteLine(); var directory = Path.GetDirectoryName(path); if (string.IsNullOrEmpty(directory)) { directory = "."; } var fileName = Path.GetFileName(path); IEnumerable <string> paths; if (fileName.Contains("*")) { paths = Directory.EnumerateFiles( directory, fileName, _recursiveOption.HasValue() ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); } else { paths = new[] { path }; } foreach (var packagePath in paths) { if (await IsValidAsync(packageService, state, packagePath)) { validCount++; } else { invalidCount++; } } } // Summarize the results. _console.WriteLine($"Valid package count: {validCount}"); _console.WriteLine($"Invalid package count: {invalidCount}"); if (invalidCount > 0) { _console.WriteLine(); _console.WriteLine("The metadata validation used the following property names and JSON ruleset."); _console.WriteLine(); var sb = new StringBuilder(); GetPropertyNamesAndRuleSet(sb, state); _console.WriteLine(sb.ToString()); } return(invalidCount); }