| 32 | | var _exts = Dictionary<of String, bool>() # should be Set |
| 33 | | |
| 34 | | get excludeNames as List<of String> |
| 35 | | return List<of String>('.svn _svn'.split) |
| 36 | | |
| 37 | | get binExtensions as List<of String> |
| 38 | | return List<of String>('dll doc exe pdb mdb gif png pyc pyo jpg jpeg snk tif tiff xls'.split) |
| 39 | | |
| 40 | | get textExtensions as List<of String> |
| 41 | | return List<of String>('bat c cobra config cpp cs css dif diff h htm html kv m patch py sh sql svg text txt xhtml xml'.split) |
| | 47 | var _exts = Set<of String>() |
| | 48 | |
| | 49 | get excludeNames as IList<of String> |
| | 50 | if not _excludeNamesCache.count |
| | 51 | _excludeNamesCache = List<of String>(_excludeNamesSpec.split) |
| | 52 | return _excludeNamesCache |
| | 53 | |
| | 54 | get binExtensions as IList<of String> |
| | 55 | if not _binExtensionsCache.count |
| | 56 | _binExtensionsCache = List<of String>(_binExtensionsSpec.split) |
| | 57 | return _binExtensionsCache |
| | 58 | |
| | 59 | get textExtensions as IList<of String> |
| | 60 | if not _textExtensionsCache.count |
| | 61 | _textExtensionsCache = List<of String>(_textExtensionsSpec.split) |
| | 62 | return _textExtensionsCache |
| 47 | | print 'This program always starts with the current directory and descends into subdirectories.' |
| 48 | | print 'The program source contains names to ignore (.svn/_svn) and extension types (text vs. binary).' |
| 49 | | print |
| 50 | | print 'With -r or -report, information is printed and no actions are taken.' |
| 51 | | print 'With -to-unix, files that are detected with DOS endings are rewritten.' |
| 52 | | print 'With -to-dos, files that are detected with UNIX endings are rewritten.' |
| 53 | | |
| 54 | | def run |
| | 68 | print 'le <option> \[<paths>]' |
| | 69 | print |
| | 70 | print ' -r or -report: information is printed and no actions are taken.' |
| | 71 | print ' -to-unix: files that are detected with DOS endings are rewritten.' |
| | 72 | print ' -to-dos: files that are detected with UNIX endings are rewritten.' |
| | 73 | print |
| | 74 | print 'You can specify one or more files or directories after the option.' |
| | 75 | print 'Or if you specify none, then the currect directory is processed.' |
| | 76 | print 'Directories are always processed recursively.' |
| | 77 | print |
| | 78 | print 'The program source contains names to ignore (.svn/_svn) and ' |
| | 79 | print 'extension types (text vs. binary).' |
| | 80 | print |
| | 81 | |
| | 82 | def reset |
| | 83 | _action = Action.None |
| | 84 | _exts.clear |
| | 85 | _excludeNamesCache.clear |
| | 86 | _binExtensionsCache.clear |
| | 87 | _textExtensionsCache.clear |
| | 88 | |
| | 89 | def run(args as List<of String>) |
| | 90 | require args.count > 0 # first arg is program name/path |
| 62 | | else if args.count == 2 |
| 63 | | arg = args[1] |
| 64 | | branch arg |
| 65 | | on '-h' or '-help' or '--help' |
| 66 | | .printHelp |
| 67 | | return |
| 68 | | on '-r' or '-report' |
| 69 | | _action = Action.None |
| 70 | | on '-to-unix' |
| 71 | | _action = Action.ToUnix |
| 72 | | on '-to-dos' |
| 73 | | _action = Action.ToDos |
| 74 | | else |
| 75 | | print 'Unknown option. Try -h' |
| 76 | | return |
| 77 | | else |
| 78 | | print 'Too many arguments. Try -h' |
| | 97 | arg = args[1] |
| | 98 | branch arg |
| | 99 | on '-h' or '-help' or '--help' |
| | 100 | .printHelp |
| | 101 | return |
| | 102 | on '-r' or '-report' |
| | 103 | _action = Action.None |
| | 104 | on '-to-unix' |
| | 105 | _action = Action.ToUnix |
| | 106 | on '-to-dos' |
| | 107 | _action = Action.ToDos |
| | 108 | else |
| | 109 | print 'Unknown option. Try -h' |
| | 110 | return |
| | 111 | assert args.count >= 2 |
| | 112 | if args.count == 2 |
| | 113 | args.add('.') # current directory |
| | 114 | |
| | 115 | # CC: |
| | 116 | # paths, badPaths = for arg in args[2:] splitby Directory.exists(arg) or File.exists(arg) |
| | 117 | # kind of like: |
| | 118 | # paths = for arg in args[2:] where Directory.exists(arg) or File.exists(arg) |
| | 119 | |
| | 120 | paths = List<of String>() |
| | 121 | for arg in args[2:] |
| | 122 | if File.exists(arg) |
| | 123 | paths.add(arg) |
| | 124 | else if Directory.exists(arg) |
| | 125 | paths.add(arg) |
| | 126 | else |
| | 127 | print 'Unknown path:', arg |
| | 128 | return |
| | 129 | |
| 102 | | if fi.name in excludeNames, continue |
| 103 | | ext = (fi.extension ? '').toLower |
| 104 | | if ext.startsWith('.'), ext = ext[1:] |
| 105 | | _exts[ext] = true |
| 106 | | if ext in binExts |
| 107 | | print 'skipping bin file : [fi]' |
| 108 | | else if ext in textExts or ext == '' |
| 109 | | # In practice, I find that files without extensions are text files. |
| 110 | | # Usually captured text output from a command or a shell script. |
| 111 | | type = .lineEndingsType(fi) |
| 112 | | pad = if(type=='dos', ' ', '') |
| 113 | | print 'found text file ([type]) [pad]: [fi]' |
| 114 | | .act(type, fi) |
| 115 | | else |
| 116 | | print 'unknown file type : "[ext]" for [fi]' |
| | 155 | .scanFile(fi) |
| | 159 | |
| | 160 | def scanFile(path as String) |
| | 161 | .scanFile(FileInfo(path)) |
| | 162 | |
| | 163 | def scanFile(fi as FileInfo) |
| | 164 | if fi.name in .excludeNames, return |
| | 165 | ext = (fi.extension ? '').toLower |
| | 166 | if ext.startsWith('.'), ext = ext[1:] |
| | 167 | _exts.add(ext) |
| | 168 | if ext in .binExtensions |
| | 169 | print 'skipping bin file : [fi]' |
| | 170 | else if ext in .textExtensions or ext == '' |
| | 171 | # In practice, I find that files without extensions are text files. |
| | 172 | # Usually captured text output from a command or a shell script. |
| | 173 | type = .lineEndingsType(fi) |
| | 174 | pad = if(type=='dos', ' ', '') |
| | 175 | print 'found text file ([type]) [pad]: [fi]' |
| | 176 | .act(type, fi) |
| | 177 | else |
| | 178 | print 'unknown file type : "[ext]" for [fi]' |