Class: CutAGemCommand

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/cutagem.rb

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Instance Method Summary collapse

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.banner = "      Usage: \#$0 [options] gemname\n    EOB\n\n    parser.separator \"\"\n    parser.separator \"Options:\"\n\n    parser.on(\"-s\", \"--select\", \"Select template interactively.\") do |@select|\n    end\n\n    parser.on(\"-d\", \"--desc\", \"Describe this gem.\") do |@description|\n    end\n\n    parser.on(\"-c\", \"--config\", \"Configure user values. Use $EDITOR\") do |c|\n      @config.parent.mkpath\n      unless @config.exist?\n        @config.open(\"w\") do |f|\n          f << <<-EOF.gsub(/^\\t+/, \"\")\n          author: \"\#{ENV['USER']}\"\n          email:  \"\#{ENV['USER']}@\#{ENV['HOST']}\"\n          EOF\n        end\n      end\n      exec(ENV[\"EDITOR\"], @config.to_s)\n    end\n\n    parser.on(\"--copy-template NAME\", \"Copy template to user template dir naming NAME\") do |name|\n      path = Pathname.new(ENV[\"HOME\"]) + \".cutagem/templates\" + name\n      if path.exist?\n        puts \"\#{path} is already exists.\"\n        exit 1\n      end\n      template = select_template(true)\n      cp_r template, path, :verbose => true\n      exit\n    end\n\n    parser.on(\"--version\", \"Show version string `\#{VERSION}'\") do\n      puts VERSION\n      exit\n    end\n  end\nend\n".gsub(/^\t+/, "")

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

#runObject



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

  author      = 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)
    author = 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