用ruby写了一个生成xpcom组件模板的小工具

写xpcom组件我们经常copy paste,因为xpcom组件的与业务无关的注册、QueryInterface代码都是基本固定的,
写了一个自动生成xpcom组件的template,这样就可以只关心核心业务,避免书写样板式的代码。

Ruby代码 复制代码
  1. =begin  
  2. NOTICE:   
  3. Before use the code:   
  4. You should use command "gem install uuid" to install uuid gem for ruby   
  5. Once installed, create a uuid.state file by running command "uuid-setup"  
  6. =end  
  7.   
  8. require 'uuid'  
  9.   
  10. puts "please input the component interface name:"  
  11. component_interface_name = gets.chomp   
  12.   
  13. puts "please input the component class name:"  
  14. class_name = gets.chomp   
  15. cid = "{#{UUID.new}}";   
  16.   
  17. template = <<EOF   
  18. const Cc = Components.classes;   
  19. const Ci = Components.interfaces;   
  20. const Cr = Components.results;   
  21.   
  22. const nsISupports = Ci.nsISupports;   
  23. const #{component_interface_name}  =  Ci.#{component_interface_name};   
  24.   
  25. const CLASS_ID = Components.ID("#{cid}");   
  26. const CLASS_NAME = "#{class_name}";   
  27. const CONTRACT_ID = "@ibm.com/#{class_name.downcase};1";   
  28.   
  29. function #{class_name}(){   
  30. };   
  31.   
  32. #{class_name}.prototype = {   
  33.   
  34.     QueryInterface: function(aIID){   
  35.         if(!aIID.equals(#{component_interface_name}) &&    
  36.            !aIID.equals(nsISupports))    
  37.             throw Cr.NS_ERROR_NO_INTERFACE;   
  38.         return this;   
  39.     },   
  40.        
  41.        
  42. var #{class_name}Factory = {   
  43.     createInstance: function(aOuter, aIID){   
  44.         if(aOuter != null)   
  45.             throw Cr.NS_ERROR_NO_AGGREGATION;   
  46.         return (new #{class_name}()).QueryInterface(aIID);   
  47.     }   
  48. };   
  49.   
  50. var #{class_name}Module = {   
  51.     registerSelf: function(aComMgr, aFileSpec, aLocation, aType){   
  52.         aComMgr = aComMgr.QueryInterface(Ci.nsIComponentRegistrar);   
  53.         aComMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);   
  54.     },   
  55.        
  56.     unregisterSelf: function(aComMgr, aLocation, aType){   
  57.         aComMgr = aComMgr.QueryInterface(Ci.nsIComponentRegistrar);   
  58.         aComMgr.unregisterFactoryLocation(CLASS_ID, aLocation);   
  59.     },   
  60.        
  61.     getClassObject: function(aComMgr, aCID, aIID){   
  62.         if(!aIID.equals(Ci.nsIFactory))   
  63.             throw Cr.NS_ERROR_NO_IMPLEMENTED;   
  64.         if(aCID.equals(CLASS_ID))   
  65.             return #{class_name}Factory;   
  66.         throw Cr.NS_ERROR_NO_INTERFACE;   
  67.     },   
  68.        
  69.     canUnload: function(aComMgr){   
  70.         return true;   
  71.     }   
  72. };   
  73.   
  74. function NSGetModule(aComMgr, aFileSpec){   
  75.     return #{class_name}Module;   
  76. };   
  77. EOF   
  78.   
  79. file_name = "#{class_name}.js"  
  80.   
  81. File.open(file_name,"w"do |file|   
  82.    file.puts template   
  83. end  

自我的JavaEye博客

  
  
   
   

  
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值