Module: GenGen

Defined in:
lib/gengen.rb

Constant Summary collapse

REGEXP =
/{{{\s*(.*?)\s*}}}/

Class Method Summary collapse

Class Method Details

.extract_otpions!(args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/gengen.rb', line 56

def extract_otpions!(args)
  options = {}
  args.delete_if do |_|
    if _ =~ /^(\w+)=(\w+)$/
      options[$1] = $2
      true
    end
  end
  options
end

.fetch(git_url) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/gengen.rb', line 67

def fetch(git_url)
  temp_dir = Dir.mktmpdir
  puts "git clone #{git_url} ..."
  if system 'git', 'clone', git_url, temp_dir
    FileUtils.rm_rf(File.join(temp_dir, '.git'))
    temp_dir
  else
    abort "[error] faild to git clone '#{git_url}'!"
  end
end

.gen(args) ⇒ Object



10
11
12
13
14
15
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
# File 'lib/gengen.rb', line 10

def gen(args)
  args = args.dup
  options = extract_otpions!(args)

  local = true if args.delete('-l') || args.delete('--local')

  if local
    git_url = args[0]
  else
    github_project = args[0]
    unless github_project && github_project =~ /^[^\/]+\/[^\/]+$/
      usage!
    end
    github_project += '.gengen' unless github_project =~ /\.gengen$/
    git_url = "https://github.com/#{github_project}.git"
  end

  dest_dir = if args[1]
      args[1]
    else
      print "[directory]: "
      STDIN.gets.strip
    end

  if File.exists?(dest_dir)
    abort "[error] '#{dest_dir}' already exists"
  end

  temp_dir = fetch(git_url)
  process(temp_dir, options)

  FileUtils.mv(temp_dir, dest_dir)
end

.process(dir, vars) ⇒ Object



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
# File 'lib/gengen.rb', line 78

def process(dir, vars)
  Dir[File.join(dir, '**', '*')].each do |filepath|
    next unless File.file?(filepath)

    basename = File.basename(filepath)
    if basename =~ REGEXP
      old_path = filepath
      new_path = File.join File.dirname(filepath), replace(basename, vars)
      puts "renaming... #{old_path.sub(dir + '/', '')} => #{new_path.sub(dir + '/', '')}"
      FileUtils.mv(old_path, new_path)
    end
  end

  Dir[File.join(dir, '**', '*')].each do |filepath|
    next unless File.file?(filepath)

    puts "processing... #{filepath.sub(dir + '/', '')}"
    contents = replace(File.read(filepath), vars)
    File.open(filepath, 'w') do |file|
      file.write contents
    end
  end

  puts 'done!'
end

.replace(text, vars) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gengen.rb', line 104

def replace(text, vars)
  text.gsub(REGEXP) do |_|
    var_name = $1
    unless vars.key?(var_name)
      print "[#{var_name}]: "
      value = STDIN.gets.strip
      vars[var_name] = value.empty? ? var_name : value
    end
    vars[var_name]
  end
end

.usage!Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gengen.rb', line 44

def usage!
  abort <<-D
Usage:
  gengen user/template [directory] [foo=bar ...]
  gengen --local(-l) git_repository_path [directory] [foo=bar ...]

Examples:
  gengen jugyo/sublime-plugin RubyUtils name=RubyUtils command=test
  gengen -l /path/to/sublime-plugin RubyUtils name=RubyUtils command=test
  D
end