boiler 0.0.10 → 0.0.11

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.
data/bin/boiler CHANGED
@@ -34,6 +34,24 @@ Escort::App.create do |app|
34
34
  end
35
35
  end
36
36
 
37
+ app.command(:gem) do |command|
38
+ command.summary "Create boilerplate for a new gem"
39
+ command.description "Create all boilerplate for a new gem"
40
+
41
+ command.action do |options, arguments|
42
+ Boiler::Generator::Gem.new(options, arguments).execute
43
+ end
44
+ end
45
+
46
+ app.command(:thrift) do |command|
47
+ command.summary "Create a brand new Thrift service repo"
48
+ command.description "Create a brand new Thrift service repo"
49
+
50
+ command.action do |options, arguments|
51
+ Boiler::Generator::Thrift.new(options, arguments).execute
52
+ end
53
+ end
54
+
37
55
  app.action do |options, arguments|
38
56
  raise "Must provide an argument. Run -h for options."
39
57
  end
data/boiler.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |gem|
22
22
  gem.add_development_dependency 'chalk-rake'
23
23
  gem.add_dependency 'escort'
24
24
  gem.add_dependency 'chalk-log'
25
+ gem.add_dependency 'rubysh'
25
26
  end
@@ -0,0 +1,15 @@
1
+ module Boiler
2
+ module GemRelated
3
+ def gem
4
+ File.basename(@directory)
5
+ end
6
+
7
+ def server
8
+ gem.split('-')[-1]
9
+ end
10
+
11
+ def module
12
+ gem.split('-').map {|c| c.capitalize}.join('::')
13
+ end
14
+ end
15
+ end
@@ -1,10 +1,13 @@
1
1
  require 'fileutils'
2
+ require 'rubysh'
2
3
  require 'chalk-log'
3
4
 
4
5
  module Boiler::Generator
5
6
  class Base
6
7
  include Chalk::Log
7
8
 
9
+ attr_reader :options, :arguments
10
+
8
11
  def initialize(options, arguments)
9
12
  @options = options
10
13
  @arguments = arguments
@@ -16,6 +19,43 @@ module Boiler::Generator
16
19
 
17
20
  protected
18
21
 
22
+ # Manipulations
23
+
24
+ def directory
25
+ raise "Must set @directory to the base" unless @directory
26
+ @directory
27
+ end
28
+
29
+ def relative_mkdir(subdir)
30
+ path = path(subdir)
31
+ mkdir(path)
32
+ end
33
+
34
+ def relative_write_file(file, contents)
35
+ path = path(file)
36
+ write_file(path, contents)
37
+ end
38
+
39
+ def relative_rm_r(tree)
40
+ path = path(tree)
41
+ rm_r(path)
42
+ end
43
+
44
+ def relative_mv(src, dst)
45
+ src_path = path(src)
46
+ dst_path = path(dst)
47
+ mv(src_path, dst_path)
48
+ end
49
+
50
+ def relative_chmod(perms, file)
51
+ path = path(file)
52
+ chmod(perms, path)
53
+ end
54
+
55
+ def path(file)
56
+ File.join(directory, file)
57
+ end
58
+
19
59
  def write_file(file, contents)
20
60
  log.info('writing file', :bytes => contents.bytesize, :file => file)
21
61
  # TODO: disable O_CREAT
@@ -37,5 +77,83 @@ module Boiler::Generator
37
77
  log.info('mkdir', :dir => dir)
38
78
  FileUtils.mkdir(dir)
39
79
  end
