ferrets_on_fire 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e10279082200ad3c59578490354c5583b2d55053
4
+ data.tar.gz: e4270068759afb2b7d642330f99e72dfab844d8d
5
+ SHA512:
6
+ metadata.gz: af775ab186a031447f7cba5e7ef4c488027c763387f825d0ada33380efbf88487fdcd1303dd4fda9b0a805396a5150cfe582e035d1de40dd6102ce17c8fb1cba
7
+ data.tar.gz: f827d326ee118b2743fcb4c185828df2481b7ae13712a9cc2f4e69f542317975069cf658320eb6b6c924a174b1b161ee83931a25b4a1bea31c64112b2551a5de
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Ferrets on Fire
2
+
3
+ Supercharge your CLI tools. This opinionated, burning gem helps you to write powerful CLI tools of all sizes without
4
+ caring about the clutter!
5
+
6
+ **WARNING: This project is work in progress, use with care.**
7
+
8
+
9
+ ## Features
10
+
11
+ - Super simple and lightweight DSL.
12
+ - Beautiful output (especially on OSX).
13
+ - Builtin tasks for git, bundler, ...
14
+ - Framework for system calls: Logging, benchmarking, error handling and more.
15
+ - Support CLI switches, options, arguments and commands.
16
+ - Supports Logging, colors, progress bars, questions and much more.
17
+ - Builtin IoC container.
18
+ - Ferrets.
19
+
20
+ ## TODO
21
+
22
+ - Command API
23
+ - Code formatting, refactoring, documentation
24
+ - Project, usage & API documentation
25
+ - Progress bars
26
+ - Highline
27
+ - DSL for IoC container
28
+ - RSpecs
29
+ - CI build
30
+
31
+
32
+ ## Installation
33
+
34
+ ```bash
35
+ $ gem install ferrets_on_fire
36
+ ```
37
+
38
+ or via `Gemfile`:
39
+
40
+ ```ruby
41
+ gem 'ferrets_on_fire'
42
+ ```
43
+
44
+ (Don't forget to run `bundle install`)
45
+
46
+
47
+ ## Usage
48
+
49
+ Loading the Ferrets on Fire framework is made for lazy developers (like me) and requires nearly no code:
50
+
51
+ ```ruby
52
+ require '_'
53
+ ```
54
+
55
+ This is it. No verbose filenames. No `include`s. No setup.
56
+
57
+ After that you can already you the fancy ferret DSL:
58
+
59
+ ```ruby
60
+ require '_'
61
+
62
+ # Some output
63
+ info 'Running the test suit!'
64
+
65
+ # Run `bundle exec rspec spec/`
66
+ run 'rspec spec/', bundler: true
67
+ ```
68
+
69
+
70
+
71
+ ## Open Source
72
+
73
+ MIT Licence. Feel free to contribute.
data/lib/_.rb ADDED
@@ -0,0 +1,14 @@
1
+ # Enable `require '_'`
2
+
3
+ require 'ferrets_on_fire'
4
+ require 'ferrets_on_fire/option'
5
+ require 'ferrets_on_fire/switch'
6
+ require 'ferrets_on_fire/argument'
7
+ require 'ferrets_on_fire/command'
8
+ require 'ferrets_on_fire/gli_wrapper'
9
+
10
+ module FoF
11
+ include FerretsOnFire
12
+ end
13
+
14
+ include FoF
@@ -0,0 +1,9 @@
1
+ class FerretsOnFire::Argument
2
+ attr_reader :name, :short, :default, :desc
3
+
4
+ public def initialize(name, default, desc)
5
+ @name = name.to_sym
6
+ @default = default
7
+ @desc = desc
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require 'ferrets_on_fire/dsl'
2
+
3
+ class FerretsOnFire::Command
4
+ attr_reader :name, :desc, :default, :action_block
5
+ attr_accessor :global_options, :options, :args
6
+
7
+ include FerretsOnFire::DSL
8
+
9
+ public def initialize(name, desc, default, &block)
10
+ @name = name.to_sym
11
+ @desc = desc
12
+ @default = default
13
+
14
+ define_singleton_method(:dispatch, block)
15
+ dispatch
16
+ end
17
+
18
+ public def action(&block)
19
+ @action_block = block
20
+ end
21
+
22
+ public def run
23
+ yield @block
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ module FerretsOnFire::DSL::Declaration
2
+ public def bundle(dir: nil)
3
+ # Set bundler parallel jobs count
4
+ if run('sysctl -n hw.ncpu', quiet: true, return_exit_code: true).zero?
5
+ number_of_cores = run('sysctl -n hw.ncpu', quiet: true).strip.to_i - 1
6
+ run("bundle config --global jobs #{number_of_cores}", quiet: true)
7
+ end
8
+
9
+ run 'bundle', dir: dir, as: 'bundler'
10
+ end
11
+
12
+
13
+ public def description(desc)
14
+ @desc = desc
15
+ end
16
+ alias_method :desc, :description
17
+
18
+
19
+ public def option(name, short, param_name: 'PARAM', default: nil, desc: '')
20
+ @options ||= []
21
+ @options << Option.new(name, param_name, short, default, desc)
22
+ end
23
+ alias_method :opt, :option
24
+
25
+
26
+ public def switch(name, short, default: false, desc: '')
27
+ @switches ||= []
28
+ @switches << Switch.new(name, short, default, desc)
29
+ end
30
+
31
+
32
+ public def argument(name, desc: '', default: nil)
33
+ @arguments ||= []
34
+ @arguments << Argument.new(name, desc, default)
35
+ end
36
+ alias_method :arg, :argument
37
+
38
+
39
+ public def command(name, desc: '', default: false, &block)
40
+ @commands ||= []
41
+ @commands << Command.new(name, desc, default, &block)
42
+ end
43
+ alias_method :cmd, :command
44
+ end
@@ -0,0 +1,25 @@
1
+ module FerretsOnFire::DSL::Git
2
+ # Updates the git repository in the current directory and runs bin/update
3
+ # @param [String] path The directory to use, default is the current one
4
+ public def update_repo(path: nil, desired_branch: nil)
5
+ run 'git pull -r --autostash', dir: path, as: 'git pull'
6
+
7
+ # get current branch name
8
+ if desired_branch
9
+ current_branch = run('git branch | grep \*', dir: path, quiet: true).sub('*', '').strip
10
+
11
+ unless current_branch == desired_branch
12
+ warn "WARNING: Current branch is not #{desired_branch} (it's #{current_branch})"
13
+ end
14
+ end
15
+
16
+ run('bin/update', dir: path, as: 'bin/update', bundler: true) if File.exists?('bin/update')
17
+ end
18
+
19
+
20
+ public def clone_repo(remote, target: '', desired_branch: 'develop')
21
+ run "git clone #{remote} #{target}", as: 'clone'
22
+ run "git checkout #{desired_branch}", as: "checkout #{desired_branch}", dir: target
23
+ run('bin/setup', dir: target, as: 'bin/setup', bundler: true) if File.exists?('bin/setup')
24
+ end
25
+ end
@@ -0,0 +1,117 @@
1
+ require 'colorize'
2
+ require 'benchmark'
3
+
4
+ module FerretsOnFire::DSL::Logger
5
+ COLORS = {
6
+ info: :white,
7
+ warn: :yellow,
8
+ error: :red,
9
+ success: :green,
10
+ question: :magenta,
11
+ action: :light_blue
12
+ }.freeze
13
+
14
+ FANCY_PREFIXES = {
15
+ info: 'ℹ️️ ',
16
+ warn: '⚠️ ',
17
+ error: '‼️️ ',
18
+ success: '✨ ',
19
+ question: '❓ ',
20
+ action: '⚙️ '
21
+ }.freeze
22
+
23
+ DEFAULT_PREFIXES = {
24
+ info: '[i]',
25
+ warn: '[!]',
26
+ error: '[X]',
27
+ success: '[✔]',
28
+ question: '[?]',
29
+ action: '[$]'
30
+ }.freeze
31
+
32
+ SUFFIXES = {
33
+ info: '',
34
+ warn: '',
35
+ error: '',
36
+ success: '',
37
+ question: ' >',
38
+ action: ''
39
+ }.freeze
40
+
41
+ # Outputs a info message
42
+ # @param [String] msg The message
43
+ public def info(msg)
44
+ puts log(msg, :info)
45
+ end
46
+
47
+ # Outputs a warning message
48
+ # @param [String] msg The message
49
+ public def warn(msg)
50
+ puts log(msg, :warn)
51
+ end
52
+
53
+
54
+ # Outputs a error message
55
+ # @param [String] msg The message
56
+ public def error(msg)
57
+ puts log(msg, :error)
58
+ end
59
+
60
+
61
+
62
+ # Outputs a success message
63
+ # @param [String] msg The message
64
+ public def success(msg)
65
+ puts log(msg, :success)
66
+ end
67
+
68
+
69
+ # Generates a question with the specified msg. If output is true (default) it will be printed.
70
+ # @param [String] msg The message
71
+ # @param [Boolean] output (Optional, default is true). Print the question?
72
+ # @return [String] The question
73
+ public def question(msg, output = true)
74
+ puts log(msg, :question) if output
75
+ end
76
+
77
+
78
+ public def linebreak
79
+ puts
80
+ end
81
+
82
+
83
+ public def crash(msg, output, command: nil, exit_on_failure: true)
84
+ error msg
85
+ puts
86
+ puts ' ============='
87
+ puts "COMMAND: #{command}" unless command.nil?
88
+ puts output
89
+ puts ' ============='
90
+ puts
91
+ exit 1 if exit_on_failure
92
+ end
93
+
94
+ # Logs that an action will happen. You have to call action_end after that
95
+ # @param [String] msg Message to display
96
+ public def action(msg, &block)
97
+ print log(msg, :action, ' ... ')
98
+ cmd_output = ''
99
+ bm = ::Benchmark.measure { cmd_output = yield }
100
+ time = '%.2f' % bm.real.round(2)
101
+ info = "#{time.rjust(6)}s"
102
+ puts "\r#{log(msg, :success, info)}"
103
+
104
+ [cmd_output, $?.to_i]
105
+ end
106
+
107
+
108
+ private def log(msg, type, info = '')
109
+ prefix = (RUBY_PLATFORM.include?('darwin') && $stdout.tty? ? FANCY_PREFIXES : DEFAULT_PREFIXES)[type]
110
+ suffix = SUFFIXES[type]
111
+ color = COLORS[type]
112
+
113
+ info = info.empty? ? '' : "[#{info}] "
114
+
115
+ "#{prefix} #{info}#{msg}#{suffix}".send(color)
116
+ end
117
+ end
@@ -0,0 +1,60 @@
1
+ module FerretsOnFire::DSL::Shell
2
+ # Runs a shell command. Stderr is redirected to Stdout
3
+ # @param [String] cmd Command to run
4
+ # @param [String] dir Directory to run the command in
5
+ # @param [Boolean] exit_on_failure Optional (default: true). If true, the script will exited if the command fails
6
+ # @param [Boolean] quiet Optional (default: true). If true, the command is not printed
7
+ # @param [Boolean] return_exit_code Optional (default: false). If true, the exit code is returned instead of
8
+ # the output
9
+ # @param [String] as Optional. Label for the action output (instead of the command)
10
+ # @param [Boolean] bundler Optional. Run with `bundle exec`?
11
+ # @param [Symbol] rails_env Optional. Set RAILS_ENV.
12
+ # @return [String] Stdout or exit code
13
+ public def run(cmd, dir: nil, exit_on_failure: true, quiet: false, return_exit_code: false, as: nil, bundler: false,
14
+ rails_env: nil)
15
+
16
+ # Build complete command
17
+ cmd = build_command(cmd, dir, rails_env, bundler)
18
+
19
+ # Run the command and print the output if wished
20
+ output, status = exec(cmd, quiet, as)
21
+
22
+ # Error handling
23
+ if status.nonzero? && return_exit_code == false
24
+ crash "COMMAND EXITED WITH #{status}!", output, command: cmd, exit_on_failure: exit_on_failure
25
+ end
26
+
27
+ return_exit_code ? status : output
28
+ end
29
+
30
+
31
+ # Executes a shell command in the specified dir. Includes benchmarking and debug output.
32
+ # @param [String] cmd Command to run
33
+ # @param [String] quiet Optional. Suppress output
34
+ # @param [String] label Optional. Label
35
+ # @return [Array] 0 => output, 1 => exit code
36
+ private def exec(cmd, quiet = false, label = nil)
37
+ label ||= cmd
38
+
39
+ # Run the command
40
+ if quiet
41
+ cmd_output = `#{cmd} 2>&1`
42
+ status = $?.to_i
43
+ else
44
+ cmd_output, status = action(label) { `#{cmd} 2>&1` }
45
+ end
46
+
47
+ # Return exit code and output
48
+ [cmd_output, status]
49
+ end
50
+
51
+
52
+ def build_command(cmd, dir, rails_env, bundler)
53
+ result = ''
54
+ result += "cd #{dir} && " unless dir.nil?
55
+ result += "RAILS_ENV=#{rails_env} " unless rails_env.nil?
56
+ result += 'bundle exec ' if bundler
57
+
58
+ result + cmd
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+ module FerretsOnFire::DSL
2
+ require 'ferrets_on_fire/dsl/declaration'
3
+ require 'ferrets_on_fire/dsl/shell'
4
+ require 'ferrets_on_fire/dsl/logger'
5
+ require 'ferrets_on_fire/dsl/git'
6
+
7
+ include FerretsOnFire::DSL::Declaration
8
+ include FerretsOnFire::DSL::Shell
9
+ include FerretsOnFire::DSL::Logger
10
+ include FerretsOnFire::DSL::Git
11
+
12
+ public def get(name)
13
+ @options[name.to_sym] || @global_options[name.to_sym]
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ require 'gli'
2
+
3
+ class FerretsOnFire::GliWrapper
4
+ include GLI::App
5
+
6
+ public def initialize(desc, commands, options, switches, arguments)
7
+ @_desc = desc
8
+ @_commands = commands || []
9
+ @_options = options || []
10
+ @_switches = switches || []
11
+ @_arguments = arguments || []
12
+
13
+ _setup_basics
14
+ _setup_switches
15
+ _setup_options
16
+ _setup_commands
17
+ end
18
+
19
+ private def _setup_basics
20
+ program_desc @_desc
21
+ end
22
+
23
+ private def _setup_switches
24
+ @_switches.each do |s|
25
+ switch [s.short, s.name], default_value: s.default, desc: s.desc
26
+ end
27
+ end
28
+
29
+ private def _setup_options
30
+ @_options.each do |opt|
31
+ flag [opt.short, opt.name], arg_name: opt.param_name, default_value: opt.default,
32
+ desc: opt.desc, required: !opt.default.nil?
33
+ end
34
+ end
35
+
36
+ private def _setup_commands
37
+ @_commands.each do |cmd|
38
+ desc cmd.desc
39
+ command cmd.name do |api|
40
+ api.action do |global_options, options, args|
41
+ cmd.global_options = global_options
42
+ cmd.options = options
43
+ cmd.args = args
44
+ cmd.action_block.call
45
+ end
46
+ end
47
+
48
+ default_command(cmd.name) if cmd.default
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ class FerretsOnFire::Option
2
+ attr_reader :name, :short, :param_name, :default, :desc
3
+
4
+ public def initialize(name, short, param_name, default, desc)
5
+ @name = name.to_sym
6
+ @short = short.to_sym
7
+ @param_name = param_name
8
+ @default = default
9
+ @desc = desc
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class FerretsOnFire::Switch
2
+ attr_reader :name, :short, :default, :desc
3
+
4
+ public def initialize(name, short, default, desc)
5
+ @name = name.to_sym
6
+ @short = short.to_sym
7
+ @default = default
8
+ @desc = desc
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module FerretsOnFire
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,15 @@
1
+ module FerretsOnFire
2
+ require 'ferrets_on_fire/dsl'
3
+ include FerretsOnFire::DSL
4
+
5
+ public def execute
6
+ @gli = setup_gli
7
+ @gli.run(ARGV)
8
+ exit
9
+ end
10
+
11
+
12
+ public def setup_gli
13
+ GliWrapper.new(@desc, @commands, @options, @switches, @arguments)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ferrets_on_fire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - phortx
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: micon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gli
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.15'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.15'
69
+ description: Supercharge your CLI tools. This gem helps you to write powerful CLI
70
+ tools without caring about the clutter
71
+ email:
72
+ - benny@itws.de
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - README.md
78
+ - lib/_.rb
79
+ - lib/ferrets_on_fire.rb
80
+ - lib/ferrets_on_fire/argument.rb
81
+ - lib/ferrets_on_fire/command.rb
82
+ - lib/ferrets_on_fire/dsl.rb
83
+ - lib/ferrets_on_fire/dsl/declaration.rb
84
+ - lib/ferrets_on_fire/dsl/git.rb
85
+ - lib/ferrets_on_fire/dsl/logger.rb
86
+ - lib/ferrets_on_fire/dsl/shell.rb
87
+ - lib/ferrets_on_fire/gli_wrapper.rb
88
+ - lib/ferrets_on_fire/option.rb
89
+ - lib/ferrets_on_fire/switch.rb
90
+ - lib/ferrets_on_fire/version.rb
91
+ homepage: https://github.com/phortx/ferrets-on-fire
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.10
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Supercharge your CLI
115
+ test_files: []