aboutsummaryrefslogtreecommitdiff
path: root/adapters/gtest/templating.go
blob: 41c54c1f1c6963f238a4f178d562dda2ccb4a583 (plain)
1
2
3
4
5
6
7
8
9
10
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package gtest

import (
	"io"
	"log"
        "os"
	"golang.flu0r1ne.net/planr"
        "text/template"
)

type cmakeUnit struct {
  ExeNm string
  File  string
  Srcs  string
};

func generateCmakeScript(out string, units []cmakeUnit) {
  file, err := os.OpenFile(out, os.O_RDWR | os.O_CREATE, 0644)
  defer func () {
    err := file.Close()

    if err != nil {
      log.Fatal(err)
    }
  }()

  if err != nil {
    log.Fatalf("Could not open CMakeFile (%s)\n%v", out, err)
  }

  writeCmakeBoilerplate(file)
  
  tmpl := unitTemplate()

  for _, unit := range units {
    if err := tmpl.Execute(file, unit); err != nil {
      log.Fatalf("Failed to generate unit %s: %v", unit.ExeNm, err);
    }
  }
}


// TODO: Add comments
func unitTemplate() *template.Template {
  tmpl, err := template.New("gtest_unit").Parse(`

################################################

## {{.ExeNm}}

add_executable(
  "{{.ExeNm}}"
  "{{.File}}"
  {{.Srcs}}
)

target_link_libraries(
  "{{.ExeNm}}"
  gtest_main
)

gtest_discover_tests(
  "{{.ExeNm}}"
)
`)

  if err != nil {
    log.Fatalf("Cannot load Gtest unit template %v", err)
  }

  return tmpl
}

const GOOGLE_TEST_URL = "https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip"

func writeCmakeBoilerplate(w io.Writer) {
  tmpl := boilderTemplate()
  
  tmpl.Execute(w, struct {
    Url     string
    Version string
  }{
    Url: GOOGLE_TEST_URL,
    Version: planr.VERSION,
  })
}

func boilderTemplate() *template.Template {
  tmpl, err := template.New("gtest_boilerplate").Parse(`
# AUTOMATICALLY GENERATED BY PLANR VERSION {{.Version}}

cmake_minimum_required (VERSION 3.1.0)

project(PlanRGtestAdapter)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL {{.Url}}
)

include(GoogleTest)
FetchContent_MakeAvailable(googletest)
`)
  
  if err != nil {
    log.Fatalf("Cannot load Gtest Cmake boilerplate")
  }

  return tmpl
}