command_butler 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0eeab5518d79fe43f062c86c4d4482d6bd906032
4
+ data.tar.gz: 2b1dc6526ef7c67e8513a2bd13f3a67965dcd961
5
+ SHA512:
6
+ metadata.gz: aab346606ee81ea6d5ae597fb076b3ea8a1792df31fd846c9a1f6d60638e84d9a40d3fb248d411ea1dd53669975cb2a16fbda498498d3c301a7c7715f7589b52
7
+ data.tar.gz: 985c1405eae4832f159eec2ac4d778ac9c6ced951b46152b775d5115fdf6e98075fe4c9c24b85d405558bb497d60b97fec6907154679f9040ee7828b201faa3e
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ tags
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in command_butler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 motsat
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Copipe
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'copipe'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install copipe
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/copipe/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ Bundler.setup
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "run spec"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = ["-c", "-fd"]
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'command_butler'
2
+ require 'pry'
3
+ CommandButler::Executer.start
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'command_butler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "command_butler"
8
+ spec.version = CommandButler::VERSION
9
+ spec.authors = ["motsat"]
10
+ spec.email = ["konpeiex@gmail.com"]
11
+ spec.summary = %q{Commands Executer}
12
+ spec.description = %q{Executer to execute the command written to the file}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_runtime_dependency "thor"
27
+
28
+ end
@@ -0,0 +1,6 @@
1
+ require "command_butler/version"
2
+ require "command_butler/command"
3
+
4
+ module CommandButler
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ require 'command_butler'
2
+ require 'thor'
3
+ require 'command_butler/executer'
@@ -0,0 +1,47 @@
1
+ module CommandButler
2
+ class CommandObject
3
+ attr_reader :command, :description, :need_confirm, :chdir, :set_val, :original_command
4
+ def initialize(vars)
5
+ @original_command = vars["command"]
6
+ @command = vars["command"]
7
+ @description = vars["description"]
8
+ @need_confirm = !vars["need_confirm"]
9
+ @chdir = vars["chdir"]
10
+ @set_val = vars["set_val"]
11
+ end
12
+
13
+ def replaced?
14
+ @command && (@command != @original_command)
15
+ end
16
+
17
+ def replace_command(val:val)
18
+ return unless @command
19
+ val.each_pair do |k, v|
20
+ @command = @command.gsub k, v # original_commandと別の参照にするためgsub!は使わない
21
+ end
22
+ end
23
+
24
+ # こういうのを文字列で返したい
25
+ # {"original_command"=>"date",
26
+ # "command"=>"date",
27
+ # "description"=>nil,
28
+ # "need_confirm"=>true,
29
+ # "chdir"=>nil,
30
+ # "set_val"=>"$DATE_VALUE"}
31
+ def params
32
+ instance_keys = instance_variables.map{|v| v.to_s }
33
+ instance_values = instance_keys.map{|k| instance_variable_get(k) }
34
+ key_and_values_array = [instance_keys.map{|k| k.gsub("@","")}, instance_values].transpose.flatten
35
+ Hash[*key_and_values_array]
36
+ end
37
+
38
+ def need_confirm?
39
+ @need_confirm
40
+ end
41
+
42
+ def set_val_command?
43
+ !@set_val.nil?
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,12 @@
1
+ require 'command_butler/command_object'
2
+ require 'yaml'
3
+ module CommandButler
4
+ class CommandParser
5
+ def self.parse(file_name:file_name)
6
+ YAML.load_file(file_name).map do |y|
7
+ vars = (y.is_a? String)? {"command"=> y} : y
8
+ CommandObject.new(vars)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'command_butler/mediator'
2
+ module CommandButler
3
+ class Executer < Thor
4
+ # default_task :execute
5
+ desc :filename, "commands list file"
6
+ option :val_file, :type => :string
7
+ def execute(file_name)
8
+ Mediator.new.execute(file_name, options)
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,53 @@
1
+ module CommandButler
2
+
3
+ class Input
4
+ attr_reader :input_value
5
+ module VALUE
6
+ EXECUTE = %w(y yes)
7
+ SKIP = %w(s skip n no)
8
+ ABORT = %w(a abort q quit)
9
+ end
10
+
11
+ def initialize(input_value:input_value)
12
+ @input_value = input_value
13
+ end
14
+
15
+ def self.constant_inputs?(input_value)
16
+ VALUE.constants.map{|c| VALUE.const_get(c)}.flatten.include? input_value
17
+ end
18
+
19
+ def self.skip_instance
20
+ self.new(input_value:VALUE::SKIP)
21
+ end
22
+
23
+ def self.execute_instance
24
+ self.new(input_value:VALUE::EXECUTE)
25
+ end
26
+
27
+ def self.start
28
+ while(true)
29
+ puts "[#{Dir.pwd}] $ execute Enter (no: n, quit: q, jump : command-number, skip : s)\n"
30
+ input_value = STDIN.gets.chomp
31
+ input_value = 'y' if input_value == "" # エンターの実行はyと同じにする
32
+ return self.new(input_value:input_value) if constant_inputs? input_value
33
+ return self.new(input_value:input_value.to_i) if input_value.to_i.nonzero? # jumpコマンド
34
+ end
35
+ end
36
+
37
+ def jump?
38
+ @input_value.is_a? Integer
39
+ end
40
+
41
+ def execute?
42
+ (VALUE::EXECUTE).include? @input_value
43
+ end
44
+
45
+ def skip?
46
+ (VALUE::SKIP).include? @input_value
47
+ end
48
+
49
+ def abort?
50
+ (VALUE::ABORT).include? @input_value
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,22 @@
1
+ module CommandButler
2
+ class LineDecorator
3
+ def self.decoration(command:command, index:index, current_index: current_index, input: input)
4
+ mark = if current_index == index
5
+ "\e[33m" + " > "
6
+ elsif input
7
+ "\e[32m" + (input.execute?? " o " : " - ")
8
+ else
9
+ "\e[37m" + " "
10
+ end
11
+ ret = "#{index + 1} " + "["+ mark + "\e[0m" + "]"
12
+ if command.command # コマンド
13
+ ret += " " + command.command
14
+ else
15
+ ret += " - "
16
+ end
17
+ ret += " <= chdir to #{command.chdir}" if command.chdir # ディレクトリ移動
18
+ ret
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,18 @@
1
+ module CommandButler
2
+ class LineDetailDecorator
3
+ def self.decoration(command:command)
4
+ res = ""
5
+ res += " note: #{command.params["description"]}\n" unless command.params["description"].nil?
6
+ res += " original : #{command.original_command} " if command.replaced?
7
+
8
+ command.params.each_pair do |k,v|
9
+ next if %w(chdir description original_command command need_confirm).include? k
10
+ next if v.nil?
11
+ res += " / "if 0 < res.size
12
+ res += " #{k}: #{v.nil?? '-' : v}"
13
+ end
14
+ res = " " + res if 0 < res.size
15
+ "\e[34m" + res + "\e[0m"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,79 @@
1
+ require 'command_butler/command_parser'
2
+ require 'command_butler/val_parser'
3
+ require 'command_butler/line_decorator'
4
+ require 'command_butler/line_detail_decorator'
5
+ require 'command_butler/val_decorator'
6
+ require 'command_butler/result_decorator'
7
+ require 'command_butler/title_decorator'
8
+ require 'command_butler/input'
9
+ module CommandButler
10
+ class Mediator
11
+ def execute(file_name, options)
12
+ inputs = {}
13
+ @commands = CommandParser.parse(file_name:file_name)
14
+
15
+ @vals = {}
16
+ if options[:val_file]
17
+ @vals = ValParser.parse(file_name:options[:val_file])
18
+ @vals.each_pair do |k,v|
19
+ @commands.each {|c| c.replace_command(val:{k=>v})}
20
+ end
21
+ end
22
+ jump_index = -1 # jump制御
23
+
24
+ contents = (0 < @vals.size)? ValDecorator.decoration(vals:@vals) : ""
25
+ puts TitleDecorator.decoration(file_name:file_name, contents:contents)
26
+
27
+ @commands.each_with_index do |command, index|
28
+
29
+ # jumpが設定されていたらskipしたことにする
30
+ if (index < jump_index)
31
+ inputs[index] = Input.skip_instance
32
+ next
33
+ end
34
+
35
+ # show all comamnds
36
+ show_commands(current_index: index, inputs: inputs) # 1件実行ごとにコマンドを表示する
37
+ # execute
38
+ input = command.need_confirm?? Input.start : Input.execute_instance
39
+ exit 1 if input.abort?
40
+ jump_index = (input.input_value - 1) if input.jump?
41
+ inputs[index] = input
42
+ if input.execute?
43
+ res = execute_command(command:command, index:index)
44
+ @commands.each {|c| c.replace_command(val:res[:set_val])} if res[:set_val]
45
+ end
46
+ end
47
+ end
48
+
49
+ def execute_command(command:command, index:index)
50
+ res = {set_val:nil}
51
+ Dir.chdir(command.chdir) if command.chdir
52
+ return res unless command.command
53
+ # set_valコマンドの時は標準出力を取りたいのでバッククオート実行
54
+ # その他は、都度出力されるものを表示したいのでsystemで実行
55
+
56
+ ResultDecorator.decoration(command: command, index: index) do
57
+ if command.set_val_command?
58
+ val = `#{command.command}`.chomp
59
+ puts val
60
+ res[:set_val] = {command.set_val => val}
61
+ puts " (set_val : #{command.set_val})"
62
+ else
63
+ system command.command
64
+ end
65
+ end
66
+ sleep 0.5 # 表示がいっきに流れて見失しなうのでsleep
67
+ res
68
+ end
69
+
70
+ def show_commands(current_index:current_index, inputs:inputs)
71
+ @commands.each_with_index do |command, index|
72
+ puts LineDecorator.decoration command: command, index: index, current_index: current_index, input: inputs[index]
73
+ detail = LineDetailDecorator.decoration command: command
74
+ puts detail if 0 < detail.size
75
+ end
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,10 @@
1
+ module CommandButler
2
+ class ResultDecorator
3
+ def self.decoration(command:command,index:index, &block)
4
+ puts head = "-- result #{index+1}" + "-" * 40
5
+ yield
6
+ puts "-" * head.length
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,13 @@
1
+ module CommandButler
2
+ class TitleDecorator
3
+ def self.decoration(file_name:file_name, contents:contents)
4
+ res = ""
5
+ head = "--- start" + ("-" * 40) + "\n"
6
+ res += head
7
+ res += contents + "\n"
8
+ res += "-" * (head.length - 1)
9
+ res
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ module CommandButler
2
+ class ValDecorator
3
+ def self.decoration(vals:vals)
4
+ res = " replace vals : "
5
+ res += vals.to_s
6
+ res
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'command_butler/command_object'
2
+ require 'yaml'
3
+ module CommandButler
4
+ class ValParser
5
+ def self.parse(file_name:file_name)
6
+ # 今のとここれだけだが
7
+ YAML.load_file(file_name)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module CommandButler
2
+ VERSION = "0.0.1"
3
+ end
data/sample/com.yml ADDED
@@ -0,0 +1,9 @@
1
+ - cd /Users
2
+ - ls -al
3
+ - sleep 3
4
+ -
5
+ chdir: /Users
6
+ - pwd
7
+ -
8
+ command: pwd
9
+ description: "pwdです"
@@ -0,0 +1,11 @@
1
+ -
2
+ command: date
3
+ set_val: $DATE_VALUE
4
+ - echo $DATE_VALUE
5
+ - sleep 3
6
+ -
7
+ chdir: /Users
8
+ - echo $DATE_VALUE
9
+ -
10
+ command: pwd
11
+ description: "pwdです"
@@ -0,0 +1,4 @@
1
+ -
2
+ description: replace string 'STATION_NAME' and 'STATION_WIKI_URL' with sample/val_file/station.yml
3
+ - echo "STATION_NAME"
4
+ - echo "url = STATION_WIKI_URL"
@@ -0,0 +1,2 @@
1
+ STATION_NAME: Shinjyuku
2
+ STATION_WIKI_URL: http://ja.wikipedia.org/wiki/%E6%96%B0%E5%AE%BF
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ describe CommandButler::CommandObject do
3
+
4
+ before do
5
+ @original_command = "hello REPLACE_STRING"
6
+ end
7
+
8
+ subject (:command) do
9
+ command = CommandButler::CommandObject.new({"command"=> @original_command})
10
+ command.replace_command(val:{"REPLACE_STRING"=>"world"})
11
+ command
12
+ end
13
+
14
+ it "replaced? changed" do
15
+ is_expected.to be_replaced
16
+ end
17
+
18
+ it "command string changed" do
19
+ expect(command.command).to eq("hello world")
20
+ end
21
+
22
+ it "original command is no change" do
23
+ expect(command.original_command).to eq(@original_command)
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,2 @@
1
+ require 'rubygems'
2
+ require 'command_butler'
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_butler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - motsat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Executer to execute the command written to the file
84
+ email:
85
+ - konpeiex@gmail.com
86
+ executables:
87
+ - command_butler
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/command_butler
97
+ - command_butler.gemspec
98
+ - lib/command_butler.rb
99
+ - lib/command_butler/command.rb
100
+ - lib/command_butler/command_object.rb
101
+ - lib/command_butler/command_parser.rb
102
+ - lib/command_butler/executer.rb
103
+ - lib/command_butler/input.rb
104
+ - lib/command_butler/line_decorator.rb
105
+ - lib/command_butler/line_detail_decorator.rb
106
+ - lib/command_butler/mediator.rb
107
+ - lib/command_butler/result_decorator.rb
108
+ - lib/command_butler/title_decorator.rb
109
+ - lib/command_butler/val_decorator.rb
110
+ - lib/command_butler/val_parser.rb
111
+ - lib/command_butler/version.rb
112
+ - sample/com.yml
113
+ - sample/replace.yml
114
+ - sample/replace_with_valfile.yml
115
+ - sample/val_file/station.yml
116
+ - spec/command_butler_spec.rb
117
+ - spec/command_object_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.2.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Commands Executer
143
+ test_files:
144
+ - spec/command_butler_spec.rb
145
+ - spec/command_object_spec.rb
146
+ - spec/spec_helper.rb