Class: CutAGemCommand
Constant Summary collapse
- VERSION =
"0.0.7"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(argv) ⇒ CutAGemCommand
constructor
A new instance of CutAGemCommand.
- #run ⇒ Object
-
#select_template(select) ⇒ Object
Select template from system templates and user templtes.
Constructor Details
#initialize(argv) ⇒ CutAGemCommand
Returns a new instance of CutAGemCommand.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cutagem.rb', line 16 def initialize(argv) @argv = argv @config = Pathname.new(ENV["HOME"]) + ".cutagem/config.yaml" @parser = OptionParser.new do |parser| parser. = <<-EOB.gsub(/^\t+/, "") Usage: #$0 [options] gemname EOB parser.separator "" parser.separator "Options:" parser.on("-s", "--select", "Select template interactively.") do |@select| end parser.on("-d", "--desc", "Describe this gem.") do |@description| end parser.on("-c", "--config", "Configure user values. Use $EDITOR") do |c| @config.parent.mkpath unless @config.exist? @config.open("w") do |f| f << <<-EOF.gsub(/^\t+/, "") author: "#{ENV['USER']}" email: "#{ENV['USER']}@#{ENV['HOST']}" EOF end end exec(ENV["EDITOR"], @config.to_s) end parser.on("--copy-template NAME", "Copy template to user template dir naming NAME") do |name| path = Pathname.new(ENV["HOME"]) + ".cutagem/templates" + name if path.exist? puts "#{path} is already exists." exit 1 end template = select_template(true) cp_r template, path, :verbose => true exit end parser.on("--version", "Show version string `#{VERSION}'") do puts VERSION exit end end end |
Class Method Details
.run(argv) ⇒ Object
12 13 14 |
# File 'lib/cutagem.rb', line 12 def self.run(argv) new(argv.dup).run end |
Instance Method Details
#run ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cutagem.rb', line 65 def run @parser.order!(@argv) unless @argv.first puts "gemname must be required." exit end pwd = Pathname.pwd = ENV['USER'] email = "#{ENV['USER']}@#{ENV['HOST']}" gemname = @argv.shift gemid = gemname.gsub("-", "") gempath = gemname.gsub("-", "/") gemclass = gempath.split("/").map {|c| c.split(/_/).collect {|i| i.capitalize }.join("") }.join("::") description = @description template = select_template(@select) gemdir = pwd + gemname if gemdir.exist? puts "#{gemdir.basename} is already exists." exit end config = {} begin config = YAML.load(@config.read) = config["author"] if config["author"] email = config["email"] if config["email"] puts "~/.cutagem/config.yaml is found. Use it." rescue Errno::ENOENT puts "~/.cutagem/config.yaml is not found. Use default." end begin cp_r template, gemdir, :verbose => true Pathname.glob(gemdir + "**/gemname*") do |f| new = f.parent + f.basename.to_s.sub(/gemname/, gemname) puts "Rename #{f.relative_path_from(gemdir)} to #{new.relative_path_from(gemdir)}" f.rename(new) end Pathname.glob(gemdir + "**/gempath*") do |f| new = f.parent + f.basename.to_s.sub(/gempath/, gempath) puts "Rename #{f.relative_path_from(gemdir)} to #{new.relative_path_from(gemdir)}" new.parent.mkpath f.rename(new) end Pathname.glob(gemdir + "**/*") do |f| next unless f.file? f.open("r+") do |f| content = f.read f.rewind f.puts ERB.new(content).result(binding) f.truncate(f.tell) end end rescue gemdir.rmtree raise end puts "Done." if ENV["EDITOR"] puts "Type any key to edit Rakefile." gets exec(ENV["EDITOR"], gemdir + "Rakefile") end end |
#select_template(select) ⇒ Object
Select template from system templates and user templtes. if select
is true, select templates interactively.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/cutagem.rb', line 140 def select_template(select) @templates = Pathname.new(File.dirname(__FILE__)).realpath + '../templates' @user_templates = Pathname.new(ENV["HOME"]).realpath + '.cutagem/templates' templates = [] u_templates = [] if @user_templates.exist? Pathname.glob(@user_templates + "*").each do |t| t = [".cutagem/templates/#{t.basename}", t] if t[1].basename.to_s == "default" u_templates.unshift(t) else u_templates << t end end end Pathname.glob(@templates + "*").each do |t| t = ["#{t.basename}", t] if t[1].basename.to_s == "default" templates.unshift(t) else templates << t end end templates = u_templates + templates if select puts "Select template:" templates.each_with_index do |item,index| puts "% 2d. %s" % [index+1, item.first] end input = gets.chomp case input when "" template = templates.first when /^\d+$/ template = templates[input.to_i-1] else template = nil puts "Canceled" exit end else template = templates.first end unless template puts "Not select template." exit end puts "Using Template: %s" % template template[1] end |