dev_commands 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/Timer.rb +41 -0
- data/lib/command.rb +59 -0
- data/spec/command_spec.rb +84 -0
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbbbe9db2da5300e3ca36aa1783b832d9057b1ef
|
4
|
+
data.tar.gz: 32924b87160449f1f8784e8b192c53ae85e2ec8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42b40ebf3b8660a149f3c76dbdb9aed8b662b2e15a3a9d0cacf61223a0100a77ce727eff17991f28be7e4b36261df778f2f770328588bd2e5be94f740c137d3e
|
7
|
+
data.tar.gz: 202bd1b20dc7886eb0faa95b4bf0f5ecf76296034bdb1fef1ee53a231ff0a88eb7c09fea8dc4778528b4c5110fe9e8223800b339f8eb986ae21065ab7599b4e1
|
data/lib/Timer.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
class Timer
|
2
|
+
attr_accessor :start_time
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@start_time=Time.now
|
6
|
+
end
|
7
|
+
|
8
|
+
def elapsed # in seconds
|
9
|
+
return Time.now-@start_time
|
10
|
+
end
|
11
|
+
|
12
|
+
def elapsed_str
|
13
|
+
elapsed_str="[" + "%.0f" %(elapsed) + "s]"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.elapsed_exceeds?(name,duration_seconds)
|
17
|
+
if(Timer.get_elapsed(name).nil? || Timer.get_elapsed(name) > duration_seconds)
|
18
|
+
return true
|
19
|
+
end
|
20
|
+
return false
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.get_elapsed(name)
|
24
|
+
timestamp=get_timestamp(name)
|
25
|
+
return Time.now-timestamp if(!timestamp.nil?)
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.get_timestamp(name)
|
30
|
+
dir=Rake.application.original_dir
|
31
|
+
if(File.exists?("#{DEV[:dev_root]}/log/#{name}.timestamp"))
|
32
|
+
return Time.parse(File.read("#{DEV[:dev_root]}/log/#{name}.timestamp").strip)
|
33
|
+
end
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.set_timestamp(name)
|
38
|
+
Dir.mkdir("#{DEV_TASKS[:dev_root]}/log") if(!Dir.exists?("#{DEV_TASKS[:dev_root]}/log"))
|
39
|
+
File.open("#{DEV_TASKS[:dev_root]}/log/#{name}.timestamp",'w'){|f|f.puts(Time.now.to_s)}
|
40
|
+
end
|
41
|
+
end
|
data/lib/command.rb
CHANGED
@@ -1,2 +1,61 @@
|
|
1
|
+
require_relative('./timer.rb')
|
2
|
+
|
1
3
|
class Command < Hash
|
4
|
+
def initialize command
|
5
|
+
|
6
|
+
if(command.kind_of?(String))
|
7
|
+
self[:input] = command
|
8
|
+
self[:timeout] = 0
|
9
|
+
self[:directory] = ''
|
10
|
+
self[:exit_code] = 0
|
11
|
+
self[:output] = ''
|
12
|
+
self[:error] = ''
|
13
|
+
self[:machine_name] = ''
|
14
|
+
self[:user_name] = ''
|
15
|
+
self[:start_time] = nil
|
16
|
+
self[:end_time] = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
if(command.kind_of?(Hash))
|
20
|
+
command.each{|k,v|
|
21
|
+
self[k.to_sym]=v
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def execute
|
27
|
+
|
28
|
+
#Logger.start_command self
|
29
|
+
|
30
|
+
pwd=Dir.pwd
|
31
|
+
Dir.chdir(self[:directory]) if(self.has_key?(:directory) && File.exists?(self[:directory]))
|
32
|
+
#print " " + Color.green + self[:input] + Color.clear
|
33
|
+
|
34
|
+
self[:start_time]=Time.now
|
35
|
+
timer=Timer.new
|
36
|
+
if self[:input].include?('<%') && self[:input].include?('%>')
|
37
|
+
ruby = self[:input].gsub("<%","").gsub("%>","")
|
38
|
+
|
39
|
+
begin
|
40
|
+
puts "eval(#{ruby})"
|
41
|
+
self[:output]=eval(ruby)
|
42
|
+
rescue
|
43
|
+
puts "unable to eval(#{ruby})"
|
44
|
+
raise "unable to eval(#{ruby})"
|
45
|
+
end
|
46
|
+
|
47
|
+
#puts " " + timer.elapsed_str
|
48
|
+
self[:elapsed] = timer.elapsed_str
|
49
|
+
self[:end_time] = Time.now
|
50
|
+
else
|
51
|
+
self[:output] = `#{self[:input]}`
|
52
|
+
self[:elapsed] = timer.elapsed_str
|
53
|
+
self[:end_time] = Time.now
|
54
|
+
self[:exit_code]=$?.to_i
|
55
|
+
end
|
56
|
+
|
57
|
+
Dir.chdir(pwd) if pwd != Dir.pwd
|
58
|
+
#Logger.end_command self
|
59
|
+
end
|
60
|
+
|
2
61
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require_relative '../lib/command.rb'
|
2
|
+
require 'json'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
describe Command do
|
6
|
+
it "should be able to execute ruby --version command" do
|
7
|
+
cmd=Command.new("ruby --version")
|
8
|
+
# Timeout
|
9
|
+
expect(cmd[:timeout]).to eq(0)
|
10
|
+
cmd[:timeout]=3000
|
11
|
+
expect(cmd[:timeout]).to eq(3000)
|
12
|
+
|
13
|
+
# Directory
|
14
|
+
expect(cmd[:directory]).to eq("")
|
15
|
+
cmd[:directory] = File.dirname(__FILE__)
|
16
|
+
expect(cmd[:directory]).to eq(File.dirname(__FILE__))
|
17
|
+
|
18
|
+
# ExitCode
|
19
|
+
expect(cmd[:exit_code]).to eq(0)
|
20
|
+
cmd[:exit_code] = 1
|
21
|
+
expect(cmd[:exit_code]).to eq(1)
|
22
|
+
|
23
|
+
# Input
|
24
|
+
expect(cmd[:input]).to eq("ruby --version")
|
25
|
+
cmd2 = Command.new('')
|
26
|
+
expect(cmd2[:input]).to eq('')
|
27
|
+
|
28
|
+
# Output
|
29
|
+
expect(cmd[:output]).to eq('')
|
30
|
+
cmd[:output]='test'
|
31
|
+
expect(cmd[:output]).to eq('test')
|
32
|
+
|
33
|
+
# Error
|
34
|
+
expect(cmd[:error]).to eq('')
|
35
|
+
cmd[:error]='error_test'
|
36
|
+
expect(cmd[:error]).to eq('error_test')
|
37
|
+
|
38
|
+
# MachineName
|
39
|
+
expect(cmd[:machine_name]).to eq('')
|
40
|
+
cmd[:machine_name]='machine_name_test'
|
41
|
+
expect(cmd[:machine_name]).to eq('machine_name_test')
|
42
|
+
|
43
|
+
# UserName
|
44
|
+
expect(cmd[:user_name]).to eq('')
|
45
|
+
cmd[:user_name]='user_name_test'
|
46
|
+
expect(cmd[:user_name]).to eq('user_name_test')
|
47
|
+
|
48
|
+
# StartTime
|
49
|
+
expect(cmd[:start_time]).to eq(nil)
|
50
|
+
cmd[:start_time]=Time.now
|
51
|
+
expect(cmd[:start_time]).not_to eq(nil)
|
52
|
+
|
53
|
+
# EndTime
|
54
|
+
expect(cmd[:end_time]).to eq(nil)
|
55
|
+
cmd[:end_time]=Time.now
|
56
|
+
expect(cmd[:end_time]).not_to eq(nil)
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should be able to write to/load from JSON" do
|
61
|
+
cmd=Command.new("ruby --version")
|
62
|
+
expect(cmd[:timeout]).to eq(0)
|
63
|
+
expect(cmd[:input]).to eq("ruby --version")
|
64
|
+
cmd2=Command.new(JSON.parse(cmd.to_json))
|
65
|
+
expect(cmd2[:timeout]).to eq(0)
|
66
|
+
expect(cmd2[:input]).to eq("ruby --version")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should be able to execute rake command in specific directory" do
|
70
|
+
dir="#{File.dirname(__FILE__)}/tmp/rake_test"
|
71
|
+
FileUtils.mkdir_p(dir) if(!File.exists?(dir))
|
72
|
+
File.open("#{dir}/rakefile.rb","w") { |f|
|
73
|
+
f.puts "task :default do"
|
74
|
+
f.puts " puts 'rake_test'"
|
75
|
+
f.puts "end"
|
76
|
+
}
|
77
|
+
cmd=Command.new("rake")
|
78
|
+
cmd[:directory]=dir
|
79
|
+
cmd.execute
|
80
|
+
FileUtils.rm_r("#{File.dirname(__FILE__)}/tmp")
|
81
|
+
expect(cmd[:output].include?('rake_test')).to eq(true)
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dev_commands
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lou Parslow
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: execution of system commands
|
42
56
|
email: lou.parslow@gmail.com
|
43
57
|
executables: []
|
@@ -45,6 +59,8 @@ extensions: []
|
|
45
59
|
extra_rdoc_files: []
|
46
60
|
files:
|
47
61
|
- lib/command.rb
|
62
|
+
- lib/Timer.rb
|
63
|
+
- spec/command_spec.rb
|
48
64
|
- LICENSE
|
49
65
|
- README.md
|
50
66
|
homepage: http://github.com/lou-parslow/dev_commands.gem
|