github.com/torresashjian/cli@v0.10.1-0.20210916231452-89080fe7069c/api/legacy.go (about) 1 package api 2 3 //Legacy Helper Functions 4 import ( 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "strings" 11 "text/template" 12 13 "github.com/torresashjian/cli/common" 14 "github.com/torresashjian/cli/util" 15 ) 16 17 const ( 18 pkgLegacySupport = "github.com/project-flogo/legacybridge" 19 ) 20 21 func InstallLegacySupport(project common.AppProject) error { 22 //todo make sure we only install once 23 pkgLegacySupportImport, err := util.NewFlogoImportFromPath(pkgLegacySupport) 24 if err != nil { 25 return err 26 } 27 err = project.AddImports(false, true, pkgLegacySupportImport) 28 if err == nil { 29 fmt.Println("Installed Legacy Support") 30 } 31 return err 32 } 33 34 func CreateLegacyMetadata(path, contribType, contribPkg string) error { 35 36 var mdGoFilePath string 37 38 tplMetadata := "" 39 40 switch contribType { 41 case "action": 42 //ignore 43 return nil 44 case "trigger": 45 fmt.Printf("Generating metadata for legacy trigger: %s\n", contribPkg) 46 mdGoFilePath = filepath.Join(path, "trigger_metadata.go") 47 tplMetadata = tplTriggerMetadataGoFile 48 case "activity": 49 fmt.Printf("Generating metadata for legacy actvity: %s\n", contribPkg) 50 mdGoFilePath = filepath.Join(path, "activity_metadata.go") 51 tplMetadata = tplActivityMetadataGoFile 52 default: 53 return nil 54 } 55 56 mdFilePath := filepath.Join(path, contribType+".json") 57 pkg := filepath.Base(path) 58 59 if idx := strings.Index(pkg, "@"); idx > 0 { 60 pkg = pkg[:idx] 61 } 62 63 raw, err := ioutil.ReadFile(mdFilePath) 64 if err != nil { 65 return err 66 } 67 68 info := &struct { 69 Package string 70 MetadataJSON string 71 }{ 72 Package: pkg, 73 MetadataJSON: string(raw), 74 } 75 76 err = os.Chmod(path, 0777) 77 if err != nil { 78 return err 79 } 80 defer os.Chmod(path, 0555) 81 82 f, err := os.Create(mdGoFilePath) 83 if err != nil { 84 return err 85 } 86 RenderTemplate(f, tplMetadata, info) 87 f.Close() 88 89 return nil 90 } 91 92 var tplActivityMetadataGoFile = `package {{.Package}} 93 94 import ( 95 "github.com/project-flogo/legacybridge" 96 "github.com/TIBCOSoftware/flogo-lib/core/activity" 97 ) 98 99 var jsonMetadata = ` + "`{{.MetadataJSON}}`" + ` 100 101 // init create & register activity 102 func init() { 103 md := activity.NewMetadata(jsonMetadata) 104 legacybridge.RegisterLegacyActivity(NewActivity(md)) 105 } 106 ` 107 108 var tplTriggerMetadataGoFile = `package {{.Package}} 109 110 import ( 111 "github.com/project-flogo/legacybridge" 112 "github.com/TIBCOSoftware/flogo-lib/core/trigger" 113 ) 114 115 var jsonMetadata = ` + "`{{.MetadataJSON}}`" + ` 116 117 // init create & register trigger factory 118 func init() { 119 md := trigger.NewMetadata(jsonMetadata) 120 legacybridge.RegisterLegacyTriggerFactory(md.ID, NewFactory(md)) 121 } 122 ` 123 124 //RenderTemplate renders the specified template 125 func RenderTemplate(w io.Writer, text string, data interface{}) { 126 t := template.New("top") 127 t.Funcs(template.FuncMap{"trim": strings.TrimSpace}) 128 template.Must(t.Parse(text)) 129 if err := t.Execute(w, data); err != nil { 130 panic(err) 131 } 132 }