Datei: NDOPackage/Commands/AddPersistentClassCs.cs
Last Commit (3030986)
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 = EnvDTE. 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. FileName) + "\\" + className + ". cs"; |
56 | ················string partialFileName = fileName.Substring( 0, fileName.Length - 3 ); |
57 | ················partialFileName += ".ndo.cs"; |
58 | string namespc = ( string) project. Properties. Item( "RootNamespace" ) . Value; |
59 | ················using (sw = new StreamWriter( fileName, false, System.Text.Encoding.UTF8 )) |
60 | ················{ |
61 | ····················StringBuilder sb = new StringBuilder(); |
62 | |
63 | ····················sb.Append( "using System;\n" ); |
64 | ····················sb.Append( "using System.Linq;\n" ); |
65 | ····················sb.Append( "using System.Collections.Generic;\n" ); |
66 | ····················sb.Append( "using NDO;\n\n" );···················· |
67 | ····················sb.Append( "namespace " + namespc + "\n" ); |
68 | ····················sb.Append( "{\n" ); |
69 | |
70 | ····················sb.Append( "\t/// <summary>\n" ); |
71 | ····················sb.Append( "\t/// Summary for " + className + "\n" ); |
72 | ····················sb.Append( "\t/// </summary>\n" ); |
73 | ····················sb.Append( "\t[NDOPersistent" ); |
74 | ····················if (this.isSerializable) |
75 | ························sb.Append( ", Serializable" ); |
76 | ····················sb.Append( "]\n" ); |
77 | ····················sb.Append( "\tpublic partial class " + className + "\n" ); |
78 | ····················sb.Append( "\t{\n" ); |
79 | ····················sb.Append( "\t\tpublic " + className + "()\n" ); |
80 | ····················sb.Append( "\t\t{\n" ); |
81 | ····················sb.Append( "\t\t}\n" ); |
82 | ····················sb.Append( "\t}\n" ); |
83 | ····················sb.Append( "}\n" ); |
84 | ····················string result = sb.ToString(); |
85 | ····················TabProperty tp = TabProperties.Instance.CSharp; |
86 | ····················if (tp.UseSpaces) |
87 | ························sw.Write( result.Replace( "\t", tp.Indent ) ); |
88 | ····················else |
89 | ························sw.Write( result ); |
90 | ················} |
91 | ProjectItem pi = null; |
92 | ················if ( parentItem == null ) |
93 | ····················pi = project.ProjectItems.AddFromFile( fileName ); |
94 | ················else |
95 | ····················pi = parentItem.ProjectItems.AddFromFile( fileName ); |
96 | |
97 | ················using (sw = new StreamWriter( partialFileName )) |
98 | ················{ |
99 | ····················string newPartial = partialTemplate.Replace( "#ns#", namespc ); |
100 | ····················newPartial = newPartial.Replace( "#cl#", className ); |
101 | ····················sw.Write( newPartial ); |
102 | ················} |
103 | |
104 | pi. ProjectItems. AddFromFile( partialFileName ) ; |
105 | CodeGenHelper. ActivateAndGetTextDocument( project, Path. GetFileName( fileName) ) ; |
106 | ············} |
107 | ············catch(Exception ex) |
108 | ············{ |
109 | ················MessageBox.Show(ex.Message); |
110 | ············} |
111 | ········} |
112 | |
113 | ········readonly string partialTemplate = @"using System; |
114 | using NDO; |
115 | |
116 | namespace #ns# |
117 | { |
118 | ····// Don't change this code. |
119 | ····// This interface implementation exists only for intellisense support. |
120 | ····// Any code in this file will be replaced by the enhancer. |
121 | ····public partial class #cl# : IPersistentObject |
122 | ····{ |
123 | ········#region IPersistentObject Members |
124 | |
125 | ········public void NDOMarkDirty() |
126 | ········{ |
127 | ············throw new NotImplementedException(); |
128 | ········} |
129 | |
130 | ········public ObjectId NDOObjectId |
131 | ········{ |
132 | ············get |
133 | ············{ |
134 | ················throw new NotImplementedException(); |
135 | ············} |
136 | ············set |
137 | ············{ |
138 | ················throw new NotImplementedException(); |
139 | ············} |
140 | ········} |
141 | |
142 | ········public NDOObjectState NDOObjectState |
143 | ········{ |
144 | ············get |
145 | ············{ |
146 | ················throw new NotImplementedException(); |
147 | ············} |
148 | ············set |
149 | ············{ |
150 | ················throw new NotImplementedException(); |
151 | ············} |
152 | ········} |
153 | |
154 | ········public Guid NDOTimeStamp |
155 | ········{ |
156 | ············get |
157 | ············{ |
158 | ················throw new NotImplementedException(); |
159 | ············} |
160 | ············set |
161 | ············{ |
162 | ················throw new NotImplementedException(); |
163 | ············} |
164 | ········} |
165 | |
166 | ········#endregion |
167 | ····} |
168 | } |
169 | "; |
170 | ····} |
171 | } |
172 |
New 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 |