process_sip 0.1.0

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
+ SHA256:
3
+ metadata.gz: 17b9364de22ae3b02b0a75a8784b4c901aeef3b54dbc6e5f38d6da60650a69b5
4
+ data.tar.gz: f461fc3bf4e53946752c408e66573dc74ab02e18f0df6bd6d886c06b2e3ab985
5
+ SHA512:
6
+ metadata.gz: af80279a0da2234735734fb08bf8ac28601cccd2527bb527135436d10749c510ab8f69719266dcbe04fb7c109bdec562be9469def2953945152a0f52b730d175
7
+ data.tar.gz: 6e541702d7999251bb03e55a08e26fa4bbda7d47fd916ae0c652fe3a6b3b9c67feee09557c686f321308a98cba76e3686fdbd20492f3dce6d4325bcff5d63ad2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-09-03
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in process_sip.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest"
11
+ gem "minitest-sprint"
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ process_sip (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.22.2)
10
+ minitest-sprint (1.2.2)
11
+ path_expander (~> 1.1)
12
+ path_expander (1.1.1)
13
+ rake (13.1.0)
14
+
15
+ PLATFORMS
16
+ arm64-darwin-20
17
+ arm64-darwin-23
18
+
19
+ DEPENDENCIES
20
+ minitest
21
+ minitest-sprint
22
+ process_sip!
23
+ rake (~> 13.0)
24
+
25
+ BUNDLED WITH
26
+ 2.5.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Kasper Timm Hansen
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,82 @@
1
+ # ProcessSip
2
+
3
+ ProcessSip lets you make ad-hoc adapters for CLIs to interface with from Ruby.
4
+
5
+ Here's wrapping `git`:
6
+
7
+ ```ruby
8
+ git = ProcessSip.git # Connect an adapter to an executable, here `git`.
9
+
10
+ # Any subcommands are automatically proxied, so:
11
+ git.branch.call # Calls `git branch` on the commandline.
12
+ git.branch :remote # Automatically calls when passed arguments, `git branch --remote`.
13
+ git.branch :r # `git branch -r`. 1-count symbols get prefixed with -, while anything else gets --.
14
+
15
+ git.branch :show_current # Get the current branch: `git branch --show-current`.
16
+
17
+ git.diff.call # `git diff`
18
+ git.status :name_only # `git diff --name-only`
19
+ ```
20
+
21
+ And here's `bundle`:
22
+
23
+ ```ruby
24
+ # Run `bundle install` and stream each line into the block:
25
+ ProcessSip.bundle.install.stream do |line|
26
+ puts line
27
+ end
28
+
29
+ ProcessSip.bundle.update.call # `bundle update`
30
+ ProcessSip.bundle.update :only_bundler # `bundle update --only-bundler`
31
+
32
+ ProcessSip.gem.update :system # `gem update --system`
33
+ ```
34
+
35
+ The real power of ProcessSip is to extend the default proxying with your ad-hoc needs. So you can extend the `git` adapter like this:
36
+
37
+ ```ruby
38
+ # Open an adapter for a specific executable, here `git`.
39
+ ProcessSip.git do
40
+ def commit_all(message)
41
+ add "." and commit message
42
+ end
43
+
44
+ def commit(message)
45
+ super :m, message
46
+ end
47
+
48
+ def with_work_tree = with(work_tree: __dir__)
49
+ def with_git_dir = with(git_dir: __dir__ + "/.git")
50
+ end
51
+
52
+ git = ProcessSip.git.with_work_tree.with_git_dir
53
+
54
+ # Now every git command will use the --work-tree and --git-dir context arguments
55
+ git.branch.call # `git --work-tree= --git-dir branch`
56
+
57
+ deleted_lines = git.diff.select { _1.start_with?("- ") } # `git --work-tree= --git-dir diff`
58
+ ```
59
+
60
+ ## Installation
61
+
62
+ Install the gem and add to the application's Gemfile by executing:
63
+
64
+ $ bundle add process_sip
65
+
66
+ If bundler is not being used to manage dependencies, install the gem by executing:
67
+
68
+ $ gem install process_sip
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kaspth/process_sip.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ProcessSip::Refinements
4
+ refine(Symbol) { def dasherize = name.dasherize }
5
+ refine(String) { def dasherize = tr("_", "-") }
6
+
7
+ refine Object do
8
+ def clone_with(**instance_variables)
9
+ clone.tap do |object|
10
+ instance_variables.each { object.instance_variable_set "@#{_1}", _2 }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ProcessSip
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shellwords"
4
+ require_relative "process_sip/version"
5
+
6
+ module ProcessSip
7
+ require_relative "process_sip/refinements"
8
+ using Refinements
9
+
10
+ # ProcessSip.load_each :git, :curl
11
+ def self.load_each(*names)
12
+ names.each do
13
+ require "lib/process_sip/adapters/#{_1}"
14
+ end
15
+ end
16
+
17
+ def self.method_missing(name, &)
18
+ Class.new(Adapter, &).new(name).tap do |adapter|
19
+ define_singleton_method(name) do |&block|
20
+ adapter.class.class_eval(&block) if block
21
+ adapter
22
+ end
23
+ end
24
+ end
25
+
26
+ class Adapter
27
+ def initialize(name)
28
+ @name, @context, @preprint = name.dasherize, Context.new, false
29
+ end
30
+
31
+ def omit(*keys) = clone_with(context: @context.except(*keys))
32
+ def with(**options) = clone_with(context: @context.merge(**options))
33
+
34
+ def call(name, *, **, &block)
35
+ chain = [@name, @context.arguments, name.to_s.dasherize, process(*, **)].flatten.map(&:shellescape).join(" ")
36
+ puts chain if @preprint
37
+
38
+ IO.popen(chain, &block || -> { _1.read.chomp })
39
+ end
40
+
41
+ ruby2_keywords def method_missing(name, *arguments, &block)
42
+ Subcommand.new(self, name).then do |apply|
43
+ if block_given? || arguments.any?
44
+ apply.call(*arguments, &block)
45
+ else
46
+ apply
47
+ end
48
+ end
49
+ end
50
+
51
+ def preprint = clone_with(preprint: true)
52
+ def silent = clone_with(preprint: false)
53
+
54
+ private
55
+ def process(*arguments, **options)
56
+ arguments.map { option_name _1 } + options.transform_keys { option_name _1 }.flatten
57
+ end
58
+
59
+ def option_name(name)
60
+ name.is_a?(Symbol) ? "#{name.size > 1 ? "--" : "-"}#{name.dasherize}" : name
61
+ end
62
+ end
63
+
64
+ class Subcommand < Data.define(:adapter, :name)
65
+ include Enumerable
66
+
67
+ def each(&) = stream(&)
68
+ alias each_line each
69
+
70
+ def match?(...) = call.match?(...)
71
+ def readlines(chomp: true) = call { _1.readlines(chomp:) }
72
+
73
+ def stream(*, **)
74
+ return enum_for(:stream, *, **) unless block_given?
75
+
76
+ call(*, **) do |io|
77
+ while line = io.gets&.chomp; yield line; end
78
+ end
79
+ end
80
+
81
+ def call(...) = adapter.call(name, ...)
82
+ alias method_missing call
83
+ alias read call
84
+ end
85
+
86
+ class Context
87
+ def initialize(**options)
88
+ @options = options
89
+ @arguments = @options.map { ["--#{_1.dasherize}", _2.shellescape].join("=") }
90
+ end
91
+ attr_reader :arguments
92
+
93
+ def except(*) = self.class.new(**@options.except(*))
94
+ def merge(**options) = self.class.new(**@options, **options)
95
+ end
96
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/process_sip/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "process_sip"
7
+ spec.version = ProcessSip::VERSION
8
+ spec.authors = ["Kasper Timm Hansen"]
9
+ spec.email = ["hey@kaspth.com"]
10
+
11
+ spec.summary = "Make ad-hoc adapters for CLIs to interface with from Ruby."
12
+ spec.homepage = "https://github.com/kaspth/process_sip"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.3.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ end
data/test.rb ADDED
@@ -0,0 +1,38 @@
1
+ require_relative "lib/process_sip"
2
+
3
+ # lib/process_sip/executables/git.rb
4
+ git = ProcessSip.git do
5
+ def commit_all(message)
6
+ add "." and commit message
7
+ end
8
+
9
+ def commit(message)
10
+ super :m, message
11
+ end
12
+ end
13
+ p git
14
+
15
+ git = ProcessSip.git do
16
+ def with_work_tree = with(work_tree: __dir__)
17
+ def with_git_dir = with(git_dir: __dir__ + "/.git")
18
+ end
19
+ p git
20
+
21
+ git = ProcessSip.git.preprint
22
+ p git
23
+ define_method(:git) { git }
24
+
25
+ binding.irb
26
+
27
+ # git.with_work_tree.with(git_dir: __dir__ + "/.git").omit(:git_dir, :work_tree)
28
+ # git.with_work_tree.with_git_dir.omit(:git_dir, :work_tree)
29
+ # git.with_work_tree.silent.preprint.branch :d, "branch"
30
+
31
+ # def git.commit(message) = super(:m, message)
32
+ #
33
+ # def git.commit_all(message)
34
+ # add "." and commit message
35
+ # end
36
+ #
37
+ # git_dir = git.with(git_dir: __dir__)
38
+ # define_method(:git_dir) { git_dir }
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: process_sip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Timm Hansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - hey@kaspth.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/process_sip.rb
27
+ - lib/process_sip/refinements.rb
28
+ - lib/process_sip/version.rb
29
+ - process_sip.gemspec
30
+ - test.rb
31
+ homepage: https://github.com/kaspth/process_sip
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/kaspth/process_sip
36
+ source_code_uri: https://github.com/kaspth/process_sip
37
+ changelog_uri: https://github.com/kaspth/process_sip/blob/main/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.3.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.5.6
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Make ad-hoc adapters for CLIs to interface with from Ruby.
57
+ test_files: []