diff options
author | John Olheiser <42128690+jolheiser@users.noreply.github.com> | 2020-01-28 07:57:15 -0600 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2020-01-28 13:57:15 +0000 |
commit | 28216bde46a8cf415755ca41f3d58843eeb45e7c (patch) | |
tree | a07282f9c102d0c4605094eeeb497453aa0dc36f /modules/repository/generate.go | |
parent | 206a031b38a766d0ce89ae94a304f7d418ccdafb (diff) | |
download | gitea-28216bde46a8cf415755ca41f3d58843eeb45e7c.tar.gz gitea-28216bde46a8cf415755ca41f3d58843eeb45e7c.zip |
More expansions in template repositories (#10021)
* Super expansion
* Explain which features are in 1.11 vs 1.12
* Move imports
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Diffstat (limited to 'modules/repository/generate.go')
-rw-r--r-- | modules/repository/generate.go | 138 |
1 files changed, 82 insertions, 56 deletions
diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 96ce25e59f..52502e8eb3 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -16,38 +16,62 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + + "github.com/huandu/xstrings" ) +type transformer struct { + Name string + Transform func(string) string +} + +type expansion struct { + Name string + Value string + Transformers []transformer +} + +var defaultTransformers = []transformer{ + {Name: "SNAKE", Transform: xstrings.ToSnakeCase}, + {Name: "KEBAB", Transform: xstrings.ToKebabCase}, + {Name: "CAMEL", Transform: func(str string) string { + return xstrings.FirstRuneToLower(xstrings.ToCamelCase(str)) + }}, + {Name: "PASCAL", Transform: xstrings.ToCamelCase}, + {Name: "LOWER", Transform: strings.ToLower}, + {Name: "UPPER", Transform: strings.ToUpper}, + {Name: "TITLE", Transform: strings.Title}, +} + func generateExpansion(src string, templateRepo, generateRepo *models.Repository) string { + expansions := []expansion{ + {Name: "REPO_NAME", Value: generateRepo.Name, Transformers: defaultTransformers}, + {Name: "TEMPLATE_NAME", Value: templateRepo.Name, Transformers: defaultTransformers}, + {Name: "REPO_DESCRIPTION", Value: generateRepo.Description, Transformers: nil}, + {Name: "TEMPLATE_DESCRIPTION", Value: templateRepo.Description, Transformers: nil}, + {Name: "REPO_OWNER", Value: generateRepo.OwnerName, Transformers: defaultTransformers}, + {Name: "TEMPLATE_OWNER", Value: templateRepo.OwnerName, Transformers: defaultTransformers}, + {Name: "REPO_LINK", Value: generateRepo.Link(), Transformers: nil}, + {Name: "TEMPLATE_LINK", Value: templateRepo.Link(), Transformers: nil}, + {Name: "REPO_HTTPS_URL", Value: generateRepo.CloneLink().HTTPS, Transformers: nil}, + {Name: "TEMPLATE_HTTPS_URL", Value: templateRepo.CloneLink().HTTPS, Transformers: nil}, + {Name: "REPO_SSH_URL", Value: generateRepo.CloneLink().SSH, Transformers: nil}, + {Name: "TEMPLATE_SSH_URL", Value: templateRepo.CloneLink().SSH, Transformers: nil}, + } + + var expansionMap = make(map[string]string) + for _, e := range expansions { + expansionMap[e.Name] = e.Value + for _, tr := range e.Transformers { + expansionMap[fmt.Sprintf("%s_%s", e.Name, tr.Name)] = tr.Transform(e.Value) + } + } + return os.Expand(src, func(key string) string { - switch key { - case "REPO_NAME": - return generateRepo.Name - case "TEMPLATE_NAME": - return templateRepo.Name - case "REPO_DESCRIPTION": - return generateRepo.Description - case "TEMPLATE_DESCRIPTION": - return templateRepo.Description - case "REPO_OWNER": - return generateRepo.OwnerName - case "TEMPLATE_OWNER": - return templateRepo.OwnerName - case "REPO_LINK": - return generateRepo.Link() - case "TEMPLATE_LINK": - return templateRepo.Link() - case "REPO_HTTPS_URL": - return generateRepo.CloneLink().HTTPS - case "TEMPLATE_HTTPS_URL": - return templateRepo.CloneLink().HTTPS - case "REPO_SSH_URL": - return generateRepo.CloneLink().SSH - case "TEMPLATE_SSH_URL": - return templateRepo.CloneLink().SSH - default: - return key + if expansion, ok := expansionMap[key]; ok { + return expansion } + return key }) } @@ -104,41 +128,43 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp return fmt.Errorf("checkGiteaTemplate: %v", err) } - if err := os.Remove(gt.Path); err != nil { - return fmt.Errorf("remove .giteatemplate: %v", err) - } - - // Avoid walking tree if there are no globs - if len(gt.Globs()) > 0 { - tmpDirSlash := strings.TrimSuffix(filepath.ToSlash(tmpDir), "/") + "/" - if err := filepath.Walk(tmpDirSlash, func(path string, info os.FileInfo, walkErr error) error { - if walkErr != nil { - return walkErr - } + if gt != nil { + if err := os.Remove(gt.Path); err != nil { + return fmt.Errorf("remove .giteatemplate: %v", err) + } - if info.IsDir() { - return nil - } + // Avoid walking tree if there are no globs + if len(gt.Globs()) > 0 { + tmpDirSlash := strings.TrimSuffix(filepath.ToSlash(tmpDir), "/") + "/" + if err := filepath.Walk(tmpDirSlash, func(path string, info os.FileInfo, walkErr error) error { + if walkErr != nil { + return walkErr + } - base := strings.TrimPrefix(filepath.ToSlash(path), tmpDirSlash) - for _, g := range gt.Globs() { - if g.Match(base) { - content, err := ioutil.ReadFile(path) - if err != nil { - return err - } + if info.IsDir() { + return nil + } - if err := ioutil.WriteFile(path, - []byte(generateExpansion(string(content), templateRepo, generateRepo)), - 0644); err != nil { - return err + base := strings.TrimPrefix(filepath.ToSlash(path), tmpDirSlash) + for _, g := range gt.Globs() { + if g.Match(base) { + content, err := ioutil.ReadFile(path) + if err != nil { + return err + } + + if err := ioutil.WriteFile(path, + []byte(generateExpansion(string(content), templateRepo, generateRepo)), + 0644); err != nil { + return err + } + break } - break } + return nil + }); err != nil { + return err } - return nil - }); err != nil { - return err } } |