github.com/secoba/wails/v2@v2.6.4/pkg/templates/generate/generate.go (about)

     1  package main
     2  
     3  import (
     4  	"embed"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/leaanthony/debme"
     9  	"github.com/leaanthony/gosod"
    10  	"github.com/secoba/wails/v2/internal/s"
    11  )
    12  
    13  //go:embed assets/common/*
    14  var common embed.FS
    15  
    16  //go:embed assets/svelte/*
    17  var svelte embed.FS
    18  
    19  //go:embed assets/svelte-ts/*
    20  var sveltets embed.FS
    21  
    22  //go:embed assets/lit/*
    23  var lit embed.FS
    24  
    25  //go:embed assets/lit-ts/*
    26  var litts embed.FS
    27  
    28  //go:embed assets/vue/*
    29  var vue embed.FS
    30  
    31  //go:embed assets/vue-ts/*
    32  var vuets embed.FS
    33  
    34  //go:embed assets/react/*
    35  var react embed.FS
    36  
    37  //go:embed assets/react-ts/*
    38  var reactts embed.FS
    39  
    40  //go:embed assets/preact/*
    41  var preact embed.FS
    42  
    43  //go:embed assets/preact-ts/*
    44  var preactts embed.FS
    45  
    46  //go:embed assets/vanilla/*
    47  var vanilla embed.FS
    48  
    49  //go:embed assets/vanilla-ts/*
    50  var vanillats embed.FS
    51  
    52  func checkError(err error) {
    53  	if err != nil {
    54  		println("\nERROR:", err.Error())
    55  		os.Exit(1)
    56  	}
    57  }
    58  
    59  type template struct {
    60  	Name          string
    61  	ShortName     string
    62  	Description   string
    63  	Assets        embed.FS
    64  	FilesToDelete []string
    65  	DirsToDelete  []string
    66  }
    67  
    68  var templates = []*template{
    69  	{
    70  		Name:          "Svelte + Vite",
    71  		ShortName:     "Svelte",
    72  		Description:   "Svelte + Vite development server",
    73  		Assets:        svelte,
    74  		FilesToDelete: []string{"frontend/index.html", "frontend/.gitignore", "frontend/src/app.css", "frontend/src/assets/svelte.svg"},
    75  		DirsToDelete:  []string{"frontend/public", "frontend/src/lib"},
    76  	},
    77  	{
    78  		Name:          "Svelte + Vite (Typescript)",
    79  		ShortName:     "Svelte-TS",
    80  		Description:   "Svelte + TS + Vite development server",
    81  		Assets:        sveltets,
    82  		FilesToDelete: []string{"frontend/index.html", "frontend/.gitignore", "frontend/src/app.css", "frontend/src/assets/svelte.svg"},
    83  		DirsToDelete:  []string{"frontend/public", "frontend/src/lib"},
    84  	},
    85  	{
    86  		Name:          "Lit + Vite",
    87  		ShortName:     "Lit",
    88  		Description:   "Lit + Vite development server",
    89  		Assets:        lit,
    90  		FilesToDelete: []string{"frontend/index.html", "frontend/vite.config.js"},
    91  	},
    92  	{
    93  		Name:          "Lit + Vite (Typescript)",
    94  		ShortName:     "Lit-TS",
    95  		Description:   "Lit + TS + Vite development server",
    96  		Assets:        litts,
    97  		FilesToDelete: []string{"frontend/index.html", "frontend/src/favicon.svg"},
    98  	},
    99  	{
   100  		Name:          "Vue + Vite",
   101  		ShortName:     "Vue",
   102  		Description:   "Vue + Vite development server",
   103  		Assets:        vue,
   104  		FilesToDelete: []string{"frontend/index.html", "frontend/.gitignore"},
   105  		DirsToDelete:  []string{"frontend/src/assets", "frontend/src/components", "frontend/public"},
   106  	},
   107  	{
   108  		Name:          "Vue + Vite (Typescript)",
   109  		ShortName:     "Vue-TS",
   110  		Description:   "Vue + Vite development server",
   111  		Assets:        vuets,
   112  		FilesToDelete: []string{"frontend/index.html", "frontend/.gitignore"},
   113  		DirsToDelete:  []string{"frontend/src/assets", "frontend/src/components", "frontend/public"},
   114  	},
   115  	{
   116  		Name:          "React + Vite",
   117  		ShortName:     "React",
   118  		Description:   "React + Vite development server",
   119  		Assets:        react,
   120  		FilesToDelete: []string{"frontend/src/index.css", "frontend/src/favicon.svg", "frontend/src/logo.svg", "frontend/.gitignore", "frontend/index.html"},
   121  	},
   122  	{
   123  		Name:          "React + Vite (Typescript)",
   124  		ShortName:     "React-TS",
   125  		Description:   "React + Vite development server",
   126  		Assets:        reactts,
   127  		FilesToDelete: []string{"frontend/src/index.css", "frontend/src/favicon.svg", "frontend/src/logo.svg", "frontend/.gitignore", "frontend/index.html"},
   128  	},
   129  	{
   130  		Name:          "Preact + Vite",
   131  		ShortName:     "Preact",
   132  		Description:   "Preact + Vite development server",
   133  		Assets:        preact,
   134  		FilesToDelete: []string{"frontend/src/index.css", "frontend/src/favicon.svg", "frontend/src/logo.jsx", "frontend/.gitignore", "frontend/index.html"},
   135  		DirsToDelete:  []string{"frontend/public"},
   136  	},
   137  	{
   138  		Name:          "Preact + Vite (Typescript)",
   139  		ShortName:     "Preact-TS",
   140  		Description:   "Preact + Vite development server",
   141  		Assets:        preactts,
   142  		FilesToDelete: []string{"frontend/src/index.css", "frontend/src/favicon.svg", "frontend/src/assets/preact.svg", "frontend/src/logo.tsx", "frontend/.gitignore", "frontend/index.html"},
   143  		DirsToDelete:  []string{"frontend/public"},
   144  	},
   145  	{
   146  		Name:          "Vanilla + Vite",
   147  		ShortName:     "Vanilla",
   148  		Description:   "Vanilla + Vite development server",
   149  		Assets:        vanilla,
   150  		FilesToDelete: []string{"frontend/.gitignore", "frontend/index.html", "frontend/favicon.svg", "frontend/main.js", "frontend/style.css"},
   151  	},
   152  	{
   153  		Name:          "Vanilla + Vite (Typescript)",
   154  		ShortName:     "Vanilla-TS",
   155  		Description:   "Vanilla + Vite development server",
   156  		Assets:        vanillats,
   157  		FilesToDelete: []string{"frontend/.gitignore", "frontend/index.html", "frontend/favicon.svg", "frontend/src/main.ts", "frontend/src/style.css"},
   158  	},
   159  }
   160  
   161  func main() {
   162  	rebuildRuntime()
   163  
   164  	for _, t := range templates {
   165  		createTemplate(t)
   166  	}
   167  
   168  	// copy plain template
   169  	s.ECHO("Copying plain template")
   170  	s.RMDIR("../templates/plain")
   171  	s.COPYDIR("plain", "../templates/plain")
   172  
   173  	s.ECHO(`Until an auto fix is done, add "@babel/types": "^7.17.10" to vue-ts/frontend/package.json`)
   174  }
   175  
   176  func rebuildRuntime() {
   177  	s.ECHO("Generating Runtime")
   178  	cwd := s.CWD()
   179  	const runtimeDir = "../../../internal/frontend/runtime/"
   180  	const commonDir = "./assets/common/frontend/wailsjs/runtime/"
   181  	s.CD(runtimeDir)
   182  	s.EXEC("npm run build")
   183  	s.ECHO("Copying new files")
   184  	s.CD("wrapper")
   185  	s.COPY("package.json", commonDir+"package.json")
   186  	s.COPY("runtime.js", commonDir+"runtime.js")
   187  	s.COPY("runtime.d.ts", commonDir+"runtime.d.ts")
   188  	s.CD(cwd)
   189  }
   190  
   191  func createTemplate(template *template) {
   192  	cwd := s.CWD()
   193  	name := template.Name
   194  	shortName := strings.ToLower(template.ShortName)
   195  	assets, err := debme.FS(template.Assets, "assets/"+shortName)
   196  	checkError(err)
   197  	commonAssets, err := debme.FS(common, "assets/common")
   198  	checkError(err)
   199  
   200  	s.CD("..")
   201  	s.ENDIR("templates")
   202  	s.CD("templates")
   203  	s.RMDIR(shortName)
   204  	s.COPYDIR("../base", shortName)
   205  	s.CD(shortName)
   206  	s.ECHO("Generating vite template: " + shortName)
   207  	s.EXEC("npm create vite@latest frontend --template " + shortName)
   208  
   209  	// Clean up template
   210  	for _, fileToDelete := range template.FilesToDelete {
   211  		s.DELETE(fileToDelete)
   212  	}
   213  	for _, dirToDelete := range template.DirsToDelete {
   214  		s.RMDIR(dirToDelete)
   215  	}
   216  	s.REPLACEALL("README.md", s.Sub{"$NAME": template.ShortName})
   217  	s.REPLACEALL("template.json", s.Sub{"$NAME": name, "$SHORTNAME": shortName, "$DESCRIPTION": template.Description})
   218  
   219  	// Add common files
   220  	g := gosod.New(commonAssets)
   221  	g.SetTemplateFilters([]string{})
   222  	err = g.Extract(".", nil)
   223  	checkError(err)
   224  
   225  	// Add custom files
   226  	g = gosod.New(assets)
   227  	g.SetTemplateFilters([]string{})
   228  	err = g.Extract(".", nil)
   229  	checkError(err)
   230  
   231  	s.CD(cwd)
   232  }