Class: Waw::Wawgen::Create

Inherits:
Object show all
Defined in:
lib/waw/wawgen/create.rb

Overview

Implementation of ‘waw create’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCreate

Creates a command instance



15
16
17
18
# File 'lib/waw/wawgen/create.rb', line 15

def initialize
  @layout = 'empty'
  @force = false
end

Instance Attribute Details

#forceObject (readonly)

Force overrides?



12
13
14
# File 'lib/waw/wawgen/create.rb', line 12

def force
  @force
end

#layoutObject (readonly)

Which layout



9
10
11
# File 'lib/waw/wawgen/create.rb', line 9

def layout
  @layout
end

Instance Method Details

#exit(msg) ⇒ Object

Puts an error message and exits



95
96
97
98
# File 'lib/waw/wawgen/create.rb', line 95

def exit(msg)
  puts msg
  Kernel.exit(-1)
end

#generate(project) ⇒ Object

Generates the project



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/waw/wawgen/create.rb', line 148

def generate(project)
  # A small debug message and we start
  puts "Generating project with names #{project.upname} inside #{project.lowname} using layout #{layout}"
  
  # 1) Create the output folder if it not exists
  if File.exists?(project.folder) and not(force)
    exit("The project folder #{project.lowname} already exists. Remove it first")
  else
    FileUtils.rm_rf project.folder if File.exists?(project.folder)
    FileUtils.mkdir_p project.folder
  end

  generate_layout(project, layout)
  FileUtils.chmod 0755, File.join(project.root, 'config.ru')
end

#generate_layout(project, layout) ⇒ Object

Generates a given layout for a specific project



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

def generate_layout(project, layout)
  # Locate the layout folder
  layout_folder = File.join(File.dirname(__FILE__), '..', '..', '..', 'layouts', layout)
  puts File.expand_path(layout_folder)
  exit("Unknown layout #{layout}") unless File.exists?(layout_folder)
  
  # Handle dependencies
  dependencies_file = File.join(layout_folder, 'dependencies')
  if File.exists?(dependencies_file)
    File.readlines(dependencies_file).each do |line|
      next if /^#/ =~ (line = line.strip)
      line.split(/\s/).each do |word| 
        generate_layout(project, word)
      end
    end
  end
  
  # Let recursive generation occur
  puts "Generating recursively project, #{layout_folder}, #{project.folder}"
  generate_recursively(project, layout_folder, project.folder)
end

#generate_recursively(project, layout_folder, target_folder) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/waw/wawgen/create.rb', line 100

def generate_recursively(project, layout_folder, target_folder)
  # Generate files now
  Dir.new(layout_folder).each do |file|
    next if ['.', '..', 'dontforgetme'].include?(file)
    next if 'dependencies'==file
    target = File.join(target_folder, file.gsub('project', project.lowname))
    puts "Generating #{target} from #{layout}/#{file}"

    if File.directory?(source=File.join(layout_folder, file))
      FileUtils.mkdir_p target unless File.exists?(target)
      generate_recursively(project, source, target)
    else
      File.open(target, 'w') do |io|
        if /jquery/ =~ file
          io << File.read(File.join(layout_folder, file))
        else
          context = {"project" => project}
          io << WLang.file_instantiate(File.join(layout_folder, file), context, 'wlang/active-string', :parentheses).to_s
        end
      end
    end
  end
end

#optionsObject

Parses commandline options provided as an array of Strings.



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
64
# File 'lib/waw/wawgen/create.rb', line 21

def options
  @options  ||= OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = WLang::VERSION
    opt.release = nil
    opt.summary_indent = ' ' * 4
    banner = <<-EOF
      # Usage waw create [options] ProjectName

      # Creates an initial waw project structure
    EOF
    opt.banner = banner.gsub(/[ \t]+# /, "")
    
    opt.separator nil
    opt.separator "Options:"
    
    opt.on("--force", "-f", "Erase any exsting file on conflict") do |value|
      @force = true
    end

    opt.on("--layout=LAYOUT", "-l", "Use a specific waw layout (default empty)") do |value|
      @layout = value
    end

    opt.on("--verbose", "-v", "Display extra progress as we parse.") do |value|
      @verbosity = 2
    end

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opt.on_tail("-h", "--help", "Show this message") do
      puts options
      exit
    end

    # Another typical switch to print the version.
    opt.on_tail("--version", "Show version") do
      puts "waw version " << Waw::VERSION << " (c) University of Louvain, Bernard & Louis Lambeau"
      exit
    end
    
    opt.separator nil
  end
end

#run(argv) ⇒ Object

Runs the command



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/waw/wawgen/create.rb', line 67

def run(argv)
  # parse options
  rest = options.parse!(argv)
  if rest.length != 1
    puts options
    exit(-1)
  end
  
  # check project name
  project_name = rest[0]
  exit("Invalid project name #{project_name}, must start with [a-zA-Z]") unless /^[a-zA-Z]/ =~ project_name
  if project_name =~ /^[a-z]/
    project_name = (project_name[0...1].upcase + project_name[1..-1]) 
    puts "Warning: using #{project_name} as project name for ruby needs..."
  end
  
  # create project
  project = ::Waw::Wawgen::Project.new(project_name)
  begin
    generate(project)
  rescue WLang::Error => ex
    puts ex.message
    puts ex.wlang_backtrace.join("\n")
    puts ex.backtrace.join("\n")
  end
end