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 }