Class: Gloss::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gloss/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



8
9
10
# File 'lib/gloss/cli.rb', line 8

def initialize(argv)
  @argv = argv
end

Instance Method Details

#runObject



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
93
94
95
# File 'lib/gloss/cli.rb', line 11

def run()
  command = @argv.first
  files = @argv.[](((1)..(-1)))
  err_msg = catch(:"error") { ||
case command
      when "init"
        force = false
        OptionParser.new() { |opt|
          opt.on("--force", "-f") { ||
            force = true
          }
        }
.parse(@argv)
        Initializer.new(force)
.run
      when "version", "--version", "-v"
        puts(Gloss::VERSION)
      when "watch", "build"
        Gloss.load_config
        type_checker = TypeChecker.new(Config.src_dir)
        type_checker = ProgLoader.new(type_checker)
.run
        (if command.==("watch")
          files = files.map() { |f|
            path = (if Pathname.new(f)
.absolute?
              f
            else
              File.join(Dir.pwd, f)
            end)
            (if Pathname.new(path)
.exist?
              path
            else
              throw(:"error", "Pathname #{f} does not exist")
            end)
          }
          Watcher.new(files)
.watch
        else
          (if command.==("build")
            entry_tree = Parser.new(File.read(Config.entrypoint))
.run
            Visitor.new(entry_tree, type_checker)
.run
            (if files.empty?
              files = Dir.glob("#{Config.src_dir}/**/*.gl")
            end)
            files.each() { |fp|
              fp = File.absolute_path(fp)
              preloaded_output = OUTPUT_BY_PATH.fetch(fp) { ||
nil                  }
              (if preloaded_output
                rb_output = preloaded_output
              else
                Gloss.logger
.info("Building #{fp}")
                content = File.read(fp)
                tree_hash = Parser.new(content)
.run
                rb_output = Visitor.new(tree_hash, type_checker)
.run
              end)
              Gloss.logger
.info("Type checking #{fp}")
              type_checker.run(fp, rb_output)
            }
            files.each() { |fp|
              fp = File.absolute_path(fp)
              rb_output = OUTPUT_BY_PATH.fetch(fp)
              Gloss.logger
.info("Writing #{fp}")
              Writer.new(rb_output, fp)
.run
            }
          end)
        end)
      else
        throw(:"error", "Gloss doesn't know how to #{command}")
    end
nil      }
  (if err_msg
    abort(err_msg)
  end)
end