rake_script 0.1.0

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
+ SHA256:
3
+ metadata.gz: aa2e617b398ab4bc774b5f411ebfc81fee8116ffb04f0c18d9e5b7b0ac0bd4b2
4
+ data.tar.gz: 34879e6fc7b6025cc95b512627d4cdb39da85aff70ee9eaeeaa6276e42befc25
5
+ SHA512:
6
+ metadata.gz: 9dc81afecb3c9480da8733b167305da5804cfd9d9572a857bda00142c819a01fe2ce1fcaf2a14ebf2f550622e90ee3259a8868bc5ae459a063dfe44cc86cf6dd
7
+ data.tar.gz: a1547b8dc64dae71194c8bd50da819f0205cbd9d0b35173ed634985afe11dbf83644178455b4505fd4a22a4bb438f15d0d300175318108e7ef75cdae4db72a5c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ ### Ruby template
2
+ pkg/*.gem
3
+ Gemfile.lock
4
+
5
+ # Ignore rvm files
6
+ .ruby-version
7
+ .ruby-gemset
8
+ .rvmrc
9
+
10
+ # Ignore Byebug command history file.
11
+ .byebug_history
12
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rake_script.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Denis Talakevich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # RakeScript
2
+
3
+ RakeScript is a scripts helper for rake tasks that allows easy managing file system and shell commands.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rake_script', require: false
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rake_script
20
+
21
+ ## Usage
22
+
23
+ add rake file `something.rake` to your project with following code
24
+
25
+ ```ruby
26
+ require 'rake_script'
27
+
28
+ namespace :something do
29
+ include RakeScript::RakeMethods
30
+
31
+ desc 'Task that do something useful'
32
+ task :some_task do
33
+ with_puts_benchmark('Some task') do
34
+ cmd 'foo --bar', env: { BAZ: 'boo' }
35
+ abort('fail') unless folder_exist?('/path/to/created/folder')
36
+ end
37
+ end
38
+ end
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/senid231/rake_script.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
48
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/test_*.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,9 @@
1
+ require 'rake_script/version'
2
+ require 'rake_script/file_system'
3
+ require 'rake_script/logging'
4
+ require 'rake_script/shell'
5
+ require 'rake_script/rake_methods'
6
+
7
+ module RakeScript
8
+ #
9
+ end
@@ -0,0 +1,12 @@
1
+ module RakeScript
2
+ module FileSystem
3
+ # RakeScript::FileSystem helper allows easy file system manipulations.
4
+
5
+ # Check if folder exists.
6
+ # @param path [String] folder path.
7
+ # @return [TruClass,FalseClass] boolean
8
+ def folder_exist?(path)
9
+ File.directory?(path)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,81 @@
1
+ module RakeScript
2
+ module Logging
3
+ # RakeScript::Logging helper allows to print useful and colorized output.
4
+ # @see https://misc.flogisoft.com/bash/tip_colors_and_formatting
5
+
6
+ PROMPT_STYLES = {
7
+ normal: 0,
8
+ bold: 1,
9
+ dim: 2,
10
+ underlined: 4,
11
+ blink: 5,
12
+ inverted: 7,
13
+ hidden: 8
14
+ }
15
+
16
+ PROMPT_COLORS = {
17
+ default: 39,
18
+ black: 30,
19
+ red: 31,
20
+ green: 32,
21
+ yellow: 33,
22
+ blue: 34,
23
+ magenta: 35,
24
+ cyan: 36,
25
+ light_grey: 37,
26
+ dark_grey: 90,
27
+ light_red: 91,
28
+ light_green: 92,
29
+ light_yellow: 93,
30
+ light_blue: 94,
31
+ light_magenta: 95,
32
+ light_cyan: 96,
33
+ white: 97
34
+ }
35
+
36
+ RESET_PROMPT = "\e[0m".freeze
37
+
38
+ def format_text(text, color: :default, background: :default, style: :normal)
39
+ fg = PROMPT_COLORS.fetch(color)
40
+ bg = PROMPT_COLORS.fetch(background) + 10
41
+ fmt = PROMPT_STYLES.fetch(style)
42
+ format = "\e[#{fmt};#{fg};#{bg}m"
43
+ "#{format}#{text}#{RESET_PROMPT}"
44
+ end
45
+
46
+ def puts_colored(*lines)
47
+ options = lines.pop
48
+ raise ArgumentError, 'last argument must be options hash'.freeze unless options.is_a?(Hash)
49
+ raise ArgumentError, 'provide at least one line'.freeze if lines.empty?
50
+
51
+ formatted_lines = lines.map { |line| format_text(line, options) }
52
+ puts(*formatted_lines)
53
+ end
54
+
55
+ def puts_time(prefix: nil, color: :yellow, style: :bold)
56
+ time = Time.now.utc.strftime('%F %T %Z'.freeze)
57
+ puts_colored("#{prefix}[#{time}]", color: color, style: style)
58
+ end
59
+
60
+ def puts_info(text, color: :blue, style: :bold)
61
+ puts_colored(">>> #{text}", color: color, style: style)
62
+ end
63
+
64
+ def with_puts_benchmark(title, show_time: true)
65
+ puts_info title
66
+ puts_time(prefix: "[#{title} begin]") if show_time
67
+ time = Time.now.to_i
68
+ begin
69
+ yield
70
+ took = Time.now.to_i - time
71
+ puts_time(prefix: "[#{title} completed]") if show_time
72
+ puts_colored("[#{title}] took #{took} seconds.", color: :yellow, style: :bold)
73
+ rescue StandardError => e
74
+ took = Time.now.to_i - time
75
+ puts_time(prefix: "[#{title} failed]") if show_time
76
+ puts_colored("[#{title}] failed after #{took} seconds.", color: :yellow, style: :bold)
77
+ raise e
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,13 @@
1
+ require 'rake_script/logging'
2
+ require 'rake_script/shell'
3
+ require 'rake_script/file_system'
4
+
5
+ module RakeScript
6
+ module RakeMethods
7
+ # RakeScript::RakeMethods aggregate al helper modules from this gem to single mixin.
8
+
9
+ include RakeScript::Logging
10
+ include RakeScript::Shell
11
+ include RakeScript::FileSystem
12
+ end
13
+ end
@@ -0,0 +1,84 @@
1
+ require 'open3'
2
+
3
+ module RakeScript
4
+ module Shell
5
+ # RakeScript::Shell helper allows easy shell commands execution.
6
+
7
+ # Wrapper around #execute which raise RuntimeError when command was exited unsuccessfully.
8
+ # Prefer to use this method unless you allow some command to fail.
9
+ # @param command [String] shell command.
10
+ # @param arguments [Array<String, Hash>] command arguments with optional last hash.
11
+ def cmd(command, *arguments)
12
+ result = execute(command, *arguments)
13
+ if result[:code] != 0
14
+ raise RuntimeError, "command #{result[:command].inspect} failed with code: #{result[:code]}"
15
+ end
16
+ end
17
+
18
+ # Executes shell command and stream it's output.
19
+ # @param command [String] shell command.
20
+ # @param arguments [Array<String, Hash>] command arguments with optional last hash,
21
+ # last arguments hash keys:
22
+ # :stdout [Proc<String>] each command output line will be passed to this proc (print to STDOUT by default),
23
+ # :stderr [Proc<String>] each command error output line will be passed to this proc (print to STDERR by default),
24
+ # :env [Hash] optional environment hash,
25
+ # :debug [TruClass,FalseClass] prints command before execution (true by default),
26
+ # :debug_color [Symbol] color of debug print (@see RakeScript::Formatting::PROMPT_COLORS),
27
+ # :debug_style [Symbol] style of debug print (@see RakeScript::Formatting::PROMPT_STYLES).
28
+ # @return [Hash] hash with keys:
29
+ # :code [Integer] exit code status of command,
30
+ # :command [String] command line without environment.
31
+ def execute(command, *arguments)
32
+ # extracting options
33
+ options = arguments.last.is_a?(Hash) ? arguments.pop : {}
34
+
35
+ # extract option keys
36
+ stdout = options[:stdout] || proc { |raw_line| STDOUT.puts(raw_line) }
37
+ stderr = options[:stderr] || proc { |raw_line| STDERR.puts(raw_line) }
38
+ env = options[:env] || {}
39
+ debug = options.fetch(:debug, true)
40
+ debug_color = options[:debug_color] || :light_cyan
41
+ debug_style = options[:debug_style] || :underlined
42
+
43
+ # calculating command line
44
+ env_str = env.map { |k, v| "#{k}=#{v}" }.join(' ')
45
+ command_line = ([env_str, command] + arguments).reject(&:empty?).join(' ')
46
+
47
+ # debugging
48
+ if debug
49
+ if respond_to?(:puts_colored, true)
50
+ puts_colored(command_line, color: debug_color, style: debug_style)
51
+ else
52
+ puts command_line
53
+ end
54
+ end
55
+
56
+ # execution
57
+ status = raw_execute(command_line, stdout: stdout, stderr: stderr)
58
+
59
+ # response
60
+ { code: status.exitstatus, command: command_line }
61
+ end
62
+
63
+ # Executes shell command and stream it's output.
64
+ # @param command_line [String] command line to execute.
65
+ # @param stdout [Proc] each command output line will be passed to this proc.
66
+ # @param stderr [Proc] each command error output line will be passed to this proc.
67
+ # @return [Process::Status] status object for command.
68
+ def raw_execute(command_line, stdout:, stderr:)
69
+ Open3.popen3(command_line) do |_stdin_io, stdout_io, stderr_io, wait_thread|
70
+ Thread.new do
71
+ until (raw_line = stdout_io.gets).nil? do
72
+ stdout.call(raw_line)
73
+ end
74
+ end
75
+ Thread.new do
76
+ until (raw_line = stderr_io.gets).nil? do
77
+ stderr.call(raw_line)
78
+ end
79
+ end
80
+ wait_thread.value
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module RakeScript
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'rake_script/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'rake_script'
9
+ spec.version = RakeScript::VERSION
10
+ spec.authors = ['Denis Talakevich']
11
+ spec.email = 'senid231@gmail.com'
12
+ spec.date = '2019-11-29'
13
+
14
+ spec.summary = 'Rake scripts helper for file system and shell commands.'
15
+ spec.description = "Rake scripts helper for file system and shell commands."
16
+ spec.homepage = 'https://github.com/senid231/rake_script'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(\.gitignore|test)/}) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'rake'
23
+
24
+ spec.add_development_dependency 'minitest', '~> 5.13'
25
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake_script
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Talakevich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.13'
41
+ description: Rake scripts helper for file system and shell commands.
42
+ email: senid231@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/rake_script.rb
53
+ - lib/rake_script/file_system.rb
54
+ - lib/rake_script/logging.rb
55
+ - lib/rake_script/rake_methods.rb
56
+ - lib/rake_script/shell.rb
57
+ - lib/rake_script/version.rb
58
+ - rake_script.gemspec
59
+ homepage: https://github.com/senid231/rake_script
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.0.6
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Rake scripts helper for file system and shell commands.
82
+ test_files: []