Datei: NDOPackage/Commands/AddPersistentClassCs.cs
Last Commit (ed9120d)
1 | // |
2 | // Copyright (c) 2002-2019 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Text; |
25 | using System.IO; |
26 | using System.Windows.Forms; |
27 | using EnvDTE; |
28 | using Project = Community.VisualStudio.Toolkit.Project; |
29 | |
30 | namespace NDOVsPackage. Commands |
31 | { |
32 | ····/// <summary> |
33 | ····/// Zusammenfassung für AddPersistentClassCs. |
34 | ····/// </summary> |
35 | ····internal class AddPersistentClassCs |
36 | ····{ |
37 | ········Project project; |
38 | ········string className; |
39 | ········bool isSerializable; |
40 | ········ProjectItem parentItem; |
41 | |
42 | public AddPersistentClassCs( Project project, string className, bool isSerializable, ProjectItem parentItem) |
43 | ········{ |
44 | ············this.project = project; |
45 | ············this.className = className; |
46 | ············this.isSerializable = isSerializable; |
47 | ············this.parentItem = parentItem; |
48 | ········} |
49 | |
50 | ········public void DoIt() |
51 | ········{ |
52 | ············try |
53 | ············{ |
54 | ················StreamWriter sw; |
55 | string fileName = Path. GetDirectoryName( project. FullPath) + "\\" + className + ". cs"; |
56 | ················string partialFileName = fileName.Substring( 0, fileName.Length - 3 ); |
57 | ················partialFileName += ".ndo.cs"; |
58 | var dteProject = project. DteProject( ) ; |
59 | ················string namespc = (string) dteProject.Properties.Item( "RootNamespace" ).Value; |
60 | ················using (sw = new StreamWriter( fileName, false, System.Text.Encoding.UTF8 )) |
61 | ················{ |
62 | ····················StringBuilder sb = new StringBuilder(); |
63 | |
64 | ····················sb.Append( "using System;\n" ); |
65 | ····················sb.Append( "using System.Linq;\n" ); |
66 | ····················sb.Append( "using System.Collections.Generic;\n" ); |
67 | ····················sb.Append( "using NDO;\n\n" );···················· |
68 | ····················sb.Append( "namespace " + namespc + "\n" ); |
69 | ····················sb.Append( "{\n" ); |
70 | |
71 | ····················sb.Append( "\t/// <summary>\n" ); |
72 | ····················sb.Append( "\t/// Summary for " + className + "\n" ); |
73 | ····················sb.Append( "\t/// </summary>\n" ); |
74 | ····················sb.Append( "\t[NDOPersistent" ); |
75 | ····················if (this.isSerializable) |
76 | ························sb.Append( ", Serializable" ); |
77 | ····················sb.Append( "]\n" ); |
78 | ····················sb.Append( "\tpublic partial class " + className + "\n" ); |
79 | ····················sb.Append( "\t{\n" ); |
80 | ····················sb.Append( "\t\tpublic " + className + "()\n" ); |
81 | ····················sb.Append( "\t\t{\n" ); |
82 | ····················sb.Append( "\t\t}\n" ); |
83 | ····················sb.Append( "\t}\n" ); |
84 | ····················sb.Append( "}\n" ); |
85 | ····················string result = sb.ToString(); |
86 | ····················TabProperty tp = TabProperties.Instance.CSharp; |
87 | ····················if (tp.UseSpaces) |
88 | ························sw.Write( result.Replace( "\t", tp.Indent ) ); |
89 | ····················else |
90 | ························sw.Write( result ); |
91 | ················} |
92 | project. AddExistingFilesAsync( new[] { fileName } ) . Wait( ) ; |
93 | |
94 | ················using (sw = new StreamWriter( partialFileName )) |
95 | ················{ |
96 | ····················string newPartial = partialTemplate.Replace( "#ns#", namespc ); |
97 | ····················newPartial = newPartial.Replace( "#cl#", className ); |
98 | ····················sw.Write( newPartial ); |
99 | ················} |
100 | |
101 | project. AddExistingFilesAsync( partialFileName ) ; |
102 | CodeGenHelper. ActivateTextDocumentAsync( project, fileName) ; |
103 | ············} |
104 | catch( Exception ex) |
105 | ············{ |
106 | MessageBox. Show( ex. Message) ; |
107 | ············} |
108 | ········} |
109 | |
110 | ········readonly string partialTemplate = @"using System; |
111 | using NDO; |
112 | |
113 | namespace #ns# |
114 | { |
115 | ····// Don't change this code. |
116 | ····// This interface implementation exists only for intellisense support. |
117 | ····// Any code in this file will be replaced by the enhancer. |
118 | ····public partial class #cl# : IPersistentObject |
119 | ····{ |
120 | ········#region IPersistentObject Members |
121 | |
122 | ········public void NDOMarkDirty() |
123 | ········{ |
124 | ············throw new NotImplementedException(); |
125 | ········} |
126 | |
127 | ········public ObjectId NDOObjectId |
128 | ········{ |
129 | ············get |
130 | ············{ |
131 | ················throw new NotImplementedException(); |
132 | ············} |
133 | ············set |
134 | ············{ |
135 | ················throw new NotImplementedException(); |
136 | ············} |
137 | ········} |
138 | |
139 | ········public NDOObjectState NDOObjectState |
140 | ········{ |
141 | ············get |
142 | ············{ |
143 | ················throw new NotImplementedException(); |
144 | ············} |
145 | ············set |
146 | ············{ |
147 | ················throw new NotImplementedException(); |
148 | ············} |
149 | ········} |
150 | |
151 | ········public Guid NDOTimeStamp |
152 | ········{ |
153 | ············get |
154 | ············{ |
155 | ················throw new NotImplementedException(); |
156 | ············} |
157 | ············set |
158 | ············{ |
159 | ················throw new NotImplementedException(); |
160 | ············} |
161 | ········} |
162 | |
163 | ········#endregion |
164 | ····} |
165 | } |
166 | "; |
167 | ····} |
168 | } |
169 | lementedException(); |
170 | ············} |
171 | ········} |
172 | |
173 | ········#endregion |
174 | ····} |
175 | } |
176 | "; |
177 | ····} |
178 | } |
179 |
New Commit (946ad0e)
1 | // |
2 | // Copyright (c) 2002-2019 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Text; |
25 | using System.IO; |
26 | using System.Windows.Forms; |
27 | using EnvDTE; |
28 | |
29 | namespace NETDataObjects. NDOVSPackage |
30 | { |
31 | ····/// <summary> |
32 | ····/// Zusammenfassung für AddPersistentClassCs. |
33 | ····/// </summary> |
34 | ····internal class AddPersistentClassCs |
35 | ····{ |
36 | ········Project project; |
37 | ········string className; |
38 | ········bool isSerializable; |
39 | ········ProjectItem parentItem; |
40 | |
41 | public AddPersistentClassCs( Project project, string className, bool isSerializable, ProjectItem parentItem ) |
42 | ········{ |
43 | ············this.project = project; |
44 | ············this.className = className; |
45 | ············this.isSerializable = isSerializable; |
46 | ············this.parentItem = parentItem; |
47 | ········} |
48 | |
49 | ········public void DoIt() |
50 | ········{ |
51 | ············try |
52 | ············{ |
53 | ················StreamWriter sw; |
54 | string fileName = Path. GetDirectoryName( project. FileName ) + "\\" + className + ". cs"; |
55 | ················string partialFileName = fileName.Substring( 0, fileName.Length - 3 ); |
56 | ················partialFileName += ".ndo.cs"; |
57 | string namespc = ( string) project. Properties. Item( "RootNamespace" ) . Value; |
58 | ················using (sw = new StreamWriter( fileName, false, System.Text.Encoding.UTF8 )) |
59 | ················{ |
60 | ····················StringBuilder sb = new StringBuilder(); |
61 | |
62 | ····················sb.Append( "using System;\n" ); |
63 | ····················sb.Append( "using System.Linq;\n" ); |
64 | ····················sb.Append( "using System.Collections.Generic;\n" ); |
65 | ····················sb.Append( "using NDO;\n\n" ); |
66 | ····················sb.Append( "namespace " + namespc + "\n" ); |
67 | ····················sb.Append( "{\n" ); |
68 | |
69 | ····················sb.Append( "\t/// <summary>\n" ); |
70 | ····················sb.Append( "\t/// Summary for " + className + "\n" ); |
71 | ····················sb.Append( "\t/// </summary>\n" ); |
72 | ····················sb.Append( "\t[NDOPersistent" ); |
73 | ····················if (this.isSerializable) |
74 | ························sb.Append( ", Serializable" ); |
75 | ····················sb.Append( "]\n" ); |
76 | ····················sb.Append( "\tpublic partial class " + className + "\n" ); |
77 | ····················sb.Append( "\t{\n" ); |
78 | ····················sb.Append( "\t\tpublic " + className + "()\n" ); |
79 | ····················sb.Append( "\t\t{\n" ); |
80 | ····················sb.Append( "\t\t}\n" ); |
81 | ····················sb.Append( "\t}\n" ); |
82 | ····················sb.Append( "}\n" ); |
83 | ····················string result = sb.ToString(); |
84 | ····················TabProperty tp = TabProperties.Instance.CSharp; |
85 | ····················if (tp.UseSpaces) |
86 | ························sw.Write( result.Replace( "\t", tp.Indent ) ); |
87 | ····················else |
88 | ························sw.Write( result ); |
89 | ················} |
90 | ProjectItem pi = null; |
91 | ················if (parentItem == null) |
92 | ····················pi = project.ProjectItems.AddFromFile( fileName ); |
93 | ················else |
94 | ····················pi = parentItem.ProjectItems.AddFromFile( fileName ); |
95 | |
96 | ················using (sw = new StreamWriter( partialFileName )) |
97 | ················{ |
98 | ····················string newPartial = partialTemplate.Replace( "#ns#", namespc ); |
99 | ····················newPartial = newPartial.Replace( "#cl#", className ); |
100 | ····················sw.Write( newPartial ); |
101 | ················} |
102 | |
103 | pi. ProjectItems. AddFromFile( partialFileName ) ; |
104 | CodeGenHelper. ActivateAndGetTextDocument( project, Path. GetFileName( fileName ) ) ; |
105 | ············} |
106 | catch ( Exception ex) |
107 | ············{ |
108 | MessageBox. Show( ex. Message ) ; |
109 | ············} |
110 | ········} |
111 | |
112 | ········readonly string partialTemplate = @"using System; |
113 | using NDO; |
114 | |
115 | namespace #ns# |
116 | { |
117 | ····// Don't change this code. |
118 | ····// This interface implementation exists only for intellisense support. |
119 | ····// Any code in this file will be replaced by the enhancer. |
120 | ····public partial class #cl# : IPersistentObject |
121 | ····{ |
122 | ········#region IPersistentObject Members |
123 | |
124 | ········public void NDOMarkDirty() |
125 | ········{ |
126 | ············throw new NotImplementedException(); |
127 | ········} |
128 | |
129 | ········public ObjectId NDOObjectId |
130 | ········{ |
131 | ············get |
132 | ············{ |
133 | ················throw new NotImplementedException(); |
134 | ············} |
135 | ············set |
136 | ············{ |
137 | ················throw new NotImplementedException(); |
138 | ············} |
139 | ········} |
140 | |
141 | ········public NDOObjectState NDOObjectState |
142 | ········{ |
143 | ············get |
144 | ············{ |
145 | ················throw new NotImplementedException(); |
146 | ············} |
147 | ············set |
148 | ············{ |
149 | ················throw new NotImplementedException(); |
150 | ············} |
151 | ········} |
152 | |
153 | ········public Guid NDOTimeStamp |
154 | ········{ |
155 | ············get |
156 | ············{ |
157 | ················throw new NotImplementedException(); |
158 | ············} |
159 | ············set |
160 | ············{ |
161 | ················throw new NotImplementedException(); |
162 | ············} |
163 | ········} |
164 | |
165 | ········#endregion |
166 | ····} |
167 | } |
168 | "; |
169 | ····} |
170 | } |
171 |