sqt 1.0.1 → 1.1.0

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 +64 -74
  2. data/lib/sqt.rb +103 -81
  3. metadata +42 -11
data/bin/sqt CHANGED
@@ -1,74 +1,64 @@
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
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # Sarbotte Quality Tool v1.0
5
+ # Copyright Sarbotte Designs
6
+ # Open Sarbotte License
7
+
8
+ require 'sqt'
9
+
10
+ options = {}
11
+ filesProperties = []
12
+
13
+ # Montre le messge d'aide si aucun argument n'est passé
14
+ ARGV.push "-h" if ARGV.empty?
15
+
16
+ # Définition des options du script
17
+ OptionParser.new do |opts|
18
+ opts.banner = "Sarbotte Quality Tool"
19
+ opts.separator "Utilisation : rb [options]"
20
+
21
+ opts.on("-f", "--file FILE", "Fichier à sarbottiser.") do |f|
22
+ options[:file] = f || ""
23
+ end
24
+
25
+ opts.on("-p", "--path [PATH]", "Répertoire à sarbottiser.") do |p|
26
+ options[:path] = p || "."
27
+ end
28
+
29
+ opts.on("-e", "--extension [EXTENSION]", "Extension recherchée.") do |e|
30
+ options[:extension] = e || ""
31
+ end
32
+
33
+ opts.on("-c", "--curl [URL]", "Curl.") do |u|
34
+ options[:url] = u || ""
35
+ end
36
+
37
+ opts.on("-w", "--write [FILENAME]", "Écrit les résultats dans un fichier.") do |w|
38
+ options[:fileName] = w || "Sarbotte Quality.txt"
39
+ end
40
+
41
+ opts.on("-h", "--help", "Affiche l'aide.") do
42
+ puts opts
43
+ exit
44
+ end
45
+ end.parse!
46
+
47
+ # Si le chemin d'un répertoire est donné en option
48
+ if options[:path]
49
+ filesProperties = SQT.sarbottePath(options[:path], options[:extension])
50
+ # Si le chemin vers un fichier est donné en option
51
+ elsif options[:file]
52
+ filesProperties.push SQT.sarbotteFile(options[:file])
53
+ # Si une url est donnée en option
54
+ elsif options[:url]
55
+ filesProperties.push SQT.sarbotteCurl(options[:url])
56
+ end
57
+
58
+ # Si on a pu calculer le sqi d'un ou plusieurs fichiers
59
+ unless filesProperties.empty?
60
+ filesProperties.sort_by! { |a| a[:sqi] }
61
+ SQT.sarbottePrint(filesProperties, options)
62
+ SQT.sarbotteWrite(filesProperties, options) if options[:fileName]
63
+ end
64
+
data/lib/sqt.rb CHANGED
@@ -1,81 +1,103 @@
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
-
1
+ # encoding: utf-8
2
+
3
+ # Sarbotte Quality Tool v1.0
4
+ # Copyright Sarbotte Designs
5
+ # Open Sarbotte License
6
+
7
+ module SQT
8
+
9
+ require 'optparse'
10
+ require 'nokogiri'
11
+
12
+ require 'colorize'
13
+
14
+ class Sqt
15
+
16
+ # Construit l'objet qui sera affiché, en fonction de son uri et de son contenu
17
+ def self.buildResult(uri, file)
18
+ fileProperties = {}
19
+ fileProperties[:uri] = uri
20
+ fileProperties[:totalLength] = file.bytesize
21
+ fileProperties[:jsAndCssLength] = getJsAndCssLength file
22
+ fileProperties[:sqi] = sarbotteQuality(fileProperties[:jsAndCssLength], fileProperties[:totalLength])
23
+ fileProperties
24
+ end
25
+
26
+ # Récupère le nombre de caractères dans les balises script et style d'un fichier
27
+ def self.getJsAndCssLength(file)
28
+ xmlFile = Nokogiri::HTML(file)
29
+ jsLength = xmlFile.search('//script').reduce(0) { |total, script| total + script.text.bytesize }
30
+ xmlFile.search('//style').reduce(jsLength) { |total, style| total + style.text.bytesize }
31
+ end
32
+
33
+ # Définit le Sarbotte Quality Index d'un fichier
34
+ def self.sarbotteQuality(jsAndCss, total)
35
+ (1 - jsAndCss.to_f/total.to_f) * 100
36
+ end
37
+
38
+ end
39
+
40
+ def self.sarbottePath(path, extension)
41
+ path.gsub!('\\', File::SEPARATOR)
42
+ Dir["#{path}/**/*\.#{extension}"].map{ |p| Sqt.buildResult(p, File.read(p))}
43
+ end
44
+
45
+ def self.sarbotteFile(file)
46
+ Sqt.buildResult(file, File.read(file))
47
+ end
48
+
49
+ def self.sarbotteString(string)
50
+ Sqt.buildResult("", string)
51
+ end
52
+
53
+ def self.sarbotteCurl(url)
54
+ require 'curb'
55
+ http = Curl.get(url)
56
+ file = http.body_str
57
+ Sqt.buildResult(url, file)
58
+ end
59
+
60
+ # Affiche en console les résultats
61
+ def self.sarbottePrint(filesProperties, options)
62
+ system("cls")
63
+ puts "\nSarbotte Quality Tool\n".colorize( :cyan )
64
+ if options[:path] then
65
+ puts "Chemin : " + options[:path] + "\n\n"
66
+ end
67
+ if filesProperties.size > 1 then
68
+ average = filesProperties.reduce(0) { |total, fP| total + fP[:sqi] }.to_f / filesProperties.size
69
+ print "Moyenne : "
70
+ puts "#{'%.2f' % average}".colorize( average < 0.5 ? :red : average < 0.8 ? :yellow : :green )
71
+ puts ""
72
+ end
73
+ puts "Fichiers : "
74
+ filesProperties.each do |fP|
75
+ sqi = fP[:sqi]
76
+ print " #{fP[:uri].gsub(options[:path] && options[:path] != "." ? options[:path] : "", "")} : "
77
+ print "#{'%.2f' % fP[:sqi]}".colorize( sqi < 50 ? :red : sqi < 80 ? :yellow : :green )
78
+ puts " (#{fP[:jsAndCssLength]}/#{fP[:totalLength]})"
79
+ end
80
+ end
81
+
82
+ # Écrit dans un fichier les résultats
83
+ def self.sarbotteWrite(filesProperties, options)
84
+ result = "Sarbotte Quality Tool\n\n"
85
+ if options[:path] then
86
+ result += "Chemin : " + options[:path] + "\n\n"
87
+ end
88
+ if filesProperties.size > 1 then
89
+ average = filesProperties.reduce(0) { |total, fP| total + fP[:sqi] }.to_f / filesProperties.size
90
+ result += "Moyenne : "
91
+ result += "#{'%.2f' % average}\n\n"
92
+ end
93
+ result += "Fichiers : \n"
94
+ filesProperties.each do |fP|
95
+ sqi = fP[:sqi]
96
+ result += " #{fP[:uri].gsub(options[:path] && options[:path] != "." ? options[:path] : "", "")} : "
97
+ result += "#{'%.2f' % fP[:sqi]}"
98
+ result += "(#{fP[:jsAndCssLength]}/#{fP[:totalLength]})\n"
99
+ end
100
+ File.open(options[:fileName], 'w') {|f| f.write(result) }
101
+ end
102
+
103
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &21882804 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 1.5.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21882804
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.6
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: colorize
27
- requirement: &21882504 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,18 +37,44 @@ dependencies:
32
37
  version: 0.5.8
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *21882504
36
- - !ruby/object:Gem::Dependency
37
- name: win32console
38
- requirement: &21882216 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
39
41
  none: false
40
42
  requirements:
41
43
  - - ~>
42
44
  - !ruby/object:Gem::Version
43
- version: 1.3.2
45
+ version: 0.5.8
46
+ - !ruby/object:Gem::Dependency
47
+ name: curb
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *21882216
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
47
78
  description: Client side quality validator
48
79
  email: n.barbotte@gmail.com
49
80
  executables:
@@ -73,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
104
  version: '0'
74
105
  requirements: []
75
106
  rubyforge_project:
76
- rubygems_version: 1.8.11
107
+ rubygems_version: 1.8.23
77
108
  signing_key:
78
109
  specification_version: 3
79
110
  summary: Sarbotte Quality Tool