Class: Scrag::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/scrag/compiler.rb

Constant Summary collapse

SPECIALS =
[:top_module,:exec,:gemspec]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler

Returns a new instance of Compiler.



12
13
14
# File 'lib/scrag/compiler.rb', line 12

def initialize options={}
  @options=options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/scrag/compiler.rb', line 9

def options
  @options
end

#project_nameObject

Returns the value of attribute project_name.



10
11
12
# File 'lib/scrag/compiler.rb', line 10

def project_name
  @project_name
end

Instance Method Details

#check_doesnt_exists(dir) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scrag/compiler.rb', line 71

def check_doesnt_exists dir
  if Dir.exists?(dir)
    if options[:force]
      puts "WARNING : project already exists !"
      puts "Are you sure you want to force generation (Y) ? files will be lost !"
      key=$stdin.gets.chomp
      if key=='Y'
        puts "proceeding"
      else
        puts "aborting"
        abort
      end
    else
      puts "Scrag ERROR : project '#{project_name}' already exists. Type -v for options."
      abort
    end
  end
end

#compile(project_name) ⇒ Object



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/scrag/compiler.rb', line 16

def compile project_name
  @project_name=project_name.downcase
  puts "project #{project_name}".center(65,'=')
  @files_for_dir={
    project_name          => [:gemspec],
    "bin"                 => [:exec],
    "lib"                 => [:top_module],
    "lib/#{project_name}" => [
                              :ast,
                              :code,
                              :compiler,
                              :exec,
                              :generic_lexer,
                              :generic_parser,
                              :lexer,
                              :parser,
                              :pretty_printer,
                              :runner,
                              :token,
                              :top_module,
                              :transformer,
                              :version,
                              :visitor,
                            ]
  }
  generate
end

#create(dir) ⇒ Object



66
67
68
69
# File 'lib/scrag/compiler.rb', line 66

def create dir
  item "creating dir '#{dir}'"
  create_stuff dir
end

#create_file(file) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/scrag/compiler.rb', line 105

def create_file file

  gen_file=(SPECIALS.include?(file)) ? project_name : file
  ext=case file
  when :exec    then ""
  when :gemspec then ".gemspec"
  else ".rb"
  end
  item 1,"creating file '#{gen_file}'"
  template=load_template(file)
  renderer=ERB.new(template)
  gen_code=renderer.result(binding)
  code=Code.new(gen_code)
  code.save_as "#{gen_file}#{ext}",verbose=false
end

#create_stuff(dir) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/scrag/compiler.rb', line 90

def create_stuff dir
  FileUtils.rm_rf dir
  Dir.mkdir dir
  Dir.chdir dir
  rel_dir=dir.split('/')[1..-1].join('/')
  files_to_create=@files_for_dir[rel_dir]
  if files_to_create
    files_to_create.each do |file|
      create_file(file)
    end
  end
  Dir.chdir @pwd
end

#generateObject



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

def generate
  @pwd=Dir.pwd
  check_doesnt_exists(project_name)
  create "#{project_name}"
  create "#{project_name}/bin"
  create "#{project_name}/lib"
  create "#{project_name}/lib/#{project_name}"
  create "#{project_name}/tests"
end

#item(n = 0, str) ⇒ Object



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

def item n=0,str
  subitem=(n==0) ? "": "  "
  spaces=case n
  when 0 then 0
  when 1 then 1
  else
    1+(n-1)*4
  end

  puts " "*spaces+"#{subitem}[+] #{str}"
end

#load_template(file) ⇒ Object



121
122
123
124
# File 'lib/scrag/compiler.rb', line 121

def load_template file
  template="#{__dir__}/../templates/#{file}.erb"
  template_str=IO.read template
end