github.com/AiRISTAFlowInc/fs-cli@v0.2.6/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/AiRISTAFlowInc/fs-cli/common" 14 "github.com/AiRISTAFlowInc/fs-cli/util" 15 ) 16 17 const ( 18 pkgLegacySupport = "github.com/AiRISTAFlowInc/fs-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 var mdGoFilePath string 36 37 tplMetadata := "" 38 39 switch contribType { 40 case "action": 41 //ignore 42 return nil 43 case "trigger": 44 fmt.Printf("Generating metadata for legacy trigger: %s\n", contribPkg) 45 mdGoFilePath = filepath.Join(path, "trigger_metadata.go") 46 tplMetadata = tplTriggerMetadataGoFile 47 case "activity": 48 fmt.Printf("Generating metadata for legacy actvity: %s\n", contribPkg) 49 mdGoFilePath = filepath.Join(path, "activity_metadata.go") 50 tplMetadata = tplActivityMetadataGoFile 51 default: 52 return nil 53 } 54 55 mdFilePath := filepath.Join(path, contribType+".json") 56 pkg := filepath.Base(path) 57 58 if idx := strings.Index(pkg, "@"); idx > 0 { 59 pkg = pkg[:idx] 60 } 61 62 raw, err := ioutil.ReadFile(mdFilePath) 63 if err != nil { 64 return err 65 } 66 67 info := &struct { 68 Package string 69 MetadataJSON string 70 }{ 71 Package: pkg, 72 MetadataJSON: string(raw), 73 } 74 75 err = os.Chmod(path, 0777) 76 if err != nil { 77 return err 78 } 79 defer os.Chmod(path, 0555) 80 81 f, err := os.Create(mdGoFilePath) 82 if err != nil { 83 return err 84 } 85 RenderTemplate(f, tplMetadata, info) 86 f.Close() 87 88 return nil 89 } 90 91 var tplActivityMetadataGoFile = `package {{.Package}} 92 93 import ( 94 "github.com/AiRISTAFlowInc/fs-legacybridge" 95 "github.com/TIBCOSoftware/flogo-lib/core/activity" 96 ) 97 98 var jsonMetadata = ` + "`{{.MetadataJSON}}`" + ` 99 100 // init create & register activity 101 func init() { 102 md := activity.NewMetadata(jsonMetadata) 103 legacybridge.RegisterLegacyActivity(NewActivity(md)) 104 } 105 ` 106 107 var tplTriggerMetadataGoFile = `package {{.Package}} 108 109 import ( 110 "github.com/AiRISTAFlowInc/fs-legacybridge" 111 "github.com/TIBCOSoftware/flogo-lib/core/trigger" 112 ) 113 114 var jsonMetadata = ` + "`{{.MetadataJSON}}`" + ` 115 116 // init create & register trigger factory 117 func init() { 118 md := trigger.NewMetadata(jsonMetadata) 119 legacybridge.RegisterLegacyTriggerFactory(md.ID, NewFactory(md)) 120 } 121 ` 122 123 // RenderTemplate renders the specified template 124 func RenderTemplate(w io.Writer, text string, data interface{}) { 125 t := template.New("top") 126 t.Funcs(template.FuncMap{"trim": strings.TrimSpace}) 127 template.Must(t.Parse(text)) 128 if err := t.Execute(w, data); err != nil { 129 panic(err) 130 } 131 }