Skip to content

Commit

Permalink
[TASK] added support for foreign temlates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathis Burger committed May 5, 2022
1 parent fb94f5e commit 6551f79
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ the cli will open a prompt to select a template
If this flag is added to the init command, the project name will automatically
selected. If this flag is not provided the cli will open a input prompt

4. `--customTemplateUrl`
If you want to use templates that are not from the softwareTemplates cli, you
can do so with this flag. Just insert the git url of you repository and the template
will be used.<br>
<strong>NOTE: The templates must have the valid init structure. Otherwise it will not work with the cli.


# Installation

Expand All @@ -64,3 +70,17 @@ selected. If this flag is not provided the cli will open a input prompt
<strong>Windows:</strong><br>
Move copy your file to `C:\Program Files`

# Creating templates

If you want to create your own project template for this cli, you can
create a new repository and start writing your project infrastructure.

If you want to use shell scripts to initialize your project you can
put files called `init.sh` and `init.bat` into your root directory of the
repository.
If you want your project to be supported directly by the cli, you can try to transfer
the ownership of your repository to the SoftwareTemplates organisation.
But I will check your template, because the cli only supports safe templates out of the box.

If you want to use a custom repository use the `--customTemplateUrl` flag for using foreign templates.

6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /bin/sh

GOOS=linux GOARCH=arm64 go build -o "softwareTemplates (macOS)" cmd/main.go
GOOS=linux go build -o "softwareTemplates (linux)" cmd/main.go
GOOS=windows go build -o "softwareTemplates (windows)" cmd/main.go
GOOS=linux GOARCH=arm64 go build -o "softwareTemplates-macOS-m1" cmd/main.go
GOOS=linux go build -o "softwareTemplates-linux-macOS" cmd/main.go
GOOS=windows go build -o "softwareTemplates-windows.exe" cmd/main.go
7 changes: 6 additions & 1 deletion internal/commandHandler/CommandHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func (h InternalHandler) HandleCommand() []*cli.Command {
Aliases: []string{"i"},
Usage: "Creates a project base on a template",
Action: commands.InitCommand,
Flags: []cli.Flag{flagHandler.InitGitFlag, flagHandler.TemplateFlag, flagHandler.ProjectNameFlag},
Flags: []cli.Flag{
flagHandler.InitGitFlag,
flagHandler.TemplateFlag,
flagHandler.ProjectNameFlag,
flagHandler.CustomTemplateUrlFlag,
},
},
}
}
Expand Down
18 changes: 13 additions & 5 deletions internal/commands/InitCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ func getProjectName(ctx *cli.Context) string {
// required for initializing a project from template
func InitCommand(ctx *cli.Context) error {
projectName := getProjectName(ctx)
name := templateLoader.GetTemplateName(ctx)

// Clones the project and deletes the old git repo
err := templateLoader.LoadTemplate(name, formatter.FormatProjectName(projectName))
if err != nil {
return err
customTemlateFlag := ctx.String("customTemplateUrl")

if customTemlateFlag != "" {
err := templateLoader.LoadTemplate(customTemlateFlag, formatter.FormatProjectName(projectName), true)
if err != nil {
return err
}
} else {
name := templateLoader.GetTemplateName(ctx)
err := templateLoader.LoadTemplate(name, formatter.FormatProjectName(projectName), false)
if err != nil {
return err
}
}

ScriptExecutor.ExecuteScript(projectName)
Expand Down
6 changes: 6 additions & 0 deletions internal/flagHandler/FlagHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ var ProjectNameFlag = &cli.StringFlag{
Usage: "The name of the project that should be used",
}

var CustomTemplateUrlFlag = &cli.StringFlag{
Name: "customTemplateUrl",
Usage: "If a custom template should be used that is not supported by the cli",
}

// HandleFlags returns all flags that are supported in the project
func HandleFlags() []cli.Flag {
return []cli.Flag{
InitGitFlag,
TemplateFlag,
ProjectNameFlag,
CustomTemplateUrlFlag,
}
}
2 changes: 0 additions & 2 deletions internal/templateInit/TemplateInit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package templateInit

import (
"fmt"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -34,7 +33,6 @@ func InitGitRepoIfRequired(ctx *cli.Context, projectName string) {

createGit := getShouldCreateGit(ctx)
if createGit {
fmt.Println("create")
cmd := exec.Command("git", "init", projectName)
err := cmd.Run()
if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions internal/templateLoader/TemplateLoader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package templateLoader

import (
"cli/internal/formatter"
"context"
"errors"
"github.com/google/go-github/github"
Expand Down Expand Up @@ -72,24 +73,28 @@ func GetTemplateName(ctx *cli.Context) string {

// LoadTemplate handles all template and project specific data
// and finally saves the project and renames it
func LoadTemplate(templateName string, projectName string) error {
func LoadTemplate(templateName string, projectName string, customTemplate bool) error {

templates := GetAllTemplates()
if !checkTemplateInArray(templates, templateName) {
if !checkTemplateInArray(templates, templateName) && !customTemplate {
return errors.New("this template does not exist")
}
templateRepo := getTemplateByName(templates, templateName)

cmd := exec.Command("git", "clone", *templateRepo.HTMLURL)
cmd := exec.Command("git", "clone", templateName)
projectPath := formatter.FormatProjectName(templateName)
if !customTemplate {
templateRepo := getTemplateByName(templates, templateName)
cmd = exec.Command("git", "clone", *templateRepo.HTMLURL)
projectPath = *templateRepo.Name
}
err := cmd.Run()
if err != nil {
return err
}
err = os.RemoveAll("./" + *templateRepo.Name + "/.git")
err = os.RemoveAll("./" + projectPath + "/.git")
if err != nil {
return err
}
err = os.Rename("./"+*templateRepo.Name, "./"+projectName)
err = os.Rename("./"+projectPath, "./"+projectName)
if err != nil {
return err
}
Expand Down

0 comments on commit 6551f79

Please sign in to comment.