github.com/AiRISTAFlowInc/fs-cli@v0.2.6/commands/build.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  
     9  	"github.com/AiRISTAFlowInc/fs-cli/api"
    10  	"github.com/AiRISTAFlowInc/fs-cli/common"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var buildShim string
    15  var buildOptimize bool
    16  var buildEmbed bool
    17  var syncImport bool
    18  var flogoJsonFile string
    19  
    20  func init() {
    21  	buildCmd.Flags().StringVarP(&buildShim, "shim", "", "", "use shim trigger")
    22  	buildCmd.Flags().BoolVarP(&buildOptimize, "optimize", "o", false, "optimize build")
    23  	buildCmd.Flags().BoolVarP(&buildEmbed, "embed", "e", false, "embed configuration in binary")
    24  	buildCmd.Flags().StringVarP(&flogoJsonFile, "file", "f", "", "specify a flogo.json to build")
    25  	buildCmd.Flags().BoolVarP(&syncImport, "sync", "s", false, "sync imports during build")
    26  	rootCmd.AddCommand(buildCmd)
    27  }
    28  
    29  // Build the project.
    30  var buildCmd = &cobra.Command{
    31  	Use:              "build",
    32  	Short:            "build the flogo application",
    33  	Long:             `Build the flogo application.`,
    34  	PersistentPreRun: func(cmd *cobra.Command, args []string) {},
    35  	Run: func(cmd *cobra.Command, args []string) {
    36  		var err error
    37  		if flogoJsonFile == "" {
    38  			preRun(cmd, args, verbose)
    39  			options := common.BuildOptions{Shim: buildShim, OptimizeImports: buildOptimize, EmbedConfig: buildEmbed}
    40  
    41  			if syncImport {
    42  				err = api.SyncProjectImports(common.CurrentProject())
    43  				if err != nil {
    44  					fmt.Fprintf(os.Stderr, "Error synchronzing imports: %v\n", err)
    45  					os.Exit(1)
    46  				}
    47  			}
    48  
    49  			err = api.BuildProject(common.CurrentProject(), options)
    50  			if err != nil {
    51  				fmt.Fprintf(os.Stderr, "Error building project: %v\n", err)
    52  				os.Exit(1)
    53  			}
    54  		} else {
    55  			//If a jsonFile is specified in the build.
    56  			//Create a new project in the temp folder and copy the bin.
    57  
    58  			tempDir, err := api.GetTempDir()
    59  			if err != nil {
    60  				fmt.Fprintf(os.Stderr, "Error getting temp dir: %v\n", err)
    61  				os.Exit(1)
    62  			}
    63  
    64  			api.SetVerbose(verbose)
    65  			tempProject, err := api.CreateProject(tempDir, "", flogoJsonFile, "latest")
    66  			if err != nil {
    67  				fmt.Fprintf(os.Stderr, "Error creating temp project: %v\n", err)
    68  				os.Exit(1)
    69  			}
    70  
    71  			common.SetCurrentProject(tempProject)
    72  
    73  			options := common.BuildOptions{Shim: buildShim, OptimizeImports: buildOptimize, EmbedConfig: buildEmbed}
    74  
    75  			err = api.BuildProject(common.CurrentProject(), options)
    76  			if err != nil {
    77  				fmt.Fprintf(os.Stderr, "Error building temp project: %v\n", err)
    78  				os.Exit(1)
    79  			}
    80  
    81  			copyBin(verbose, tempProject)
    82  		}
    83  	},
    84  }
    85  
    86  func copyBin(verbose bool, tempProject common.AppProject) {
    87  
    88  	currDir, err := os.Getwd()
    89  	if err != nil {
    90  		fmt.Fprintf(os.Stderr, "Error determining working directory: %v\n", err)
    91  		os.Exit(1)
    92  	}
    93  
    94  	if verbose {
    95  		fmt.Printf("Copying the binary from  %s to %s \n", tempProject.BinDir(), currDir)
    96  	}
    97  
    98  	if runtime.GOOS == "windows" || api.GOOSENV == "windows" {
    99  		err = os.Rename(tempProject.Executable(), filepath.Join(currDir, "main.exe"))
   100  		if err != nil {
   101  			fmt.Fprintf(os.Stderr, "Error renaming executable: %v\n", err)
   102  			os.Exit(1)
   103  		}
   104  	} else {
   105  		err = os.Rename(tempProject.Executable(), filepath.Join(currDir, tempProject.Name()))
   106  		if err != nil {
   107  			fmt.Fprintf(os.Stderr, "Error renaming executable: %v\n", err)
   108  			os.Exit(1)
   109  		}
   110  	}
   111  
   112  	if verbose {
   113  		fmt.Printf("Removing the temp dir: %s\n ", tempProject.Dir())
   114  	}
   115  
   116  	err = os.RemoveAll(tempProject.Dir())
   117  	if err != nil {
   118  		fmt.Fprintf(os.Stderr, "Error removing temp dir: %v\n", err)
   119  		os.Exit(1)
   120  	}
   121  }