80
+
81
+ def rm_r(tree)
82
+ log.info('rm -r', :tree => tree)
83
+ FileUtils.rm_r(tree)
84
+ end
85
+
86
+ def mv(src, dst)
87
+ log.info('mv', :src => src, :dst => dst)
88
+ FileUtils.mv(src, dst)
89
+ end
90
+
91
+ def rubysh(*cmd)
92
+ log.info('rubysh', :cmd => cmd)
93
+ Rubysh.check_call(*cmd)
94
+ end
95
+
96
+ def commit(message)
97
+ Rubysh.check_call('git', 'add', '--all', '.', :cwd => directory)
98
+ Rubysh.check_call('git', 'commit', '-m', message, :cwd => directory)
99
+ end
100
+
101
+ # Check for files
102
+
103
+ def get_gemspec
104
+ glob = path('*.gemspec')
105
+ gemspecs = Dir[glob]
106
+ if gemspecs.length == 0
107
+ raise "No gemspec found matching #{glob}!"
108
+ elsif gemspecs.length > 1
109
+ raise "Found #{gemspecs.length} gemspecs matching #{glob}!"
110
+ end
111
+ gemspecs[0]
112
+ end
113
+
114
+ def get_rakefile
115
+ path = path('Rakefile')
116
+ raise "No Rakefile found at #{path}" unless File.exists?(path)
117
+ path
118
+ end
119
+
120
+ def get_gemfile
121
+ path = path('Gemfile')
122
+ raise "No Gemfile found at #{path}" unless File.exists?(path)
123
+ path
124
+ end
125
+
126
+ def get_license
127
+ path = path('LICENSE.txt')
128
+ raise "No Gemfile found at #{path}" unless File.exists?(path)
129
+
130
+ # Look in environment
131
+ owner = ENV['BOILER_OWNER']
132
+
133
+ # Look in git config
134
+ unless owner
135
+ begin
136
+ runner = rubysh('git', 'config', 'boiler.owner', Rubysh.>)
137
+ rescue Rubysh::Error::BadExitError
138
+ else
139
+ output = runner.read.chomp
140
+ owner = output if output.length > 0
141
+ end
142
+ end
143
+
144
+ unless owner
145
+ log.error("No license owner set (this is used for touching up the LICENSE.txt file). License owner is extracted from ENV['BOILER_OWNER'], followed by `git config boiler.owner'.")
146
+ owner = Readline.readline("Provide your code's license owner: ")
147
+ rubysh('git', 'config', '--global', 'boiler.owner', owner)
148
+ end
149
+
150
+ [path, owner]
151
+ end
152
+
153
+ def get_gitignore
154
+ path = path('.gitignore')
155
+ raise "No .gitignore found at #{path}" unless File.exists?(path)
156
+ path
157
+ end
40
158
  end
41
159
  end
