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