1
-
2
- using System ;
3
- using System . Collections . Generic ;
4
- using System . IO ;
1
+ using System . Collections . Generic ;
5
2
using System . Linq ;
6
- using pyRevitAssemblyBuilder . Config ;
3
+ using pyRevitExtensionParser ;
7
4
using pyRevitAssemblyBuilder . Shared ;
8
5
9
- namespace pyRevitAssemblyBuilder . FolderParser
6
+ namespace pyRevitAssemblyBuilder . SessionManager
10
7
{
11
8
public class ExtensionManagerService
12
9
{
13
- private readonly List < string > _extensionRoots ;
14
-
15
- private static readonly string [ ] BundleTypes = new [ ]
16
- {
17
- ".tab" , ".panel" , ".stack" , ".splitbutton" , ".splitpushbutton" , ".pulldown" , ".smartbutton" , ".pushbutton"
18
- } ;
19
-
20
- public ExtensionManagerService ( )
21
- {
22
- _extensionRoots = GetExtensionRoots ( ) ;
23
- }
24
-
25
10
public IEnumerable < IExtension > GetInstalledExtensions ( )
26
11
{
27
- foreach ( var root in _extensionRoots )
12
+ foreach ( var parsedExtension in ExtensionParser . ParseInstalledExtensions ( ) )
28
13
{
29
- if ( ! Directory . Exists ( root ) )
30
- continue ;
31
-
32
- foreach ( var dir in Directory . GetDirectories ( root ) )
33
- {
34
- if ( ! dir . EndsWith ( ".extension" , StringComparison . OrdinalIgnoreCase ) )
35
- continue ;
36
-
37
- var extensionName = Path . GetFileNameWithoutExtension ( dir ) ;
38
- var metadata = LoadMetadata ( dir ) ;
39
- var children = LoadBundleComponents ( dir ) ;
40
-
41
- if ( children . Any ( ) )
42
- yield return new FileSystemExtension ( extensionName , dir , children , metadata ) ;
43
- }
14
+ yield return new FileSystemExtension (
15
+ name : parsedExtension . Name ,
16
+ path : parsedExtension . Directory ,
17
+ commands : parsedExtension . Children . Select ( ConvertComponent ) . ToList ( ) ,
18
+ metadata : parsedExtension . Metadata
19
+ ) ;
44
20
}
45
21
}
46
22
47
- private List < string > GetExtensionRoots ( )
23
+ private ICommandComponent ConvertComponent ( ParsedComponent parsed )
48
24
{
49
- var roots = new List < string > ( ) ;
50
-
51
- var current = Path . GetDirectoryName ( typeof ( ExtensionManagerService ) . Assembly . Location ) ;
52
- var defaultPath = Path . GetFullPath ( Path . Combine ( current , ".." , ".." , ".." , ".." , "extensions" ) ) ;
53
- roots . Add ( defaultPath ) ;
54
-
55
- var configPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "pyRevit" , "pyRevit_config.ini" ) ;
56
- if ( File . Exists ( configPath ) )
57
- {
58
- var config = PyRevitConfig . Load ( configPath ) ;
59
- if ( config . UserExtensions != null && config . UserExtensions . Count > 0 )
60
- roots . AddRange ( config . UserExtensions ) ;
61
- }
62
-
63
- return roots ;
64
- }
65
-
66
- private IEnumerable < ICommandComponent > LoadBundleComponents ( string baseDir )
67
- {
68
- var components = new List < ICommandComponent > ( ) ;
69
-
70
- foreach ( var dir in Directory . GetDirectories ( baseDir ) )
71
- {
72
- var type = Path . GetExtension ( dir ) . ToLowerInvariant ( ) ;
73
- if ( ! BundleTypes . Contains ( type ) )
74
- continue ;
75
-
76
- components . Add ( ParseComponent ( dir , type ) ) ;
77
- }
78
-
79
- return components ;
80
- }
81
-
82
- private FileCommandComponent ParseComponent ( string dir , string type )
83
- {
84
- var name = Path . GetFileNameWithoutExtension ( dir ) ;
85
- var children = LoadBundleComponents ( dir ) ;
86
- var scriptPath = Path . Combine ( dir , "script.py" ) ;
87
-
88
25
return new FileCommandComponent
89
26
{
90
- Name = name ,
91
- ScriptPath = File . Exists ( scriptPath ) ? scriptPath : null ,
92
- Tooltip = $ "Command: { name } " ,
93
- UniqueId = $ " { Path . GetFileNameWithoutExtension ( dir ) } . { name } " ,
94
- ExtensionName = FindExtensionNameFromPath ( dir ) ,
95
- Type = type ,
96
- Children = children . Cast < object > ( ) . ToList ( )
27
+ Name = parsed . Name ,
28
+ ScriptPath = parsed . ScriptPath ,
29
+ Tooltip = parsed . Tooltip ,
30
+ UniqueId = parsed . UniqueId ,
31
+ ExtensionName = parsed . UniqueId . Split ( '.' ) [ 0 ] ,
32
+ Type = parsed . Type ,
33
+ Children = parsed . Children ? . Select ( ConvertComponent ) . Cast < object > ( ) . ToList ( ) ?? new List < object > ( )
97
34
} ;
98
35
}
99
36
100
- private string FindExtensionNameFromPath ( string path )
101
- {
102
- var segments = path . Split ( Path . DirectorySeparatorChar ) ;
103
- var extDir = segments . FirstOrDefault ( s => s . EndsWith ( ".extension" ) ) ;
104
- return extDir != null ? Path . GetFileNameWithoutExtension ( extDir ) : "UnknownExtension" ;
105
- }
106
-
107
- private ExtensionMetadata LoadMetadata ( string extensionPath )
108
- {
109
- var yamlPath = Path . Combine ( extensionPath , "extension.yaml" ) ;
110
- if ( ! File . Exists ( yamlPath ) )
111
- return null ;
112
-
113
- try
114
- {
115
- var yamlText = File . ReadAllText ( yamlPath ) ;
116
- return ParseYamlToMetadata ( yamlText ) ;
117
- }
118
- catch
119
- {
120
- return null ;
121
- }
122
- }
123
-
124
- private ExtensionMetadata ParseYamlToMetadata ( string yaml )
125
- {
126
- var metadata = new ExtensionMetadata ( ) ;
127
- var lines = yaml . Split ( '\n ' ) ;
128
- foreach ( var line in lines )
129
- {
130
- var trimmed = line . Trim ( ) ;
131
- if ( trimmed . StartsWith ( "author:" ) )
132
- metadata . Author = trimmed . Substring ( "author:" . Length ) . Trim ( ) ;
133
- else if ( trimmed . StartsWith ( "version:" ) )
134
- metadata . Version = trimmed . Substring ( "version:" . Length ) . Trim ( ) ;
135
- else if ( trimmed . StartsWith ( "description:" ) )
136
- metadata . Description = trimmed . Substring ( "description:" . Length ) . Trim ( ) ;
137
- }
138
- return metadata ;
139
- }
140
-
141
37
private class FileSystemExtension : IExtension
142
38
{
143
39
private readonly IEnumerable < ICommandComponent > _commands ;
144
40
145
- public FileSystemExtension ( string name , string path , IEnumerable < ICommandComponent > commands , ExtensionMetadata metadata )
41
+ public FileSystemExtension ( string name , string path , IEnumerable < ICommandComponent > commands , ParsedExtensionMetadata metadata )
146
42
{
147
43
Name = name ;
148
44
Directory = path ;
@@ -152,7 +48,7 @@ public FileSystemExtension(string name, string path, IEnumerable<ICommandCompone
152
48
153
49
public string Name { get ; }
154
50
public string Directory { get ; }
155
- public ExtensionMetadata Metadata { get ; }
51
+ public ParsedExtensionMetadata Metadata { get ; }
156
52
157
53
public string GetHash ( ) => Directory . GetHashCode ( ) . ToString ( "X" ) ;
158
54
@@ -174,12 +70,5 @@ private class FileCommandComponent : ICommandComponent
174
70
public string Type { get ; set ; }
175
71
public IEnumerable < object > Children { get ; set ; } = Enumerable . Empty < object > ( ) ;
176
72
}
177
-
178
- public class ExtensionMetadata
179
- {
180
- public string Author { get ; set ; }
181
- public string Version { get ; set ; }
182
- public string Description { get ; set ; }
183
- }
184
73
}
185
74
}
0 commit comments