@@ -0,0 +1,45 @@
1
+ module Boiler::Generator
2
+ class Gem < Base
3
+ include Boiler::GemRelated
4
+
5
+ def execute
6
+ raise "Must provide a base directory" unless @directory = @arguments.first
7
+ raise "Directory #{directory} already exists" if File.exists?(directory)
8
+
9
+ Rubysh.check_call('bundle', 'gem', directory)
10
+ Touchup.new(options, arguments).execute
11
+ Suite.new(options, arguments).execute
12
+
13
+ fix_nesting
14
+ commit("Initial commit (generated via `boiler gem')")
15
+ end
16
+
17
+ private
18
+
19
+ def fix_nesting
20
+ # Create lib/chalk-my-thing
21
+ relative_mkdir("lib/#{gem}")
22
+
23
+ # Move version file to lib/chalk-my-thing/version.rb
24
+ relative_mv("lib/#{gem.gsub('-', '/')}/version.rb", "lib/#{gem}/version.rb")
25
+
26
+ # Create lib/chalk-my-thing.rb
27
+ relative_write_file("lib/#{gem}.rb", <<EOF)
28
+ require '#{gem}/version'
29
+
30
+ module #{self.module}
31
+ end
32
+ EOF
33
+
34
+ # Nuke the now-unneeded lib/chalk/my... directory
35
+ relative_rm_r("lib/#{gem.split('-')[0]}")
36
+
37
+ # Update the gemspec to require 'chalk-my-thing/version' rather
38
+ # than 'chalk/my/thing/version'
39
+ gemspec = get_gemspec
40
+ contents = File.read(gemspec)
41
+ contents = contents.gsub(/^require '.*$/, "require '#{gem}/version'")
42
+ write_file(gemspec, contents)
43
+ end
44
+ end
45
+ end
@@ -16,56 +16,22 @@ module Boiler::Generator
16
16
  def template
17
17
  <<-EOF
18
18
  #!/usr/bin/env ruby
19
- require 'logger'
20
- require 'optparse'
21
19
 
22
- $log = Logger.new(STDOUT)
23
- $log.level = Logger::WARN
20
+ require 'chalk-cli'
24
21
 
25
- module MyScript
26
- class MyRunner
27
- def initialize
28
- end
29
-
30
- def run
31
- end
22
+ class ExampleCommand < Chalk::CLI::Command
23
+ def invoke
24
+ log.info("Shaving \#{options[:yaks]} yaks...")
32
25
  end
33
- end
34
-
35
- def main
36
- options = {}
37
- optparse = OptionParser.new do |opts|
38
- opts.banner = "Usage: \#{$0} [options]"
39
-
40
- opts.on('-v', '--verbosity', 'Verbosity of debugging output') do
41
- $log.level -= 1
42
- end
43
26
 
44
- opts.on('-h', '--help', 'Display this message') do
45
- puts opts
46
- exit(1)
47
- end
48
- end
49
- optparse.parse!
50
-
51
- if ARGV.length != 0
52
- puts optparse
53
- return 1
27
+ App = Chalk::CLI::App.new("Usage: \#{$0} [OPTIONS] ARGS")
28
+ App.action(self)
29
+ App.options do |opts|
30
+ opts.opt :yaks, "How many yaks to shave", :short => 'y', :default => 1
54
31
  end
55
-
56
- runner = MyScript::MyRunning.new
57
- runner.run
58
- return 0
59
32
  end
60
33
 
61
- if $0 == __FILE__
62
- ret = main
63
- begin
64
- exit(ret)
65
- rescue TypeError
66
- exit(0)
67
- end
68
- end
34
+ ExampleCommand::App.run_if_invoked(__FILE__)
69
35
  EOF
70
36
  end
71
37
  end
@@ -10,20 +10,6 @@ module Boiler::Generator
10
10
 
11
11
  private
12
12
 
13
- def relative_mkdir(subdir)
14
- path = path(subdir)
15
- mkdir(path)
16
- end
17
-
18
- def relative_write_file(file, contents)
19
- path = path(file)
20
- write_file(path, contents)
21
- end
22
-
23
- def path(file)
24
- File.join(@directory, file)
25
- end
26
-
27
13
  def make_subdirectories
28
14
  [
29
15
  'test',
@@ -0,0 +1,128 @@
1
+ module Boiler::Generator
2
+ class Thrift < Base
3
+ include Boiler::GemRelated
4
+
5
+ def execute
6
+ Gem.new(options, arguments).execute
7
+
8
+ @directory = @arguments.first
9
+ @port = rand(8976) + 1024
10
+
11
+ relative_mkdir('bin')
12
+ create_server
13
+ create_client
14
+ create_thrift_rb
15
+ create_thrift_dir
16
+ add_dependencies
17
+ commit("Add thrift boilerplate (generated via `boiler thrift')")
18
+ end
19
+
20
+ private
21
+
22
+ def create_server
23
+ relative_write_file("bin/#{server}_server.rb", <<EOF)
24
+ #!/usr/bin/env ruby
25
+ require 'bundler/setup'
26
+ require '#{gem}'
27
+
28
+ Thrifty.require('#{server}_service')
29
+
30
+ class #{self.module}::#{server.capitalize}Server < Chalk::Thrift::Server::Base
31
+ interface #{self.module}::Thrift::#{server.capitalize}Service
32
+ set :port, #{@port}
33
+
34
+ def example(arg)
35
+ 'hi'
36
+ end
37
+ end
38
+
39
+ def einhorn_main
40
+ main
41
+ end
42
+
43
+ def main
44
+ #{self.module}::#{server.capitalize}Server.run!
45
+ return 0
46
+ end
47
+
48
+ if $0 == __FILE__
49
+ ret = main
50
+ begin
51
+ exit(ret)
52
+ rescue TypeError
53
+ exit(0)
54
+ end
55
+ end
56
+ EOF
57
+ relative_chmod(0755, "bin/#{server}_server.rb")
58
+ end
59
+
60
+ def create_client
61
+ relative_write_file("bin/#{server}_client.rb", <<EOF)
62
+ #!/usr/bin/env ruby
63
+
64
+ require 'bundler/setup'
65
+ require 'chalk-cli'
66
+ require '#{gem}'
67
+
68
+ Thrifty.require('#{server}_service')
69
+
70
+ class #{self.module}::Client < Chalk::CLI::Command
71
+ def invoke
72
+ thrift_client = Chalk::Thrift::Client::Base.new(
73
+ ['127.0.0.1:#{@port}'],
74
+ :interface => #{self.module}::Thrift::#{server.capitalize}Service
75
+ )
76
+ result = thrift_client.example('argument')
77
+ puts result
78
+ end
79
+
80
+ App = Chalk::CLI::App.new("Usage: #{$0} [OPTIONS] ARGS")
81
+ App.action(self)
82
+ end
83
+
84
+ #{self.module}::Client::App.run_if_invoked(__FILE__)
85
+ EOF
86
+ relative_chmod(0755, "bin/#{server}_client.rb")
87
+ end
88
+
89
+ def create_thrift_rb
90
+ relative_write_file("lib/#{gem}/thrift.rb", <<EOF)
91
+ require 'chalk-thrift'
92
+ require 'thrifty'
93
+ Thrifty.register('thrift/#{server}.thrift',
94
+ relative_to: File.join(__FILE__, '../..')
95
+ )
96
+ EOF
97
+
98
+ gem_rb = path("lib/#{gem}.rb")
99
+ contents = File.read(gem_rb)
100
+ split = contents.split("\n")
101
+ split.insert(1, "require '#{gem}/thrift'")
102
+ write_file(gem_rb, split.join("\n"))
103
+ end
104
+
105
+ def create_thrift_dir
106
+ relative_mkdir('thrift')
107
+ relative_write_file("thrift/#{server}.thrift", <<EOF)
108
+ namespace rb #{self.module.gsub('::', '.')}.Thrift
109
+
110
+ service #{server.capitalize}Service {
111
+ string example(1: string arg);
112
+ }
113
+ EOF
114
+ end
115
+
116
+ def add_dependencies
117
+ gemspec = get_gemspec
118
+ contents = File.read(gemspec)
119
+ contents = contents.gsub(/^end$/, <<EOF.chomp)
120
+ spec.add_dependency 'chalk-thrift'
121
+ spec.add_dependency 'thrifty'
122
+ spec.add_dependency 'chalk-cli'
123
+ end
124
+ EOF
125
+ write_file(gemspec, contents)
126
+ end
127
+ end
128
+ end
@@ -1,3 +1,5 @@
1
+ require 'readline'
2
+
1
3
  module Boiler::Generator
2
4
  class Touchup < Base
3
5
  def execute
@@ -8,10 +10,14 @@ module Boiler::Generator
8
10
  gemspec = get_gemspec
9
11
  rakefile = get_rakefile
10
12
  gemfile = get_gemfile
13
+ license, owner = get_license
14
+ gitignore = get_gitignore
11
15
 
12
16
  modify_gemspec(gemspec)
13
17
  modify_rakefile(rakefile)
14
18
  modify_gemfile(gemfile)
19
+ modify_license(license, owner)
20
+ modify_gitignore(gitignore)
15
21
  end
16
22
 
17
23
  private
@@ -20,29 +26,6 @@ module Boiler::Generator
20
26
  File.join(@directory, file)
21
27
  end
22
28
 
23
- def get_gemspec
24
- glob = path('*.gemspec')
25
- gemspecs = Dir[glob]
26
- if gemspecs.length == 0
27
- raise "No gemspec found matching #{glob}!"
28
- elsif gemspecs.length > 1
29
- raise "Found #{gemspecs.length} gemspecs matching #{glob}!"
30
- end
31
- gemspecs[0]
32
- end
33
-
34
- def get_rakefile
35
- path = path('Rakefile')
36
- raise "No Rakefile found at #{path}" unless File.exists?(path)
37
- path
38
- end
39
-
40
- def get_gemfile
41
- path = path('Gemfile')
42
- raise "No Gemfile found at #{path}" unless File.exists?(path)
43
- path
44
- end
45
-
46
29
  def modify_gemspec(gemspec)
47
30
  lines = File.read(gemspec).gsub('"', "'").split("\n")
48
31
  index = lines.index {|line| line =~ /(gem|spec)\.require_paths/}
@@ -95,5 +78,22 @@ EOF
95
78
  end
96
79
  contents = write_file(gemfile, contents)
97
80
  end
81
+
82
+ def modify_license(license, owner)
83
+ contents = File.read(license)
84
+ contents = contents.gsub(/\A(Copyright.*\d+ )(.*)/, '\1' + owner)
85
+ old_owner = $2
86
+
87
+ unless owner == old_owner
88
+ log.info('license update', :old_owner => old_owner, :owner => owner)
89
+ contents = write_file(license, contents)
90
+ end
91
+ end
92
+
93
+ def modify_gitignore(gitignore)
94
+ append_to_file(gitignore, <<EOF)
95
+ build-thrifty
96
+ EOF
97
+ end
98
98
  end
99
99
  end
@@ -2,6 +2,8 @@ module Boiler::Generator
2
2
  end
3
3
 
4
4
  require 'boiler/generator/base'
5
+ require 'boiler/generator/gem'
5
6
  require 'boiler/generator/script'
6
7
  require 'boiler/generator/suite'
7
8
  require 'boiler/generator/touchup'
9
+ require 'boiler/generator/thrift'
@@ -1,3 +1,3 @@
1
1
  module Boiler
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
data/lib/boiler.rb CHANGED
@@ -2,4 +2,5 @@ module Boiler
2
2
  end
3
3
 
4
4
  require 'boiler/version'
5
+ require 'boiler/gem_related'
5
6
  require 'boiler/generator'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-18 00:00:00.000000000 Z
12
+ date: 2013-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rubysh
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  description: A boilerplate generator
111
127
  email:
112
128
  - gdb@gregbrockman.com
@@ -123,10 +139,13 @@ files:
123
139
  - bin/boiler
124
140
  - boiler.gemspec
125
141
  - lib/boiler.rb
142
+ - lib/boiler/gem_related.rb
126
143
  - lib/boiler/generator.rb
127
144
  - lib/boiler/generator/base.rb
145
+ - lib/boiler/generator/gem.rb
128
146
  - lib/boiler/generator/script.rb
129
147
  - lib/boiler/generator/suite.rb
148
+ - lib/boiler/generator/thrift.rb
130
149
  - lib/boiler/generator/touchup.rb
131
150
  - lib/boiler/version.rb
132
151
  homepage: ''
@@ -143,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
162
  version: '0'
144
163
  segments:
145
164
  - 0
146
- hash: -780451879940532576
165
+ hash: 2570016117693499363
147
166
  required_rubygems_version: !ruby/object:Gem::Requirement
148
167
  none: false
149
168
  requirements:
@@ -152,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
171
  version: '0'
153
172
  segments:
154
173
  - 0
155
- hash: -780451879940532576
174
+ hash: 2570016117693499363
156
175
  requirements: []
157
176
  rubyforge_project:
158
177
  rubygems_version: 1.8.23