Skip to content

Commit e602e1a

Browse files
committed
Move ComponentCatalog from a static class to a member of IHostEnvironment.
1 parent 1c9e9ff commit e602e1a

File tree

27 files changed

+225
-287
lines changed

27 files changed

+225
-287
lines changed

src/Common/AssemblyLoadingUtils.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ internal static class AssemblyLoadingUtils
1515
/// <summary>
1616
/// Make sure the given assemblies are loaded and that their loadable classes have been catalogued.
1717
/// </summary>
18-
public static void LoadAndRegister(string[] assemblies)
18+
public static void LoadAndRegister(IHostEnvironment env, string[] assemblies)
1919
{
20+
Contracts.AssertValue(env);
21+
2022
if (Utils.Size(assemblies) > 0)
2123
{
2224
foreach (string path in assemblies)
@@ -26,7 +28,7 @@ public static void LoadAndRegister(string[] assemblies)
2628
{
2729
// REVIEW: Will LoadFrom ever return null?
2830
Contracts.CheckNonEmpty(path, nameof(path));
29-
var assem = LoadAssembly(path);
31+
var assem = LoadAssembly(env, path);
3032
if (assem != null)
3133
continue;
3234
}
@@ -68,7 +70,7 @@ public static void LoadAndRegister(string[] assemblies)
6870
throw Contracts.ExceptIO(e, "Extracting extra assembly zip failed: '{0}'", path);
6971
}
7072

71-
LoadAssembliesInDir(dir);
73+
LoadAssembliesInDir(env, dir);
7274
}
7375
}
7476
}
@@ -86,7 +88,7 @@ private static string GetTempPath()
8688
return Path.GetFullPath(Path.Combine(Path.GetTempPath(), "MLNET_" + guid.ToString()));
8789
}
8890

89-
private static void LoadAssembliesInDir(string dir)
91+
private static void LoadAssembliesInDir(IHostEnvironment env, string dir)
9092
{
9193
if (!Directory.Exists(dir))
9294
return;
@@ -95,19 +97,19 @@ private static void LoadAssembliesInDir(string dir)
9597
var paths = Directory.EnumerateFiles(dir, "*.dll");
9698
foreach (string path in paths)
9799
{
98-
LoadAssembly(path);
100+
LoadAssembly(env, path);
99101
}
100102
}
101103

