toys 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddcf431eae87a4ef5781ee240d99dca2bf39b9ec3d4f2343158c4f1a3e593078
4
- data.tar.gz: b7b0fb7173d9e58c42814def124141af2383024c982a30096773dc835718f57c
3
+ metadata.gz: 562abaf505246d6b3a84c800908b19cfe9acbc6a57d5432bceccac31eaa107ef
4
+ data.tar.gz: 39efb6120be94eeb4479e8ae4fe782d796a49e5dcfd4e837735dd69986e3cfb4
5
5
  SHA512:
6
- metadata.gz: 5dc3a473a1a2288cfc58f928629d491c4227d73a9765aae48b4384fee391999d4e096fd1fc46a1eb3290a982877a46028c5279c6ed3d863e95b11d6f6a916249
7
- data.tar.gz: 378aa6465225148659697d6c4c4b75720c6f182c7539b7ef922da601fac94d5ad950a50265452d4439e9c178744b3d4906ec22d9319d444fd4c8163ac97396a0
6
+ metadata.gz: fe6196e43402bce7c7e916dcc75906e5b420ce1882ed0ca6104b6f0a54c6263feba8bf2284c1e8b8d2ca783ed1dacb291078622912f84483628b9f003397ca9e
7
+ data.tar.gz: bd24a58dcdc1fd2f34c50b97d57129e031f17aaa77044af1c6b62a86d67a1ba32506d3234156cb66576f282de82953e95cf52460fc409f86a82ff5d6c64e42de
data/lib/toys.rb CHANGED
@@ -6,5 +6,6 @@ require "toys/context"
6
6
  require "toys/errors"
7
7
  require "toys/lookup"
8
8
  require "toys/parser"
9
+ require "toys/template"
9
10
  require "toys/tool"
10
11
  require "toys/version"
@@ -1,9 +1,39 @@
1
1
  short_desc "A collection of system commands for toys"
2
2
  long_desc "A collection of system commands for toys"
3
3
 
4
+
4
5
  name "version" do
6
+
5
7
  short_desc "Print current toys version"
8
+
6
9
  execute do
7
10
  puts Toys::VERSION
8
11
  end
12
+
13
+ end
14
+
15
+
16
+ name "update" do
17
+
18
+ short_desc "Update toys if a newer version is available"
19
+
20
+ helper_module :exec
21
+
22
+ execute do
23
+ version_info = capture("gem query -q -r -e toys")
24
+ if version_info =~ /toys\s\((.+)\)/
25
+ latest_version = Gem::Version.new($1)
26
+ cur_version = Gem::Version.new(Toys::VERSION)
27
+ if latest_version > cur_version
28
+ logger.warn("Updating toys from #{cur_version} to #{latest_version}...")
29
+ sh("gem install toys")
30
+ else
31
+ logger.warn("Toys is already at the latest version: #{latest_version}")
32
+ end
33
+ else
34
+ logger.error("Could not get latest toys version")
35
+ exit(1)
36
+ end
37
+ end
38
+
9
39
  end
data/lib/toys/context.rb CHANGED
@@ -25,7 +25,7 @@ module Toys
25
25
  tool.execute(self, args.slice(tool.full_name.length..-1))
26
26
  end
27
27
 
28
- def exit_with_code(code)
28
+ def exit(code)
29
29
  throw :result, code
30
30
  end
31
31
 
@@ -40,7 +40,7 @@ module Toys
40
40
 
41
41
  def handle_status(status)
42
42
  if status != 0 && @config[:report_subprocess_errors]
43
- @context.exit_with_code(status)
43
+ @context.exit(status)
44
44
  end
45
45
  status
46
46
  end
@@ -0,0 +1,7 @@
1
+ require "fileutils"
2
+
3
+ module Toys
4
+ module Helpers
5
+ FileUtils = ::FileUtils
6
+ end
7
+ end
data/lib/toys/parser.rb CHANGED
@@ -60,6 +60,20 @@ module Toys
60
60
  self
61
61
  end
62
62
 
63
+ def expand(template, opts={})
64
+ if template.is_a?(Symbol)
65
+ template = template.to_s
66
+ file_name = template.gsub(/([a-zA-Z])([A-Z])/){ |m| "#{$1}_#{$2.downcase}" }.downcase
67
+ require "toys/templates/#{file_name}"
68
+ const_name = template.gsub(/(^|_)([a-zA-Z0-9])/){ |m| $2.upcase }
69
+ template = Toys::Templates.const_get(const_name)
70
+ end
71
+ opts = template.opts_initializer.call(opts)
72
+ yield opts if block_given?
73
+ instance_exec(opts, &template.expander)
74
+ self
75
+ end
76
+
63
77
  def long_desc(desc)
64
78
  @tool.long_desc = desc
65
79
  self
