Example #1
0
func Generate() error {
	compiledTemplates.Lock()
	defer compiledTemplates.Unlock()
	// First clear compiledTemplates map (theme could have been changed)
	compiledTemplates.m = make(map[string]*structure.Helper)
	// Compile all template files
	err := checkThemes()
	if err != nil {
		return err
	}
	// If the dev flag is set, watch the theme directory and the plugin directoy for changes
	// TODO: It seems unclean to do the watching of the plugins in the templates package. Move this somewhere else.
	if flags.IsInDevMode {
		// Get the currently used theme path
		activeTheme, err := database.RetrieveActiveTheme()
		if err != nil {
			return err
		}
		currentThemePath := filepath.Join(filenames.ThemesFilepath, *activeTheme)
		// Create watcher
		err = watcher.Watch([]string{currentThemePath, filenames.PluginsFilepath}, map[string]func() error{".hbs": Generate, ".lua": plugins.Load})
		if err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
func checkThemes() error {
	// Get currently set theme from database
	activeTheme, err := database.RetrieveActiveTheme()
	if err != nil {
		return err
	}
	currentThemePath := filepath.Join(filenames.ThemesFilepath, *activeTheme)
	err = compileTheme(currentThemePath)
	if err == nil {
		return nil
	}
	// If the currently set theme couldnt be compiled, try the default theme (promenade)
	err = compileTheme(filepath.Join(filenames.ThemesFilepath, "promenade"))
	if err == nil {
		// Update the theme name in the database
		err = methods.UpdateActiveTheme("promenade", 1)
		if err != nil {
			return err
		}
		return nil
	}
	// If all of that didn't work, try the available themes in order
	allThemes := GetAllThemes()
	for _, theme := range allThemes {
		err = compileTheme(filepath.Join(filenames.ThemesFilepath, theme))
		if err == nil {
			// Update the theme name in the database
			err = methods.UpdateActiveTheme(theme, 1)
			if err != nil {
				return err
			}
			return nil
		}
	}
	return errors.New("Couldn't find a theme to use in " + filenames.ThemesFilepath)
}