Skip to content

Commit b38cb81

Browse files
committedFeb 6, 2019
Add support for themesDir and cache folder
1 parent 2e4f840 commit b38cb81

File tree

7 files changed

+99
-55
lines changed

7 files changed

+99
-55
lines changed
 

‎cmd/render.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,47 @@ import (
55
"io/ioutil"
66

77
"github.com/resumic/schema/render"
8+
"github.com/resumic/schema/theme"
89
"github.com/spf13/cobra"
910
)
1011

1112
func renderRun(cmd *cobra.Command, args []string) error {
1213
resumePath := args[0]
1314
htmlPath := args[1]
14-
themeName, err := cmd.Flags().GetString("theme")
15+
cacheDir, err := cmd.Flags().GetString("cacheDir")
1516
if err != nil {
1617
panic(err)
1718
}
19+
themesName, err := cmd.Flags().GetString("theme")
20+
if err != nil {
21+
panic(err)
22+
}
23+
themesDir, err := cmd.Flags().GetString("themesDir")
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
if themesDir == "" {
29+
theme.UpdateCache(cacheDir)
30+
themesDir, err = theme.GetThemesPath(themesName, cacheDir)
31+
if err != nil {
32+
return err
33+
}
34+
}
35+
1836
resume, err := ioutil.ReadFile(resumePath)
1937
if err != nil {
2038
return fmt.Errorf("Couldn't read the json resume from %s: %s", resumePath, err)
2139
}
22-
html, err := render.RenderHTML(resume, themeName)
40+
html, err := render.RenderHTML(resume, themesDir)
2341
if err != nil {
2442
return fmt.Errorf("Couldn't render the html resume: %s", err)
2543
}
2644
err = ioutil.WriteFile(htmlPath, html, 0600)
2745
if err != nil {
2846
return fmt.Errorf("Couldn't write the html resume to %s: %s", htmlPath, err)
2947
}
30-
fmt.Printf("%s rendered successfully to %s using %s as theme\n", resumePath, htmlPath, themeName)
48+
fmt.Printf("%s rendered successfully to %s using %s as theme\n", resumePath, htmlPath, themesDir)
3149
return nil
3250
}
3351

@@ -40,5 +58,6 @@ var renderCmd = &cobra.Command{
4058

4159
func init() {
4260
renderCmd.Flags().StringP("theme", "t", "test-theme", "Theme to use")
61+
renderCmd.Flags().StringP("themesDir", "d", "", "Filesystem path to themes directory")
4362
rootCmd.AddCommand(renderCmd)
4463
}

‎cmd/resumic.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cmd
22

33
import (
4+
"os"
5+
"path"
6+
47
"github.com/spf13/cobra"
58
)
69

@@ -9,6 +12,15 @@ var rootCmd = &cobra.Command{
912
Short: "Resumic is a standardized and generic data schema for your resume/CV",
1013
}
1114

15+
func init() {
16+
userCache, err := os.UserCacheDir()
17+
if err != nil {
18+
userCache = path.Join(os.TempDir(), "cache")
19+
}
20+
cacheDir := path.Join(userCache, "resumic")
21+
rootCmd.PersistentFlags().String("cacheDir", cacheDir, "Filesystem path to cache directory.")
22+
}
23+
1224
func Execute() {
1325
rootCmd.Execute()
1426
}

‎render/render.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,19 @@ type frontmatter struct {
1818
Layout string `json:"layout"`
1919
}
2020

21-
func build(root string) error {
22-
resp := commands.Execute([]string{"--quiet", "-s", root})
21+
func build(root, themePath string) error {
22+
resp := commands.Execute([]string{"--quiet", "-s", root, "--themesDir", themePath})
2323
return resp.Err
2424
}
2525

26-
func RenderHTML(resume []byte, theme string) ([]byte, error) {
26+
func RenderHTML(resume []byte, themePath string) ([]byte, error) {
2727
sitePath, err := ioutil.TempDir(os.TempDir(), "resumic")
2828
if err != nil {
2929
return nil, err
3030
}
3131
defer os.RemoveAll(sitePath)
3232

33-
themePath := path.Join(sitePath, "themes")
34-
err = extractTheme(themePath, theme)
35-
if err != nil {
36-
return nil, err
37-
}
38-
3933
config := siteConfig{}
40-
config.Theme = theme
4134
config.DisableKinds = []string{"taxonomy", "taxonomyTerm", "category", "sitemap", "RSS", "404", "robotsTXT", "home", "section"}
4235

4336
configJSON, err := json.MarshalIndent(config, "", " ")
@@ -77,7 +70,7 @@ func RenderHTML(resume []byte, theme string) ([]byte, error) {
7770
return nil, err
7871
}
7972

80-
err = build(sitePath)
73+
err = build(sitePath, themePath)
8174
if err != nil {
8275
return nil, err
8376
}

‎render/theme.go

-41
This file was deleted.

‎theme/default_themes.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package theme
2+
3+
import (
4+
"io"
5+
"os"
6+
"path"
7+
8+
"github.com/gobuffalo/packd"
9+
packr "github.com/gobuffalo/packr/v2"
10+
)
11+
12+
func getDefaultThemesPath(cachePath string) string {
13+
return path.Join(cachePath, "default_themes")
14+
}
15+
16+
func extractDefaultThemes(root string) error {
17+
box := packr.New("themes", "../theme/defaults")
18+
err := os.RemoveAll(root)
19+
if err != nil {
20+
return err
21+
}
22+
return box.Walk(func(name string, file packd.File) error {
23+
p := path.Join(root, name)
24+
os.MkdirAll(path.Dir(p), 0777)
25+
dst, err := os.Create(p)
26+
defer dst.Close()
27+
if err != nil {
28+
return err
29+
}
30+
_, err = io.Copy(dst, file)
31+
return err
32+
})
33+
}

‎theme/theme.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package theme
2+
3+
import (
4+
"os"
5+
"path"
6+
)
7+
8+
type ThemeNotFoundError struct {
9+
name string
10+
}
11+
12+
func (e *ThemeNotFoundError) Error() string {
13+
return "theme: " + e.name + " could not be found."
14+
}
15+
16+
func UpdateCache(cachePath string) error {
17+
defaultPath := getDefaultThemesPath(cachePath)
18+
return extractDefaultThemes(defaultPath)
19+
}
20+
21+
func GetThemesPath(themesName, cachePath string) (string, error) {
22+
defaultPath := getDefaultThemesPath(cachePath)
23+
themesPath := path.Join(defaultPath, themesName)
24+
if _, err := os.Stat(themesPath); os.IsNotExist(err) {
25+
return "", &ThemeNotFoundError{name: themesName}
26+
}
27+
return themesPath, nil
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.