-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathproject_init.go
128 lines (111 loc) · 4.14 KB
/
project_init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package commands
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/symfony-cli/console"
"github.com/symfony-cli/symfony-cli/git"
"github.com/symfony-cli/symfony-cli/local/platformsh"
"github.com/symfony-cli/terminal"
)
var projectInitCmd = &console.Command{
Category: "project",
Name: "init",
Aliases: []*console.Alias{{Name: "init"}},
Usage: "Initialize a new project using templates",
Description: `Initialize a new project using templates.
Templates used by this tool are fetched from ` + templatesGitRepository + `.
`,
Flags: []console.Flag{
dirFlag,
&console.StringFlag{Name: "template", Usage: "Project template to use", DefaultText: "autodetermined"},
&console.StringFlag{Name: "title", Usage: "Project title", DefaultText: "autodetermined based on directory name"},
&console.StringFlag{Name: "slug", DefaultValue: "app", Usage: "Project slug"},
&console.StringFlag{Name: "php", Usage: "PHP version to use"},
&console.BoolFlag{Name: "upsun", Usage: "Initialize Upsun"},
// FIXME: services should also be used to configure Docker? Instead of Flex?
// FIXME: services can also be guessed via the existing Docker Compose file?
&console.StringSliceFlag{Name: "service", Usage: "Configure some services", Hidden: true},
&console.BoolFlag{Name: "dump", Usage: "Dump file content instead of writing them on disk"},
&console.BoolFlag{Name: "force", Usage: "Force the overwrite of the files even if they already exists", Hidden: true},
},
Before: CheckGitIsAvailable,
Action: func(c *console.Context) error {
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)
projectDir, err := getProjectDir(c.String("dir"))
if err != nil {
return err
}
minorPHPVersion, err := forcePHPVersion(c.String("php"), projectDir)
if err != nil {
return err
}
if buf, err := gitInit(projectDir); err != nil {
fmt.Print(buf.String())
return err
}
slug := c.String("slug")
if slug == "" {
slug = "app"
}
cloudServices, err := parseCloudServices(projectDir, c.StringSlice("service"))
if err != nil {
return err
}
brand := platformsh.PlatformshBrand
if c.Bool("upsun") {
brand = platformsh.UpsunBrand
}
createdFiles, err := createRequiredFilesProject(brand, projectDir, slug, c.String("template"), minorPHPVersion, cloudServices, c.Bool("dump"), c.Bool("force"))
if err != nil {
return err
}
if c.Bool("dump") {
return nil
}
terminal.Println("\n<info>Project configured</>")
terminal.Println("")
if len(createdFiles) > 0 {
terminal.Println("The following files were created automatically:")
for _, file := range createdFiles {
terminal.Println("", file)
}
terminal.Println("")
ui.Section("Next Steps")
terminal.Println(" * Adapt the generated files if needed")
terminal.Printf(" * Commit them: <info>git add %s && git commit -m\"Add %s configuration\"</>\n", strings.Join(createdFiles, " "), brand)
terminal.Printf(" * Deploy: <info>%s deploy</>\n", c.App.HelpName)
} else {
terminal.Printf("Deploy the project via <info>%s deploy</>.\n", c.App.HelpName)
}
return nil
},
}
func gitInit(cwd string) (*bytes.Buffer, error) {
if _, err := os.Stat(filepath.Join(cwd, ".git")); err == nil || !os.IsNotExist(err) {
return nil, nil
}
// project:init is only used in a Cloud context, so we can safely force the
// branch to be "main"
return git.Init(cwd, true, false)
}