@@ -0,0 +1,21 @@
1
+ module Toys
2
+ class Template
3
+ def initialize
4
+ @opts_initializer = ->(opts){opts}
5
+ @expander = ->(opts){}
6
+ end
7
+
8
+ def to_init_opts(&block)
9
+ @opts_initializer = block
10
+ self
11
+ end
12
+
13
+ def to_expand(&block)
14
+ @expander = block
15
+ self
16
+ end
17
+
18
+ attr_reader :opts_initializer
19
+ attr_reader :expander
20
+ end
21
+ end
@@ -0,0 +1,63 @@
1
+ require "rubygems/package"
2
+
3
+ module Toys
4
+ module Templates
5
+ GemBuild = Toys::Template.new
6
+
7
+ GemBuild.to_init_opts do |opts|
8
+ {
9
+ name: "build",
10
+ gem_name: nil,
11
+ push_gem: false,
12
+ tag: false,
13
+ push_tag: false
14
+ }.merge(opts)
15
+ end
16
+
17
+ GemBuild.to_expand do |opts|
18
+ toy_name = opts[:name] || "build"
19
+ gem_name = opts[:gem_name]
20
+ unless gem_name
21
+ candidates = ::Dir.glob("*.gemspec")
22
+ if candidates.size > 0
23
+ gem_name = candidates.first.sub(/\.gemspec$/, "")
24
+ else
25
+ raise Toys::ToysDefinitionError, "Could not find a gemspec"
26
+ end
27
+ end
28
+ push_gem = opts[:push_gem]
29
+ tag = opts[:tag]
30
+ push_tag = opts[:push_tag]
31
+
32
+ name toy_name do
33
+ short_desc "#{push_gem ? 'Release' : 'Build'} the gem: #{gem_name}"
34
+
35
+ helper_module :file_utils
36
+ helper_module :exec
37
+
38
+ execute do
39
+ gemspec = Gem::Specification.load "#{gem_name}.gemspec"
40
+ version = gemspec.version
41
+ gemfile = "#{gem_name}-#{version}.gem"
42
+ Gem::Package.build gemspec
43
+ mkdir_p "pkg"
44
+ mv gemfile, "pkg"
45
+ if push_gem
46
+ if File.directory?(".git") && capture("git status -s").strip != ""
47
+ logger.error "Cannot push the gem when there are uncommited changes"
48
+ exit(1)
49
+ end
50
+ sh "gem push pkg/#{gemfile}", report_subprocess_errors: true
51
+ if tag
52
+ sh "git tag v#{version}", report_subprocess_errors: true
53
+ if push_tag
54
+ push_tag = "origin" if push_tag == true
55
+ sh "git push #{push_tag} v#{version}", report_subprocess_errors: true
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,41 @@
1
+ require "shellwords"
2
+
3
+ module Toys
4
+ module Templates
5
+ Minitest = Toys::Template.new
6
+
7
+ Minitest.to_init_opts do |opts|
8
+ {
9
+ name: "test",
10
+ libs: ["lib", "test"],
11
+ test_files: [],
12
+ warning: true
13
+ }.merge(opts)
14
+ end
15
+
16
+ Minitest.to_expand do |opts|
17
+ toy_name = opts[:name] || "build"
18
+ libs = opts[:libs] || []
19
+ warning = opts[:warning]
20
+ test_files = opts[:test_files] || []
21
+ lib_path = libs.join(File::PATH_SEPARATOR)
22
+ cmd = []
23
+ cmd << File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])
24
+ cmd << "-I#{lib_path}" unless libs.empty?
25
+ cmd << "-w" if warning
26
+ cmd << "-e" << "ARGV.each{|f| load f}"
27
+ cmd << "--"
28
+ cmd = Shellwords.join(cmd + test_files)
29
+
30
+ name toy_name do
31
+ short_desc "Run minitest"
32
+
33
+ helper_module :exec
34
+
35
+ execute do
36
+ sh(cmd, report_subprocess_errors: true)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/toys/tool.rb CHANGED
@@ -156,7 +156,7 @@ module Toys
156
156
  mod = mod.to_s
157
157
  file_name = mod.gsub(/([a-zA-Z])([A-Z])/){ |m| "#{$1}_#{$2.downcase}" }.downcase
158
158
  require "toys/helpers/#{file_name}"
159
- const_name = mod.gsub(/_([a-zA-Z0-9])/){ |m| $1.upcase }.capitalize
159
+ const_name = mod.gsub(/(^|_)([a-zA-Z0-9])/){ |m| $2.upcase }
160
160
  @modules << Toys::Helpers.const_get(const_name)
161
161
  when String
162
162
  @modules << mod
data/lib/toys/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Toys
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-18 00:00:00.000000000 Z
11
+ date: 2018-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,7 +108,7 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0.9'
111
- description: Command line tool framework
111
+ description: A simple command line tool framework
112
112
  email:
113
113
  - dazuma@gmail.com
114
114
  executables:
@@ -123,8 +123,12 @@ files:
123
123
  - lib/toys/context.rb
124
124
  - lib/toys/errors.rb
125
125
  - lib/toys/helpers/exec.rb
126
+ - lib/toys/helpers/file_utils.rb
126
127
  - lib/toys/lookup.rb
127
128
  - lib/toys/parser.rb
129
+ - lib/toys/template.rb
130
+ - lib/toys/templates/gem_build.rb
131
+ - lib/toys/templates/minitest.rb
128
132
  - lib/toys/tool.rb
129
133
  - lib/toys/version.rb
130
134
  homepage: https://github.com/dazuma/toys