Class: DefaultFiles

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

Overview

class DefaultFiles

A singleton class to generate header and souce files.
TODO: consider using class source_file.rb in Pareater

Class Method Summary collapse

Class Method Details

.create_clang_format(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/default_files.rb', line 13

def create_clang_format(path)
  open_file_and_write(
    "#{path}/.clang-format",
    <<~CLANG
      BasedOnStyle: LLVM
      IndentWidth: 4
      ColumnLimit: 86
      NamespaceIndentation: All
    CLANG
  )
end

.create_config(path, compiler = 'clang++', src_sfx = 'cpp', hdr_sfx = 'hpp') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/default_files.rb', line 25

def create_config(path, compiler = 'clang++', src_sfx = 'cpp', hdr_sfx = 'hpp')
  open_file_and_write(
    "#{path}/config.json",
    <<~CONFIG
      {
          "compiler": "#{compiler}",
          "header-suffix": "#{hdr_sfx}",
          "source-suffix": "#{src_sfx}",
          "flags": {
              "compile": {
                  "opt": "-O2",
                  "debug": "-g"
              },
              "link": {

              }
          }
      }
    CONFIG
  )
end

.create_cpp(filename, src_sfx = 'cpp', hdr_sfx = 'hpp') ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/default_files.rb', line 72

def create_cpp(filename, src_sfx = 'cpp', hdr_sfx = 'hpp')
  open_file_and_write(
    "#{filename}.#{src_sfx}",
    <<~DOC
      #include "#{filename}.#{hdr_sfx}"
    DOC
  )
end

.create_hpp(workspace, prefix, filename, hdr_sfx = 'hpp') ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/default_files.rb', line 81

def create_hpp(workspace, prefix, filename, hdr_sfx = 'hpp')
  open_file_and_write(
    "#{filename}.#{hdr_sfx}",
    <<~DOC
      #ifndef __#{workspace.upcase}__#{prefix.upcase}__#{filename.upcase}__
      #define __#{workspace.upcase}__#{prefix.upcase}__#{filename.upcase}__

      #endif
    DOC
  )
end

.create_lib_header(path, lib_name, suffix = 'hpp') ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/default_files.rb', line 60

def create_lib_header(path, lib_name, suffix = 'hpp')
  open_file_and_write(
    "#{path}/#{lib_name}.#{suffix}",
    <<~DOC
      #ifndef __#{lib_name.upcase}__
      #define __#{lib_name.upcase}__

      #endif
    DOC
  )
end

.create_main(path, suffix = 'cpp') ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/default_files.rb', line 47

def create_main(path, suffix = 'cpp')
  header = suffix == 'c' ? 'stdio.h' : 'iostream'
  open_file_and_write(
    "#{path}/main.#{suffix}",
    <<~DOC
      #include <#{header}>
      int main(int argc, char *argv[]) {

      }
    DOC
  )
end

.open_file_and_write(filename, content) ⇒ Object



7
8
9
10
11
# File 'lib/default_files.rb', line 7

def open_file_and_write(filename, content)
  File.open(filename, 'w') do |f|
    f.write(content)
  end
end