thor_template 1.0.1 → 2.0.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
  SHA1:
3
- metadata.gz: 44d85a7deb099da700004cc428aef7ed734df492
4
- data.tar.gz: ddd667d6c802e10439166ef320aba8745cceecb6
3
+ metadata.gz: ab6ae7155ca474bcc468b645793b3f0ce331032d
4
+ data.tar.gz: 3f9260280b4788cd605e8f205d45507cca97e57f
5
5
  SHA512:
6
- metadata.gz: 515e6f2b5530463b3a58e09fbd74a0267d16e325b33894cc9c2163b952171e0c12be86970c7e1194732d466a4c81011b55b877fa158ef52f2da1197847b6f18a
7
- data.tar.gz: fda470e98ee5156d51dccb747d4853037a1d1f1f0f7683c021e47b7e786e51c39c5e7f83cb49506ced328a29a50e9205cf5b4e8d10dcd532198b0057234290b1
6
+ metadata.gz: c1b93e6a5b2faf163e5e7030659f9474a64f71ab40343660680e36d2677974d50933d0392989e0ee3d80df7f0340e982f28ea6d2bf291db314df2e5eeb434d8e
7
+ data.tar.gz: 0d4f7e668e79df3d3d806889fa13a8cead2211800bbb5c7dc27cc6e1f841aae79d996cf1b68dad57cb4e96039a21ddd74dee74f399a26357f6843f1d63ad92a4
data/CHANGELOG.md CHANGED
@@ -3,6 +3,11 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [2.0.0]
7
+
8
+ - Fix project names that are named with dashes and camel case.
9
+ - Add legit specs that test each case.
10
+
6
11
  ## [1.0.1]
7
12
 
