dev 2.0.46 → 2.0.47

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dev_commands.rb CHANGED
@@ -1,3 +1,1066 @@
1
+ require 'open3'
2
+ #require_relative('./array.rb')
3
+ #require_relative('./hash.rb')
4
+ #require_relative('./timer.rb')
5
+
6
+ BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
7
+
8
+ # = Command
9
+ #
10
+ # execution of system commands
11
+ #
12
+ # = Keys
13
+ #
14
+ # - :input The input of the commands.
15
+ # - :timeout The timeout in seconds.
16
+ # a value of zero is to infinite timeout.
17
+ # defaults to zero
18
+ # - :directory The working directory for the command.
19
+ # defaults to the current directory
20
+ # - :exit_code The exit code of the command
21
+ # - :output The output contains the stdout output of the command
22
+ # - :error The error contains stderr output of the command
23
+ # - :machine The name of the machine the command executed on
24
+ # - :user The user name
25
+ # - :start_time
26
+ # - :end_time
27
+ #
28
+ class Command < Hash
29
+ def initialize command
30
+
31
+ self[:input] = ''
32
+ self[:timeout] = 0
33
+ self[:directory] = ''
34
+ self[:exit_code] = 0
35
+ self[:output] = ''
36
+ self[:error] = ''
37
+ self[:machine] = ''
38
+ self[:user] = ''
39
+ self[:start_time] = nil
40
+ self[:end_time] = nil
41
+
42
+ if(command.kind_of?(String))
43
+ self[:input] = command
44
+ end
45
+
46
+ if(command.kind_of?(Hash))
47
+ command.each{|k,v|
48
+ self[k.to_sym]=v
49
+ }
50
+ end
51
+ end
52
+
53
+ def quiet?
54
+ (self.has_key?(:quiet) && self[:quiet])
55
+ end
56
+
57
+ def execute value=nil
58
+
59
+ if(!value.nil? && value.is_a?(Hash))
60
+ value.each{|k,v|self[k]=v}
61
+ end
62
+
63
+ pwd=Dir.pwd
64
+ self[:directory] = pwd if(!self.has_key?(:directory) || self[:directory].length==0)
65
+
66
+ if(self[:timeout] > 0)
67
+ puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?)
68
+ else
69
+ puts "#{self[:input]} (#{self[:directory]})" if(!quiet?)
70
+ end
71
+
72
+ self[:machine] = Command.machine
73
+ self[:user] = Command.user
74
+
75
+ self[:start_time]=Time.now
76
+ timer=Timer.new
77
+
78
+ Dir.chdir(self[:directory]) do
79
+ if self[:input].include?('<%') && self[:input].include?('%>')
80
+ ruby = self[:input].gsub("<%","").gsub("%>","")
81
+
82
+ begin
83
+ self[:output]=eval(ruby)
84
+ rescue
85
+ self[:exit_code]=1
86
+ self[:error]="unable to eval(#{ruby})"
87
+ end
88
+
89
+ self[:elapsed] = timer.elapsed_str
90
+ self[:end_time] = Time.now
91
+ else
92
+ begin
93
+ if(self[:timeout] <= 0)
94
+ self[:output],self[:error],status= Open3.capture3(self[:input])
95
+ self[:exit_code]=status.to_i
96
+ self[:elapsed] = timer.elapsed_str
97
+ self[:end_time] = Time.now
98
+ else
99
+ require_relative 'timeout.rb'
100
+ #puts run_with_timeout(self[:input], self[:timeout], 1).to_s
101
+ #self[:output] = run_with_timeout(self[:input], self[:timeout], 1)
102
+ result=run_with_timeout2(self[:directory],self[:input], self[:timeout], 2)
103
+ self[:output]=result[0]
104
+ self[:error]=result[1]
105
+ self[:exit_code]=result[2]
106
+
107
+ self[:elapsed] = timer.elapsed_str
108
+ self[:end_time] = Time.now
109
+
110
+ if(timer.elapsed >= self[:timeout])
111
+ self[:exit_code]=1
112
+ self[:error] = self[:error] + "timed out"
113
+ end
114
+ end
115
+ rescue Exception => e
116
+ self[:elapsed] = timer.elapsed_str
117
+ self[:end_time] = Time.now
118
+ self[:error] = "Exception: " + e.to_s
119
+ self[:exit_code]=1
120
+ end
121
+ end
122
+ end
123
+
124
+
125
+ if(self[:exit_code] != 0)
126
+ if(!quiet?)
127
+ puts "exit_code=#{self[:exit_code]}"
128
+ puts self[:output]
129
+ puts self[:error]
130
+ end
131
+ if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
132
+ raise "#{self[:input]} failed"
133
+ end #unless (self.has_key?(:ignore_failure) && self[:ignore_failure]==true)
134
+ end
135
+
136
+ end
137
+
138
+ def self.machine
139
+ if !ENV['COMPUTERNAME'].nil?
140
+ return ENV['COMPUTERNAME']
141
+ end
142
+
143
+ machine = `hostname`
144
+ machine = machine.split('.')[0] if machine.include?('.')
145
+ return machine.strip
146
+ end
147
+
148
+ def self.user
149
+ ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER']
150
+ end
151
+
152
+ def self.home
153
+ ["USERPROFILE","HOME"].each {|v|
154
+ return ENV[v].gsub('\\','/') unless ENV[v].nil?
155
+ }
156
+ dir="~"
157
+ dir=ENV["HOME"] unless ENV["HOME"].nil?
158
+ dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil?
159
+ return dir
160
+ end
161
+
162
+ def self.dev_root
163
+ ["DEV_HOME","DEV_ROOT"].each {|v|
164
+ return ENV[v].gsub('\\','/') unless ENV[v].nil?
165
+ }
166
+ dir=home
167
+ #dir=ENV["DEV_ROOT"].gsub('\\','/') unless ENV["DEV_ROOT"].nil?
168
+ return dir
169
+ end
170
+
171
+ def self.exit_code command
172
+ cmd = Command.new(command)
173
+ cmd[:ignore_failure]=true
174
+ cmd[:quiet]=true
175
+ cmd.execute
176
+ cmd[:exit_code]
177
+ end
178
+
179
+ def self.output command
180
+ cmd = Command.new(command)
181
+ cmd[:ignore_failure]=true
182
+ cmd[:quiet]=true
183
+ cmd.execute
184
+ cmd[:output]
185
+ end
186
+
187
+ def getFormattedTimeSpan timespan
188
+ seconds = timespan
189
+ seconds.to_s + " sec"
190
+ end
191
+
192
+ def summary
193
+ duration=""
194
+ duration=getFormattedTimeSpan(self[:end_time]-[:start_time]) + " - " if(!self[:end_time].nil?)
195
+ duration + "#{self[:exit_code].to_s} #{self[:input]} (#{self[:directory]})"
196
+ end
197
+
198
+ def to_html
199
+ if self[:exit_code] == 0
200
+ [
201
+ '<div><table><tr><td width="20"></td><td><pre>',
202
+ self[:input],
203
+ '</pre></td></tr></table></div>'
204
+ ].join
205
+ else
206
+ [
207
+ '<div><table><tr><td width="20"></td><td><pre>',
208
+ self[:input],
209
+ '</pre><table><tr><td width="20"></td><td><table>',
210
+ map { |k, v| ["<tr><td><strong>#{k}</strong></td>", v.respond_to?(:to_html) ? v.to_html : "<td><span><pre>#{v}</pre></span></td></tr>"] },
211
+ '</table>',
212
+ '</td></tr></table></td></tr></table></div>'
213
+ ].join
214
+ end
215
+ end
216
+ end
217
+ class Add < Array
218
+ def update
219
+ if(File.exists?('.git') && File.exists?('.gitignore'))
220
+ add 'git add --all'
221
+ else
222
+ if(defined?(SOURCE))
223
+ if(File.exists?('.svn'))
224
+ SOURCE.each{|f|
225
+
226
+ add "svn add #{f} --parents" if Command.output("svn status #{f}").include?('?')
227
+ add "svn add #{f} --parents" if Command.exit_code("svn status #{f}") != 0
228
+ }
229
+ end
230
+ if(File.exists?('.git'))
231
+ SOURCE.each{|f|
232
+ add "git add #{f} -v" if `git status #{f}`.include?('untracked')
233
+ }
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end
239
+ class Analyze < Array
240
+ def update
241
+ if(`gem list countloc`.include?('countloc ('))
242
+ FileUtils.mkdir('doc') if(!File.exists?('doc'))
243
+ add 'countloc -r * --html doc/countloc.html'
244
+ end
245
+ end
246
+ end
247
+ class Array
248
+ def execute value=nil
249
+ i=0
250
+ while i < self.length
251
+ self[i]=Command.new(self[i]) if(self[i].is_a?(String))
252
+ self[i]=Command.new(self[i]) if(self[i].is_a?(Hash) && !self[i].is_a?(Command))
253
+
254
+ if(!value.nil? && value.is_a?(Hash))
255
+ value.each{|k,v|self[i][k]=v}
256
+ end
257
+
258
+ self[i].execute if(self[i].is_a?(Command))
259
+ i=i+1
260
+ end
261
+ end
262
+
263
+ def add command
264
+ self << command if(!include?(command))
265
+ end
266
+
267
+ def to_html
268
+ html=Array.new
269
+ html << '<div>'
270
+ self.each{|e|
271
+ html << e.to_html if e.respond_to?(:to_html)
272
+ }
273
+ html << '</div>'
274
+ html.join
275
+ end
276
+ end
277
+ #require_relative('msbuild.rb')
278
+ #require_relative('gemspec.rb')
279
+ require 'rake'
280
+
281
+ SLN_FILES=FileList.new('*.sln','*/*.sln','*/*/*.sln')
282
+
283
+ class Build < Array
284
+ def update
285
+
286
+ changed = true
287
+ #changed = Git.has_changes? if(File.exists?('.git') && defined?(Git))
288
+ #changed = Svn.has_changes? if(File.exists?('.svn') && defined?(Svn))
289
+ if(changed)
290
+ Dir.glob('*.gemspec'){|gemspec|
291
+ add "gem build #{gemspec}" if !File.exist?(Gemspec.gemfile gemspec)
292
+ }
293
+
294
+ SLN_FILES.each{|sln_file|
295
+ vs_version=MSBuild.get_vs_version(sln_file)
296
+ if(MSBuild.has_version?(vs_version))
297
+ MSBuild.get_configurations(sln_file).each{ |configuration|
298
+ MSBuild.get_platforms(sln_file).each{|platform|
299
+ #Console.debug "configuration='#{configuration}', platform='#{platform}'"
300
+ self.add "\"#{MSBuild.get_version(vs_version)}\" \"#{sln_file}\" /nologo /p:Configuration=#{configuration} /p:Platform=\"#{platform}\""
301
+ }
302
+ }
303
+ else
304
+ "puts version #{vs_version} not found for MsBuild"
305
+ end
306
+ }
307
+ end
308
+ end
309
+ end
310
+ class Clean < Array
311
+ def update
312
+ ['.yardoc','log','tmp','obj'].each{|dir|
313
+ CLEAN.include(dir) if File.exists?(dir)
314
+ }
315
+
316
+ CLEAN.include('*.{suo,sdf}')
317
+
318
+ #add '<%Rake::Task[:clean].reenable%>'
319
+ add '<%Rake::Task[:clean].invoke%>'
320
+ end
321
+ end
322
+ class Clobber < Array
323
+ def update
324
+ ['bin'].each{|dir|
325
+ CLOBBER.include(dir) if File.exists?(dir)
326
+ }
327
+
328
+ clean=Clean.new
329
+ clean.update
330
+
331
+ CLOBBER.include('*.gem')
332
+
333
+ #add '<%Rake::Task[:clobber].reenable%>'
334
+ add '<%Rake::Task[:clobber].invoke%>'
335
+ end
336
+ end
337
+ require 'open3'
338
+ #require_relative('./array.rb')
339
+ #require_relative('./hash.rb')
340
+ #require_relative('./timer.rb')
341
+
342
+ BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
343
+
344
+ # = Command
345
+ #
346
+ # execution of system commands
347
+ #
348
+ # = Keys
349
+ #
350
+ # - :input The input of the commands.
351
+ # - :timeout The timeout in seconds.
352
+ # a value of zero is to infinite timeout.
353
+ # defaults to zero
354
+ # - :directory The working directory for the command.
355
+ # defaults to the current directory
356
+ # - :exit_code The exit code of the command
357
+ # - :output The output contains the stdout output of the command
358
+ # - :error The error contains stderr output of the command
359
+ # - :machine The name of the machine the command executed on
360
+ # - :user The user name
361
+ # - :start_time
362
+ # - :end_time
363
+ #
364
+ class Command < Hash
365
+ def initialize command
366
+
367
+ self[:input] = ''
368
+ self[:timeout] = 0
369
+ self[:directory] = ''
370
+ self[:exit_code] = 0
371
+ self[:output] = ''
372
+ self[:error] = ''
373
+ self[:machine] = ''
374
+ self[:user] = ''
375
+ self[:start_time] = nil
376
+ self[:end_time] = nil
377
+
378
+ if(command.kind_of?(String))
379
+ self[:input] = command
380
+ end
381
+
382
+ if(command.kind_of?(Hash))
383
+ command.each{|k,v|
384
+ self[k.to_sym]=v
385
+ }
386
+ end
387
+ end
388
+
389
+ def quiet?
390
+ (self.has_key?(:quiet) && self[:quiet])
391
+ end
392
+
393
+ def execute value=nil
394
+
395
+ if(!value.nil? && value.is_a?(Hash))
396
+ value.each{|k,v|self[k]=v}
397
+ end
398
+
399
+ pwd=Dir.pwd
400
+ self[:directory] = pwd if(!self.has_key?(:directory) || self[:directory].length==0)
401
+
402
+ if(self[:timeout] > 0)
403
+ puts "#{self[:input]} (#{self[:directory]}) timeout #{self[:timeout].to_s}" if(!quiet?)
404
+ else
405
+ puts "#{self[:input]} (#{self[:directory]})" if(!quiet?)
406
+ end
407
+
408
+ self[:machine] = Command.machine
409
+ self[:user] = Command.user
410
+
411
+ self[:start_time]=Time.now
412
+ timer=Timer.new
413
+
414
+ Dir.chdir(self[:directory]) do
415
+ if self[:input].include?('<%') && self[:input].include?('%>')
416
+ ruby = self[:input].gsub("<%","").gsub("%>","")
417
+
418
+ begin
419
+ self[:output]=eval(ruby)
420
+ rescue
421
+ self[:exit_code]=1
422
+ self[:error]="unable to eval(#{ruby})"
423
+ end
424
+
425
+ self[:elapsed] = timer.elapsed_str
426
+ self[:end_time] = Time.now
427
+ else
428
+ begin
429
+ if(self[:timeout] <= 0)
430
+ self[:output],self[:error],status= Open3.capture3(self[:input])
431
+ self[:exit_code]=status.to_i
432
+ self[:elapsed] = timer.elapsed_str
433
+ self[:end_time] = Time.now
434
+ else
435
+ require_relative 'timeout.rb'
436
+ #puts run_with_timeout(self[:input], self[:timeout], 1).to_s
437
+ #self[:output] = run_with_timeout(self[:input], self[:timeout], 1)
438
+ result=run_with_timeout2(self[:directory],self[:input], self[:timeout], 2)
439
+ self[:output]=result[0]
440
+ self[:error]=result[1]
441
+ self[:exit_code]=result[2]
442
+
443
+ self[:elapsed] = timer.elapsed_str
444
+ self[:end_time] = Time.now
445
+
446
+ if(timer.elapsed >= self[:timeout])
447
+ self[:exit_code]=1
448
+ self[:error] = self[:error] + "timed out"
449
+ end
450
+ end
451
+ rescue Exception => e
452
+ self[:elapsed] = timer.elapsed_str
453
+ self[:end_time] = Time.now
454
+ self[:error] = "Exception: " + e.to_s
455
+ self[:exit_code]=1
456
+ end
457
+ end
458
+ end
459
+
460
+
461
+ if(self[:exit_code] != 0)
462
+ if(!quiet?)
463
+ puts "exit_code=#{self[:exit_code]}"
464
+ puts self[:output]
465
+ puts self[:error]
466
+ end
467
+ if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
468
+ raise "#{self[:input]} failed"
469
+ end #unless (self.has_key?(:ignore_failure) && self[:ignore_failure]==true)
470
+ end
471
+
472
+ end
473
+
474
+ def self.machine
475
+ if !ENV['COMPUTERNAME'].nil?
476
+ return ENV['COMPUTERNAME']
477
+ end
478
+
479
+ machine = `hostname`
480
+ machine = machine.split('.')[0] if machine.include?('.')
481
+ return machine.strip
482
+ end
483
+
484
+ def self.user
485
+ ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER']
486
+ end
487
+
488
+ def self.home
489
+ ["USERPROFILE","HOME"].each {|v|
490
+ return ENV[v].gsub('\\','/') unless ENV[v].nil?
491
+ }
492
+ dir="~"
493
+ dir=ENV["HOME"] unless ENV["HOME"].nil?
494
+ dir=ENV["USERPROFILE"].gsub('\\','/') unless ENV["USERPROFILE"].nil?
495
+ return dir
496
+ end
497
+
498
+ def self.dev_root
499
+ ["DEV_HOME","DEV_ROOT"].each {|v|
500
+ return ENV[v].gsub('\\','/') unless ENV[v].nil?
501
+ }
502
+ dir=home
503
+ #dir=ENV["DEV_ROOT"].gsub('\\','/') unless ENV["DEV_ROOT"].nil?
504
+ return dir
505
+ end
506
+
507
+ def self.exit_code command
508
+ cmd = Command.new(command)
509
+ cmd[:ignore_failure]=true
510
+ cmd[:quiet]=true
511
+ cmd.execute
512
+ cmd[:exit_code]
513
+ end
514
+
515
+ def self.output command
516
+ cmd = Command.new(command)
517
+ cmd[:ignore_failure]=true
518
+ cmd[:quiet]=true
519
+ cmd.execute
520
+ cmd[:output]
521
+ end
522
+
523
+ def getFormattedTimeSpan timespan
524
+ seconds = timespan
525
+ seconds.to_s + " sec"
526
+ end
527
+
528
+ def self.execute command
529
+ cmd - Command.new(command)
530
+ cmd.execute
531
+ cmd[:exit_code]
532
+ end
533
+
534
+ def summary
535
+ duration=""
536
+ duration=getFormattedTimeSpan(self[:end_time]-self[:start_time]) + " - " if(!self[:end_time].nil?)
537
+ duration + "#{self[:exit_code].to_s} #{self[:input]} (#{self[:directory]})"
538
+ end
539
+
540
+ def to_html
541
+ if self[:exit_code] == 0
542
+ [
543
+ '<div><table><tr><td width="20"></td><td><pre>',
544
+ self[:input],
545
+ '</pre></td></tr></table></div>'
546
+ ].join
547
+ else
548
+ [
549
+ '<div><table><tr><td width="20"></td><td><pre>',
550
+ self[:input],
551
+ '</pre><table><tr><td width="20"></td><td><table>',
552
+ map { |k, v| ["<tr><td><strong>#{k}</strong></td>", v.respond_to?(:to_html) ? v.to_html : "<td><span><pre>#{v}</pre></span></td></tr>"] },
553
+ '</table>',
554
+ '</td></tr></table></td></tr></table></div>'
555
+ ].join
556
+ end
557
+ end
558
+ end
559
+ #require_relative('hash.rb')
560
+ #require_relative('pull.rb')
561
+ #require_relative('update.rb')
562
+ #require_relative('setup.rb')
563
+ #require_relative('build.rb')
564
+ #require_relative('test.rb')
565
+ #require_relative('analyze.rb')
566
+ #require_relative('publish.rb')
567
+ #require_relative('doc.rb')
568
+ #require_relative('clean.rb')
569
+ #require_relative('clobber.rb')
570
+ #require_relative('add.rb')
571
+ #require_relative('commit.rb')
572
+ #require_relative('push.rb')
573
+
574
+ class Commands < Hash
575
+
576
+ def initialize directory=Dir.pwd
577
+ Dir.chdir(directory) do
578
+ self[:pull]=Pull.new
579
+ self[:update]=Update.new
580
+ self[:setup]=Setup.new
581
+ self[:build]=Build.new
582
+ self[:test]=Test.new
583
+ self[:analyze]=Analyze.new
584
+ self[:doc]=Doc.new
585
+ self[:clean]=Clean.new
586
+ self[:publish]=Publish.new
587
+ self[:clobber]=Clobber.new
588
+ self[:add]=Add.new
589
+ self[:commit]=Commit.new
590
+ self[:push]=Push.new
591
+ end
592
+ end
593
+
594
+ end
595
+ #require_relative('internet.rb')
596
+
597
+ class Commit < Array
598
+ def update
599
+ if(File.exists?('.git') && `git config --list`.include?('user.name='))
600
+ if(!`git status`.include?('nothing to commit') &&
601
+ !`git status`.include?('untracked files present'))
602
+ if(File.exists?('commit.message') && File.read('commit.message').gsub(/\s+/,"").length >0)
603
+ add "git commit -a -v -m \"#{File.read('commit.message')}\""
604
+ else
605
+ add "git commit -m'all'"
606
+ end
607
+ add "<%FileUtils.rm('commit.message')%>" if File.exists?('commit.message')
608
+ end
609
+ end
610
+ if(File.exists?('.svn') && Internet.available?)
611
+ add 'svn commit -m"commit all"'
612
+ end
613
+ end
614
+ end
615
+ class Doc < Array
616
+ def update
617
+ #cmd=Command.new ({ :input => 'yard --version', :ignore_failure => true})
618
+ #cmd.execute
619
+ if(Command.exit_code('yard --version'))
620
+ add 'yard doc - LICENSE' if File.exists?('README.md') && File.exists?('LICENSE')
621
+ end
622
+ end
623
+ end
624
+ class Gemspec
625
+ def self.update gemspec_file
626
+ Text.replace_in_file gemspec_file,
627
+ /('\d{4}-\d{2}-\d{2}')/,
628
+ "'#{Time.now.strftime('%Y-%m-%d')}'"
629
+ end
630
+
631
+ def self.gemfile gemspec_file
632
+ spec=Gem::Specification.load(gemspec_file)
633
+ return "#{spec.name}-#{spec.version}.gem" if !spec.nil?
634
+ return ""
635
+ end
636
+
637
+ def self.version gemspec_file
638
+ spec=Gem::Specification.load(gemspec_file)
639
+ return spec.version.to_s
640
+ end
641
+
642
+ def self.published_version gemspec_file
643
+ published_version=''
644
+ spec=Gem::Specification.load(gemspec_file)
645
+ begin
646
+ published_version = `gem list -r #{spec.name}`.scan(/\((\d+.\d+.\d+)\)/)[0][0]
647
+ rescue
648
+ published_version=''
649
+ end
650
+ published_version
651
+ end
652
+ def self.published? gemspec_file
653
+ published_version(gemspec_file)==version(gemspec_file) ? true : false
654
+ end
655
+
656
+ def self.normalize gemspec_file
657
+ spec=Gem::Specification.load(gemspec_file)
658
+ File.open(gemspec_file,'w'){|f|f.write(spec.to_ruby)}
659
+ end
660
+
661
+ def self.upgrade gemspec_file
662
+ end
663
+ end
664
+ class Git
665
+ def self.branch
666
+ begin
667
+ `git branch`.scan(/\* ([.\w-]+)/)[0][0]
668
+ rescue
669
+ ''
670
+ end
671
+ end
672
+
673
+ def self.remote_origin directory=''
674
+ url=''
675
+ directory=Dir.pwd if directory.length == 0
676
+ Dir.chdir(directory) do
677
+ begin
678
+ url=`git remote show origin`.scan(/Fetch URL: ([\.\-:\/\w\d]+)/)[0]
679
+ rescue
680
+ url=''
681
+ end
682
+ end
683
+ url
684
+ end
685
+ end
686
+ class Hash
687
+ def execute value=nil
688
+ self.each{|k,v|
689
+ v.update if v.respond_to?(:update)
690
+ if(v.is_a?(Array) && v.length==0)
691
+ self.delete k
692
+ else
693
+ #puts "executing #{k}"
694
+
695
+ v.execute(value) if v.respond_to?(:execute)
696
+ end
697
+ }
698
+ end
699
+ def to_html
700
+ [
701
+ '<div>',
702
+ map { |k, v| ["<br/><div><strong>#{k}</strong>", v.respond_to?(:to_html) ? v.to_html : "<span>#{v}</span></div><br/>"] },
703
+ '</div>'
704
+ ].join
705
+ end
706
+ end
707
+ #require_relative 'array.rb'
708
+ INFO=Array.new
709
+
710
+ require 'open-uri'
711
+ #require 'net/http'
712
+ require 'timeout'
713
+ class Internet
714
+
715
+ @@available=true
716
+
717
+ def self.available?
718
+ return @@available if !@@available.nil?
719
+
720
+ begin
721
+ index=open('http://www.google.com').read
722
+ if index.include?('<Title>Google')
723
+ @@available = true
724
+ else
725
+ puts "open('http://www.google.com') returned false"
726
+ end
727
+ rescue Exception => e
728
+ puts "open('http://www.google.com') raised an exception: #{e.to_s}"
729
+ @@available = false
730
+ end
731
+ @@available
732
+ end
733
+ end
734
+ # Visual Studio 2008 version 9.0, solution format version 10.00
735
+ # Visual Studio 2010 version 10.0, solution format version 11.00
736
+ # Visual Studio 2012 version 11.0, solution format version 12.00
737
+ # Visual Studio 2013 version 12.0, solution format version 12.00
738
+ class MSBuild < Hash
739
+
740
+ def initialize
741
+ self[:vs9]="C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\msbuild.exe" if(File.exists?("C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\msbuild.exe"))
742
+ self[:vs12]="C:\\Program Files (x86)\\MSBuild\\12.0\\bin\\msbuild.exe" if(File.exists?("C:\\Program Files (x86)\\MSBuild\\12.0\\bin\\msbuild.exe"))
743
+ end
744
+
745
+ def self.has_version? version
746
+ if(defined?(MSBUILD))
747
+ MSBUILD.has_key?(version)
748
+ else
749
+ msb=MSBuild.new
750
+ return msb.has_key? version
751
+ end
752
+ end
753
+
754
+ def self.get_version version
755
+ if(defined?(MSBUILD))
756
+ MSBUILD[version]
757
+ else
758
+ msb=MSBuild.new
759
+ return msb[version]
760
+ end
761
+ end
762
+
763
+ def self.get_vs_version(sln_filename)
764
+ sln_text=File.read(sln_filename,:encoding=>'UTF-8')
765
+ return :vs9 if sln_text.include?('Format Version 10.00')
766
+ return :vs12
767
+ end
768
+
769
+ def self.get_configurations(sln_filename)
770
+ configs=Array.new
771
+ sln_text=File.read(sln_filename,:encoding=>'UTF-8')
772
+ sln_text.scan( /= ([\w]+)\|/ ).each{|m|
773
+ c=m.first.to_s
774
+ configs << c if !configs.include?(c)
775
+ }
776
+ return configs
777
+ end
778
+
779
+ def self.get_platforms(sln_filename)
780
+ platforms=Array.new
781
+ sln_text=File.read(sln_filename,:encoding=>"UTF-8")
782
+ sln_text.scan( /= [\w]+\|([\w ]+)/ ).each{|m|
783
+ p=m.first.to_s
784
+ platforms << p if !platforms.include?(p)
785
+ }
786
+ return platforms
787
+ end
788
+ end
789
+
790
+ #require_relative('internet.rb')
791
+ class Publish < Array
792
+ def update
793
+ if(Internet.available?)
794
+ if(File.exists?('.git'))
795
+ if(`git branch`.include?('* master'))
796
+ Dir.glob('*.gemspec').each{|gemspec_file|
797
+ add "gem push #{Gemspec.gemfile(gemspec_file)}" if !Gemspec.published? gemspec_file
798
+ }
799
+ end
800
+ end
801
+ if(File.exists?('.svn'))
802
+ if(`svn info`.include?('/trunk'))
803
+ Dir.glob('*.gemspec').each{|gemspec_file|
804
+ add "gem push #{Gemspec.gemfile(gemspec_file)}" if !Gemspec.published? gemspec_file
805
+ }
806
+ end
807
+ end
808
+ end
809
+ end
810
+ end
811
+ #require_relative('git.rb')
812
+ #require_relative('internet.rb')
813
+
814
+ class Pull < Array
815
+ def update
816
+ if(Internet.available?)
817
+ if(File.exists?('.git') && `git config --list`.include?('user.name='))
818
+ self << 'git pull' if Git.branch != 'develop'
819
+ end
820
+ end
821
+ end
822
+ end
823
+ #require_relative('internet.rb')
824
+ class Push < Array
825
+ def update
826
+ if(File.exists?('.git') && `git config --list`.include?('user.name='))
827
+ add 'git config --global push.default simple'
828
+ self << 'git push' if Git.branch != 'develop' && Internet.available?
829
+ end
830
+ end
831
+ end
832
+ #
833
+ # use the SVN_EXPORTS hash to define svn exports destined for DEV_ROOT/dep
834
+ #
835
+ # SVN_EXPORT={ 'System.Data.SQLite/1.0.93.0' => 'https://third-party.googlecode.com/svn/trunk/System.Data.SQLite/1.0.93.0' }
836
+ #
837
+ class Setup < Array
838
+ def update
839
+ add 'bundle install' if(File.exists?('Gemfile'))
840
+
841
+ Dir.glob('*.gemspec').each{|gemspec_file|
842
+ add "<%Gemspec.update('#{gemspec_file}')%>"
843
+ }
844
+
845
+ if(defined?(SVN_EXPORTS))
846
+ SVN_EXPORTS.each{|k,v|
847
+ if(!File.exists?("#{Command.dev_root}/dep/#{k}"))
848
+ FileUtils.mkdir_p(File.dirname("#{Command.dev_root}/dep/#{k}")) if !File.exists?("#{Command.dev_root}/dep/#{k}")
849
+ dest="#{Command.dev_root}/dep/#{k}"
850
+ add "svn export #{v} #{Command.dev_root}/dep/#{k}" if !dest.include?("@")
851
+ add "svn export #{v} #{Command.dev_root}/dep/#{k}@" if dest.include?("@")
852
+ end
853
+ }
854
+ end
855
+ end
856
+ end
857
+ #
858
+ # nunit dlls may be specified with
859
+ # NUNIT=FileList.new('**/*.Test.dll')
860
+ #
861
+ # for nunit dlls that must be run in x86 mode,
862
+ # NUNIT_x86=FileList.new('**/*.x86.Test.dll')
863
+ #
864
+ class Test < Array
865
+ def update
866
+ add 'rspec' if File.exists?('spec')
867
+
868
+ if(defined?(NUNIT))
869
+ NUNIT.each{|nunit_dll|
870
+ add "\"#{Test.nunit_console}\" \"#{Rake.application.original_dir}\\#{nunit_dll}\" /xml:\"#{nunit_dll}.TestResults.xml\""
871
+ }
872
+ end
873
+
874
+ if(defined?(NUNIT_X86))
875
+ NUNIT_X86.each{|nunit_dll|
876
+ add "\"#{Test.nunit_console_x86}\" \"#{Rake.application.original_dir}\\#{nunit_dll}\" /xml:\"#{nunit_dll}.TestResults.xml\""
877
+ }
878
+ end
879
+
880
+ if(defined?(TESTS))
881
+ TEST.each{|t| add t}
882
+ end
883
+ end
884
+
885
+ @@nunit_console=''
886
+ def self.nunit_console
887
+ if(!File.exists?(@@nunit_console))
888
+ if(defined?(NUNIT_CONSOLE))
889
+ @@nunit_console = NUNIT_CONSOLE
890
+ end
891
+ @@nunit_console = "C:\\Program Files (x86)\\NUnit 2.6.4\\bin\\nunit-console.exe" if(!File.exists?(@@nunit_console))
892
+ @@nunit_console = "C:\\Program Files (x86)\\NUnit 2.6.3\\bin\\nunit-console.exe" if(!File.exists?(@@nunit_console))
893
+ end
894
+ if(!File.exists?(@@nunit_console))
895
+ raise "unable to locate nunit-console.exe, assign NUNIT_CONSOLE to the correct location."
896
+ end
897
+ @@nunit_console
898
+ end
899
+
900
+ @@nunit_console_x86=''
901
+ def self.nunit_console_x86
902
+ if(!File.exists?(@@nunit_console_x86))
903
+ if(defined?(NUNIT_CONSOLE_X86))
904
+ @@nunit_console_x86 = NUNIT_CONSOLE_X86
905
+ end
906
+ @@nunit_console_x86 = "C:\\Program Files (x86)\\NUnit 2.6.4\\bin\\nunit-console-x86.exe" if(!File.exists?(@@nunit_console_x86))
907
+ @@nunit_console_x86 = "C:\\Program Files (x86)\\NUnit 2.6.3\\bin\\nunit-console-x86.exe" if(!File.exists?(@@nunit_console_x86))
908
+ end
909
+ if(!File.exists?(@@nunit_console_x86))
910
+ raise "unable to locate nunit-console-x86.exe, assign NUNIT_CONSOLE_X86 to the correct location."
911
+ end
912
+ @@nunit_console_x86
913
+ end
914
+ end
915
+ class Text
916
+ def self.replace_in_glob(glob,search,replace)
917
+ Dir.glob(glob).each{ |f| replace_in_file(f,search,replace) }
918
+ end
919
+
920
+ def self.replace_in_file(filename,search,replace)
921
+ text1 = IO.read(filename)
922
+ text2 = text1.gsub(search) { |str| str=replace }
923
+ unless text1==text2
924
+ File.open(filename,"w") { |f| f.puts text2 }
925
+ return true
926
+ end
927
+ false
928
+ end
929
+
930
+ end
931
+ ############################################################################
932
+ # The following code is based on code originally copied from
933
+ # https://gist.github.com/lpar/1032297
934
+ # Gist title: lpar/timeout.rb
935
+ ############################################################################
936
+ # Runs a specified shell command in a separate thread.
937
+ # If it exceeds the given timeout in seconds, kills it.
938
+ # Returns any output produced by the command (stdout or stderr) as a String.
939
+ # Uses Kernel.select to wait up to the tick length (in seconds) between
940
+ # checks on the command's status
941
+ #
942
+ # If you've got a cleaner way of doing this, I'd be interested to see it.
943
+ # If you think you can do it with Ruby's Timeout module, think again.
944
+ def run_with_timeout(directory,command, timeout, tick)
945
+ output = ''
946
+ exit_code=1
947
+ begin
948
+ # Start task in another thread, which spawns a process
949
+ stdin, stderrout, thread = Open3.popen2e(command, :chdir=>directory)
950
+ # Get the pid of the spawned process
951
+ pid = thread[:pid]
952
+ start = Time.now
953
+
954
+ while (Time.now - start) < timeout and thread.alive?
955
+ # Wait up to `tick` seconds for output/error data
956
+ Kernel.select([stderrout], nil, nil, tick)
957
+ # Try to read the data
958
+ begin
959
+ output << stderrout.read_nonblock(BUFFER_SIZE)
960
+ rescue IO::WaitReadable
961
+ # A read would block, so loop around for another select
962
+ rescue EOFError
963
+ # Command has completed, not really an error...
964
+ break
965
+ end
966
+ end
967
+
968
+ # Give Ruby time to clean up the other thread
969
+ sleep 1
970
+
971
+ if thread.alive?
972
+ # We need to kill the process, because killing the thread leaves
973
+ # the process alive but detached, annoyingly enough.
974
+ Process.kill("TERM", pid)
975
+ else
976
+ exit_code=thread.value
977
+ sleep 1
978
+ end
979
+
980
+ ensure
981
+ stdin.close if stdin
982
+ stderrout.close if stderrout
983
+ end
984
+ return [output,exit_code]
985
+ end
986
+
987
+ require 'timeout'
988
+ def run_with_timeout2(directory,command,timeout)
989
+ # stdout, stderr pipes
990
+ rout, wout = IO.pipe
991
+ rerr, werr = IO.pipe
992
+ output=''
993
+ error=''
994
+ exit_code=1
995
+ pid = Process.spawn(command, :chdir => directory, :out => wout, :err => werr)
996
+ begin
997
+ Timeout.timeout(timeout) do
998
+ exit_code = Process.wait2(pid)
999
+ output = rout.readlines.join("\n")
1000
+ error = rerr.readlines.join("\n")
1001
+ end
1002
+ rescue
1003
+ Proces.kill('TERM',pid)
1004
+ output = output + 'timeout occurred.'
1005
+ ensure
1006
+ rout.close
1007
+ rerr.close
1008
+ end
1009
+ [output,exit_code]
1010
+ end
1011
+
1012
+ class Timer
1013
+ attr_accessor :start_time
1014
+
1015
+ def initialize
1016
+ @start_time=Time.now
1017
+ end
1018
+
1019
+ def elapsed # in seconds
1020
+ return Time.now-@start_time
1021
+ end
1022
+
1023
+ def elapsed_str
1024
+ elapsed_str="[" + "%.0f" %(elapsed) + "s]"
1025
+ end
1026
+
1027
+ def self.elapsed_exceeds?(name,duration_seconds)
1028
+ if(Timer.get_elapsed(name).nil? || Timer.get_elapsed(name) > duration_seconds)
1029
+ return true
1030
+ end
1031
+ return false
1032
+ end
1033
+
1034
+ def self.get_elapsed(name)
1035
+ timestamp=get_timestamp(name)
1036
+ return Time.now-timestamp if(!timestamp.nil?)
1037
+ nil
1038
+ end
1039
+
1040
+ def self.get_timestamp(name)
1041
+ dir=Rake.application.original_dir
1042
+ if(File.exists?("#{DEV[:dev_root]}/log/#{name}.timestamp"))
1043
+ return Time.parse(File.read("#{DEV[:dev_root]}/log/#{name}.timestamp").strip)
1044
+ end
1045
+ nil
1046
+ end
1047
+
1048
+ def self.set_timestamp(name)
1049
+ Dir.mkdir("#{DEV_TASKS[:dev_root]}/log") if(!Dir.exists?("#{DEV_TASKS[:dev_root]}/log"))
1050
+ File.open("#{DEV_TASKS[:dev_root]}/log/#{name}.timestamp",'w'){|f|f.puts(Time.now.to_s)}
1051
+ end
1052
+ end
1053
+ class Update < Array
1054
+ def update
1055
+ self .add 'svn update' if File.exists?('.svn') && Internet.available?
1056
+ end
1057
+ end
1058
+ class Upgrade < Array
1059
+ def update
1060
+ if(File.exists?('Gemfile'))
1061
+ end
1062
+ end
1063
+ end
1
1064
  require 'json'
2
1065
  require 'rake/clean'
3
1066