cli-template 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +58 -0
  3. data/.gitignore +17 -0
  4. data/.rspec +4 -0
  5. data/CHANGELOG.md +48 -0
  6. data/Gemfile +6 -0
  7. data/Guardfile +11 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +58 -0
  10. data/Rakefile +32 -0
  11. data/cli-template.gemspec +32 -0
  12. data/exe/cli-template +9 -0
  13. data/lib/cli-template.rb +12 -0
  14. data/lib/cli-template/cli.rb +17 -0
  15. data/lib/cli-template/command.rb +47 -0
  16. data/lib/cli-template/help.rb +9 -0
  17. data/lib/cli-template/help/new.md +4 -0
  18. data/lib/cli-template/helpers.rb +12 -0
  19. data/lib/cli-template/new.rb +74 -0
  20. data/lib/cli-template/sequence.rb +45 -0
  21. data/lib/cli-template/version.rb +3 -0
  22. data/lib/templates/skeleton/%project_name%.gemspec.tt +30 -0
  23. data/lib/templates/skeleton/.gitignore +16 -0
  24. data/lib/templates/skeleton/.rspec +2 -0
  25. data/lib/templates/skeleton/CHANGELOG.md +7 -0
  26. data/lib/templates/skeleton/Gemfile.tt +6 -0
  27. data/lib/templates/skeleton/Guardfile +19 -0
  28. data/lib/templates/skeleton/LICENSE.txt +22 -0
  29. data/lib/templates/skeleton/README.md.tt +47 -0
  30. data/lib/templates/skeleton/Rakefile +6 -0
  31. data/lib/templates/skeleton/exe/%project_name%.tt +14 -0
  32. data/lib/templates/skeleton/lib/%project_name%.rb.tt +10 -0
  33. data/lib/templates/skeleton/lib/%underscored_name%/cli.rb.tt +170 -0
  34. data/lib/templates/skeleton/lib/%underscored_name%/command.rb.tt +164 -0
  35. data/lib/templates/skeleton/lib/%underscored_name%/help.rb.tt +9 -0
  36. data/lib/templates/skeleton/lib/%underscored_name%/help/hello.md.tt +3 -0
  37. data/lib/templates/skeleton/lib/%underscored_name%/help/sub/goodbye.md.tt +3 -0
  38. data/lib/templates/skeleton/lib/%underscored_name%/main.rb.tt +21 -0
  39. data/lib/templates/skeleton/lib/%underscored_name%/rake_command.rb.tt +63 -0
  40. data/lib/templates/skeleton/lib/%underscored_name%/sub.rb.tt +12 -0
  41. data/lib/templates/skeleton/lib/%underscored_name%/version.rb.tt +3 -0
  42. data/lib/templates/skeleton/spec/lib/cli_spec.rb.tt +24 -0
  43. data/lib/templates/skeleton/spec/spec_helper.rb.tt +29 -0
  44. data/spec/lib/cli_spec.rb +61 -0
  45. data/spec/spec_helper.rb +33 -0
  46. metadata +217 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 042da28b0ec9d9d406c3204bb08210c996c98022f2b75a2bfc454d7b79916c1e
4
+ data.tar.gz: 8ce3733852b7f5790a466699572323f82862a2ed18f92a42a7247a815d050879
5
+ SHA512:
6
+ metadata.gz: cfe099ee8efae48d4752a570652361ae6e147fd7be9716dcd9bfb35f58fcd61ce0d24a53749c24817abdb57f0f3dc0c8f3f9ed3ea28de537e6a38960e6b7f3a2
7
+ data.tar.gz: ad5c81b830fe057937dd74419bbac0e19de6e41f84b967572babb61ea2c8631a84f7c1899fdec6f3a4356d00d7e6aca51305adfdaa4364aa27fcb7c239d76336
@@ -0,0 +1,58 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.4.1-node-browsers
11
+
12
+ # Specify service dependencies here if necessary
13
+ # CircleCI maintains a library of pre-built images
14
+ # documented at https://circleci.com/docs/2.0/circleci-images/
15
+ # - image: circleci/postgres:9.4
16
+
17
+ working_directory: ~/repo
18
+
19
+ steps:
20
+ - checkout
21
+
22
+ # Download and cache dependencies
23
+ - restore_cache:
24
+ keys:
25
+ - v1-dependencies-{{ checksum "Gemfile" }}
26
+ # fallback to using the latest cache if no exact match is found
27
+ - v1-dependencies-
28
+
29
+ - run:
30
+ name: install dependencies
31
+ command: |
32
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
33
+
34
+ - save_cache:
35
+ paths:
36
+ - ./vendor/bundle
37
+ key: v1-dependencies-{{ checksum "Gemfile" }}
38
+
39
+ - run:
40
+ name: configure git
41
+ command: |
42
+ git config --global user.email "tongueroo@gmail.com"
43
+ git config --global user.name "Tung Nguyen"
44
+
45
+ # run tests!
46
+ - run:
47
+ name: run tests
48
+ command: |
49
+ gem install bundler # think need this because the specs shell out
50
+ mkdir /tmp/test-results
51
+ bundle exec rspec
52
+
53
+ # collect reports
54
+ - store_test_results:
55
+ path: /tmp/test-results
56
+ - store_artifacts:
57
+ path: /tmp/test-results
58
+ destination: test-results
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --exclude-pattern spec/fixtures/apps/**/*
4
+
@@ -0,0 +1,48 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
+
6
+ ## [3.0.0]
7
+ - tool is now called cli-template
8
+ - namespaced commands support
9
+ - using Thor::Group as the generator
10
+
11
+ ## [2.2.1]
12
+ - add -v as a version flag too
13
+
14
+ ## [2.2.0]
15
+ - simple help formatting
16
+ - add version command
17
+
18
+ ## [2.1.1]
19
+
20
+ - starter project use version 0.1.0
21
+
22
+ ## [2.1.0]
23
+
24
+ - improve help class
25
+ - comment out SimpleCov
26
+ - reanme bin folder to exe
27
+
28
+ ## [2.0.1]
29
+
30
+ - fix gem name that gets build for the starter project
31
+
32
+ ## [2.0.0]
33
+
34
+ - Fix project names that are named with dashes and camel case.
35
+ - Add legit specs that test each case.
36
+
37
+ ## [1.0.1]
38
+
39
+ - require "fileutils" fix
40
+
41
+ ## [1.0.0]
42
+
43
+ - allow --help or -h at the end of the command
44
+ - major version bump. been in used and tested for a while now
45
+
46
+ ## [0.0.7]
47
+
48
+ - Update starter project to use newer ruby syntax
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cli-template.gemspec
4
+ gemspec
5
+
6
+ gem "codeclimate-test-reporter", group: :test, require: nil
@@ -0,0 +1,11 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { "spec/cli-template_spec.rb" }
4
+ watch(%r{^lib/cli-template/(.+)\.rb$}) { "spec/cli-template_spec.rb" }
5
+ watch('spec/spec_helper.rb') { "spec/cli-template_spec.rb" }
6
+ end
7
+
8
+ guard 'bundler' do
9
+ watch('Gemfile')
10
+ watch(/^.+\.gemspec/)
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tung Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # CLI Template
2
+
3
+ [![CircleCI](https://circleci.com/gh/tongueroo/cli-template.svg?style=svg)](https://circleci.com/gh/tongueroo/cli-template)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/c6c4f26aaafccab10baf/maintainability)](https://codeclimate.com/github/tongueroo/cli-template/maintainability)
5
+
6
+ `cli-template` is a generator tool that builds a starter CLI project based on [Thor](http://whatisthor.com/). It is the successor tool to [thor_template](https://github.com/tongueroo/thor_template), which is also a tool that generates CLI projects. The main difference is `cli-template` allows for namespace commands. The usage section demonstrates a namespace command below.
7
+
8
+ The predecessor tool is covered in this original 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). It covers usage and also contains a video demo. An updated blog post will eventually be made, for now, refer to the original blog post.
9
+
10
+ ## Usage
11
+
12
+ ```sh
13
+ cli-template new mycli
14
+ cd mycli
15
+ exe/mycli hello world
16
+ exe/mycli sub:goodbye world
17
+ ```
18
+
19
+ The above generated a starter CLI project called `mycli` with a working hello command. The created project also has starter specs for you 😁
20
+
21
+ ```sh
22
+ $ rake
23
+ Mycli::CLI
24
+ mycli
25
+ should hello world
26
+ should goodbye world
27
+
28
+ Finished in 1.12 seconds (files took 0.71706 seconds to load)
29
+ 2 examples, 0 failures
30
+ ```
31
+
32
+ ## Release
33
+
34
+ Once you are satisfied with the CLI tool, you can release it as a gem.
35
+
36
+ 1. edit the mycli.gemspec
37
+ 2. edit lib/mycli/version.rb
38
+ 3. update the CHANGELOG.md
39
+
40
+ And run:
41
+
42
+ ```
43
+ rake release
44
+ ```
45
+
46
+ ## Installation
47
+
48
+ ```sh
49
+ gem install cli-template
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am "Add some feature"`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1,32 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
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['**/cli-template*']
16
+ begin
17
+ from = dir.pop
18
+ if from
19
+ to = from.split('/')
20
+ to[-1].gsub!('cli-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/cli-template/#{name}/g' #{path}`
27
+ `sed -i 's/CliTemplate/#{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
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cli-template/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cli-template"
8
+ spec.version = CliTemplate::VERSION
9
+ spec.authors = ["Tung Nguyen"]
10
+ spec.email = ["tongueroo@gmail.com"]
11
+ spec.description = %q{Generate a CLI tool quickly}
12
+ spec.summary = %q{Generate a CLI tool quickly}
13
+ spec.homepage = "https://gitub.com/tongueroo/cli-template"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "thor"
23
+ spec.add_dependency "colorize"
24
+ spec.add_dependency "activesupport"
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "guard"
29
+ spec.add_development_dependency "guard-bundler"
30
+ spec.add_development_dependency "guard-rspec"
31
+ spec.add_development_dependency "byebug"
32
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+
5
+ $:.unshift(File.expand_path('../../lib', __FILE__))
6
+ require 'cli-template'
7
+ require 'cli-template/cli'
8
+
9
+ CliTemplate::CLI.start(ARGV)
@@ -0,0 +1,12 @@
1
+ $:.unshift(File.expand_path("../", __FILE__))
2
+ require "cli-template/version"
3
+
4
+ module CliTemplate
5
+ autoload :Help, 'cli-template/help'
6
+ autoload :CLI, 'cli-template/cli'
7
+ autoload :Command, 'cli-template/command'
8
+ autoload :Version, 'cli-template/version'
9
+ autoload :Sequence, 'cli-template/sequence'
10
+ autoload :New, 'cli-template/new'
11
+ autoload :Helpers, 'cli-template/helpers'
12
+ end
@@ -0,0 +1,17 @@
1
+ module CliTemplate
2
+ class CLI < Command
3
+ class_option :verbose, :type => :boolean
4
+ class_option :noop, :type => :boolean
5
+
6
+ long_desc Help.text(:new)
7
+ New.cli_options.each do |args|
8
+ option *args
9
+ end
10
+ register(New, "new", "new NAME", "generates new CLI project")
11
+
12
+ desc "version", "prints version"
13
+ def version
14
+ puts VERSION
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ require 'thor'
2
+
3
+ # Override thor's long_desc identation behavior
4
+ # https://github.com/erikhuda/thor/issues/398
5
+ class Thor
6
+ module Shell
7
+ class Basic
8
+ def print_wrapped(message, options = {})
9
+ message = "\n#{message}" unless message[0] == "\n"
10
+ stdout.puts message
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ module CliTemplate
17
+ class Command < Thor
18
+ class << self
19
+ def dispatch(m, args, options, config)
20
+ # Allow calling for help via:
21
+ # cli-template command help
22
+ # cli-template command -h
23
+ # cli-template command --help
24
+ # cli-template command -D
25
+ #
26
+ # as well thor's normal way:
27
+ #
28
+ # cli-template help command
29
+ help_flags = Thor::HELP_MAPPINGS + ["help"]
30
+ if args.length > 1 && !(args & help_flags).empty?
31
+ args -= help_flags
32
+ args.insert(-2, "help")
33
+ end
34
+
35
+ # cli-template version
36
+ # cli-template --version
37
+ # cli-template -v
38
+ version_flags = ["--version", "-v"]
39
+ if args.length == 1 && !(args & version_flags).empty?
40
+ args = ["version"]
41
+ end
42
+
43
+ super
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,9 @@
1
+ module CliTemplate::Help
2
+ class << self
3
+ def text(namespaced_command)
4
+ path = namespaced_command.to_s.gsub(':','/')
5
+ path = File.expand_path("../help/#{path}.md", __FILE__)
6
+ IO.read(path) if File.exist?(path)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ Examples:
2
+
3
+ cli-template new hello
4
+ cli-template new another_project
@@ -0,0 +1,12 @@
1
+ module CliTemplate::Helpers
2
+ def project_class_name
3
+ project_name.underscore.camelize
4
+ end
5
+
6
+ # Files should be named with underscores instead of dashes even
7
+ # though project name can contain a dash. This is because autoloading
8
+ # works will underscores in the filenames only.
9
+ def underscored_name
10
+ project_name.underscore
11
+ end
12
+ end
@@ -0,0 +1,74 @@
1
+ module CliTemplate
2
+ class New < Sequence
3
+ argument :project_name
4
+
5
+ # Ugly, but when the class_option is only defined in the Thor::Group class
6
+ # it doesnt show up with cli-template new help :(
7
+ # If anyone knows how to fix this let me know.
8
+ def self.cli_options
9
+ [
10
+ [:repo, desc: "GitHub repo to use. Format: user/repo"],
11
+ [:force, type: :boolean, desc: "Bypass overwrite are you sure prompt for existing files."],
12
+ [:git, type: :boolean, default: true, desc: "Git initialize the project"],
13
+ ]
14
+ end
15
+
16
+ cli_options.each do |args|
17
+ class_option *args
18
+ end
19
+
20
+ def create_project
21
+ options[:repo] ? clone_project : copy_project
22
+
23
+ destination_root = "#{Dir.pwd}/#{project_name}"
24
+ self.destination_root = destination_root
25
+ FileUtils.cd("#{Dir.pwd}/#{project_name}")
26
+ end
27
+
28
+ def make_executable
29
+ chmod("bin", 0755 & ~File.umask, verbose: false) if File.exist?("bin")
30
+ chmod("exe", 0755 & ~File.umask, verbose: false) if File.exist?("exe")
31
+ end
32
+
33
+ def bundle_install
34
+ Bundler.with_clean_env do
35
+ system("BUNDLE_IGNORE_CONFIG=1 bundle install")
36
+ end
37
+ end
38
+
39
+ def git_init
40
+ return if !options[:git]
41
+ return unless git_installed?
42
+ return if File.exist?(".git") # this is a clone repo
43
+
44
+ run("git init")
45
+ run("git add .")
46
+ run("git commit -m 'first commit'")
47
+ end
48
+
49
+ def user_message
50
+ puts <<-EOL
51
+ #{"="*64}
52
+ Congrats 🎉 You have successfully created a CLI project.
53
+
54
+ Test the CLI:
55
+
56
+ cd #{project_name}
57
+ exe/#{project_name} hello # top-level commands
58
+ exe/#{project_name} sub:goodbye # sub commands
59
+ bundle exec rspec
60
+
61
+ To publish your CLI as a gem:
62
+
63
+ 1. edit the #{project_name}.gemspec
64
+ 2. edit lib/#{project_name}/version.rb
65
+ 3. update the CHANGELOG.md
66
+
67
+ And run:
68
+
69
+ rake release
70
+
71
+ EOL
72
+ end
73
+ end
74
+ end