8
13
  - require "fileutils" fix
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
+ [![Build Thor CLI Project in Under a Second](https://img.youtube.com/vi/GcVhdoneZYk/0.jpg)](https://www.youtube.com/watch?v=GcVhdoneZYk)
2
+
1
3
  # ThorTemplate
2
4
 
3
5
  [![Build Status](https://travis-ci.org/tongueroo/thor_template.svg?branch=generator)](https://travis-ci.org/tongueroo/thor_template)
4
6
  [![Code Climate](https://codeclimate.com/github/tongueroo/thor_template.png)](https://codeclimate.com/github/tongueroo/thor_template)
5
7
 
6
- This is a generator tool that builds a starter cli project based on thor.
8
+ `thor_template` is a generator tool that builds a starter CLI project based on [Thor](). This blog post, [Build Thor CLI Project in Under a Second](https://blog.boltops.com/2017/09/14/build-thor-cli-project-in-under-a-second), also covers usage and also contains a video demo.
7
9
 
8
10
  ## Installation
9
11
 
@@ -14,12 +16,12 @@ gem install thor_template
14
16
  ## Usage
15
17
 
16
18
  ```sh
17
- thor_template new foo
18
- cd foo
19
- bin/foo hello world
19
+ thor_template new mycli
20
+ cd mycli
21
+ bin/mycli hello world
20
22
  ```
21
23
 
22
- The above generated a starter cli project called foo with a working hello command. The generated project also has starter specs for you 😁
24
+ The above generated a starter CLI project called mycli with a working hello command. The generated project also has starter specs for you 😁
23
25
 
24
26
  ```sh
25
27
  $ rake
@@ -32,26 +34,3 @@ Finished in 0.09672 seconds (files took 0.20945 seconds to load)
32
34
  1 example, 0 failures
33
35
  $
34
36
  ```
35
-
36
- ## Dashes Problem
37
-
38
- Names with dashes don't work quite right with this tool. For example:
39
-
40
- ```
41
- thor_template new my-project
42
- ```
43
-
44
- This results in some the files and text not being renamed correctly. I usually work around this by creating a project with the underscored name first:
45
-
46
- ```
47
- thor_template new my_project
48
- ```
49
-
50
- Then I rename things 3 spots:
51
-
52
- * my_project.gemspec - Change the name from my_project to my-project. So the gem name has a dash.
53
- * bin/my_project - Change the name from bin/my_project to bin/my-project. So the binary has a dash. The require in the file also needs to be fixed.
54
- * lib/my_project.rb - Change the name from lib/my_project.rb to lib/my-project.rb. So if the project is required it is `require my-project`.
55
- * spec/spec_helper.rb - Change the require lib/my_project.rb to lib/my-project.rb. And spec/lib/cli_spec. So the test passes.
56
-
57
- Hope to fix this one day! Also, pull request are appreciated!
@@ -1,6 +1,6 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in thor_template.gemspec
3
+ # Specify your gem dependencies in thor_template.gemspec
4
4
  gemspec
5
5
 
6
6
  gem "codeclimate-test-reporter", group: :test, require: nil
@@ -1,12 +1,12 @@
1
- guard 'rspec' do
1
+ guard "rspec" do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/(.+)\.rb$}) { "spec/thor_template_spec.rb" }
4
4
  watch(%r{^lib/thor_template/(.+)\.rb$}) { "spec/thor_template_spec.rb" }
5
- watch('spec/spec_helper.rb') { "spec/thor_template_spec.rb" }
5
+ watch("spec/spec_helper.rb") { "spec/thor_template_spec.rb" }
6
6
  watch(%r{^lib/thor_template/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
7
  end
8
8
 
9
- guard 'bundler' do
10
- watch('Gemfile')
9
+ guard "bundler" do
10
+ watch("Gemfile")
11
11
  watch(/^.+\.gemspec/)
12
12
  end
@@ -12,7 +12,7 @@ TODO: Write a gem description
12
12
 
13
13
  Add this line to your application's Gemfile:
14
14
 
15
- gem 'thor_template'
15
+ gem "thor_template"
16
16
 
17
17
  And then execute:
18
18
 
@@ -32,13 +32,13 @@ rake rename
32
32
  rm -rf .git
33
33
  git init
34
34
  git add .
35
- git commit -m 'init commit'
35
+ git commit -m "init commit"
36
36
  </pre>
37
37
 
38
38
  ## Contributing
39
39
 
40
40
  1. Fork it
41
41
  2. Create your feature branch (`git checkout -b my-new-feature`)
42
- 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 3. Commit your changes (`git commit -am "Add some feature"`)
43
43
  4. Push to the branch (`git push origin my-new-feature`)
44
44
  5. Create new Pull Request
@@ -1,32 +1,6 @@
1
- # require "bundler/gem_tasks"
1
+ require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
4
  task :default => :spec
5
5
 
6
6
  RSpec::Core::RakeTask.new
7
-
8
- # DELETE AFTER USING
9
- desc "Rename project"
10
- task :rename do
11
- name = ENV['NAME'] || File.basename(Dir.pwd)
12
- camelize = lambda do |str|
13
- str.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
14
- end
15
- dir = Dir['**/thor_template*']
16
- begin
17
- from = dir.pop
18
- if from
19
- to = from.split('/')
20
- to[-1].gsub!('thor_template', name)
21
- FileUtils.mv(from, to.join('/'))
22
- end
23
- end while dir.length > 0
24
- Dir["**/*"].each do |path|
25
- if File.file?(path)
26
- `sed -i '' 's/thor_template/#{name}/g' #{path}`
27
- `sed -i '' 's/ThorTemplate/#{camelize.call(name)}/g' #{path}`
28
- no_space = File.read(path).gsub(/\s+\z/, '')
29
- File.open(path, 'w') { |f| f.write(no_space) }
30
- end
31
- end
32
- end
@@ -7,8 +7,8 @@ Signal.trap("INT") {
7
7
  exit
8
8
  }
9
9
 
10
- $:.unshift(File.expand_path('../../lib', __FILE__))
11
- require 'thor_template'
12
- require 'thor_template/cli'
10
+ $:.unshift(File.expand_path("../../lib", __FILE__))
11
+ require "USER_PROVIDED_NAME"
12
+ require "thor_template/cli"
13
13
 
14
14
  ThorTemplate::CLI.start(ARGV)
@@ -1,5 +1,5 @@
1
- require 'thor'
2
- require 'thor_template/cli/help'
1
+ require "thor"
2
+ require "thor_template/cli/help"
3
3
 
4
4
  module ThorTemplate
5
5
 
@@ -9,7 +9,7 @@ module ThorTemplate
9
9
 
10
10
  desc "hello NAME", "say hello to NAME"
11
11
  long_desc Help.hello
12
- option :from, desc: 'from person'
12
+ option :from, desc: "from person"
13
13
  def hello(name)
14
14
  puts "from: #{options[:from]}" if options[:from]
15
15
  puts "Hello #{name}"
@@ -1,4 +1,4 @@
1
- require 'thor'
1
+ require "thor"
2
2
 
3
3
  module ThorTemplate
4
4
  class Command < Thor
@@ -2,6 +2,6 @@ $:.unshift(File.expand_path("../", __FILE__))
2
2
  require "thor_template/version"
3
3
 
4
4
  module ThorTemplate
5
- autoload :Command, 'thor_template/command'
6
- autoload :CLI, 'thor_template/cli'
5
+ autoload :Command, "thor_template/command"
6
+ autoload :CLI, "thor_template/cli"
7
7
  end
@@ -1,8 +1,8 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
- # to run specs with what's remembered from vcr
3
+ # to run specs with what"s remembered from vcr
4
4
  # $ rake
5
- #
5
+ #
6
6
  # to run specs with new fresh data from aws api calls
7
7
  # $ rake clean:vcr ; time rake
8
8
  describe ThorTemplate::CLI do
@@ -10,9 +10,9 @@ describe ThorTemplate::CLI do
10
10
  @args = "--from Tung"
11
11
  end
12
12
 
13
- describe "thor_template" do
13
+ describe "USER_PROVIDED_NAME" do
14
14
  it "should hello world" do
15
- out = execute("bin/thor_template hello world #{@args}")
15
+ out = execute("bin/USER_PROVIDED_NAME hello world #{@args}")
16
16
  expect(out).to include("from: Tung\nHello world")
17
17
  end
18
18
  end
@@ -1,18 +1,18 @@
1
- ENV['TEST'] = '1'
1
+ ENV["TEST"] = "1"
2
2
 
3
- require 'simplecov'
3
+ require "simplecov"
4
4
  SimpleCov.start
5
5
 
6
6
  require "pp"
7
7
 
8
- root = File.expand_path('../../', __FILE__)
9
- require "#{root}/lib/thor_template"
8
+ root = File.expand_path("../../", __FILE__)
9
+ require "#{root}/lib/USER_PROVIDED_NAME"
10
10
 
11
11
  module Helpers
12
12
  def execute(cmd)
13
- puts "Running: #{cmd}" if ENV['DEBUG']
13
+ puts "Running: #{cmd}" if ENV["DEBUG"]
14
14
  out = `#{cmd}`
15
- puts out if ENV['DEBUG']
15
+ puts out if ENV["DEBUG"]
16
16
  out
17
17
  end
18
18
  end
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'thor_template/version'
4
+ require "thor_template/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "thor_template"
@@ -1,17 +1,20 @@
1
1
  require 'fileutils'
2
+ require 'colorize'
3
+ require 'active_support/core_ext/string'
4
+ require "byebug"
2
5
 
3
6
  module ThorTemplate
4
7
  class Generator
5
8
  attr_reader :options
6
9
  def initialize(options={})
7
10
  @options = options
11
+ Dir.chdir(options[:cwd]) if options[:cwd]
8
12
  @name = options[:name]
9
13
  end
10
14
 
11
15
  def run
12
16
  copy
13
17
  rename
14
- rewrite
15
18
  git
16
19
  puts "Created #{@name} project!"
17
20
  end
@@ -26,9 +29,9 @@ module ThorTemplate
26
29
  dest = "#{project_root}/#{dest}"
27
30
 
28
31
  if File.exist?(dest) and !options[:force]
29
- puts "already exists: #{dest}" unless options[:quiet]
32
+ puts "already exists: #{dest}".colorize(:yellow) unless options[:quiet]
30
33
  else
31
- puts "creating: #{dest}" unless options[:quiet]
34
+ puts "creating: #{dest}".colorize(:green) unless options[:quiet]
32
35
  dirname = File.dirname(dest)
33
36
  FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
34
37
  FileUtils.cp(src, dest)
@@ -37,11 +40,7 @@ module ThorTemplate
37
40
  end
38
41
 
39
42
  def rename
40
- system("cd #{@name} && rake rename")
41
- end
42
-
43
- def rewrite
44
- template("Rakefile", "Rakefile")
43
+ Renamer.new(@name).rename!
45
44
  end
46
45
 
47
46
  def git
@@ -0,0 +1,63 @@
1
+ module ThorTemplate
2
+ class Renamer
3
+ def initialize(name)
4
+ @name = name
5
+ end
6
+
7
+ def rename!
8
+ Dir.chdir(@name) do
9
+ Dir.glob("**/*") do |path|
10
+ next unless File.file?(path)
11
+ rename_content(path)
12
+
13
+ next unless File.file?(path)
14
+ rename_path(path)
15
+ end # Dir.glob
16
+
17
+ remove_empty_directories
18
+ end
19
+ end
20
+
21
+ def rename_content(path)
22
+ content = IO.readlines(path)
23
+ result = content.map do |line|
24
+ line = line.gsub(/ThorTemplate/, @name.underscore.camelize)
25
+ line = line.gsub(/thor_template/, @name.underscore)
26
+ line = line.gsub("USER_PROVIDED_NAME", @name) # special case
27
+ line
28
+ end
29
+ IO.write(path, result.join(''))
30
+ end
31
+
32
+ def rename_path(src)
33
+ dest = special_rename?(src) ?
34
+ src.gsub(/thor_template/, @name) :
35
+ src.gsub(/thor_template/, @name.underscore)
36
+
37
+ folder = File.dirname(dest)
38
+ FileUtils.mkdir_p(folder) unless File.exist?(folder)
39
+
40
+ FileUtils.mv(src, dest) unless src == dest
41
+ end
42
+
43
+ # These paths should be rename with the actually named provied by the
44
+ # user, the the underscored version.
45
+ def special_rename?(path)
46
+ %w[
47
+ thor_template.gemspec
48
+ bin/thor_template
49
+ lib/thor_template.rb
50
+ ].include?(path)
51
+ end
52
+
53
+ # Thanks https://stackoverflow.com/users/123094/db
54
+ # https://stackoverflow.com/questions/1290670/ruby-how-do-i-recursively-find-and-remove-empty-directories
55
+ def remove_empty_directories
56
+ Dir['**/*'].
57
+ select { |d| File.directory? d }.
58
+ select { |d| (Dir.entries(d) - %w[ . .. ]).empty? }.
59
+ each { |d| Dir.rmdir d }
60
+ end
61
+
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module ThorTemplate
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/thor_template.rb CHANGED
@@ -6,4 +6,5 @@ module ThorTemplate
6
6
  autoload :Command, 'thor_template/command'
7
7
  autoload :Version, 'thor_template/version'
8
8
  autoload :Generator, 'thor_template/generator'
9
+ autoload :Renamer, 'thor_template/renamer'
9
10
  end
data/spec/lib/cli_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  # to run specs with what's remembered from vcr
4
4
  # $ rake
5
- #
5
+ #
6
6
  # to run specs with new fresh data from aws api calls
7
7
  # $ rake clean:vcr ; time rake
8
8
  describe ThorTemplate::CLI do
@@ -11,13 +11,45 @@ describe ThorTemplate::CLI do
11
11
  end
12
12
 
13
13
  describe "new" do
14
- it "should generate" do
15
- out = execute("cd tmp && ../bin/thor_template new hello #{@args}")
16
- expect(out).to include("Created hello project!")
14
+ context("simple single name") do
15
+ it "should generate" do
16
+ out = execute("cd tmp && ../bin/thor_template new hello #{@args}")
17
+ expect(out).to include("Created hello project!")
18
+ out = execute("cd tmp/hello && rake")
19
+ expect(out).to include("0 failures")
20
+ end
21
+ end
22
+
23
+ context("underscored name") do
24
+ it "should generate" do
25
+ out = execute("cd tmp && ../bin/thor_template new my_cli #{@args}")
26
+ expect(out).to include("Created my_cli project!")
27
+ out = execute("cd tmp/my_cli && rake")
28
+ expect(out).to include("0 failures")
29
+ end
30
+ end
31
+
32
+ context("dasherized name") do
33
+ it "should generate" do
34
+ out = execute("cd tmp && ../bin/thor_template new my-cli #{@args}")
35
+ expect(out).to include("Created my-cli project!")
36
+ out = execute("cd tmp/my-cli && rake")
37
+ expect(out).to include("0 failures")
38
+ end
39
+ end
40
+
41
+ # CamelCase is ugly :(
42
+ context("simple CamelCase name") do
43
+ it "should generate" do
44
+ out = execute("cd tmp && ../bin/thor_template new MyCli #{@args}")
45
+ expect(out).to include("Created MyCli project!")
46
+ out = execute("cd tmp/MyCli && rake")
47
+ expect(out).to include("0 failures")
48
+ end
17
49
  end
18
50
  end
19
51
  end
20
52
 
21
53
  VCR.configure do |config|
22
54
  config.ignore_hosts 'codeclimate.com'
23
- end
55
+ end
@@ -2,20 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  # to run specs with what's remembered from vcr
4
4
  # $ rake
5
- #
5
+ #
6
6
  # to run specs with new fresh data from aws api calls
7
7
  # $ rake clean:vcr ; time rake
8
8
  describe ThorTemplate::Generator do
9
9
  before(:each) do
10
10
  options = {
11
- name: "tmp/hello"
11
+ cwd: "tmp",
12
+ name: "hello"
12
13
  }
13
14
  @generator = ThorTemplate::Generator.new(options)
14
15
  end
15
16
 
16
17
  it "should generate" do
17
18
  @generator.run
18
- expect(File.exist?("tmp/hello")).to be_truthy
19
- expect(File.exist?("tmp/hello/hello.gemspec")).to be_truthy
19
+ expect(File.exist?("hello")).to be_truthy
20
+ expect(File.exist?("hello/hello.gemspec")).to be_truthy
20
21
  end
21
22
  end
data/spec/spec_helper.rb CHANGED
@@ -27,7 +27,8 @@ RSpec.configure do |c|
27
27
  c.include Helpers
28
28
  c.before(:all) do
29
29
  FileUtils.mkdir('tmp') unless File.exist?('tmp')
30
- FileUtils.rm_rf("tmp/hello")
30
+ FileUtils.rm_rf("tmp")
31
+ FileUtils.mkdir("tmp")
31
32
  end
32
33
  c.around(:each) do |example|
33
34
  VCR.use_cassette(example.metadata[:full_description], :serialize_with => :json) do
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency "thor"
22
22
  spec.add_dependency "hashie"
23
23
  spec.add_dependency "colorize"
24
+ spec.add_dependency "activesupport"
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
26
27
  spec.add_development_dependency "rake"
@@ -28,4 +29,5 @@ Gem::Specification.new do |spec|
28
29
  spec.add_development_dependency "guard"
29
30
  spec.add_development_dependency "guard-bundler"
30
31
  spec.add_development_dependency "guard-rspec"
32
+ spec.add_development_dependency "byebug"
31
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thor_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +150,20 @@ dependencies:
136
150
  - - ">="
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
139
167
  description: Generates starter cli tool project.
140
168
  email:
141
169
  - tongueroo@gmail.com
@@ -168,12 +196,12 @@ files:
168
196
  - lib/starter_project/spec/lib/cli_spec.rb
169
197
  - lib/starter_project/spec/spec_helper.rb
170
198
  - lib/starter_project/thor_template.gemspec
171
- - lib/templates/Rakefile
172
199
  - lib/thor_template.rb
173
200
  - lib/thor_template/cli.rb
174
201
  - lib/thor_template/cli/help.rb
175
202
  - lib/thor_template/command.rb
176
203
  - lib/thor_template/generator.rb
204
+ - lib/thor_template/renamer.rb
177
205
  - lib/thor_template/version.rb
178
206
  - spec/lib/cli_spec.rb
179
207
  - spec/lib/generator_spec.rb
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- task :default => :spec
5
-
6
- RSpec::Core::RakeTask.new