Datei: NDOPackage/Commands/AddPersistentClassCs.cs

Last 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
New Commit (33e9857)
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 MessageBox = System. Windows. Forms. MessageBox;
27 using EnvDTE;
28
29 #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
30
31 namespace NDOVsPackage. Commands
32 {
33 ····/// <summary>
34 ····/// Zusammenfassung für AddPersistentClassCs.
35 ····/// </summary>
36 ····internal class AddPersistentClassCs
37 ····{
38 EnvDTE. Project project;
39 ········string className;
40 ········bool isSerializable;
41 ········ProjectItem parentItem;
42
43 public AddPersistentClassCs( EnvDTE. Project project, string className, bool isSerializable, ProjectItem parentItem )
44 ········{
45 ············this.project = project;
46 ············this.className = className;
47 ············this.isSerializable = isSerializable;
48 ············this.parentItem = parentItem;
49 ········}
50
51 ········public void DoIt()
52 ········{
53 ············try
54 ············{
55 ················StreamWriter sw;
56 ················string fileName = Path.GetDirectoryName( project.FileName ) + "\\" + className + ".cs";
57 ················string partialFileName = fileName.Substring( 0, fileName.Length - 3 );
58 ················partialFileName += ".ndo.cs";
59 ················string namespc = (string) project.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 ················ProjectItem pi = null;
93 ················if (parentItem == null)
94 ····················pi = project.ProjectItems.AddFromFile( fileName );
95 ················else
96 ····················pi = parentItem.ProjectItems.AddFromFile( fileName );
97
98 ················using (sw = new StreamWriter( partialFileName ))
99 ················{
100 ····················string newPartial = partialTemplate.Replace( "#ns#", namespc );
101 ····················newPartial = newPartial.Replace( "#cl#", className );
102 ····················sw.Write( newPartial );
103 ················}
104
105 ················pi.ProjectItems.AddFromFile( partialFileName );
106 ················CodeGenHelper.ActivateAndGetTextDocument( project, Path.GetFileName( fileName ) );
107 ············}
108 ············catch (Exception ex)
109 ············{
110 ················MessageBox.Show( ex.Message );
111 ············}
112 ········}
113
114 ········readonly string partialTemplate = @"using System;
115 using NDO;
116
117 namespace #ns#
118 {
119 ····// Don't change this code.
120 ····// This interface implementation exists only for intellisense support.
121 ····// Any code in this file will be replaced by the enhancer.
122 ····public partial class #cl# : IPersistentObject
123 ····{
124 ········#region IPersistentObject Members
125
126 ········public void NDOMarkDirty()
127 ········{
128 ············throw new NotImplementedException();
129 ········}
130
131 ········public ObjectId NDOObjectId
132 ········{
133 ············get
134 ············{
135 ················throw new NotImplementedException();
136 ············}
137 ············set
138 ············{
139 ················throw new NotImplementedException();
140 ············}
141 ········}
142
143 ········public NDOObjectState NDOObjectState
144 ········{
145 ············get
146 ············{
147 ················throw new NotImplementedException();
148 ············}
149 ············set
150 ············{
151 ················throw new NotImplementedException();
152 ············}
153 ········}
154
155 ········public Guid NDOTimeStamp
156 ········{
157 ············get
158 ············{
159 ················throw new NotImplementedException();
160 ············}
161 ············set
162 ············{
163 ················throw new NotImplementedException();
164 ············}
165 ········}
166
167 ········#endregion
168 ····}
169 }
170 ";
171 ····}
172 }
173
174 #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
175