NodeJs:基於Node的代碼生成器 之 入門教程


背景

為了方便團隊和其它朋友使用我開發的這款代碼生成器,特此寫一篇入門教程。

環境要求

Node:http://nodejs.org/

.Net4.5:http://www.microsoft.com/zh-cn/download/details.aspx?id=30653

happy.node:http://happy.codeplex.com/SourceControl/latest

目錄結構

模板文件和最終生成的代碼文件

生成程序

app.js

 1 var _ = require('underscore');
 2 
 3 var DatabaseSchemaReader = require('happy.database.schem.reader').DatabaseSchemaReader;
 4 var CodeGenerator = require('happy.code.generator').CodeGenerator;
 5 
 6 var project = {
 7     projectDirectory: './outputs', //輸出目錄
 8     rootNamespace: 'AutoGenerateWcfService', //根命名空間
 9     connectionString: 'Data Source=192.168.0.14\\SQLEXPRESS;Initial Catalog=Tenoner3.0;Persist Security Info=True;User ID=sa;Password=woshishui2010;', //連接字符串
10     openTables: ['HrEmployees'] //要生成的表,也可以全部生成。
11 };
12 
13 //和數據庫無關的代碼生成
14 CodeGenerator.executeTemplateDirectory('./templates/commons', project);
15 
16 //和數據庫相關的代碼生成
17 var schemaReader = DatabaseSchemaReader.createSqlClientReader(project.connectionString);
18 schemaReader.readAll(function (error, schema) {
19     if (error) {
20         console.log(error)
21 
22         return;
23     }
24 
25     _.each(schema.tables, function (table) {
26         if (_.contains(project.openTables, table.name)) {
27             var ejsOptions = _.extend({
28                 table: table,
29                 baseNamespace: 'Test'
30             }, project);
31 
32             CodeGenerator.executeTemplateDirectory('./templates/tables', ejsOptions);
33         }
34     });
35 
36     console.log('success!')
37 });

常見問題

  1. 如何學習模板語法?
    語法采用的是ejs模板的語法,不懂請參考:https://github.com/visionmedia/ejs,非常簡單,十分鍾學不會找我。
  2. 如何詳細學習API呢?

  3. 如何指定輸出的文件路徑呢?
  4. 如何設置只生成一次呢?如果文件已經生成,就不要生成了。
  5. 如何擴展呢?
    隨便使用了,有問題可以隨時交流。

備注

寫的還是比較簡單,作為一個開始吧!工具里自帶了一些demo,也可以作為更好的入門教程使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM