francis 1.0.0 → 2.0.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/francis +1 -4
  3. data/lib/francis.rb +92 -129
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c1b4d040d1adc0b565872ed025acdc501ccaa8ee55d5554765c33a1fd3a758f
4
- data.tar.gz: b8510bb2fa5aed08c0a552b8817ef361c02115159bb1e884a6ee094057eaadbd
3
+ metadata.gz: 7f74c91f9218cf3c3236ec871f13c213955788fac080a658486143251617bb04
4
+ data.tar.gz: 3b3e6df458522097ecff512084e7ba0dd827fed1d74d33a297c7de612cc10f96
5
5
  SHA512:
6
- metadata.gz: b0fd9dd66fc727614fa511d5da0a0648f6fc5985b39ad0e3a683ad381313a190c4fa11ceaa76bce4fdc885f8b41c6fd65a11aee9016c5ccd9a87e3ff1e594ee7
7
- data.tar.gz: 4e7397f747fbd4f39c661635851690bea0ae9ec0b1e6a90ee8db5a991f1bc2da57a25c40a8a2207c9b798011f0caf142fd00ac57ce565f1edefcd7d7e191a211
6
+ metadata.gz: 2be6192cc9991c6430b5caa8aab21d4e34988b42e1de0600eb63f9d6d655ee71620873b727b651da60d479d0d887938a885ccbe095a52b0c3f0d3e3c4f923f05
7
+ data.tar.gz: 35144bcf547123a1df2bb1aa0bb725e580005e7c0865ac627ff0d5be98eb6bded53e4b6fb1b27086e6a19419ba02d56eae340831007f428249fc615e3daa6475
@@ -1,6 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'francis'
3
- args = ARGV.dup
4
- ARGV.clear
5
- command = args.shift.strip
6
- Francis.new(command, args)
3
+ Francis::CLI.start(ARGV)
@@ -1,149 +1,112 @@
1
- # A simple gem & CLI for setting up a working directory
2
- # structure for Sinatra apps.
3
- #
4
- # Author:: Ian Kent (mailto:ian@iankent.me)
5
- # License:: Distributed under the same terms as Ruby
6
-
7
- # This is the only and main class for the gem.
8
-
9
- class Francis
10
-
11
-
12
- attr_accessor :folders, :files, :newhelp, :foldershelp, :fileshelp
13
-
14
- # Creates a new instance of the class and directs the gem
15
- # to the method based on the command passed to it.
16
- def initialize(command, args)
17
- @folders = ["app","public","views"]
18
- @files = ["app/app.rb", "Gemfile","config.ru"]
19
- @newhelp = %Q(
20
- Usage:
21
-
22
- francis new <appname> [--git][--proc][--readme][--unicorn][-r <gems>]
23
-
24
- Creates a new Sinatra app.
25
-
26
- <appname> => Name of the new directory
27
-
28
- Options:
29
-
30
- --git => sets up the directory up as a git repo
31
- --proc => sets up the directory with a Procfile
32
- --readme => sets up the directory with a readme.md
33
- --unicorn => sets up the directory with config/unicorn.rb, pids/unicorn.pid and logs/unicorn.log
34
- -r <gems> => adds gems to the Gemfile
35
- )
36
-
37
- @foldershelp = %Q(
38
- francis folders
39
-
40
- Prints a list of folders that are created when francis new is run.
41
- )
42
- @fileshelp = %Q(
43
- francis files
44
-
45
- Prints a list of files that are created when francis new is run.
46
- )
47
- case command
48
- when 'new'
49
- run(args[0],args[1..-1])
50
- when 'folders'
51
- puts folders
52
- when 'files'
53
- puts files
54
- when 'help'
55
- help(args)
56
- else
57
- puts "I don't understand"
1
+ require 'thor'
2
+
3
+ module Francis
4
+
5
+ class CLI < Thor
6
+ desc "create NAME", "Create a new sinatra app called NAME"
7
+ option :git, :type => :boolean, :aliases => "-g", :desc => "Create app as a git respository and include a .gitignore file"
8
+ option :readme, :type => :boolean, :aliases => "-R", :desc => "Include a readme.md file"
9
+ option :unicorn, :type => :boolean, :aliases => "-u", :desc => "Include a config/unicorn.rb file with default unicorn config"
10
+ option :require_gem, :type => :array, :aliases => "-r", :desc => "Add required gems that will be included in the Gemfile and required within app.rb"
11
+ option :whenever, :type => :boolean, :aliases => "-w", :desc => "Include a config/schedule.rb file"
12
+ def create(name)
13
+ Francis::FileStructure.new(name, options)
58
14
  end
59
15
  end
60
-
61
- # Main run method, this will create the working directory
62
- # and add any further files and folders based on the options passed to it.
63
- def run(name, options)
64
- dir = Dir.getwd
65
- if Dir.exists?("#{dir}/#{name}")
66
- abort("There is already a folder with that name, please choose another.")
67
- else
68
- Dir.mkdir("#{dir}/#{name}")
16
+ class FileStructure
17
+ def initialize(name, options)
18
+ @name = name
19
+ sinatra_gem_version = Gem.latest_version_for 'sinatra'
20
+ @structure = {
21
+ "/views" => {},
22
+ "/public" => {},
23
+ "/" => {
24
+ "config.ru" => %Q(require 'bundler'\nBundler.require\nrequire_relative 'app'\nrun #{@name.capitalize}),
25
+ "app.rb" => %Q(require 'sinatra'\nclass #{@name.capitalize} < Sinatra::Base\n\tget '/' do\n\t\t'Hello world!'\n\tend\nend),
26
+ "Gemfile" => %Q(source 'https://rubygems.org'\ngem 'sinatra', '#{sinatra_gem_version.approximate_recommendation}'\n)
27
+ }
28
+ }
29
+ @options = options
30
+ @wd = "#{Dir.getwd}/#{@name}"
31
+ parse_options
32
+ execute!
69
33
  end
70
- Dir.chdir("#{dir}/#{name}")
71
- folders.each do |f|
72
- Dir.mkdir(f)
34
+ def add_folder(name)
35
+ @structure["/#{name}"] = {}
73
36
  end
74
- files.each do |f|
75
- File.new(f,"w")
37
+ def remove_folder(name)
38
+ @structure.delete("/#{name}"){|el| "#{el} not found"}
76
39
  end
77
- File.open("Gemfile","a") do |f|
78
- f.write("source 'https://rubygems.org'\ngem 'sinatra'\n")
40
+ def parse_options
41
+ git
42
+ readme
43
+ unicorn
44
+ whenever
45
+ require_gem
79
46
  end
80
- if options.include?("--proc")
81
- File.open("Procfile","a") do |f|
82
- f.write("web: bundle exec rackup config.ru -p $PORT")
47
+ def execute!
48
+ # create target directory
49
+ Dir.mkdir(@wd)
50
+ # cycle through structure and create folders and files
51
+ @structure.each{|folder,files|
52
+ if folder != "/"
53
+ Dir.mkdir("#{@wd}#{folder}")
54
+ end
55
+ files.each{|name,contents|
56
+ File.open("#{@wd}#{folder}/#{name}", "w"){|f|
57
+ f.write(contents)
58
+ }
59
+ }
60
+ }
61
+ # if git option is given, execute git init command
62
+ if @options[:git]
63
+ Dir.chdir(@wd)
64
+ system "git init"
83
65
  end
66
+ puts "#{@name} created as Sinatra app"
84
67
  end
85
- File.open("app/app.rb","a") do |f|
86
- f.write("get '/' do\n 'Hello world!'\nend")
87
- end
88
- File.open("config.ru","a") do |f|
89
- f.write("require 'bundler'\nBundler.require\nrequire './app/app'\nrequire './db/db'\nrun Sinatra::Application")
90
- end
91
- if options.include?("--git")
92
- system("git init")
68
+ def git
69
+ if @options[:git]
70
+ @structure["/"][".gitignore"] = %Q(.DS_Store\nGemfile.lock\npids/*\nlogs/*\n*.gem)
71
+ end
93
72
  end
94
- if options.include?("--readme")
95
- File.open("README.md","a") do |f|
96
- f.write("# #{name.upcase}")
73
+ def readme
74
+ if @options[:readme]
75
+ @structure["/"]["readme.md"] = %Q(# #{@name.capitalize}\n\nTODO: Write a readme file)
97
76
  end
98
77
  end
99
- if options.include?("-r")
100
- gems = options.select { |a| a[0] != "-" }
101
- File.open("Gemfile","a") do |f|
102
- gems.each do |g|
103
- f.write("gem '#{g}'\n")
104
- end
78
+ def unicorn
79
+ if @options[:unicorn]
80
+ add_folder "/config"
81
+ add_folder "/pids"
82
+ add_folder "/logs"
83
+ @structure["/config"]["unicorn.rb"] = %Q(working_directory '#{@wd}'\npid '#{@wd}/pids/unicorn.pid'\nstderr_path '#{@wd}/logs/unicorn.log'\nstdout_path '#{@wd}/logs/unicorn.log'\nlisten '/tmp/#{@name}.sock'\nworker_processes 2\ntimeout 30)
84
+ @structure["/pids"]["unicorn.pid"] = ""
85
+ @structure["/logs"]["unicorn.log"] = ""
105
86
  end
106
87
  end
107
- if options.include?("--unicorn")
108
- ["config", "pids", "logs"].each do |f|
109
- Dir.mkdir(f)
88
+ def whenever
89
+ if @options[:whenever]
90
+ add_folder "/config"
91
+ @structure["/config"]["schedule.rb"] = ""
110
92
  end
111
- File.open("config/unicorn.rb","a") do |f|
112
- f.write %Q(
113
- app_path = File.expand_path(File.dirname(__FILE__) + '/..')
114
- working_directory app_path
115
- pid app_path + "/pids/unicorn.pid"
116
- stderr_path app_path + "/logs/unicorn.log"
117
- stdout_path app_path + "/logs/unicorn.log"
118
- listen "/tmp/unicorn.#{Dir.pwd}.sock"
119
- worker_processes 2
120
- timeout 60
121
- )
93
+ end
94
+ def require_gem
95
+ if @options[:require_gem]
96
+ @options[:require_gem].each{|r|
97
+ version = Gem.latest_version_for(r)
98
+ if version
99
+ @structure["/"]["Gemfile"] << %Q(gem '#{r}', '#{version.approximate_recommendation}'\n)
100
+ else
101
+ warn "Could not find gem: #{r}, skipping inclusion in Gemfile"
102
+ end
103
+ }
122
104
  end
123
-
124
105
  end
125
-
126
- puts "Sinatra app '#{name}' created!"
127
- end
128
-
129
-
130
- # Prints the help file.
131
- def help(*method)
132
- if method.empty?
133
- puts newhelp + foldershelp + fileshelp
134
- elsif method.length == 1
135
- case method[0][0]
136
- when "new"
137
- puts newhelp
138
- when "folders"
139
- puts foldershelp
140
- when "files"
141
- puts fileshelp
142
- else
143
- puts newhelp + foldershelp + fileshelp
106
+ def add_folder(folder)
107
+ if !@structure.has_key?(folder)
108
+ @structure["#{folder}"] = {}
144
109
  end
145
- else
146
- puts newhelp + foldershelp + fileshelp
147
110
  end
148
111
  end
149
112
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: francis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Kent
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-26 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
13
27
  description: A command line tool for quickly setting up Sinatra apps
14
28
  email: ian@iankent.me
15
29
  executables: