github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/cmd/vpm/compile.go (about)

     1  /*
     2   * Copyright (c) 2023-present unTill Pro, Ltd.
     3   * @author Alisher Nurmanov
     4   */
     5  
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/voedger/voedger/pkg/compile"
    17  	coreutils "github.com/voedger/voedger/pkg/utils"
    18  )
    19  
    20  func newCompileCmd(params *vpmParams) *cobra.Command {
    21  	cmd := &cobra.Command{
    22  		Use:   "compile",
    23  		Short: "compile voedger application",
    24  		RunE: func(cmd *cobra.Command, args []string) (err error) {
    25  			_, err = compile.Compile(params.Dir)
    26  			return
    27  		},
    28  	}
    29  	return cmd
    30  }
    31  
    32  func makeAbsPath(dir string) (string, error) {
    33  	if !filepath.IsAbs(dir) {
    34  		wd, err := os.Getwd()
    35  		if err != nil {
    36  			// notest
    37  			return "", fmt.Errorf("failed to get working directory: %w", err)
    38  		}
    39  		dir = filepath.Clean(filepath.Join(wd, dir))
    40  	}
    41  	exists, err := coreutils.Exists(dir)
    42  	if err != nil {
    43  		// notest
    44  		return "", err
    45  	}
    46  	if !exists {
    47  		return "", fmt.Errorf("%s not exists", dir)
    48  	}
    49  	return dir, nil
    50  }
    51  
    52  func prepareParams(cmd *cobra.Command, params *vpmParams, args []string) (err error) {
    53  	if len(args) > 0 {
    54  		switch {
    55  		case strings.Contains(cmd.Use, "init"):
    56  			params.PackagePath = args[0]
    57  		case strings.Contains(cmd.Use, "baseline") || strings.Contains(cmd.Use, "compat"):
    58  			params.TargetDir = filepath.Clean(args[0])
    59  		}
    60  	}
    61  	params.Dir, err = makeAbsPath(params.Dir)
    62  	if err != nil {
    63  		return
    64  	}
    65  	if params.IgnoreFile != "" {
    66  		params.IgnoreFile = filepath.Clean(params.IgnoreFile)
    67  	}
    68  	if params.TargetDir == "" {
    69  		params.TargetDir = params.Dir
    70  	}
    71  	return nil
    72  }