raykit 0.0.23 → 0.0.24

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cb2d9f485d024b8377b94fdc26e19b31591434cc9fa7b8950fb1c72eab40b95
4
- data.tar.gz: 107f01c5d139b50dd8ecdda997d88e24f77d08a91d4128d211323bb6b2bff80e
3
+ metadata.gz: f933eede56a78983aa2f51561dbccd13f82843dbdb181cc68f8b1d0d90bd03b4
4
+ data.tar.gz: db666a66afaa823fe210bd837abd6472932390d507ddac6f0809bb8b938fa18e
5
5
  SHA512:
6
- metadata.gz: 8bbf1552d25efa9077457472b6c131f5a9781368c3e81202c9cf4f5bf1c0b34bbdf55d2205f437a0f5518095b346d45723c2bd6c0d8e191b32effb01556a5765
7
- data.tar.gz: bc7528ff64cc3440819bef1db925737a82fe757daafa2612d8c9c407d5f5e2865810bbb5553105081a89d9112567a3ecb2fd90188cd6efa9e9749bbb07829bba
6
+ metadata.gz: 81b4a53ed5022c0ed8269d58b78b7416f4a0cf9ceaa2f0c72afd4144b92f023457170458b9f3cbbcdcd5ed235ef10dcfde0a8941f71b6e311267da9ba6b47479
7
+ data.tar.gz: 113633993c47c3023b622341fdb9c8fb0c658c6655e7a392f29c90feb44a0a8fccd2a63fb0e210afec96ba54f349882ba6e25f249c69ad229be93f006b7098ad
@@ -3,6 +3,7 @@ require_relative './raykit/git.rb'
3
3
  require_relative './raykit/dotnet.rb'
4
4
  require_relative './raykit/version.rb'
5
5
  require_relative './raykit/environment.rb'
6
+ require_relative './raykit/command.rb'
6
7
  require 'rainbow'
7
8
  require 'rake/clean'
8
9
  require 'open3'
@@ -11,20 +12,18 @@ CLEAN.include("**/bin","**/obj")
11
12
 
12
13
  module Raykit
13
14
  def Raykit.run(command)
14
- timer = Timer.new
15
- exit,exit,exit = Open3.capture3(command)
16
- if(exit == 0)#system(command))
17
- puts timer.elapsed_str + " " + Rainbow(command).yellow.bright
18
- return timer.elapsed_str + " " + command
15
+ cmd = Command.new
16
+ cmd.command = command
17
+ cmd.run()#command)
18
+ elapsed_str = Timer.get_elapsed_str(cmd.elapsed)
19
+ if(cmd.exitstatus == 0)
20
+ puts elapsed_str + " " + Rainbow(cmd.command).yellow.bright
21
+ return elapsed_str + " " + cmd.command
19
22
  else
20
- #puts ''
21
- puts "\r\n" + command + "\r\n"
22
- #puts ''
23
- system(command)
23
+ puts "\r\n" + cmd.command + "\r\n"
24
+ system(cmd.command)
24
25
  puts ''
25
- abort Rainbow(timer.elapsed_str).red.bright + " " + Rainbow(command).white
26
+ abort Rainbow(elapsed_str).red.bright + " " + Rainbow(cmd.command).white
26
27
  end
27
- end
28
-
29
-
28
+ end
30
29
  end
@@ -1,20 +1,102 @@
1
1
  require 'open3'
2
+ require 'timeout'
3
+ require 'json'
4
+ require 'logger'
5
+ BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
2
6
 
3
7
  module Raykit
4
8
  class Command
