-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathFunctionDescriptor.cs
More file actions
616 lines (532 loc) · 21.7 KB
/
FunctionDescriptor.cs
File metadata and controls
616 lines (532 loc) · 21.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Dynamo.Configuration;
using Dynamo.Graph.Nodes;
using Dynamo.Interfaces;
using Dynamo.Library;
using ProtoCore;
using ProtoCore.DSASM;
using ProtoCore.Utils;
namespace Dynamo.Engine
{
/// <summary>
/// Describes a function, whether imported or defined in a custom node.
/// </summary>
public interface IFunctionDescriptor
{
/// <summary>
/// Name to be displayed for the function.
/// </summary>
string DisplayName { get; }
/// <summary>
/// An unique name to identify a function. It is used to create
/// a corresponding node instance
/// </summary>
string MangledName { get; }
/// <summary>
/// Return keys for multi-output functions.
/// </summary>
IEnumerable<string> ReturnKeys { get; }
/// <summary>
/// Function parameters
/// </summary>
IEnumerable<TypedParameter> Parameters { get; }
/// <summary>
/// Function name.
/// </summary>
string FunctionName { get; }
/// <summary>
/// Return Type
/// </summary>
ProtoCore.Type ReturnType { get; }
}
/// <summary>
/// Contains parameters for function description.
/// </summary>
public class FunctionDescriptorParams
{
/// <summary>
/// Initializes a new instance of the <see cref="FunctionDescriptorParams"/> class.
/// </summary>
public FunctionDescriptorParams()
{
IsVisibleInLibrary = true;
Parameters = new List<TypedParameter>();
ReturnKeys = new List<string>();
ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Var);
}
/// <summary>
/// Returns full path to the assembly the defined this function.
/// This is not always an assembly path, it might be a path to a .ds file
/// or builtin token like 'operators'.
/// </summary>
public string Assembly { get; set; }
/// <summary>
/// Returns class name of this function. If the functinon is global, return String.Empty.
/// </summary>
public string ClassName { get; set; }
/// <summary>
/// Returns function name.
/// </summary>
public string FunctionName { get; set; }
/// <summary>
/// Returns comment describing the function along with the signature
/// </summary>
public string Summary { get; set; }
/// <summary>
/// Message specified if function is obsolete
/// </summary>
public string ObsoleteMsg { get; set; }
/// <summary>
/// Returns function parameters data
/// </summary>
public IEnumerable<TypedParameter> Parameters { get; set; }
/// <summary>
/// Describes the type of object to return by the function
/// </summary>
public ProtoCore.Type ReturnType { get; set; }
/// <summary>
/// Describes type of function
/// </summary>
public FunctionType FunctionType { get; set; }
/// <summary>
/// This attribute sets, if this function is shown in library or not.
/// </summary>
public bool IsVisibleInLibrary { get; set; }
/// <summary>
/// This attribute sets whether the function enables periodic update of the workspace.
/// </summary>
public bool CanUpdatePeriodically { get; set; }
/// <summary>
/// If the function returns a dictionary, ReturnKeys is the key collection
/// used in returned dictionary.
/// </summary>
public IEnumerable<string> ReturnKeys { get; set; }
/// <summary>
/// Returns instance of IPathManager
/// </summary>
public IPathManager PathManager { get; set; }
/// <summary>
/// Does the function accept a variable number of arguments?
/// </summary>
public bool IsVarArg { get; set; }
/// <summary>
/// Indicates if it is built-in function
/// </summary>
public bool IsBuiltIn { get; set; }
/// <summary>
/// Indicates if the function is packaged element (either zero-touch DLLs or DYFs)
/// </summary>
public bool IsPackageMember { get; set; }
/// <summary>
/// Indicates if the lacing strategy is disabled on the function
/// </summary>
public bool IsLacingDisabled { get; set; }
//TODO - should this somehow contain more info - ExperimentalInfo{IsExperimental, ExperimentalMessage/url}?}
/// <summary>
/// Experimental/Unstable function
/// </summary>
internal bool IsExperimental { get; set; }
}
/// <summary>
/// Describe a DesignScript function in a imported library
/// </summary>
public class FunctionDescriptor : IFunctionDescriptor
{
/// <summary>
/// A comment describing the Function
/// </summary>
private string summary;
private readonly IPathManager pathManager;
/// <summary>
/// Initializes a new instance of the <see cref="FunctionDescriptor"/> class.
/// </summary>
/// <param name="funcDescParams">Function descriptor parameters.</param>
public FunctionDescriptor(FunctionDescriptorParams funcDescParams)
{
if (!String.IsNullOrEmpty(funcDescParams.Summary))
{
summary = funcDescParams.Summary;
}
pathManager = funcDescParams.PathManager;
Assembly = funcDescParams.Assembly;
ClassName = funcDescParams.ClassName;
FunctionName = funcDescParams.FunctionName;
Parameters = funcDescParams.Parameters.Select(
x =>
{
x.UpdateFunctionDescriptor(this);
return x;
}).ToList();
var type = funcDescParams.FunctionType;
var inputParameters = new List<Tuple<string, string>>();
//Add instance parameter as one of the inputs for instance method as well as properties.
if (type == FunctionType.InstanceMethod || type == FunctionType.InstanceProperty)
inputParameters.Add(Tuple.Create(UnqualifedClassName.ToLower(), UnqualifedClassName));
if (Parameters.Any())
{
inputParameters.AddRange(Parameters.Select(
par => Tuple.Create(par.Name, par.DisplayTypeName)));
}
InputParameters = inputParameters;
ReturnType = funcDescParams.ReturnType;
Type = type;
ReturnKeys = funcDescParams.ReturnKeys;
IsVarArg = funcDescParams.IsVarArg;
IsVisibleInLibrary = funcDescParams.IsVisibleInLibrary;
ObsoleteMessage = funcDescParams.ObsoleteMsg;
CanUpdatePeriodically = funcDescParams.CanUpdatePeriodically;
IsBuiltIn = funcDescParams.IsBuiltIn;
IsPackageMember = funcDescParams.IsPackageMember;
IsLacingDisabled = funcDescParams.IsLacingDisabled;
IsExperimental = funcDescParams.IsExperimental || CheckIfFunctionIsMarkedExperimentalByPrefs(this);
}
/// <summary>
/// Indicates if the function overloads
/// </summary>
public bool IsOverloaded { get; set; }
/// <summary>
/// Returns full path to the assembly the defined this function.
/// This is not always an assembly path, it might be a path to a .ds file
/// or builtin token like 'operators'.
/// </summary>
public string Assembly { get; private set; }
/// <summary>
/// Class name of this function. If the function is global, return String.Empty.
/// </summary>
public string ClassName { get; private set; }
/// <summary>
/// Function name.
/// </summary>
public string FunctionName { get; private set; }
/// <summary>
/// Function parameters.
/// </summary>
public IEnumerable<TypedParameter> Parameters { get; private set; }
/// <summary>
/// Function return type.
/// </summary>
public ProtoCore.Type ReturnType { get; private set; }
/// <summary>
/// If the function returns a dictionary, ReturnKeys is the key collection
/// used in returned dictionary.
/// </summary>
public IEnumerable<string> ReturnKeys { get; private set; }
/// <summary>
/// Does the function accept a variable number of arguments?
/// </summary>
public bool IsVarArg { get; private set; }
/// <summary>
/// Indicates if it is a built-in function
/// </summary>
public bool IsBuiltIn { get; private set; }
/// <summary>
/// Indicates if the function is packaged element (either zero-touch DLLs or DYFs)
/// </summary>
public bool IsPackageMember { get; private set; }
/// <summary>
/// Message specified if function is obsolete
/// </summary>
public string ObsoleteMessage { get; protected set; }
/// <summary>
/// Indicates if the function is obsolete
/// </summary>
public bool IsObsolete { get { return !string.IsNullOrEmpty(ObsoleteMessage); } }
/// <summary>
/// Function type.
/// </summary>
public FunctionType Type { get; private set; }
/// <summary>
/// Returns summary of the function from its documentation xml
/// using the corresponding FunctionDescriptor object.
/// </summary>
public string Summary
{
get { return summary ?? (summary = this.GetSummary()); }
}
/// <summary>
/// A comment describing the function along with the signature
/// </summary>
public string Description
{
get { return !String.IsNullOrEmpty(Summary) ? Summary : string.Empty; }
}
private IEnumerable<Tuple<string, string>> returns;
/// <summary>
/// If the XML documentation for the function includes a returns field,
/// this parameter contains a collection of tuples of output names to
/// descriptions.
///
/// Otherwise, this list will be empty.
/// </summary>
public IEnumerable<Tuple<string, string>> Returns { get { return returns ?? (returns = this.GetReturns()); } }
/// <summary>
/// Inputs for Node
/// </summary>
public IEnumerable<Tuple<string, string>> InputParameters
{
get;
private set;
}
private string category;
/// <summary>
/// The category of this function.
/// </summary>
public string Category
{
get
{
if (category != null)
{
return category;
}
var categoryBuf = new StringBuilder();
categoryBuf.Append(GetRootCategory());
//if this is not BuiltIn function and not a function defined in a .ds file
//search the containing assembly for the NodeCategoryAttribute.
if (ClassName != null &&
Assembly!=null && !Assembly.ToLowerInvariant().EndsWith(".ds"))
{
try
{
#if DEBUG
var LoadedAssemblyCount = AppDomain.CurrentDomain.GetAssemblies().Length;
#endif
var asm = Dynamo.Utilities.AssemblyHelper.LoadInALCFrom(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), Assembly));
#if DEBUG
Debug.Assert(AppDomain.CurrentDomain.GetAssemblies().Length == LoadedAssemblyCount,
"Assembly count should not have changed, because should not have actually performed a load.");
#endif
if (asm != null)
{
//get class type of function
var type = asm.GetType(ClassName);
if (type != null)
{
//get NodeCategoryAttribute for this function if it was defined
var nodeCat = type.GetMethods().Where(x => x.Name == FunctionName)
.Select(x => x.GetCustomAttribute(typeof(NodeCategoryAttribute)))
.Where(x => x != null)
.Cast<NodeCategoryAttribute>()
.Select(x => x.ElementCategory)
.FirstOrDefault();
//if attribute is found compose node category string with last part from attribute
if (!string.IsNullOrEmpty(nodeCat) && (
nodeCat == LibraryServices.Categories.Constructors
|| nodeCat == LibraryServices.Categories.Properties
|| nodeCat == LibraryServices.Categories.MemberFunctions))
{
categoryBuf.Append("." + UnqualifedClassName + "." + nodeCat);
category = categoryBuf.ToString();
return category;
}
}
}
}
catch (Exception e)
{
//TODO ideally this would surface to dynamo logger somehow.
Console.WriteLine($"Error while generating function descriptor category:{Assembly} {ClassName} {FunctionName} {e}");
}
}
switch (Type)
{
case FunctionType.Constructor:
categoryBuf.Append(
"." + UnqualifedClassName + "." + LibraryServices.Categories.Constructors);
break;
case FunctionType.StaticMethod:
case FunctionType.InstanceMethod:
categoryBuf.Append(
"." + UnqualifedClassName + "." + LibraryServices.Categories.MemberFunctions);
break;
case FunctionType.StaticProperty:
case FunctionType.InstanceProperty:
categoryBuf.Append(
"." + UnqualifedClassName + "." + LibraryServices.Categories.Properties);
break;
}
return categoryBuf.ToString();
}
}
/// <summary>
/// The string that is used to search for this function.
/// </summary>
public string QualifiedName
{
get
{
return FunctionType.GenericFunction == Type
? UserFriendlyName
: ClassName + "." + UserFriendlyName;
}
}
/// <summary>
/// A unique name to identify a function. It is necessary when a
/// function is overloaded.
/// </summary>
public string MangledName
{
get
{
return Parameters != null && Parameters.Any()
? QualifiedName + "@" + string.Join(",", Parameters.Select(p => p.Type))
: QualifiedName;
}
}
/// <summary>
/// The full signature of the function.
/// </summary>
public string Signature
{
get
{
var descBuf = new StringBuilder();
descBuf.Append(DisplayName);
if (Parameters != null && Parameters.Any())
{
string signature = string.Join(", ", Parameters.Select(p => p.ToString()));
descBuf.Append(" (");
descBuf.Append(signature);
descBuf.Append(")");
}
else if (FunctionType.InstanceProperty != Type && FunctionType.StaticProperty != Type)
descBuf.Append(" ( )");
var typeName = ReturnType.ToShortString();
if (!string.IsNullOrEmpty(typeName))
descBuf.Append(": " + typeName);
return descBuf.ToString();
}
}
/// <summary>
/// Return a user friendly name. E.g., for operator '+' it will return
/// 'Add'
/// </summary>
public string UserFriendlyName
{
get
{
if (FunctionName.StartsWith(Constants.kInternalNamePrefix))
{
string name = FunctionName.Substring(Constants.kInternalNamePrefix.Length);
Operator op;
if (Enum.TryParse(name, out op))
name = Op.GetOpSymbol(op);
return name;
}
return FunctionName;
}
}
/// <summary>
/// QualifiedName with leading namespaces removed.
/// </summary>
public string DisplayName
{
get
{
if (FunctionType.GenericFunction == Type)
return UserFriendlyName;
int idx = ClassName.LastIndexOf('.');
return idx < 0
? QualifiedName
: string.Format("{0}.{1}", ClassName.Substring(idx + 1), UserFriendlyName);
}
}
/// <summary>
/// This attribute sets, if this function is shown in library or not.
/// </summary>
public bool IsVisibleInLibrary { get; private set; }
/// <summary>
/// This attribute sets whether the function enables periodic update of the workspace.
/// </summary>
public bool CanUpdatePeriodically { get; private set; }
/// <summary>
/// Returns class name without namespace
/// </summary>
public string UnqualifedClassName
{
get
{
if (string.IsNullOrEmpty(ClassName))
return string.Empty;
int idx = ClassName.LastIndexOf('.');
return idx < 0 ? ClassName : ClassName.Substring(idx + 1);
}
}
/// <summary>
/// Returns namespace where the function is specified
/// </summary>
public string Namespace
{
get
{
if (string.IsNullOrEmpty(ClassName))
return string.Empty;
int idx = ClassName.LastIndexOf('.');
return idx < 0 ? String.Empty : ClassName.Substring(0, idx);
}
}
/// <summary>
/// Returns instance of IPathManager
/// </summary>
public IPathManager PathManager { get { return pathManager; } }
/// <summary>
/// Returns if the lacing strategy is disabled
/// </summary>
public bool IsLacingDisabled { get; private set; }
/// <summary>
/// Overrides equality check of two <see cref="FunctionDescriptor"/> objects
/// </summary>
/// <param name="obj"><see cref="FunctionDescriptor"/> object to compare
/// with the current one</param>
/// <returns>Returns true if two <see cref="FunctionDescriptor"/> objects
/// are equals</returns>
public override bool Equals(object obj)
{
if (null == obj || GetType() != obj.GetType())
return false;
return MangledName.Equals(obj as FunctionDescriptor);
}
/// <summary>
/// Overrides computing the hash code for the <see cref="FunctionDescriptor"/>
/// </summary>
/// <returns>The hash code for this <see cref="FunctionDescriptor"/></returns>
public override int GetHashCode()
{
return MangledName.GetHashCode();
}
private string GetRootCategory()
{
if (string.IsNullOrEmpty(Assembly))
{
return CoreUtils.IsInternalMethod(FunctionName)
? LibraryServices.Categories.Operators
: LibraryServices.Categories.BuiltIn;
}
LibraryCustomization cust = LibraryCustomizationServices.GetForAssembly(Assembly, pathManager);
if (cust != null)
{
string f = cust.GetNamespaceCategory(Namespace);
if (!String.IsNullOrEmpty(f))
return f;
}
string filename = Path.GetFileNameWithoutExtension(Assembly);
return string.IsNullOrEmpty(Namespace) ? filename : filename + "." + Namespace;
}
private bool CheckIfFunctionIsMarkedExperimentalByPrefs(FunctionDescriptor fd)
{
if (PreferenceSettings.InitialExperimentalLib_Namespaces.
Where(x => x.StartsWith(fd.Assembly + ":")).Select(x => x.Split(":").LastOrDefault()).Any(nsp => fd.QualifiedName.StartsWith(nsp)))
{
return true;
}
return false;
}
internal bool IsExperimental { get; }
}
}