2

I'm recently trialling CodeSmith Generator Professional 7.1.0, There is one thing keep troubling me, every time when I generating some files with it, the file format always is UTF-8 with BOM, I really need the file format is UTF-8 without BOM, But I can not find any settings or configurations to do this, I'm searching on Google for days and got nothing useful. Any thoughts will be appreciated!

Here is template code below:

<% @CodeTemplate Language = "C#"
TargetLanguage = "C#"
ResponseEncoding = "UTF-8"
Description = "Generates a very simple business object." %>
  <% @Property Name = "SourceDatabase"
Type = "SchemaExplorer.DatabaseSchema"
DeepLoad = "True"
Optional = "False"
Category = "01. Getting Started - Required"
Description = "Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>
  <% @Property Name = "SourceTable"
Type = "SchemaExplorer.TableSchema"
Category = "Context"
Description = "Table that the object is based on." %>
  <% @Assembly Name = "SchemaExplorer" %>
  <% @Assembly Name = "System.Data" %>
  <% @Import Namespace = "SchemaExplorer" %>
  <% @Import Namespace = "System.Data" %>
  <% @Assembly Name = "MySql.Data" %>
  <% @Import NameSpace = "MySql.Data.MySqlClient" %>
  <? php

//Language File for <%= SourceTable.Name.ToLower() %> [EN]
define('LANG_<% = SourceTable.Name.ToUpper() %>', '<% = GetTablebPerfix(SourceTable.Name) %> List'); <%
for (int i = 0; i < SourceTable.NonForeignKeyColumns.Count; i++) { %>
  define('LANG_<% = SourceTable.NonForeignKeyColumns[i].Name.ToUpper() %>', '<% = GetColumnComment(SourceTable.Columns[i].Name) %>'); <%
} %>
?>

< script runat = "template" >
  public string GetColumnComment(string _columnname) {
    DataSet _auditTables = new DataSet();
    string ConnectionString = "server=localhost; user id = root; password = xxxxx; database =" + SourceDatabase + "";
    MySqlConnection conn = new MySqlConnection(ConnectionString);
    MySqlDataAdapter adapter = new MySqlDataAdapter(conn.CreateCommand());
    adapter.SelectCommand.CommandText = "select * from information_schema.columns where (table_schema='" + SourceDatabase + "') and (table_name='" + SourceTable + "') and (column_name='" + _columnname + "')";
    adapter.Fill(_auditTables);
    string en_comment = "";
    if (_auditTables.Tables.Count > 0) {
      DataTable dt = _auditTables.Tables[0];
      if (dt.Rows.Count > 0) {
        string comments = dt.Rows[0][dt.Columns.Count - 1].ToString();
        if (comments.IndexOf('/') == -1) {
          en_comment = comments;
        } else {
          en_comment = comments.Split('/')[1];
        }
      }
    }

    conn.Close();
    return en_comment;

  }

public string GetTablebPerfix(string _tablename) {
  string _tbnamewhtprefix = "";
  if (_tablename.IndexOf('_') == -1) {
    _tbnamewhtprefix = _tablename;
  } else {
    _tbnamewhtprefix = _tablename.Split('_')[1];
  }

  return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(_tbnamewhtprefix);
} < /script>
tripleee
  • 139,311
  • 24
  • 207
  • 268
Jack
  • 131
  • 12
  • BTW, I'm using Windows 7 Ultimate x64, Chinese version. – Jack Jan 18 '16 at 18:48
  • 1
    Possible duplicate of [How to set standard encoding in Visual Studio](http://stackoverflow.com/questions/696627/how-to-set-standard-encoding-in-visual-studio) – Paul Sweatte Mar 19 '16 at 10:25
  • @Paul Sweatte: What's the relationship between CodeSmith Generator and Visual Studio? What makes that question a duplicate? – Adrian McCarthy Mar 19 '16 at 15:09
  • @AdrianMcCarthy CodeSmith Generator is [integrated with Visual Studio](http://blog.codesmithtools.com/category/visual-studio/). The question is `I am searching for a way to setup Visual Studio so it always saves my files in UTF-8.` vs `I really need the file format is UTF-8 without BOM`. – Paul Sweatte Mar 19 '16 at 18:48
  • can you post the template code? are you adding the BOM yourself in the code? http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom – greg Apr 17 '16 at 17:00
  • 1
    The nominated duplicate doesn't address the problem that the UTF-8 file has a BOM. – tripleee Apr 19 '16 at 04:19

1 Answers1

2

There are two properties you can set to control this property on the Code Template directive Encoding and ResponseEncoding attributes will control how the template is rendered and saved.

https://codesmith.atlassian.net/wiki/display/Generator/The+CodeTemplate+Directive

Blake Niemyjski
  • 3,192
  • 3
  • 23
  • 40
  • Oh,that's so long, and can not have a chance to test it out, but seems is the right way. THANKS – Jack Dec 06 '16 at 05:38