5
- attr_accessor :log_level
6
- def run(command)
9
+
10
+ @@enable_logging = true
11
+ def self.enable_logging
12
+ @@enable_logging
13
+ end
14
+ def self.enable_logging=(enable)
15
+ @@enable_logging = enable
16
+ end
17
+ @@logger = nil
18
+ def self.log(command)
19
+ if(@@enable_logging)
20
+ if(@@logger.nil?)
21
+ log_dir = "#{Raykit::Environment.log_dir}/Raykit/Command"
22
+ if(!Dir.exist?(log_dir))
23
+ FileUtils.mkdir_p(log_dir)
24
+ end
25
+ Dir.chdir(log_dir) do
26
+ # start the log over whenever the log exceeds 100 megabytes in size
27
+ @@logger = Logger.new('log.txt',0,100*1024*1024)
28
+ end
29
+ end
30
+ if(command.exitstatus == 0)
31
+ @@logger.info JSON.generate(command.to_hash)
32
+ else
33
+ @@logger.error JSON.generate(command.to_hash)
34
+ end
35
+ end
36
+ end
37
+
38
+ attr_accessor :directory,:timeout,:start_time,:elapsed
39
+ attr_accessor :command,:output,:error,:exitstatus
40
+
41
+ def initialize()
42
+ @timeout=0
43
+ @directory = Dir.pwd
44
+ @output = ''
45
+ @error = ''
46
+ @exitstatus = 0
47
+ end
48
+
49
+ def run()
50
+ @start_time = Time.now
7
51
  timer = Timer.new
8
- exit,exit,exit = Open3.capture3(command)
9
- if(exit == 0)#system(command))
10
- puts timer.elapsed_str + " " + Rainbow(command).yellow.bright
11
- return timer.elapsed_str + " " + command
52
+ if(@timeout == 0)
53
+ @output,@error,process_status = Open3.capture3(@command)
54
+ @exitstatus=process_status.exitstatus
12
55
  else
13
- puts "\r\n" + command + "\r\n"
14
- system(command)
15
- puts ''
16
- abort Rainbow(timer.elapsed_str).red.bright + " " + Rainbow(command).white
56
+ Open3.popen3(@command, :chdir=>@directory) { |stdin,stdout,stderr,thread|
57
+ tick=2
58
+ pid = thread.pid
59
+ start = Time.now
60
+ elapsed = Time.now-start
61
+ while (elapsed) < @timeout and thread.alive?
62
+ Kernel.select([stdout,stderr], nil, nil, tick)
63
+ begin
64
+ @output << stdout.read_nonblock(BUFFER_SIZE)
65
+ @error << stderr.read_nonblock(BUFFER_SIZE)
66
+ rescue IO::WaitReadable
67
+ rescue EOFError
68
+ @exitstatus=thread.value.exitstatus
69
+ break
70
+ end
71
+ elapsed = Time.now-start
72
+ end
73
+ if thread.alive?
74
+ if(Gem.win_platform?)
75
+ `taskkill /f /pid #{pid}`
76
+ else
77
+ Process.kill("TERM", pid)
78
+ end
79
+ @exitstatus=5
80
+ else
81
+ @exitstatus=thread.value.exitstatus
82
+ end
83
+ }
17
84
  end
85
+ @elapsed = timer.elapsed
86
+ Command::log(self)
87
+ end
88
+
89
+ def to_hash()
90
+ hash = Hash.new
91
+ hash[:command] = @command
92
+ hash[:directory] = @directory
93
+ hash[:timeout] = @timeout
94
+ hash[:start_time] = @start_time
95
+ hash[:elapsed] = @elapsed
96
+ hash[:output] = @output
97
+ hash[:error] = @error
98
+ hash[:exitstatus] = @exitstatus
99
+ hash
18
100
  end
19
101
  end
20
102
  end
@@ -16,6 +16,14 @@ module Raykit
16
16
  def self.home_dir
17
17
  get_env('HOME').gsub('\\','/')
18
18
  end
19
+
20
+ def self.log_dir
21
+ log_dir = "#{Environment::root_dir}/log"
22
+ if(!Dir.exist?(log_dir))
23
+ Dir.mkdir(log_dir)
24
+ end
25
+ log_dir
26
+ end
19
27
  end
20
28
  end
21
29
 
@@ -13,6 +13,11 @@ module Raykit
13
13
  end
14
14
 
15
15
  def elapsed_str
16
+ get_elapsed_str(elapsed)
17
+ #"[" + "%.0f" % (elapsed) + "s]"
18
+ end
19
+
20
+ def self.get_elapsed_str(elapsed)
16
21
  "[" + "%.0f" % (elapsed) + "s]"
17
22
  end
18
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raykit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lou Parslow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-08 00:00:00.000000000 Z
11
+ date: 2019-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler