Class: PartGen

Inherits:
Gen
  • Object
show all
Defined in:
lib/gen/part/gen.rb

Overview

gen part - Nitro part generator.

This generator copies a part into your existing Nitro application. Be sure to be in it’s main directory when calling this generator.

Example

gen part partname

Constant Summary

Constants inherited from Gen

Gen::LibPath, Gen::Version

Instance Attribute Summary

Attributes inherited from Gen

#generator_dir

Instance Method Summary collapse

Methods inherited from Gen

#teardown, #usage

Instance Method Details

#part_directory(partname) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gen/part/gen.rb', line 27

def part_directory(partname)
  part_paths = Array.new

  $:.each do |in_load_path|
    dir=Dir.new(in_load_path) rescue next
    dir.each do |a|
      part_paths << File.expand_path(File.join("#{in_load_path}","#{a}")) if a.to_s == "part"
    end
  end

  if part_paths.size < 1
    STDERR.puts "ERROR: part not found! Aborting!"
    exit 1
  end

  part_paths.each do |p|
    Dir.new(p).each do |name|
      return "#{p}/#{name}" if name == partname
    end
  end

  STDERR.puts "ERROR: part not found! Aborting!"
  exit 1
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gen/part/gen.rb', line 52

def run
  if File.exists?(File.join(@lib_path, @partname))
    STDERR.puts "ERROR: Path #{@lib_path} already exists! Aborting!"
    exit 1
  end

  # Copying Part to current directory /lib
  puts "Copying #{@partname} dir to '#{@lib_path}/#{@partname}'"
  FileUtils.mkdir_p(@lib_path)
  part_src = part_directory(@partname)
  FileUtils.cp_r(part_src, @lib_path)
  FileUtils.cp("#{part_src}.rb", @lib_path)

  # Moving public stuff to apps public/ dir if present
  if File.directory?(File.join("#{@lib_path}", "#{@partname}", "public"))
    puts "Moving #{@partname} public dir to #{@public_path}/#{@partname}"
    FileUtils.mkdir_p(File.join(@public_path, @partname))
    FileUtils.mv(Dir.glob(File.join(@lib_path, @partname, "public", "*")), File.join(@public_path, @partname))
  else
    puts "Part doesn't have public/ directory, skipping."
  end

  puts 'Done'
end

#setupObject



21
22
23
24
25
# File 'lib/gen/part/gen.rb', line 21

def setup
  @partname = ARGV[0] || usage()
  @lib_path = File.expand_path(File.join("lib", "part"))
  @public_path = File.expand_path(File.join("public", "part"))
end