102104
/// <summary>
103105
/// Given an assembly path, load the assembly and register it with the ComponentCatalog.
104106
/// </summary>
105-
private static Assembly LoadAssembly(string path)
107+
private static Assembly LoadAssembly(IHostEnvironment env, string path)
106108
{
107109
try
108110
{
109111
var assembly = Assembly.LoadFrom(path);
110-
ComponentCatalog.RegisterAssembly(assembly);
112+
env.ComponentCatalog.RegisterAssembly(assembly);
111113
return assembly;
112114
}
113115
catch (Exception)

src/Microsoft.ML.Api/ComponentCreation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ private static TRes CreateCore<TRes, TArgs, TSig>(IHostEnvironment env, TArgs ar
439439
{
440440
env.CheckValue(args, nameof(args));
441441

442-
var classes = ComponentCatalog.FindLoadableClasses<TArgs, TSig>();
442+
var classes = env.ComponentCatalog.FindLoadableClasses<TArgs, TSig>();
443443
if (classes.Length == 0)
444444
throw env.Except("Couldn't find a {0} class that accepts {1} as arguments.", typeof(TRes).Name, typeof(TArgs).FullName);
445445
if (classes.Length > 1)

src/Microsoft.ML.Core/CommandLine/CmdParser.cs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public static string CombineSettings(string[] settings)
414414
}
415415

416416
// REVIEW: Add a method for cloning arguments, instead of going to text and back.
417-
public static string GetSettings(IExceptionContext ectx, object values, object defaults, SettingsFlags flags = SettingsFlags.Default)
417+
public static string GetSettings(IHostEnvironment env, object values, object defaults, SettingsFlags flags = SettingsFlags.Default)
418418
{
419419
Type t1 = values.GetType();
420420
Type t2 = defaults.GetType();
@@ -430,7 +430,7 @@ public static string GetSettings(IExceptionContext ectx, object values, object d
430430
return null;
431431

432432
var info = GetArgumentInfo(t, defaults);
433-
return GetSettingsCore(ectx, info, values, flags);
433+
return GetSettingsCore(env, info, values, flags);
434434
}
435435

436436
public static IEnumerable<KeyValuePair<string, string>> GetSettingPairs(IHostEnvironment env, object values, object defaults, SettingsFlags flags = SettingsFlags.None)
@@ -862,20 +862,20 @@ private bool Parse(ArgumentInfo info, string[] strs, object destination)
862862
return !hadError;
863863
}
864864

865-
private static string GetSettingsCore(IExceptionContext ectx, ArgumentInfo info, object values, SettingsFlags flags)
865+
private static string GetSettingsCore(IHostEnvironment env, ArgumentInfo info, object values, SettingsFlags flags)
866866
{
867867
StringBuilder sb = new StringBuilder();
868868

869869
if (info.ArgDef != null)
870870
{
871871
var val = info.ArgDef.GetValue(values);
872-
info.ArgDef.AppendSetting(ectx, sb, val, flags);
872+
info.ArgDef.AppendSetting(env, sb, val, flags);
873873
}
874874

875875
foreach (Argument arg in info.Args)
876876
{
877877
var val = arg.GetValue(values);
878-
arg.AppendSetting(ectx, sb, val, flags);
878+
arg.AppendSetting(env, sb, val, flags);
879879
}
880880

881881
return sb.ToString();
@@ -886,7 +886,7 @@ private static string GetSettingsCore(IExceptionContext ectx, ArgumentInfo info,
886886
/// It deals with custom "unparse" functionality, as well as quoting. It also appends to a StringBuilder
887887
/// instead of returning a string.
888888
/// </summary>
889-
private static void AppendCustomItem(IExceptionContext ectx, ArgumentInfo info, object values, SettingsFlags flags, StringBuilder sb)
889+
private static void AppendCustomItem(IHostEnvironment env, ArgumentInfo info, object values, SettingsFlags flags, StringBuilder sb)
890890
{
891891
int ich = sb.Length;
892892
// We always call unparse, even when NoUnparse is specified, since Unparse can "cleanse", which
@@ -902,28 +902,28 @@ private static void AppendCustomItem(IExceptionContext ectx, ArgumentInfo info,
902902
if (info.ArgDef != null)
903903
{
904904
var val = info.ArgDef.GetValue(values);
905-
info.ArgDef.AppendSetting(ectx, sb, val, flags);
905+
info.ArgDef.AppendSetting(env, sb, val, flags);
906906
}
907907

908908
foreach (Argument arg in info.Args)
909909
{
910910
var val = arg.GetValue(values);
911-
arg.AppendSetting(ectx, sb, val, flags);
911+
arg.AppendSetting(env, sb, val, flags);
912912
}
913913

914914
string str = sb.ToString(ich, sb.Length - ich);
915915
sb.Length = ich;
916916
CmdQuoter.QuoteValue(str, sb, force: true);
917917
}
918918

919-
private IEnumerable<KeyValuePair<string, string>> GetSettingPairsCore(IExceptionContext ectx, ArgumentInfo info, object values, SettingsFlags flags)
919+
private IEnumerable<KeyValuePair<string, string>> GetSettingPairsCore(IHostEnvironment env, ArgumentInfo info, object values, SettingsFlags flags)
920920
{
921921
StringBuilder buffer = new StringBuilder();
922922
foreach (Argument arg in info.Args)
923923
{
924924
string key = arg.GetKey(flags);
925925
object value = arg.GetValue(values);
926-
foreach (string val in arg.GetSettingStrings(ectx, value, buffer))
926+
foreach (string val in arg.GetSettingStrings(env, value, buffer))
927927
yield return new KeyValuePair<string, string>(key, val);
928928
}
929929
}
@@ -943,13 +943,13 @@ public ArgumentHelpStrings(string syntax, string help)
943943
/// <summary>
944944
/// A user friendly usage string describing the command line argument syntax.
945945
/// </summary>
946-
private string GetUsageString(IExceptionContext ectx, ArgumentInfo info, bool showRsp = true, int? columns = null)
946+
private string GetUsageString(IHostEnvironment env, ArgumentInfo info, bool showRsp = true, int? columns = null)
947947
{
948948
int screenWidth = columns ?? Console.BufferWidth;
949949
if (screenWidth <= 0)
950950
screenWidth = 80;
951951

952-
ArgumentHelpStrings[] strings = GetAllHelpStrings(ectx, info, showRsp);
952+
ArgumentHelpStrings[] strings = GetAllHelpStrings(env, info, showRsp);
953953

954954
int maxParamLen = 0;
955955
foreach (ArgumentHelpStrings helpString in strings)
@@ -1039,17 +1039,17 @@ private static void AddNewLine(string newLine, StringBuilder builder, ref int cu
10391039
currentColumn = 0;
10401040
}
10411041

1042-
private static ArgumentHelpStrings[] GetAllHelpStrings(IExceptionContext ectx, ArgumentInfo info, bool showRsp)
1042+
private ArgumentHelpStrings[] GetAllHelpStrings(IHostEnvironment env, ArgumentInfo info, bool showRsp)
10431043
{
10441044
List<ArgumentHelpStrings> strings = new List<ArgumentHelpStrings>();
10451045

10461046
if (info.ArgDef != null)
1047-
strings.Add(GetHelpStrings(ectx, info.ArgDef));
1047+
strings.Add(GetHelpStrings(env, info.ArgDef));
10481048

10491049
foreach (Argument arg in info.Args)
10501050
{
10511051
if (!arg.IsHidden)
1052-
strings.Add(GetHelpStrings(ectx, arg));
1052+
strings.Add(GetHelpStrings(env, arg));
10531053
}
10541054

10551055
if (showRsp)
@@ -1058,9 +1058,9 @@ private static ArgumentHelpStrings[] GetAllHelpStrings(IExceptionContext ectx, A
10581058
return strings.ToArray();
10591059
}
10601060

1061-
private static ArgumentHelpStrings GetHelpStrings(IExceptionContext ectx, Argument arg)
1061+
private ArgumentHelpStrings GetHelpStrings(IHostEnvironment env, Argument arg)
10621062
{
1063-
return new ArgumentHelpStrings(arg.GetSyntaxHelp(), arg.GetFullHelpText(ectx));
1063+
return new ArgumentHelpStrings(arg.GetSyntaxHelp(), arg.GetFullHelpText(env, this));
10641064
}
10651065

10661066
private bool LexFileArguments(string fileName, out string[] arguments)
@@ -1994,7 +1994,7 @@ private bool ParseValue(CmdParser owner, string data, out object value)
19941994
return false;
19951995
}
19961996

1997-
private void AppendHelpValue(IExceptionContext ectx, StringBuilder builder, object value)
1997+
private void AppendHelpValue(IHostEnvironment env, CmdParser owner, StringBuilder builder, object value)
19981998
{
19991999
if (value == null)
20002000
builder.Append("{}");
@@ -2006,19 +2006,19 @@ private void AppendHelpValue(IExceptionContext ectx, StringBuilder builder, obje
20062006
foreach (object o in (System.Array)value)
20072007
{
20082008
builder.Append(pre);
2009-
AppendHelpValue(ectx, builder, o);
2009+
AppendHelpValue(env, owner, builder, o);
20102010
pre = ", ";
20112011
}
20122012
}
20132013
else if (value is IComponentFactory)
20142014
{
20152015
string name;
2016-
var catalog = ModuleCatalog.CreateInstance(ectx);
2016+
var catalog = owner._catalog.Value;
20172017
var type = value.GetType();
20182018
bool success = catalog.TryGetComponentShortName(type, out name);
20192019
Contracts.Assert(success);
20202020

2021-
var settings = GetSettings(ectx, value, Activator.CreateInstance(type));
2021+
var settings = GetSettings(env, value, Activator.CreateInstance(type));
20222022
builder.Append(name);
20232023
if (!string.IsNullOrWhiteSpace(settings))
20242024
{
@@ -2035,21 +2035,21 @@ private void AppendHelpValue(IExceptionContext ectx, StringBuilder builder, obje
20352035
}
20362036

20372037
// If value differs from the default, appends the setting to sb.
2038-
public void AppendSetting(IExceptionContext ectx, StringBuilder sb, object value, SettingsFlags flags)
2038+
public void AppendSetting(IHostEnvironment env, StringBuilder sb, object value, SettingsFlags flags)
20392039
{
20402040
object def = DefaultValue;
20412041
if (!IsCollection)
20422042
{
20432043
if (value == null)
20442044
{
20452045
if (def != null || IsRequired)
2046-
AppendSettingCore(ectx, sb, "", flags);
2046+
AppendSettingCore(env, sb, "", flags);
20472047
}
20482048
else if (def == null || !value.Equals(def))
20492049
{
20502050
var buffer = new StringBuilder();
2051-
if (!(value is IComponentFactory) || (GetString(ectx, value, buffer) != GetString(ectx, def, buffer)))
2052-
AppendSettingCore(ectx, sb, value, flags);
2051+
if (!(value is IComponentFactory) || (GetString(env, value, buffer) != GetString(env, def, buffer)))
2052+
AppendSettingCore(env, sb, value, flags);
20532053
}
20542054
return;
20552055
}
@@ -2076,10 +2076,10 @@ public void AppendSetting(IExceptionContext ectx, StringBuilder sb, object value
20762076
}
20772077

20782078
foreach (object x in vals)
2079-
AppendSettingCore(ectx, sb, x, flags);
2079+
AppendSettingCore(env, sb, x, flags);
20802080
}
20812081

2082-
private void AppendSettingCore(IExceptionContext ectx, StringBuilder sb, object value, SettingsFlags flags)
2082+
private void AppendSettingCore(IHostEnvironment env, StringBuilder sb, object value, SettingsFlags flags)
20832083
{
20842084
if (sb.Length > 0)
20852085
sb.Append(" ");
@@ -2100,11 +2100,11 @@ private void AppendSettingCore(IExceptionContext ectx, StringBuilder sb, object
21002100
else if (value is bool)
21012101
sb.Append((bool)value ? "+" : "-");
21022102
else if (IsCustomItemType)
2103-
AppendCustomItem(ectx, _infoCustom, value, flags, sb);
2103+
AppendCustomItem(env, _infoCustom, value, flags, sb);
21042104
else if (IsComponentFactory)
21052105
{
21062106
var buffer = new StringBuilder();
2107-
sb.Append(GetString(ectx, value, buffer));
2107+
sb.Append(GetString(env, value, buffer));
21082108
}
21092109
else
21102110
sb.Append(value.ToString());
@@ -2129,7 +2129,7 @@ private void ExtractTag(object value, out string tag, out object newValue)
21292129

21302130
// If value differs from the default, return the string representation of 'value',
21312131
// or an IEnumerable of string representations if 'value' is an array.
2132-
public IEnumerable<string> GetSettingStrings(IExceptionContext ectx, object value, StringBuilder buffer)
2132+
public IEnumerable<string> GetSettingStrings(IHostEnvironment env, object value, StringBuilder buffer)
21332133
{
21342134
object def = DefaultValue;
21352135

@@ -2138,12 +2138,12 @@ public IEnumerable<string> GetSettingStrings(IExceptionContext ectx, object valu
21382138
if (value == null)
21392139
{
21402140
if (def != null || IsRequired)
2141-
yield return GetString(ectx, value, buffer);
2141+
yield return GetString(env, value, buffer);
21422142
}
21432143
else if (def == null || !value.Equals(def))
21442144
{
2145-
if (!(value is IComponentFactory) || (GetString(ectx, value, buffer) != GetString(ectx, def, buffer)))
2146-
yield return GetString(ectx, value, buffer);
2145+
if (!(value is IComponentFactory) || (GetString(env, value, buffer) != GetString(env, def, buffer)))
2146+
yield return GetString(env, value, buffer);
21472147
}
21482148
yield break;
21492149
}
@@ -2171,10 +2171,10 @@ public IEnumerable<string> GetSettingStrings(IExceptionContext ectx, object valu
21712171
}
21722172

21732173
foreach (object x in vals)
2174-
yield return GetString(ectx, x, buffer);
2174+
yield return GetString(env, x, buffer);
21752175
}
21762176

2177-
private string GetString(IExceptionContext ectx, object value, StringBuilder buffer)
2177+
private string GetString(IHostEnvironment env, object value, StringBuilder buffer)
21782178
{
21792179
if (value == null)
21802180
return "{}";
@@ -2192,12 +2192,13 @@ private string GetString(IExceptionContext ectx, object value, StringBuilder buf
21922192
if (value is IComponentFactory)
21932193
{
21942194
string name;
2195-
var catalog = ModuleCatalog.CreateInstance(ectx);
2195+
// TODO: ModuleCatalog should be on env
2196+
var catalog = ModuleCatalog.CreateInstance(env);
21962197
var type = value.GetType();
21972198
bool isModuleComponent = catalog.TryGetComponentShortName(type, out name);
21982199
if (isModuleComponent)
21992200
{
2200-
var settings = GetSettings(ectx, value, Activator.CreateInstance(type));
2201+
var settings = GetSettings(env, value, Activator.CreateInstance(type));
22012202
buffer.Clear();
22022203
buffer.Append(name);
22032204
if (!string.IsNullOrWhiteSpace(settings))
@@ -2214,14 +2215,14 @@ private string GetString(IExceptionContext ectx, object value, StringBuilder buf
22142215
}
22152216
else
22162217
{
2217-
throw ectx.Except($"IComponentFactory instances either need to be EntryPointComponents or implement {nameof(ICommandLineComponentFactory)}.");
2218+
throw env.Except($"IComponentFactory instances either need to be EntryPointComponents or implement {nameof(ICommandLineComponentFactory)}.");
22182219
}
22192220
}
22202221

22212222
return value.ToString();
22222223
}
22232224

2224-
public string GetFullHelpText(IExceptionContext ectx)
2225+
public string GetFullHelpText(IHostEnvironment env, CmdParser owner)
22252226
{
22262227
if (IsHidden)
22272228
return null;
@@ -2248,7 +2249,7 @@ public string GetFullHelpText(IExceptionContext ectx)
22482249
if (builder.Length > 0)
22492250
builder.Append(" ");
22502251
builder.Append("Default value:'");
2251-
AppendHelpValue(ectx, builder, DefaultValue);
2252+
AppendHelpValue(env, owner, builder, DefaultValue);
22522253
builder.Append('\'');
22532254
}
22542255
if (Utils.Size(ShortNames) != 0)

0 commit comments

Comments
 (0)