sqt 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/sqt +74 -0
  2. data/lib/sqt.rb +81 -0
  3. metadata +80 -0
data/bin/sqt ADDED
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+ #!/usr/bin/env ruby
3
+
4
+ require 'sqt'
5
+
6
+ options = {}
7
+ paths = []
8
+ filesProperties = []
9
+
10
+ # Définition des options du script
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Sarbotte Quality Tool"
13
+ opts.separator "Utilisation : rb [options]"
14
+
15
+ opts.on("-f", "--file FILE", "Fichier à sarbottiser.") do |f|
16
+ options[:file] = f || ""
17
+ end
18
+
19
+ opts.on("-p", "--path [PATH]", "Répertoire à sarbottiser.") do |p|
20
+ options[:path] = p || "."
21
+ end
22
+
23
+ opts.on("-e", "--extension [EXTENSION]", "Extension recherchée.") do |e|
24
+ options[:extension] = e || ""
25
+ end
26
+
27
+ opts.on("-c", "--curl [URL]", "Curl.") do |u|
28
+ options[:url] = u || ""
29
+ end
30
+
31
+ opts.on("-w", "--write [FILENAME]", "Écrit les résultats dans un fichier.") do |w|
32
+ options[:fileName] = w || "Sarbotte Quality.txt"
33
+ end
34
+
35
+ opts.on("-h", "--help", "Affiche l'aide.") do
36
+ puts opts
37
+ exit
38
+ end
39
+ end.parse!
40
+
41
+ # Si le chemin d'un répertoire est donné en option
42
+ if options[:path]
43
+ paths = findFiles(options[:path], options[:extension])
44
+ end
45
+
46
+ # Si le chemin vers un fichier est donné en option
47
+ if options[:file]
48
+ paths << options[:file]
49
+ end
50
+
51
+ # Si on a pu trouver des chemins de fichiers
52
+ unless paths.empty?
53
+ paths.each do |path|
54
+ file = File.read path
55
+ filesProperties << buildResult(path, file)
56
+ end
57
+ end
58
+
59
+ # Si une url est donnée en option
60
+ if options[:url]
61
+ require 'curb'
62
+ http = Curl.get(options[:url])
63
+ file = http.body_str
64
+ filesProperties << buildResult(options[:url], file)
65
+ end
66
+
67
+ # Si on a pu calculer le sqi d'un ou plusieurs fichiers
68
+ unless filesProperties.empty?
69
+ filesProperties.sort_by! { |a| a[:sqi]}
70
+ printResults(filesProperties, options)
71
+ if options[:fileName]
72
+ writeResults(filesProperties, options)
73
+ end
74
+ end
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+
3
+ # Sarbotte Quality Tool v1.0
4
+ # Sarbotte Designs
5
+ # Open Sarbotte License
6
+
7
+ require 'optparse'
8
+ require 'nokogiri'
9
+
10
+ require 'colorize'
11
+ require 'win32console'
12
+
13
+ # Trouver des fichiers en fonction d'un chemin et d'une extension
14
+ def findFiles(path, extension)
15
+ path.gsub!('\\', File::SEPARATOR)
16
+ Dir["#{path}/**/*\.#{extension}"]
17
+ end
18
+
19
+ # Récupère le nombre de caractères dans les balises script et style d'un fichier
20
+ def getJsAndCssLength(file)
21
+ xmlFile = Nokogiri::HTML(file)
22
+ jsLength = xmlFile.search('//script').reduce(0) { |total, script| total + script.text.bytesize }
23
+ jsAndCssLength = xmlFile.search('//style').reduce(jsLength) { |total, style| total + style.text.bytesize }
24
+ end
25
+
26
+ # Définit le Sarbotte Quality Index d'un fichier
27
+ def sarbotteQuality(jsAndCss, total)
28
+ (1 - jsAndCss.to_f/total.to_f) * 100
29
+ end
30
+
31
+ # Affiche en console les résultats
32
+ def printResults(filesProperties, options)
33
+ system ("cls")
34
+ puts "\nSarbotte Quality Tool\n".colorize( :cyan )
35
+ if options[:path] then
36
+ puts "Chemin : " + options[:path] + "\n\n"
37
+ end
38
+ if filesProperties.size > 1 then
39
+ average = filesProperties.reduce(0) { |total, fP| total + fP[:sqi] }.to_f / filesProperties.size
40
+ print "Moyenne : "
41
+ puts "#{'%.2f' % average}".colorize( average < 0.5 ? :red : average < 0.8 ? :yellow : :green )
42
+ puts ""
43
+ end
44
+ puts "Fichiers : "
45
+ filesProperties.each do |fP|
46
+ sqi = fP[:sqi]
47
+ print " #{fP[:uri].gsub(options[:path] && options[:path] != "." ? options[:path] : "", "")} : "
48
+ print "#{'%.2f' % fP[:sqi]}".colorize( sqi < 50 ? :red : sqi < 80 ? :yellow : :green )
49
+ puts " (#{fP[:jsAndCssLength]}/#{fP[:totalLength]})"
50
+ end
51
+ end
52
+
53
+ # Écrit dans un fichier les résultats
54
+ def writeResults(filesProperties, options)
55
+ result = "Sarbotte Quality Tool\n\n"
56
+ if options[:path] then
57
+ result += "Chemin : " + options[:path] + "\n\n"
58
+ end
59
+ if filesProperties.size > 1 then
60
+ average = filesProperties.reduce(0) { |total, fP| total + fP[:sqi] }.to_f / filesProperties.size
61
+ result += "Moyenne : #{'%.2f' % average}\n\n"
62
+ end
63
+ result += "Fichiers : \n"
64
+ filesProperties.each do |fP|
65
+ sqi = fP[:sqi]
66
+ result += " #{fP[:uri].gsub(options[:path] && options[:path] != "." ? options[:path] : "", "")} : #{'%.2f' % fP[:sqi]} (#{fP[:jsAndCssLength]}/#{fP[:totalLength]})\n"
67
+ end
68
+ File.open(options[:fileName], 'w') {|f| f.write(result) }
69
+ end
70
+
71
+ # Construit l'objet qui sera affiché, en fonction de son uri et de son contenu
72
+ def buildResult(uri, file)
73
+ fileProperties = {}
74
+ fileProperties[:uri] = uri
75
+ fileProperties[:totalLength] = file.bytesize
76
+ fileProperties[:jsAndCssLength] = getJsAndCssLength file
77
+ fileProperties[:sqi] = sarbotteQuality(fileProperties[:jsAndCssLength], fileProperties[:totalLength])
78
+ fileProperties
79
+ end
80
+
81
+
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicolas Barbotte
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &21882804 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21882804
25
+ - !ruby/object:Gem::Dependency
26
+ name: colorize
27
+ requirement: &21882504 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.5.8
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *21882504
36
+ - !ruby/object:Gem::Dependency
37
+ name: win32console
38
+ requirement: &21882216 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.2
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *21882216
47
+ description: Client side quality validator
48
+ email: n.barbotte@gmail.com
49
+ executables:
50
+ - sqt
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/sqt.rb
55
+ - bin/sqt
56
+ homepage: https://github.com/SarbotteDesigns/sqt
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.11
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Sarbotte Quality Tool
80
+ test_files: []