Class: Alki::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/alki/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Generator

Returns a new instance of Generator.



3
4
5
6
7
# File 'lib/alki/generator.rb', line 3

def initialize(root_dir)
  @root_dir = root_dir
  @changes = []
  @triggers = []
end

Instance Method Details

#abs_path(p) ⇒ Object



173
174
175
# File 'lib/alki/generator.rb', line 173

def abs_path(p)
  File.expand_path(p,@root_dir)
end

#add_line(file, line, opts = {}) ⇒ Object



63
64
65
# File 'lib/alki/generator.rb', line 63

def add_line(file,line,opts={})
  @changes << [:add_line,file: file,line: line,opts: opts]
end

#check_add_line(file:, line:, opts:) ⇒ Object



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
# File 'lib/alki/generator.rb', line 67

def check_add_line(file:,line:,opts:)
  if File.exists? abs_path(file)
    File.open(file) do |f|
      if opts[:after]
        found = until f.eof?
          break true if f.readline.chomp == opts[:after]
        end
        unless found
          puts "File \"#{file}\" doesn't contain required line #{opts[:after]}"
          return :abort
        end
      end
      until f.eof?
        l = f.readline
        if opts[:match] ? l.chomp.match(opts[:match]) : (l.chomp == line)
          return :skip
        end
      end
    end
  else
    unless @changes.find {|c| [:create,:opt_create].include?(c[0]) && c[1][:file] == file}
      puts "File \"#{file}\" doesn't exist!"
      return :abort
    end
  end
end

#check_changesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/alki/generator.rb', line 124

def check_changes
  puts "Checking preconditions..."
  abort = false
  unless Dir.exists?(@root_dir)
    puts "Root dir doesn't exist"
    exit 1
  end
  do_changes = []
  @changes.each do |(type,args)|
    res = send "check_#{type}", args
    abort = true if res == :abort
    do_changes << [type,args] unless res == :skip
  end
  @triggers.each do |(file,cmd)|
    if check_trigger do_changes, file
      do_changes << [:trigger,cmd: cmd]
    end
  end
  exit 1 if abort
  do_changes
end

#check_create(file:, contents:, opts:) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/alki/generator.rb', line 17

def check_create(file:, contents:, opts:)
  path = abs_path( file)
  if File.exists? path
    if File.read(path) == contents
      :skip
    end
  end
end

#check_opt_create(file:, contents:) ⇒ Object



49
50
51
52
53
# File 'lib/alki/generator.rb', line 49

def check_opt_create(file:, contents:)
  if File.exists? abs_path( file)
    :skip
  end
end

#check_trigger(changes, file) ⇒ Object



112
113
114
# File 'lib/alki/generator.rb', line 112

def check_trigger(changes,file)
  changes.find {|c| c[1][:file] == file}
end

#create(file, contents) ⇒ Object



9
10
11
# File 'lib/alki/generator.rb', line 9

def create(file,contents)
  @changes << [:create,file: file,contents: contents,opts: {}]
end

#create_exec(file, contents) ⇒ Object



13
14
15
# File 'lib/alki/generator.rb', line 13

def create_exec(file,contents)
  @changes << [:create,file: file,contents: contents,opts: {exec: true}]
end

#desc_add_line(file:, line:, opts:) ⇒ Object



94
95
96
# File 'lib/alki/generator.rb', line 94

def desc_add_line(file:,line:,opts:)
  "Add line \"#{line}\" to #{file}"
end

#desc_create(file:, contents:, opts: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alki/generator.rb', line 26

def desc_create(file:, contents:, opts: {})
  path = abs_path( file)
  if opts[:exec]
    adj = "executable "
  end
  if File.exists? path
    "Overwriting #{adj}#{file}!"
  else
    "Create #{adj}#{file}"
  end
end

#desc_opt_create(opts) ⇒ Object



55
56
57
# File 'lib/alki/generator.rb', line 55

def desc_opt_create(opts)
  desc_create opts
end

#desc_trigger(cmd:) ⇒ Object



116
117
118
# File 'lib/alki/generator.rb', line 116

def desc_trigger(cmd:)
  "Run \"#{cmd}\""
end

#do_add_line(file:, line:, opts:) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/alki/generator.rb', line 98

def do_add_line(file:,line:,opts:)
  if opts[:after]
    File.write file, File.read(file).sub(/^#{Regexp.quote(opts[:after])}\n/){|m| m + line + "\n"}
  else
    File.open(file,'a') do |f|
      f.puts line
    end
  end
end

#do_create(file:, contents:, opts: {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/alki/generator.rb', line 38

def do_create(file:, contents:, opts: {})
  path = abs_path( file)
  FileUtils.mkdir_p File.dirname(path)
  File.write path, contents
  FileUtils.chmod '+x', path if opts[:exec]
end

#do_opt_create(opts) ⇒ Object



59
60
61
# File 'lib/alki/generator.rb', line 59

def do_opt_create(opts)
  do_create opts
end

#do_trigger(cmd:) ⇒ Object



120
121
122
# File 'lib/alki/generator.rb', line 120

def do_trigger(cmd:)
  system cmd
end

#opt_create(file, contents) ⇒ Object



45
46
47
# File 'lib/alki/generator.rb', line 45

def opt_create(file,contents)
  @changes << [:opt_create,file: file,contents: contents]
end


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/alki/generator.rb', line 146

def print_overview(changes)
  puts "Overview of changes to be made:"

  changes.each do |(type,args)|
    desc = send "desc_#{type}", args
    puts "  #{desc}" if desc
  end
  print "Proceed? "
  resp = STDIN.gets.chomp
  unless resp =~ /^y(es?)?/i
    puts "Aborting"
    exit
  end
end

#trigger(file, cmd) ⇒ Object



108
109
110
# File 'lib/alki/generator.rb', line 108

def trigger(file,cmd)
  @triggers << [file, cmd]
end

#writeObject



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/alki/generator.rb', line 161

def write
  puts "Using project root: #{@root_dir}\n"
  do_changes = check_changes
  print_overview do_changes

  puts "Writing changes..."
  do_changes.each do |(type,args)|
    send "do_#{type}", args
  end
  puts "Done"
end