github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/pushAura.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 var cmdPushAura = &Command{ 13 Usage: "pushAura", 14 Short: "force pushAura -filename=<filepath>", 15 Long: ` 16 force pushAura -filename <fullFilePath> 17 18 force pushAura -f=<fullFilePath> 19 20 `, 21 } 22 23 func init() { 24 cmdPushAura.Run = runPushAura 25 cmdPushAura.Flag.StringVar(fileName, "f", "", "fully qualified file name for entity") 26 // cmdPushAura.Flag.BoolVar(isBundle, "b", false, "Creating a bundle or not") 27 cmdPushAura.Flag.StringVar(createType, "t", "", "Type of entity or bundle to create") 28 } 29 30 var ( 31 fileName = cmdPushAura.Flag.String("filepath", "", "fully qualified file name for entity") 32 // isBundle = cmdPushAura.Flag.Bool("isBundle", false, "Creating a bundle or not") 33 createType = cmdPushAura.Flag.String("auraType", "", "Type of entity or bundle to create") 34 ) 35 36 func runPushAura(cmd *Command, args []string) { 37 absPath, _ := filepath.Abs(*fileName) 38 *fileName = absPath 39 40 if _, err := os.Stat(*fileName); os.IsNotExist(err) { 41 fmt.Println(err.Error()) 42 ErrorAndExit("File does not exist\n" + *fileName) 43 } 44 45 // Verify that the file is in an aura bundles folder 46 if !InAuraBundlesFolder(*fileName) { 47 ErrorAndExit("File is not in an aura bundle folder (aurabundles") 48 } 49 50 // See if this is a directory 51 info, _ := os.Stat(*fileName) 52 if info.IsDir() { 53 filepath.Walk(*fileName, func(path string, inf os.FileInfo, err error) error { 54 info, err = os.Stat(filepath.Join(*fileName, inf.Name())) 55 if err != nil { 56 fmt.Println(err.Error()) 57 } else { 58 if info.IsDir() || inf.Name() == ".manifest" { 59 fmt.Println("\nSkip") 60 } else { 61 pushAuraComponent(filepath.Join(*fileName, inf.Name())) 62 } 63 } 64 return nil 65 }) 66 } else { 67 pushAuraComponent(*fileName) 68 } 69 return 70 } 71 72 func pushAuraComponent(fname string) { 73 force, _ := ActiveForce() 74 // Check for manifest file 75 if _, err := os.Stat(filepath.Join(filepath.Dir(fname), ".manifest")); os.IsNotExist(err) { 76 // No manifest, but is in aurabundle folder, assume creating a new bundle with this file 77 // as the first artifact. 78 createNewAuraBundleAndDefinition(*force, fname) 79 } else { 80 // Got the manifest, let's update the artifact 81 updateAuraDefinition(*force, fname) 82 return 83 } 84 } 85 86 func isValidAuraExtension(fname string) bool { 87 var ext = strings.Trim(strings.ToLower(filepath.Ext(fname)), " ") 88 if ext == ".app" || ext == ".cmp" || ext == ".evt" { 89 return true 90 } else { 91 ErrorAndExit("You need to create an application (.app) or component (.cmp) or and event (.evt) as the first item in your bundle.") 92 } 93 return false 94 } 95 96 func createNewAuraBundleAndDefinition(force Force, fname string) { 97 // Creating a new bundle. We need 98 // the name of the bundle (parent folder of file) 99 // the type of artifact (based on naming convention) 100 // the contents of the file 101 if isValidAuraExtension(fname) { 102 // Need the parent folder name to name the bundle 103 var bundleName = filepath.Base(filepath.Dir(fname)) 104 // Create the manifext 105 var manifest BundleManifest 106 manifest.Name = bundleName 107 108 _, _ = getFormatByFileName(fname) 109 110 // Create a bundle defintion 111 bundle, err := force.CreateAuraBundle(bundleName) 112 if err != nil { 113 ErrorAndExit(err.Error()) 114 } 115 manifest.Id = bundle.Id 116 component, err := createBundleEntity(manifest, force, fname) 117 if err != nil { 118 ErrorAndExit(err.Error()) 119 } 120 createManifest(manifest, component, fname) 121 } 122 } 123 124 func createBundleEntity(manifest BundleManifest, force Force, fname string) (component ForceCreateRecordResult, err error) { 125 // create the bundle entity 126 format, deftype := getFormatByFileName(fname) 127 mbody, _ := readFile(fname) 128 component, err = force.CreateAuraComponent(map[string]string{"AuraDefinitionBundleId": manifest.Id, "DefType": deftype, "Format": format, "Source": mbody}) 129 return 130 } 131 132 func createManifest(manifest BundleManifest, component ForceCreateRecordResult, fname string) { 133 cfile := ComponentFile{} 134 cfile.FileName = fname 135 cfile.ComponentId = component.Id 136 137 manifest.Files = append(manifest.Files, cfile) 138 bmBody, _ := json.Marshal(manifest) 139 140 ioutil.WriteFile(filepath.Join(filepath.Dir(fname), ".manifest"), bmBody, 0644) 141 return 142 } 143 144 func updateManifest(manifest BundleManifest, component ForceCreateRecordResult, fname string) { 145 cfile := ComponentFile{} 146 cfile.FileName = fname 147 cfile.ComponentId = component.Id 148 149 manifest.Files = append(manifest.Files, cfile) 150 bmBody, _ := json.Marshal(manifest) 151 152 ioutil.WriteFile(filepath.Join(filepath.Dir(fname), ".manifest"), bmBody, 0644) 153 return 154 } 155 156 func GetManifest(fname string) (manifest BundleManifest, err error) { 157 manifestname := filepath.Join(filepath.Dir(fname), ".manifest") 158 159 if _, err = os.Stat(manifestname); os.IsNotExist(err) { 160 return 161 } 162 163 mbody, _ := readFile(filepath.Join(filepath.Dir(fname), ".manifest")) 164 json.Unmarshal([]byte(mbody), &manifest) 165 return 166 } 167 168 func updateAuraDefinition(force Force, fname string) { 169 170 //Get the manifest 171 manifest, err := GetManifest(fname) 172 173 for i := range manifest.Files { 174 component := manifest.Files[i] 175 if filepath.Base(component.FileName) == filepath.Base(fname) { 176 //Here is where we make the call to send the update 177 mbody, _ := readFile(fname) 178 err := force.UpdateAuraComponent(map[string]string{"source": mbody}, component.ComponentId) 179 if err != nil { 180 ErrorAndExit(err.Error()) 181 } 182 fmt.Printf("Aura definition updated: %s\n", filepath.Base(fname)) 183 return 184 } 185 } 186 component, err := createBundleEntity(manifest, force, fname) 187 if err != nil { 188 ErrorAndExit(err.Error()) 189 } 190 updateManifest(manifest, component, fname) 191 fmt.Println("New component in the bundle") 192 } 193 194 func getFormatByFileName(filename string) (format string, defType string) { 195 var fname = strings.ToLower(filename) 196 if strings.Contains(fname, "application.app") { 197 format = "XML" 198 defType = "APPLICATION" 199 } else if strings.Contains(fname, "component.cmp") { 200 format = "XML" 201 defType = "COMPONENT" 202 } else if strings.Contains(fname, "event.evt") { 203 format = "XML" 204 defType = "EVENT" 205 } else if strings.Contains(fname, "controller.js") { 206 format = "JS" 207 defType = "CONTROLLER" 208 } else if strings.Contains(fname, "model.js") { 209 format = "JS" 210 defType = "MODEL" 211 } else if strings.Contains(fname, "helper.js") { 212 format = "JS" 213 defType = "HELPER" 214 } else if strings.Contains(fname, "style.css") { 215 format = "CSS" 216 defType = "STYLE" 217 } else { 218 if filepath.Ext(fname) == ".app" { 219 format = "XML" 220 defType = "APPLICATION" 221 } else if filepath.Ext(fname) == ".cmp" { 222 format = "XML" 223 defType = "COMPONENT" 224 } else if filepath.Ext(fname) == ".evt" { 225 format = "XML" 226 defType = "EVENT" 227 } else if filepath.Ext(fname) == ".css" { 228 format = "CSS" 229 defType = "STYLE" 230 } else { 231 ErrorAndExit("Could not determine aura definition type.") 232 } 233 } 234 return 235 } 236 237 func getDefinitionFormat(deftype string) (result string) { 238 switch strings.ToUpper(deftype) { 239 case "APPLICATION", "COMPONENT", "EVENT": 240 result = "XML" 241 case "CONTROLLER", "MODEL", "HELPER": 242 result = "JS" 243 case "STYLE": 244 result = "CSS" 245 } 246 return 247 } 248 249 func InAuraBundlesFolder(fname string) bool { 250 var p = fname 251 var maxLoop = 3 252 for filepath.Base(p) != "aurabundles" && maxLoop != 0 { 253 p = filepath.Dir(p) 254 maxLoop -= 1 255 } 256 if filepath.Base(p) == "aurabundles" { 257 return true 258 } else { 259 return false 260 } 261 262 } 263 func readFile(filename string) (body string, err error) { 264 data, err := ioutil.ReadFile(filename) 265 if err != nil { 266 return 267 } 268 body = string(data) 269 return 270 }