PageRenderTime 76ms CodeModel.GetById 48ms RepoModel.GetById 1ms app.codeStats 0ms

/Rakefile

https://github.com/shenfeng/rssminer
Rakefile | 309 lines | 258 code | 42 blank | 9 comment | 8 complexity | 6089d3a01b963134a15bde6ebdf02d93 MD5 | raw file
  1. task :default => :all_test
  2. version = Time.now.strftime("%Y%m%d%H%M") # timestamp
  3. jscompiler = "closure-compiler.jar"
  4. htmlcompressor = "htmlcompressor.jar"
  5. luke = "lukeall-3.5.0.jar"
  6. def get_file_as_string(filename)
  7. return open(filename, 'r'){|f| f.read }
  8. end
  9. def get_dir(path)
  10. File.split(path)[0]
  11. end
  12. def gen_jstempls(folder)
  13. print "Generating #{folder}-tmpls.js, please wait....\n"
  14. html_tmpls = FileList["src/templates/tmpls/#{folder}/**/*.*"]
  15. data = "(function(){var tmpls = {};"
  16. html_tmpls.each do |f|
  17. text = get_file_as_string(f).gsub(/\s+/," ")
  18. name = File.basename(f, ".tpl")
  19. data += "tmpls." + name + " = '" + text + "';\n"
  20. end
  21. data += "window.RM = {tmpls: tmpls};})();\n"
  22. File.open("public/js/gen/#{folder}-tmpls.js", 'w') {|f| f.write(data)}
  23. end
  24. def minify_js(target, jss)
  25. jscompiler = "closure-compiler.jar"
  26. source_arg = ''
  27. jss.each do |js|
  28. source_arg += " --js #{js} "
  29. end
  30. # ADVANCED_OPTIMIZATIONS SIMPLE_OPTIMIZATIONS
  31. sh "java -jar thirdparty/#{jscompiler} --warning_level QUIET " +
  32. "--compilation_level SIMPLE_OPTIMIZATIONS " +
  33. "--js_output_file '#{target}' #{source_arg}"
  34. end
  35. file "thirdparty/#{jscompiler}" do
  36. mkdir_p "thirdparty"
  37. sh 'wget http://closure-compiler.googlecode.com/files/compiler-latest.zip' +
  38. ' -O /tmp/closure-compiler.zip'
  39. rm_rf '/tmp/compiler.jar'
  40. sh 'unzip /tmp/closure-compiler.zip compiler.jar -d /tmp'
  41. rm_rf '/tmp/closure-compiler.zip'
  42. mv '/tmp/compiler.jar', "thirdparty/#{jscompiler}"
  43. end
  44. file "thirdparty/#{htmlcompressor}" do
  45. mkdir_p "thirdparty"
  46. sh 'wget http://htmlcompressor.googlecode.com/files/htmlcompressor-1.5.3.jar' +
  47. " -O thirdparty/#{htmlcompressor}"
  48. end
  49. file "thirdparty/#{luke}" do
  50. mkdir_p "thirdparty"
  51. sh "wget http://luke.googlecode.com/files/#{luke} " +
  52. "-O thirdparty/#{luke}"
  53. end
  54. task :deps => ["thirdparty/#{jscompiler}",
  55. "thirdparty/#{luke}",
  56. "thirdparty/#{htmlcompressor}"]
  57. landing_jss = FileList['public/js/rssminer/landing.js']
  58. app_jss = FileList['public/js/lib/jquery-ui-1.8.18.custom.js',
  59. 'public/js/lib/underscore.js',
  60. 'public/js/lib/mustache.js',
  61. 'public/js/rssminer/i18n.js',
  62. 'public/js/gen/app-tmpls.js',
  63. 'public/js/rssminer/util.js',
  64. 'public/js/rssminer/placeholder.js',
  65. 'public/js/rssminer/ajax.js',
  66. 'public/js/rssminer/router.js',
  67. 'public/js/rssminer/layout.js',
  68. 'public/js/rssminer/rm_data.js',
  69. 'public/js/rssminer/search.js',
  70. 'public/js/rssminer/ct_menu.js',
  71. 'public/js/rssminer/keyboard.js',
  72. 'public/js/rssminer/tooltip.js',
  73. 'public/js/rssminer/app.js']
  74. desc "Clean generated files"
  75. task :clean do
  76. rm_rf 'public/js/rssminer/tmpls.js'
  77. rm_rf 'src/templates'
  78. rm_rf 'public/rssminer.crx'
  79. rm_rf 'public/js/gen'
  80. rm_rf "public/css"
  81. rm_rf "classes"
  82. sh 'rm -vf public/js/*min.js'
  83. # sh 'find . -name "*.class" | xargs rm'
  84. end
  85. desc "Prepare for development"
  86. task :prepare => [:css_compile,:html_compress, "js:tmpls"]
  87. desc "Prepare for production"
  88. task :prepare_prod => [:css_compile, "js:minify"]
  89. desc "lein swank"
  90. task :swank => [:javac, :prepare] do
  91. sh "lein swank"
  92. end
  93. desc "lein repl"
  94. task :repl => [:javac, :prepare] do
  95. sh "lein repl"
  96. end
  97. desc 'Deploy to production'
  98. task :deploy => [:clean, :chrome, :test, :prepare_prod] do
  99. sh "scripts/deploy"
  100. end
  101. desc "Javac"
  102. task :javac do
  103. sh "scripts/javac"
  104. end
  105. desc "Javac debug"
  106. task :javac_debug do
  107. sh "scripts/javac with-test"
  108. end
  109. desc "Run junit test"
  110. task :junit => [:javac_debug] do
  111. sh './scripts/junit_test'
  112. end
  113. desc "Run all test"
  114. task :all_test => [:test, :junit]
  115. desc "Run lein unit test"
  116. task :test => [:prepare, :javac_debug] do
  117. sh 'lein test'
  118. end
  119. desc "Generate TAGS using etags for clj"
  120. task :etags do
  121. rm_rf 'TAGS'
  122. sh %q"find . \! -name '.*' -name '*.clj' | xargs etags --regex='/[ \t\(]*def[a-z]* \([a-z-!0-9?]+\)/\1/'"
  123. end
  124. namespace :js do
  125. desc "Generate template js resouces"
  126. task :tmpls => :html_compress do
  127. mkdir_p "public/js/gen"
  128. gen_jstempls("app");
  129. gen_jstempls("chrome");
  130. # sh 'mv public/js/gen/chrome-tmpls.js chrome/tmpls.js'
  131. end
  132. desc 'Combine all js into one, minify it using google closure'
  133. task :minify => [:tmpls, :deps] do
  134. minify_js("public/js/app-min.js", app_jss);
  135. minify_js("public/js/landing-min.js", landing_jss);
  136. end
  137. end
  138. scss = FileList['scss/**/*.scss'].exclude('scss/**/_*.scss')
  139. desc 'Compile scss, compress generated css'
  140. task :css_compile do
  141. mkdir_p "public/css"
  142. scss.each do |source|
  143. target = source.sub(/scss$/, 'css').sub(/^scss/, 'public/css')
  144. sh "sass -t compressed --cache-location /tmp #{source} #{target}"
  145. end
  146. sh "find public/css/ -type f " +
  147. "| xargs -I {} sed -i -e \"s/{VERSION}/#{version}/g\" {}"
  148. # os x sed will generate many file end with -e
  149. sh "find public/css -type f -name \"*-e\" | xargs rm -f"
  150. sh 'mv public/css/chrome.css chrome/style.css'
  151. end
  152. desc "create chrome extension"
  153. task :chrome do
  154. # sh 'google-chrome --pack-extension=chrome ' + '--pack-extension-key=conf/chrome.pem'
  155. # sh 'mv chrome.crx public/rssminer.crx'
  156. end
  157. desc "update dev mysql config file, restart mysql"
  158. task :mysql_dev do
  159. sh 'sudo /etc/init.d/mysql stop'
  160. sh 'sudo rm /tmp/mysql -rf && sudo rm /tmp/mysql.log -f'
  161. sh 'mkdir /tmp/mysql'
  162. sh 'sudo cp conf/my-dev.cnf /etc/mysql/my.cnf && sudo mysql_install_db'
  163. # sh 'sudo chown mysql:mysql /tmp/mysql -R'
  164. sh 'sudo /etc/init.d/mysql start'
  165. sh './scripts/admin restore-db'
  166. end
  167. desc "update dev mysql config file, restart mysql"
  168. task :mysql_prod do
  169. sh 'sudo /etc/init.d/mysql stop'
  170. sh 'sudo cp conf/my.cnf /etc/mysql/my.cnf'
  171. sh 'sudo /etc/init.d/mysql start'
  172. # sh 'rake db:restore_db'
  173. end
  174. desc 'Compress html using htmlcompressor, save compressed to src/templates'
  175. task :html_compress => :deps do
  176. sh "scripts/compress_html"
  177. end
  178. desc "Using luke to inspect luence index"
  179. task :luke do
  180. sh "java -jar thirdparty/#{luke} -index /var/rssminer/index &"
  181. end
  182. desc "Rebuild index"
  183. task :rebuild_index => :javac do
  184. sh './scripts/admin rebuild-index'
  185. end
  186. namespace :db do
  187. desc "Reload database with production data"
  188. task :backup_prod => :javac do
  189. sh './scripts/admin backup-db && ./scripts/admin restore-db && ./scripts/admin rebuild-index'
  190. end
  191. desc "Restore db from latest backup"
  192. task :restore_db => :javac do
  193. sh './scripts/admin restore-db && ./scripts/admin rebuild-index'
  194. end
  195. end
  196. namespace :run do
  197. desc "Run server in dev profile"
  198. task :dev => [:prepare, :javac] do
  199. sh 'scripts/run --profile dev'
  200. end
  201. desc "Compile and run"
  202. task :aot do
  203. sh 'scripts/aot_run'
  204. end
  205. desc "Run server in production profile"
  206. task :prod => [:prepare_prod, :javac] do
  207. sh 'scripts/run --profile prod --static-server //192.168.1.200:9090'
  208. end
  209. desc "Restore db from latest backup, Run server in dev profile"
  210. task :restore_db_dev => ["db:restore_db", "run:dev"]
  211. end
  212. def get_mtime(patten)
  213. mtime_total = 0
  214. FileList[patten].each do |f|
  215. mtime_total += File.mtime(f).to_i
  216. end
  217. return mtime_total
  218. end
  219. def watch_change(patten, cb)
  220. t = Thread.new do
  221. mtime_total = get_mtime(patten)
  222. while true do
  223. sleep 0.1 # sleep 100ms
  224. n = get_mtime(patten)
  225. # puts n
  226. if n != mtime_total
  227. cb.call()
  228. mtime_total = n
  229. end
  230. end
  231. end
  232. return t
  233. end
  234. def has_inotify()
  235. begin
  236. sh "which inotifywait"
  237. return true
  238. rescue
  239. return false
  240. end
  241. end
  242. desc 'Watch css, html'
  243. task :watch => [:deps, :css_compile, "js:tmpls"] do
  244. sh 'http-watcher -root ~/workspace/rssminer -ignores "test/,/\.,\.css$,.#,src/templates,target/,public/,android/" -proxy 9090 -command ./scripts/preprocess'
  245. if has_inotify
  246. t1 = Thread.new {
  247. sh 'while inotifywait -r -e modify scss/; do rake css_compile reload; done'
  248. }
  249. t2 = Thread.new {
  250. sh 'while inotifywait -r -e modify templates/; do rake js:tmpls reload; done'
  251. }
  252. t3 = Thread.new {
  253. sh 'while inotifywait -r -e modify public/js/rssminer; do rake reload; done'
  254. }
  255. elsif
  256. t1 = watch_change('scss/**/*.*', lambda {sh 'rake css_compile reload'})
  257. t2 = watch_change('templates/**/*.*', lambda {sh 'rake js:tmpls reload'})
  258. t2 = watch_change('public/js/rssminer/**/*.*', lambda {sh 'rake reload'})
  259. end
  260. trap(:INT) {
  261. sh "killall inotifywait || exit 0"
  262. exit
  263. }
  264. t1.join
  265. t2.join
  266. t3.join
  267. end