github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/quarkus/create.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   *
    10   *  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  
    20  package quarkus
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
    26  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
    27  	"github.com/ory/viper"
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  func NewCreateCommand() *cobra.Command {
    32  	var cmd = &cobra.Command{
    33  		Use:   "create",
    34  		Short: "Create a Quarkus SonataFlow project",
    35  		Long: `
    36  	Creates a Quarkus SonataFlow project in the current directory.
    37  	It sets up a Quarkus project with minimal extensions to build a workflow
    38  	project.
    39  	The generated project has a "hello world" workflow.sw.json located on the
    40  	./<project-name>/src/main/resources directory.
    41  		`,
    42  		Example: `
    43  	# Create a project in the local directory
    44  	# By default the project is named "new-project"
    45  	{{.Name}} create
    46  
    47  	# Create a project with an specific name
    48  	{{.Name}} create --name myproject
    49  
    50  	# Create a project with additional extensions
    51  	# You can add multiple extensions by separating them with a comma
    52  	{{.Name}} create --extensions kogito-addons-quarkus-persistence-postgresql,quarkus-core
    53  		`,
    54  		SuggestFor: []string{"vreate", "creaet", "craete", "new"},
    55  		PreRunE:    common.BindEnv("name", "extension", "quarkus-platform-group-id", "quarkus-version"),
    56  	}
    57  
    58  	cmd.RunE = func(cmd *cobra.Command, args []string) error {
    59  		return runCreate()
    60  	}
    61  
    62  	quarkusDependencies := metadata.ResolveQuarkusDependencies()
    63  
    64  	cmd.Flags().StringP("name", "n", "new-project", "Project name created in the current directory.")
    65  	cmd.Flags().StringP("extension", "e", "", "On Quarkus projects, setup project custom Maven extensions, separated with a comma.")
    66  	cmd.Flags().StringP("quarkus-platform-group-id", "G", quarkusDependencies.QuarkusPlatformGroupId, "On Quarkus projects, setup project group id.")
    67  	cmd.Flags().StringP("quarkus-version", "V", quarkusDependencies.QuarkusVersion, "On Quarkus projects, setup the project version.")
    68  	cmd.SetHelpFunc(common.DefaultTemplatedHelp)
    69  
    70  	return cmd
    71  }
    72  
    73  func runCreate() error {
    74  	cfg, err := runCreateCmdConfig()
    75  	if err != nil {
    76  		return fmt.Errorf("initializing create config: %w", err)
    77  	}
    78  
    79  	exists, err := common.CheckIfDirExists(cfg.ProjectName)
    80  	if exists {
    81  		return fmt.Errorf("directory with name \"%s\" already exists", cfg.ProjectName)
    82  	}
    83  	if err != nil {
    84  		return fmt.Errorf("directory with name \"%s\" already exists: %w", cfg.ProjectName, err)
    85  	}
    86  
    87  	if err := common.CheckJavaDependencies(); err != nil {
    88  		return err
    89  	}
    90  
    91  	fmt.Println("🛠️ Creating a Quarkus SonataFlow project...")
    92  	if err = CreateQuarkusProject(cfg); err != nil {
    93  		fmt.Println("❌ ERROR: creating Quarkus SonataFlow project", err)
    94  		return err
    95  	}
    96  
    97  	workflowFilePath := fmt.Sprintf("./%s/src/main/resources/%s", cfg.ProjectName, metadata.WorkflowSwJson)
    98  	common.CreateWorkflow(workflowFilePath, false)
    99  
   100  	fmt.Println("🎉 Quarkus SonataFlow project successfully created")
   101  	return nil
   102  }
   103  
   104  func runCreateProject(cfg CreateQuarkusProjectConfig) (err error) {
   105  	if err = common.CheckProjectName(cfg.ProjectName); err != nil {
   106  		return err
   107  	}
   108  	exists, err := common.CheckIfDirExists(cfg.ProjectName)
   109  	if err != nil || exists {
   110  		return fmt.Errorf("directory with name \"%s\" already exists: %w", cfg.ProjectName, err)
   111  	}
   112  
   113  	create := common.ExecCommand(
   114  		"mvn",
   115  		fmt.Sprintf("%s:%s:%s:create", cfg.DependenciesVersion.QuarkusPlatformGroupId, metadata.QuarkusMavenPlugin, cfg.DependenciesVersion.QuarkusVersion),
   116  		"-DprojectGroupId=org.acme",
   117  		"-DnoCode",
   118  		fmt.Sprintf("-DplatformVersion=%s", cfg.DependenciesVersion.QuarkusVersion),
   119  		fmt.Sprintf("-DprojectArtifactId=%s", cfg.ProjectName),
   120  		fmt.Sprintf("-Dextensions=%s", cfg.Extensions))
   121  
   122  	fmt.Println("Creating a Quarkus SonataFlow project...")
   123  
   124  	if err := common.RunCommand(
   125  		create,
   126  		"create",
   127  	); err != nil {
   128  		return err
   129  	}
   130  	return
   131  }
   132  
   133  func runCreateCmdConfig() (cfg CreateQuarkusProjectConfig, err error) {
   134  	quarkusPlatformGroupId := viper.GetString("quarkus-platform-group-id")
   135  	quarkusVersion := viper.GetString("quarkus-version")
   136  
   137  	cfg = CreateQuarkusProjectConfig{
   138  		ProjectName: viper.GetString("name"),
   139  		Extensions: fmt.Sprintf("%s,%s,%s,%s",
   140  			metadata.QuarkusKubernetesExtension,
   141  			metadata.QuarkusResteasyJacksonExtension,
   142  			metadata.SmallryeHealth,
   143  			viper.GetString("extension"),
   144  		),
   145  		DependenciesVersion: metadata.DependenciesVersion{
   146  			QuarkusPlatformGroupId: quarkusPlatformGroupId,
   147  			QuarkusVersion:         quarkusVersion,
   148  		},
   149  	}
   150  	return
   151  }