Class: Linguist::Generated
- Inherits:
-
Object
- Object
- Linguist::Generated
- Defined in:
- lib/linguist/generated.rb
Instance Attribute Summary collapse
-
#extname ⇒ Object
readonly
Returns the value of attribute extname.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.generated?(name, data) ⇒ Boolean
Public: Is the blob a generated file?.
Instance Method Summary collapse
-
#compiled_coffeescript? ⇒ Boolean
Internal: Is the blob of JS generated by CoffeeScript?.
-
#compiled_cython_file? ⇒ Boolean
Internal: Is this a compiled C/C++ file from Cython?.
-
#composer_lock? ⇒ Boolean
Internal: Is the blob a generated php composer lock file?.
-
#data ⇒ Object
Lazy load blob data if block was passed in.
-
#generated? ⇒ Boolean
Internal: Is the blob a generated file?.
-
#generated_by_zephir? ⇒ Boolean
Internal: Is the blob a generated by Zephir.
-
#generated_jni_header? ⇒ Boolean
Internal: Is the blob a C/C++ header generated by the Java JNI tool javah?.
-
#generated_net_designer_file? ⇒ Boolean
Internal: Is this a codegen file for a .NET project?.
-
#generated_net_docfile? ⇒ Boolean
Internal: Is this a generated documentation file for a .NET assembly?.
-
#generated_parser? ⇒ Boolean
Internal: Is the blob of JS a parser generated by PEG.js?.
-
#generated_postscript? ⇒ Boolean
Internal: Is the blob of PostScript generated?.
-
#generated_protocol_buffer? ⇒ Boolean
Internal: Is the blob a C++, Java or Python source file generated by the Protocol Buffer compiler?.
- #generated_protocol_buffer_go? ⇒ Boolean
-
#godeps? ⇒ Boolean
Internal: Is the blob part of Godeps/, which are not meant for humans in pull requests.
-
#initialize(name, data) ⇒ Generated
constructor
Internal: Initialize Generated instance.
-
#lines ⇒ Object
Public: Get each line of data.
-
#minified_files? ⇒ Boolean
Internal: Is the blob minified files?.
-
#node_modules? ⇒ Boolean
Internal: Is the blob part of node_modules/, which are not meant for humans in pull requests.
-
#source_map? ⇒ Boolean
Internal: Is the blob a generated source map?.
-
#vcr_cassette? ⇒ Boolean
Is the blob a VCR Cassette file?.
-
#xcode_file? ⇒ Boolean
Internal: Is the blob an Xcode file?.
Constructor Details
#initialize(name, data) ⇒ Generated
Internal: Initialize Generated instance
name - String filename data - String blob data
19 20 21 22 23 |
# File 'lib/linguist/generated.rb', line 19 def initialize(name, data) @name = name @extname = File.extname(name) @_data = data end |
Instance Attribute Details
#extname ⇒ Object (readonly)
Returns the value of attribute extname.
25 26 27 |
# File 'lib/linguist/generated.rb', line 25 def extname @extname end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
25 26 27 |
# File 'lib/linguist/generated.rb', line 25 def name @name end |
Class Method Details
.generated?(name, data) ⇒ Boolean
Public: Is the blob a generated file?
name - String filename data - String blob data. A block also maybe passed in for lazy
loading. This behavior is deprecated and you should always
pass in a String.
Return true or false
11 12 13 |
# File 'lib/linguist/generated.rb', line 11 def self.generated?(name, data) new(name, data).generated? end |
Instance Method Details
#compiled_coffeescript? ⇒ Boolean
Internal: Is the blob of JS generated by CoffeeScript?
CoffeeScript is meant to output JS that would be difficult to tell if it was generated or not. Look for a number of patterns output by the CS compiler.
Return true or false
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/linguist/generated.rb', line 121 def compiled_coffeescript? return false unless extname == '.js' # CoffeeScript generated by > 1.2 include a comment on the first line if lines[0] =~ /^\/\/ Generated by / return true end if lines[0] == '(function() {' && # First line is module closure opening lines[-2] == '}).call(this);' && # Second to last line closes module closure lines[-1] == '' # Last line is blank score = 0 lines.each do |line| if line =~ /var / # Underscored temp vars are likely to be Coffee score += 1 * line.gsub(/(_fn|_i|_len|_ref|_results)/).count # bind and extend functions are very Coffee specific score += 3 * line.gsub(/(__bind|__extends|__hasProp|__indexOf|__slice)/).count end end # Require a score of 3. This is fairly arbitrary. Consider # tweaking later. score >= 3 else false end end |
#compiled_cython_file? ⇒ Boolean
Internal: Is this a compiled C/C++ file from Cython?
Cython-compiled C/C++ files typically contain: /* Generated by Cython x.x.x on … */ on the first line.
Return true or false
297 298 299 300 301 |
# File 'lib/linguist/generated.rb', line 297 def compiled_cython_file? return false unless ['.c', '.cpp'].include? extname return false unless lines.count > 1 return lines[0].include?("Generated by Cython") end |
#composer_lock? ⇒ Boolean
Internal: Is the blob a generated php composer lock file?
Returns true or false.
269 270 271 |
# File 'lib/linguist/generated.rb', line 269 def composer_lock? !!name.match(/composer\.lock/) end |
#data ⇒ Object
Lazy load blob data if block was passed in.
Awful, awful stuff happening here.
Returns String data.
32 33 34 |
# File 'lib/linguist/generated.rb', line 32 def data @data ||= @_data.respond_to?(:call) ? @_data.call() : @_data end |
#generated? ⇒ Boolean
Internal: Is the blob a generated file?
Generated source code is suppressed in diffs and is ignored by language statistics.
Please add additional test coverage to ‘test/test_blob.rb#test_generated` if you make any changes.
Return true or false
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/linguist/generated.rb', line 53 def generated? xcode_file? || generated_net_designer_file? || composer_lock? || node_modules? || godeps? || generated_by_zephir? || minified_files? || source_map? || compiled_coffeescript? || generated_parser? || generated_net_docfile? || generated_postscript? || compiled_cython_file? || generated_protocol_buffer_go? || generated_protocol_buffer? || generated_jni_header? || vcr_cassette? end |
#generated_by_zephir? ⇒ Boolean
Internal: Is the blob a generated by Zephir
Returns true or false.
276 277 278 |
# File 'lib/linguist/generated.rb', line 276 def generated_by_zephir? !!name.match(/.\.zep\.(?:c|h|php)$/) end |
#generated_jni_header? ⇒ Boolean
Internal: Is the blob a C/C++ header generated by the Java JNI tool javah?
Returns true of false.
243 244 245 246 247 248 249 |
# File 'lib/linguist/generated.rb', line 243 def generated_jni_header? return false unless extname == '.h' return false unless lines.count > 2 return lines[0].include?("/* DO NOT EDIT THIS FILE - it is machine generated */") && lines[1].include?("#include <jni.h>") end |
#generated_net_designer_file? ⇒ Boolean
Internal: Is this a codegen file for a .NET project?
Visual Studio often uses code generation to generate partial classes, and these files can be quite unwieldy. Let’s hide them.
Returns true or false
178 179 180 |
# File 'lib/linguist/generated.rb', line 178 def generated_net_designer_file? name.downcase =~ /\.designer\.cs$/ end |
#generated_net_docfile? ⇒ Boolean
Internal: Is this a generated documentation file for a .NET assembly?
.NET developers often check in the XML Intellisense file along with an assembly - however, these don’t have a special extension, so we have to dig into the contents to determine if it’s a docfile. Luckily, these files are extremely structured, so recognizing them is easy.
Returns true or false
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/linguist/generated.rb', line 161 def generated_net_docfile? return false unless extname.downcase == ".xml" return false unless lines.count > 3 # .NET Docfiles always open with <doc> and their first tag is an # <assembly> tag return lines[1].include?("<doc>") && lines[2].include?("<assembly>") && lines[-2].include?("</doc>") end |
#generated_parser? ⇒ Boolean
Internal: Is the blob of JS a parser generated by PEG.js?
PEG.js-generated parsers are not meant to be consumed by humans.
Return true or false
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/linguist/generated.rb', line 187 def generated_parser? return false unless extname == '.js' # PEG.js-generated parsers include a comment near the top of the file # that marks them as such. if lines[0..4].join('') =~ /^(?:[^\/]|\/[^\*])*\/\*(?:[^\*]|\*[^\/])*Generated by PEG.js/ return true end false end |
#generated_postscript? ⇒ Boolean
Internal: Is the blob of PostScript generated?
PostScript files are often generated by other programs. If they tell us so, we can detect them.
Returns true or false.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/linguist/generated.rb', line 205 def generated_postscript? return false unless ['.ps', '.eps'].include? extname # We analyze the "%%Creator:" comment, which contains the author/generator # of the file. If there is one, it should be in one of the first few lines. creator = lines[0..9].find {|line| line =~ /^%%Creator: /} return false if creator.nil? # Most generators write their version number, while human authors' or companies' # names don't contain numbers. So look if the line contains digits. Also # look for some special cases without version numbers. return creator =~ /[0-9]/ || creator.include?("mpage") || creator.include?("draw") || creator.include?("ImageMagick") end |
#generated_protocol_buffer? ⇒ Boolean
Internal: Is the blob a C++, Java or Python source file generated by the Protocol Buffer compiler?
Returns true of false.
233 234 235 236 237 238 |
# File 'lib/linguist/generated.rb', line 233 def generated_protocol_buffer? return false unless ['.py', '.java', '.h', '.cc', '.cpp'].include?(extname) return false unless lines.count > 1 return lines[0].include?("Generated by the protocol buffer compiler. DO NOT EDIT!") end |
#generated_protocol_buffer_go? ⇒ Boolean
222 223 224 225 226 227 |
# File 'lib/linguist/generated.rb', line 222 def generated_protocol_buffer_go? return false unless extname == '.go' return false unless lines.count > 1 return lines[0].include?("Code generated by protoc-gen-go") end |
#godeps? ⇒ Boolean
Internal: Is the blob part of Godeps/, which are not meant for humans in pull requests.
Returns true or false.
262 263 264 |
# File 'lib/linguist/generated.rb', line 262 def godeps? !!name.match(/Godeps\//) end |
#lines ⇒ Object
Public: Get each line of data
Returns an Array of lines
39 40 41 42 |
# File 'lib/linguist/generated.rb', line 39 def lines # TODO: data should be required to be a String, no nils @lines ||= data ? data.split("\n", -1) : [] end |
#minified_files? ⇒ Boolean
Internal: Is the blob minified files?
Consider a file minified if the average line length is greater then 110c.
Currently, only JS and CSS files are detected by this method.
Returns true or false.
91 92 93 94 95 96 97 98 |
# File 'lib/linguist/generated.rb', line 91 def minified_files? return unless ['.js', '.css'].include? extname if lines.any? (lines.inject(0) { |n, l| n += l.length } / lines.length) > 110 else false end end |
#node_modules? ⇒ Boolean
Internal: Is the blob part of node_modules/, which are not meant for humans in pull requests.
Returns true or false.
254 255 256 |
# File 'lib/linguist/generated.rb', line 254 def node_modules? !!name.match(/node_modules\//) end |
#source_map? ⇒ Boolean
Internal: Is the blob a generated source map?
Source Maps usually have .css.map or .js.map extensions. In case they are not following the name convention, detect them based on the content.
Returns true or false.
106 107 108 109 110 111 112 |
# File 'lib/linguist/generated.rb', line 106 def source_map? return false unless extname.downcase == '.map' name =~ /(\.css|\.js)\.map$/i || # Name convention lines[0] =~ /^{"version":\d+,/ || # Revision 2 and later begin with the version number lines[0] =~ /^\/\*\* Begin line maps\. \*\*\/{/ # Revision 1 begins with a magic comment end |
#vcr_cassette? ⇒ Boolean
Is the blob a VCR Cassette file?
Returns true or false
283 284 285 286 287 288 |
# File 'lib/linguist/generated.rb', line 283 def vcr_cassette? return false unless extname == '.yml' return false unless lines.count > 2 # VCR Cassettes have "recorded_with: VCR" in the second last line. return lines[-2].include?("recorded_with: VCR") end |
#xcode_file? ⇒ Boolean
Internal: Is the blob an Xcode file?
Generated if the file extension is an Xcode file extension.
Returns true of false.
79 80 81 |
# File 'lib/linguist/generated.rb', line 79 def xcode_file? ['.nib', '.xcworkspacedata', '.xcuserstate'].include?(extname) end |