ãããã§ï¼ã³ã¼ãã£ã³ã°è¦ç´ãå®ãããããï¼å ¨ã½ã¼ã¹ã³ã¼ãããã§ãã¯ãã¦ï¼ã«ã¼ã«éåãèªåæ¤åºï¼
ãã³ã¼ãã®èªã¿ããããã¯ï¼é常ã«éè¦ã ã
ã½ã¼ã¹ã³ã¼ããèªã¿ã¥ãããªãã¨ï¼ã³ã¼ãããä»æ§ã表ç¾ãããªããªãã
ç°¡åã«ãã°ãæ··å ¥ããï¼åããã¦ãã¾ããããã«æ°ä»ããããªããªãã
ä¿å®ãæ¹è¯ãã§ããªããªãã
ããã¸ã§ã¯ããè¡ãè©°ã¾ãã
ãã¹ãã¼ããå§ã¾ãã
ã»ã»ã»
éã«ï¼ãèªã¿ãããã³ã¼ããã§ããããã°ï¼
ã©ãã»ã©å¤ãã®ãã°ãæ··å ¥ãã¦ããã¨ãã¦ãï¼å®¹æã«ãã°ããä¿®æ£ã§ããã
ä¿å®æ§ãé«ãã®ã§ããã
ãèªã¿ãããã³ã¼ããã¯ãä»æ§ãæå¿«ã«è¡¨ç¾ãã¦ããã³ã¼ãããªã®ã§ï¼
ä»ã®äººãèªãã æï¼ä»æ§ã®èª¤è§£ãèµ·ãããªããæãã¤ããããã
ãææ¡ããããã³ã¼ããã¯ãå¤æ´ããããã³ã¼ãããªã®ã§ï¼ããã¸ã§ã¯ããã©ãã©ãåã«é²ãã
ã§ãï¼ããã確ä¿ããã«ã¯ã©ããããããã®ãï¼
ãã³ã¼ãã®èªã¿ããããã確ä¿ããã«ã¯
ä¸ç´è
ã¯ï¼èª°ã«è¨ãããªãã¦ãï¼èªç¶ã«ãèªã¿ãããã³ã¼ãããæ¸ããã¨ãã§ããã
åå¿è ãå«ããããã¸ã§ã¯ãã®å ¨å¡ããèªã¿ãããã³ã¼ãããæ¸ãããã«ããããï¼
é常ï¼ããã¸ã§ã¯ãã®æåã«ãã³ã¼ãã£ã³ã°è¦ç´ããä½ãã ããã
ã§ãï¼æéã®çµéã¨ã¨ãã«ï¼èª°ãè¦ç´ãå®ããªããªãã
å¿ãããªãã®ã§ï¼è¦ç´ãªãã¦å¿ãã¦ãã¾ãã
ãã£ã¤ãä»äºãããããã«ãªãã
ãã¹ãã¼ããå§ã¾ãã
ã»ã»ã»
ã©ããããããã
ãã®ããã«ï¼ãã³ã¼ãã®èªã¿ããããã¯ããã¸ã§ã¯ãã«ããã¦è´å½çã«éè¦ãªè¦ç´ ã§ããã«ãé¢ãããï¼
ã³ã¼ãã£ã³ã°è¦ç´ã¯ã©ããã¦ãç¡è¦ãããï¼ã¨ããäºã«ãªãã
ããã¯ï¼ä»æ¹ã®ãªãäºã ã
人éã¯ããããã§ã¯ãªãã®ã§ï¼èªåããã¨ãã¨æã£ã¦ããªãç¿æ £ã
ãã¡ãã¡æãåºããªããããã°ã©ãã³ã°ããï¼ã¨ãããã¨ã¯ã§ããªãã
ã ããï¼å¯¾çã¨ãã¦ï¼
ã³ã¼ãã£ã³ã°è¦ç´å®ç¾©æ¸ããå®è¡å¯è½ããã¥ã¡ã³ããã«ãã
ã¨ããæãæã¦ã°ããã
ããéçåä»ãè¨èªï¼Javaã¨ãï¼ã§ããã°ï¼Eclipseã®ãããªIDEãï¼èªåçã«ã³ã¼ããã©ã¼ããããã¦ãããã
ã³ã¼ãã£ã³ã°è¦ç´ã«éåãã¦ããåæãï¼ãã¼ã«ãèªåçã«ç¥ããã¦ãããã
ãããï¼ã©ãã§ããã£ã¦ããã ããã
ãããï¼åçåä»ãè¨èªï¼Rubyã¨ãï¼ã®å ´åã¯ï¼ã©ããããããã®ãï¼
åçè¨èªãªã®ã§ï¼ã³ã¼ãã®éç解æã®ããã®ä¸è¬çãªãã¼ã«ããªãã®ã ã*1
ããããå ´åï¼ããã¡ã¯ãã®IDEãåå¨ããªãã£ããããã
æåãç®è¦ã§ã®ãã§ãã¯ã«ã¯éçãããã
ã¹ãã«ã®ããéçºè ããã¤ãã½ã¼ã¹ã³ã¼ããã¬ãã¥ã¼ã§ããã°ãããï¼ãããæéçã»ã³ã¹ãçã«å¿ ãããä½è£ããªãã£ããããã
ã©ãããã°ãããã
ã²ã¨ã¤ã®æ¹æ³
ä¸ã¤ã®æ¹æ³ã¯ï¼åçè¨èªã®ã½ã¼ã¹ã³ã¼ãã«å¯¾ãã¦ï¼
ç°¡æãªéç解æï¼ãã§ãã¯ï¼ãå®è¡ãããããªããããèªåã§ä½ããã¨ã ã
以ä¸ã®ãããã使ãã°ï¼ããã¸ã§ã¯ãå ã§ï¼
åºæ¬çãªã³ã¼ãã£ã³ã°è¦ç´ã®ãã§ãã¯ãèªååã§ããã
è¨èªã®ç¨®é¡ã¯åããªãã
ãµã³ãã«ã¨ãã¦ï¼Ruby on Railsã®Webã¢ããªã±ã¼ã·ã§ã³ã«å¯¾ãããã§ãã¯ãè¨è¿°ãã¦ã¿ãã
main.js
// ããã¸ã§ã¯ãå ã®ã³ã¼ãããã§ã㯠new CodeChecker({ // ãã§ãã¯å¯¾è±¡ã®ã«ã¼ããã©ã«ã target_path : "D:\\dev\\src\rails" }).check({ file_types : [ "*.rb", "*.js" ], max_file_length : 600, // è¡æ° max_line_length : 120, // è¡ã®é·ã because : "è¡å æåæ°ã¨è¡æ°ãå¤éããªããã¨" }).check({ file_types : [ "*.rhtml" ], max_file_length : 200, max_line_length : 120, because : "è¡å æåæ°ã¨è¡æ°ãå¤éããªããã¨ï¼ãã¥ã¼ã¯ç»é¢è¨è¨ãæ示ããä»æ¹ã§ç°¡æ½ã«æ¸ããã¨ï¼" }).check({ file_types : [ "*.rb" ], limit_block_size : { // ãããã¯ã®é·ããå¶é keyword : "def", max_line_length : 80 }, because : "Rubyã§é·ãããé¢æ°ãæ¸ããªããã¨" }).check({ file_types : [ "*\\app\\models\\*.rb", "*\\app\\helpers\\*.rb", "*\\app\\views\\*.rhtml" ], must_not_contain : "params", // å«ãã§ã¯ãããªããã¼ã¯ã¼ã because : "HTTPãã©ã¡ã¼ã¿ã®è§£æã¯ã³ã³ããã¼ã©å ã§å®çµããããã¨ï¼paramsã使ããªï¼" }) ;
ã³ã¼ãã£ã³ã°è¦ç´ã JavaScript (WSH/JScript) ã§æ¸ããã¦ããã
ããã¯å®è¡å¯è½ãªããã°ã©ã ã¨ãã¦æ¸ããã¦ããã®ã§ï¼ãå®è¡å¯è½ããã¥ã¡ã³ããã§ããã
CodeCheckerã¨ãããªãã¸ã§ã¯ããçæãã¦ããï¼ãã§ãã¯ã®ã«ã¼ã«ãä¸ã¤ãã¤ï¼check() ã¨ããã¡ã½ããã®ä¸ã«è¨è¿°ãã¦ããã
åã«ã¼ã«ã«ã¯ï¼
- file_typesï¼æ¤è¨¼å¯¾è±¡ã®ãã¡ã¤ã«åã®ãã¿ã¼ã³ï¼æ¡å¼µåã¨ãï¼
- å ·ä½çãªæ¤è¨¼å 容ï¼è¡ã®é·ãã¨ãï¼
- becauseï¼æ¤è¨¼ã®çç±ãç®çï¼è¨è¨æ¹é
ãå«ã¾ãã¦ããã
ãã®ã¹ã¯ãªãããå®è¡ããããã®èµ·åãããã¯ä»¥ä¸ã
execute.bat
@cscript //nologo execute.wsf
execute.wsf
<job> <script src=".\\code_checker.js"></script> <script src=".\\main.js"></script> </job>
ããã¦ï¼è¦ã¨ãªããCodeCheckerãã®ä¸èº«ã¯ä»¥ä¸ã
ãããã®ï¼ã¤ã®ãã¡ã¤ã«ãåä¸ãã©ã«ãå ã«è¨ç½®ãï¼èµ·åããããå®è¡ããã°ï¼ãã§ãã¯ã®çµæãã³ãã³ãããã³ããã«è¡¨ç¤ºãããã
ï¼ã¿ãåºåãã§åºåãããã®ã§ï¼ãªãã¤ã¬ã¯ãããã°ï¼Excelã«è¡¨å½¢å¼ã§è²¼ãä»ããããã§ãããï¼
code_checker.js
/* ã³ã¼ãå 容ããã§ãã¯ããã¯ã©ã¹ */ var CodeChecker = function( init_hash ){ // åæå this._target_path = init_hash[ "target_path" ]; // ãã§ãã¯å®è¡ã«åãã this._init(); }; CodeChecker.prototype = { // æ¢ç´¢å¯¾è±¡ã®ã«ã¼ããã¹ _target_path : null, // å ¨ãã¡ã¤ã«ã®ãªã¹ã _all_files : {}, // åæå _init : function() { this._store_all_files_list_by_ext(); } , // -------------- ãã¡ã¤ã«å¦çç³» -------------- // å ¨ãã¡ã¤ã«ã®ãªã¹ããæ¡å¼µåå¥ã«ä¿æ _store_all_files_list_by_ext : function() { this._all_files = this.get_all_files_list(); // ç»é²çµæããããã° //this._dump_all_files_list(); } , // å ¨ãã¡ã¤ã«ã®ãã¡ã¤ã«ãã¹ã®é åãåå¾ get_all_files_list : function() { var ws = WScript.CreateObject("WScript.Shell"); ws.CurrentDirectory = this._target_path; var proc = ws.Exec("cmd.exe /c dir /A-D /s /b *"); var res = proc.StdOut.ReadAll().split("\r\n"); res.pop(); // æå¾ã®è¡ã¯ç©ºç½ return res; } , // ä¿æãã¦ãããã¡ã¤ã«ãªã¹ãããããã°ç¨ã«ãã³ã _dump_all_files_list : function() { var all_files = this._all_files; for( var i = 0; i < all_files.length; i ++ ) { this.log( all_files[ i ] ); } } , // æå®ããããã¡ã¤ã«ã¿ã¤ãã®ãã¡ã¤ã«ãå ¨æ±ºå® _specify_target_files : function( file_types ) { var all_files = this._all_files; var target_files = []; for( var i = 0; i < all_files.length; i ++ ) { for( var j = 0; j < file_types.length; j ++ ) { var file_path = all_files[ i ]; var file_type = file_types[ j ]; // ãããããã if( this._match_file_type( file_path, file_type ) ) { target_files.push( file_path ); //this.log( file_path + "ã" + file_type + "ã«ããã" ); } } } return target_files; } , // ãããã¡ã¤ã«ãã¹ããããã¡ã¤ã«ã¿ã¤ãæå®æååã«ãããããã _match_file_type : function( file_path, file_type ) { // ã¯ã¤ã«ãã«ã¼ã表è¨ãæ£è¦è¡¨ç¾è©ä¾¡ç¨ã«å¤æ var compiled_file_type = "^" + file_type .replace( /\\/g, "\\\\" ) .replace( /\./g, "\\." ) .replace( /\*/g, ".*" ) + "$" ; if( file_path.match( compiled_file_type ) ) { return true; } else { return false; } } , // ãã¡ã¤ã«å 容ãèªã¿åã(UTF8) _read_file_content : function( file_path ) { var adTypeText = 2; // ããã¹ã var adReadAll = -1; // å ¨è¡ var sr = new ActiveXObject("ADODB.Stream"); sr.Type = adTypeText; sr.charset = "utf-8"; sr.Open(); sr.LoadFromFile( file_path ); var str = sr.ReadText( adReadAll ) + ""; sr.Close(); return str; } , // ---------- ãã§ãã¯å®è¡ç³» ----------- // æå®ãããå ¨ãã¡ã¤ã«ã«å¯¾ãã¦ãã§ãã¯ãå®è¡ check : function( hash ) { // 対象ãã¡ã¤ã«ãæ±ºå® var target_files = this._specify_target_files( hash["file_types"] ); // åãã¡ã¤ã«ãã¨ã«ãã§ã㯠for( var i = 0; i < target_files.length; i ++ ) { var file_path = target_files[i]; var file_content = this._read_file_content( file_path ); this._check_one_file( file_path, file_content, hash ) } // ã¡ã½ãããã§ã¤ã³ãå¯è½ã«ãã return this; } , // 1ãã¡ã¤ã«ããã§ã㯠_check_one_file : function( file_path, file_content, hash ) { // ãã§ãã¯ã¿ã¤ãå¥ã«å®è¡ // è¡æ° if( hash["max_file_length"] ) { this._check_max_file_length( file_path, file_content, hash["max_file_length"] ); } // è¡å æåæ° if( hash["max_line_length"] ) { this._check_max_line_length( file_path, file_content, hash["max_line_length"] ); } // ãããã¯é·ãå¶é if( hash["limit_block_size"] ) { this._check_block_length( file_path, file_content, hash["limit_block_size"] ); } // ç¦æ¢ãã¼ã¯ã¼ã if( hash["must_not_contain"] ) { this._check_forbidden_keyword( file_path, file_content, hash["must_not_contain"], hash["because"] ); } } , // è¡æ°ããã§ã㯠_check_max_file_length : function( file_path, file_content, max_file_length ) { var file_length = file_content.replace( /\r\n/g, "\n" ).split("\n").length; if( file_length > max_file_length ) { this._check_err( file_path, "è¡æ°ãå¤ãããï¼" + file_length + "è¡ï¼" ); } } , // è¡å æåæ°ããã§ã㯠_check_max_line_length : function( file_path, file_content, max_line_length ) { var lines = file_content.replace( /\r\n/g, "\n" ).split("\n"); for( var i = 0; i < lines.length; i ++ ) { var line = lines[i]; if( line.length > max_line_length ) { this._check_err( file_path, "è¡ãé·ãããï¼" + ( i + 1 ) + "è¡ç®ï¼" + line.length + "æåï¼\t" + line ); } } } , // ãããã¯ã®ãµã¤ãºããã§ã㯠_check_block_length : function( file_path, file_content, limit_info ) { var lines = file_content.replace( /\r\n/g, "\n" ).split("\n"); var block_keyword = limit_info["keyword"]; var max_line_length = limit_info["max_line_length"]; var reg_keyword = "^[ \t]*" + block_keyword + "[^a-zA-Z0-9_]" ; // å ¨è¡ãé çªã«ã¹ãã£ã³ãã var block_size_counter = 0; var inside_block_flag = false; var block_init_code = ""; var block_init_line_num = 0; for( var i = 0; i < lines.length; i ++ ) { var line = lines[i]; // ãããã¯ã®å§ã¾ãã«æ¥ãã if( line.match( reg_keyword ) ) { // ã«ã¦ã³ã¿ã¼ãåæå inside_block_flag = true; block_size_counter = 0; block_init_code = line; block_init_line_num = i + 1; } // ãããã¯å ãªãã° if( inside_block_flag ) { // ï¼è¡ã«ã¦ã³ã block_size_counter ++; // è¶ éãã¦ããªãã if( block_size_counter > max_line_length ) { this._check_err( file_path, block_keyword + "ãããã¯ãé·ãããï¼" + block_init_line_num + "è¡ç®ï¼" + max_line_length + "è¡ãè¶ éï¼\t" + block_init_code ); // ãã£ããè¶ éãæ¤åºããããï¼æ¬¡ã®ãããã¯å§ã¾ãã¾ã§ã¯ç¡è¦ inside_block_flag = false; block_size_counter = 0; } } } } , // ç¦æ¢æååããã§ã㯠_check_forbidden_keyword : function( file_path, file_content, forbidden_keyword, reason ) { var lines = file_content.replace( /\r\n/g, "\n" ).split("\n"); var reg_keyword = "^[^a-zA-Z0-9_]*" + forbidden_keyword + "[^a-zA-Z0-9_]" ; for( var i = 0; i < lines.length; i ++ ) { var line = lines[i]; if( line.match( reg_keyword ) ) { this._check_err( file_path, reason // çç±ã表示ãã + "ï¼" + ( i + 1 ) + "è¡ç®ï¼\"" + forbidden_keyword + "\"ï¼\t" + line ); } } } , // ---------- ãã®ä» ----------- // ãã§ãã¯çµæã®ã¨ã©ã¼å 容ã表示 _check_err : function( file_path, err_msg ) { // ã¿ãåºåãã§è¡¨ç¤ºãã¦ï¼Excelã«è¡¨å½¢å¼ã§è²¼ãä»ããããããã«ãã this.log( "" + file_path + "\t" + err_msg ); } , log : function( s ) { WScript.Echo( s ); } };
ããï¼æ¤è¨¼å¯è½ãªã«ã¼ã«ãå¢ããããã®ã§ããã°ï¼
ä¸è¨ã³ã¼ãå ã® check() ã¡ã½ããã®ä¸ã«ï¼ç¬èªã®æ°è¦ã«ã¼ã«ãè¨è¿°ãã¦æ¡å¼µãããã¨ãã§ããã
ããã§ï¼ãã³ã¼ãã£ã³ã°è¦ç´ã«éµå®ãã¦ããäºã®æ¤æ»ããèªååã§ããã
å®è£ å·¥ç¨ã®å質ãã§ãã¯ã®ãã¡ãä¿å®æ§ãã®æ¤æ»ãï¼èªååã§ããã®ã§ããã
ãã®ããããï¼æ¯æ¥èªåçã«å®è¡ãããããã«ãã¦ããã°ï¼
ãç¥ããªãéã«ï¼ãã£ããã¢ããã§ããªããããªã¹ãã²ããã£ãªã³ã¼ãã大éçºçãã¦ããï¼ï¼ï¼ã
ã¨ããï¼æªå¯ãçãäºæ ãé¿ããããã
é·ãã¡ã½ããã®çºçã¯ææ¢ããï¼
ãã¼ã ã®ã¡ã³ãã¯å¸¸ã«ãªãã¡ã¯ã¿ãªã³ã°ãä¿ããï¼
ã³ã¼ãã®ãå±éºãªåããã¯èªåçã«éç¥ããï¼
ã³ã¼ãã¯ã¯ãªã¼ã³ãªç¶æ ã«ä¿ãããã
ããã¸ã§ã¯ãã¯ï¼åã«é²ã¿ç¶ããäºãã§ããã®ã ã
è£è¶³
ãã¡ããï¼åä½ãã¹ããããããæ¸ãã®ãè¯ãç¿æ £ã ã
ãã¹ãã«ãã£ã¦ãå®è¡æã®æåã®æ£ç¢ºããã確ä¿ã§ããã
ã¡ã½ããã®é·ãã¯ãæããï¼ããã¨ä¿å®ããããã§ãããç¨åº¦ã®é·ããã«ä¿ãããã
ã§ãï¼ããã ãã§ã¯ã³ã¼ãã®ãèªã¿ãããããæ ä¿ããããªãï¼ã¨ããé¢ãããã
ä»åã®ãããããã©ã¹ã¢ã«ãã¡ã¨ãã¦ä½¿ãã°ï¼ã³ã¼ãã®èªã¿ãããã¯ï¼å®æ§çã«ã§ã¯ãªãï¼ãå®éçã«ä¿è¨¼ããããã
â»ãã§ãã¯ã®ã«ã¼ã«ã¯ï¼ããç¨åº¦ä½è£ããã£ãã«ã¼ã«ã«ãã¦ããã¨ããã ããã
ã
*1:ããããã¯ãã£ãã»ããã°ã©ããp145ï¼ãåçè¨èªã®è§£æãã®é ãåç §ãrcov, Saikuro, flogãªã©ãç´¹ä»ããã¦ãã