ADD FIX#3
[euphorik.git] / tools / tools.rb
1 #!/usr/bin/ruby
2 # coding: utf-8
3 =begin
4 Copyright 2008 Grégory Burri
5
6 This file is part of Euphorik.
7
8 Euphorik is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Euphorik is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
20 =end
21
22 #TODO :
23 # - mettre à jour les numéros de versions (créer une classe)
24 # - création de unit tests (voir eunit) et validation avant la mise en prod
25 # - faire une classe qui vérifie tous les js avec jslint
26
27 # Classe permettant la vérification du code JS pas jslint.
28 # Passe en revu chaque fichier js de manière récursive à partir d'un dossier de départ.s
29 class VerifJS
30
31 def initialize(dossier)
32 @dossier = dossier
33 end
34
35 def verifier
36 verifierRecu(@dossier)
37 end
38
39 def verifierRecu(dossier)
40 Dir.foreach(dossier){|fichier|
41 if fichier != '.' and fichier != '..' and File.directory?(fichier) and fichier != 'dirs'
42 if not verifierRecu(dossier + '/' + fichier)
43 return false
44 end
45 elsif fichier[-3, 3] == '.js'
46 puts "== Vérification de #{dossier}/#{fichier} =="
47 system("java org.mozilla.javascript.tools.shell.Main jslint.js #{dossier}/#{fichier}")
48 if $?.exitstatus > 0
49 return false
50 end
51 end
52 }
53 return true
54 end
55 end
56
57 # Classe de gestion de la version
58 class Version
59 # @param dossier la racine du site (par exemple "/var/www/euphorik"
60 def initialize(dossier)
61 @dossier = dossier
62 File.open(@dossier + '/VERSION') {|file|
63 @version = file.readline()
64 }
65 # les fichiers HTML dans lesquels mettre à jour la version
66 @fichiers = ['/pages/about.html']
67 @balise = /(<span.+?class.*?=.*?"version".*?>).*?(<\/span>)/
68 end
69
70 # met à jour la version dans les fichiers @fichiers
71 def maj
72 @fichiers.each{|fichier|
73 fichier = @dossier + fichier
74 lines = IO.readlines(fichier)
75 File.open(fichier, 'w') {|io|
76 lines.each{|l|
77 io.write(l.sub(@balise){|m| $1 + @version + $2})
78 }
79 }
80 }
81 end
82 end
83
84
85
86 class MiseEnProd
87 @@rep_remote = '/var/www/euphorik'
88 @@host = 'euphorik.ch'
89 @@opt_rsync = ''
90
91 def initialize
92 end
93
94 def creer_remote_rep(rep)
95 begin
96 `ssh #{@@host} "mkdir #{@@rep_remote}/#{rep}"`
97 rescue
98 end
99 end
100
101 def compiler_partie_serveuse
102 Dir.chdir(@@rep_remote + '/modules')
103 puts `make`
104 if $?.exitstatus != 0
105 puts "Echec de compilation de la partie serveuse"
106 exit 1
107 end
108 Dir.chdir('..')
109 end
110
111 def creer_repertoire_bd
112 # création du repertoire BD
113 creer_remote_rep('BD')
114 creer_remote_rep('BD/backup')
115 `ssh #{@@host} "chmod g+w #{@@rep_remote}/BD"`
116 end
117
118 # css, images, html, etc..
119 def copier_partie_statique
120 print `rsync #{$opt_rsync} index.yaws #{$host}:#{$rep_remote}`
121 print `rsync #{$opt_rsync} favicon.ico #{$host}:#{$rep_remote}`
122 print `rsync #{$opt_rsync} -r css #{$host}:#{$rep_remote}`
123 print `rsync #{$opt_rsync} -r pages #{$host}:#{$rep_remote}`
124 print `rsync #{$opt_rsync} -r --exclude 'autres' img #{$host}:#{$rep_remote}`
125 end
126
127 # minification et package des fichiers js dans euphorik.js
128 def pack_js
129 # copie des js avec minification
130 rep_js = 'js'
131 creer_remote_rep(rep_js)
132 Dir.entries(rep_js).each{|fichier|
133 if fichier[0..0] != '.' and fichier != 'debug.js'
134 puts "Minimisation et copie de #{fichier}"
135 print `tools/jsmin.rb < #{rep_js}/#{fichier} | ssh #{@@host} "cat > #{@@rep_remote}/#{rep_js}/#{fichier}"`
136 end
137 }
138 end
139
140 def copie_modules_serveurs
141 # copie des modules erlang
142 creer_remote_rep('modules')
143 `rsync #{@@opt_rsync} -r --exclude 'euphorik_test.beam' modules/ebin #{@@host}:#{@@rep_remote}/modules`
144 `rsync #{@@opt_rsync} -r modules/include #{@@host}:#{@@rep_remote}/modules`
145 end
146
147 def set_droits_fichiers
148 # attribution des droits
149 `ssh #{$host} "chmod -R g+rx #{$rep_remote}"`
150 end
151
152 def maj
153 # execution du script de mise à jour
154 print `cat tools/mise_en_prod.erl | ssh #{$host} "cat > /tmp/mise_en_prod.erl"`
155 print `ssh #{$host} "chmod u+x /tmp/mise_en_prod.erl; /tmp/mise_en_prod.erl; rm /tmp/mise_en_prod.erl"`
156 end
157 end
158
159
160 #verifJS = VerifJS.new("../js")
161 #verifJS.verifier()
162
163 #version = Version.new("..")
164 #version.maj()