Advertisement
Guest User

Linqpad script to preserve comments in sqlmetal c# autogen

a guest
Oct 17th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. //Your filename goes here!
  2. string filename = "FULLPATHTOYOURCODEFILENAME"
  3. StreamReader r = new StreamReader(filename);
  4. string strCmdText = string.Format(@"/server:YOURSERVERNAME /database:YOURDATABASENAME /user:YOURUSERNAME /password:YOURPASSWORD /views /code:{0} /namespace:D /context:WebData2 /pluralize",filename);
  5. List<string> lines = new List<string>();
  6. List<string> orglines = new List<string>();
  7. List<TableComment> Tablecomments = new List<TableComment>();
  8. List<Comment> comments = new List<Comment>();
  9. //skip the first 9 lines.
  10. for(int i = 0;i<9;i++)r.ReadLine();
  11. string currenttable = null;
  12. while(!r.EndOfStream)
  13. {  
  14.     string orgline = r.ReadLine();
  15.     string line = orgline.Replace("\t","").TrimStart(' ');
  16. //  Console.WriteLine(line);
  17.     lines.Add(line);
  18.     orglines.Add(orgline);
  19.     if(line.StartsWith("public System.Data.Linq.Table<"))
  20.     {
  21.         var tc = new TableComment(){
  22.             table = line
  23.         };
  24.         int curr = lines.Count-2;
  25.         while(lines[curr].StartsWith("//"))
  26.             tc.comments.Add(orglines[curr--]);
  27.         if(tc.comments.Count>0)
  28.             Tablecomments.Add(tc);
  29.     }
  30.     if(line.StartsWith("[Table(Name="))
  31.     {      
  32.         currenttable = line;       
  33.     }
  34.     if(line.StartsWith("[Column("))
  35.     {
  36.         int curr = lines.Count-2;
  37.         Comment c = new Comment(){table = currenttable,fieldname = line};
  38.         while(lines[curr].StartsWith("//"))
  39.             c.comments.Add(orglines[curr--]);
  40.         if(c.comments.Count>0)
  41.             comments.Add(c);
  42.     }
  43. }  
  44. r.Close();
  45. //Tablecomments.Dump();
  46.  
  47. //run sqlmetal thing
  48.  
  49. var prc = System.Diagnostics.Process.Start(@"c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sqlmetal.exe",strCmdText);
  50. prc.WaitForExit();
  51.  
  52. //rebuild comments.
  53. r = new StreamReader(filename);
  54. //Save all the comments and the next line (the function line
  55. lines = new List<string>();
  56. //List<TableComment> Tablecomments = new List<TableComment>();
  57.  
  58. //skip the first 9 lines.
  59. for(int i = 0;i<10;i++)r.ReadLine();
  60. while(!r.EndOfStream)
  61. {  
  62.     string orgline = r.ReadLine();
  63.     string line = orgline.Replace("\t","").TrimStart(' ');
  64.     TableComment tc = (from b in Tablecomments.Where (p => p.table == line)
  65.              select b).FirstOrDefault();
  66.     if(tc!=null)
  67.     {
  68.         for(int i = tc.comments.Count-1;i>-1;i--)
  69.         {
  70.             lines.Add(tc.comments[i]);
  71.             //Console.WriteLine(tc.comments[i]);           
  72.         }
  73.         //Console.WriteLine(line);
  74.     }
  75.    
  76.     if(line.StartsWith("[Table(Name="))
  77.     {      
  78.         currenttable = line;       
  79.     }
  80.     if(line.StartsWith("[Column("))
  81.     {
  82.         var c = (from b in comments where b.table == currenttable  && b.fieldname == line select b).FirstOrDefault();
  83.         if(c!=null)
  84.         {
  85.             for(int i = c.comments.Count-1;i>-1;i--)
  86.             {
  87.                 lines.Add(c.comments[i]);          
  88.             }      
  89.         }
  90.     }
  91.     //lines.Add(tc.comments[i]);
  92.     //Console.WriteLine(line);
  93.     lines.Add(orgline);
  94. }  
  95. r.Close();
  96. Tablecomments = null;
  97. File.Delete(filename);
  98. StreamWriter wr = new StreamWriter(filename);
  99. foreach(string s in lines)
  100. {
  101.     wr.WriteLine(s);
  102. }
  103. wr.Flush();
  104. wr.Close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement