github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/aura.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 cmdAura = &Command{
    13  	Usage: "aura",
    14  	Short: "force aura push -fileName=<filepath>",
    15  	Long: `
    16  	The aura command needs context to work. If you execute "aura get"
    17  	it will create a folder structure that provides the context for 
    18  	aura components on disk.
    19  
    20  	The aura components will be created in "metadata/aurabundles/<componentname>"
    21  	relative to the current working directory and a .manifest file will be
    22  	created that associates components and their artifacts with their ids in
    23  	the database. 
    24  
    25  	To create a new component (application, evt or component), create a new
    26  	folder under "aurabundles". Then create a new file in your new folder. You 
    27  	must follow a naming convention for your files to enable proper definition 
    28  	of the component type.
    29  
    30  	Naming convention <compnentName><artifact type>.<file type extension>
    31  	Examples: 	metadata
    32  					aurabundles
    33  						MyApp 
    34  							MyAppApplication.app
    35  							MyAppStyle.css
    36  						MyList 
    37  							MyComponent.cmp
    38  							MyComponentHelper.js
    39  							MyComponentStyle.css
    40  
    41  	force aura push -f <fullFilePath> -b <bundle name>
    42  
    43  	force aura create -t=<entity type> <entityName>
    44  
    45  	force aura delete -f=<fullFilePath>
    46  
    47  	force aura list
    48  
    49  	`,
    50  }
    51  
    52  func init() {
    53  	cmdAura.Run = runAura
    54  	cmdAura.Flag.StringVar(fileName, "f", "", "fully qualified file name for entity")
    55  	cmdAura.Flag.StringVar(auraentitytype, "entitytype", "", "fully qualified file name for entity")
    56  	cmdAura.Flag.StringVar(auraentityname, "entityname", "", "fully qualified file name for entity")
    57  }
    58  
    59  var (
    60  	auraentitytype = cmdAura.Flag.String("t", "", "aura entity type")
    61  	auraentityname = cmdAura.Flag.String("n", "", "aura entity name")
    62  )
    63  
    64  func runAura(cmd *Command, args []string) {
    65  	if err := cmd.Flag.Parse(args[0:]); err != nil {
    66  		os.Exit(2)
    67  	}
    68  
    69  	force, _ := ActiveForce()
    70  
    71  	subcommand := args[0]
    72  	// Sublime hack - the way sublime passes parameters seems to
    73  	// break the flag parsing by sending a single element array
    74  	// for the args. ARGH!!!
    75  	if strings.HasPrefix(subcommand, "delete ") {
    76  		what := strings.Split(subcommand, " ")
    77  		if err := cmd.Flag.Parse(what[1:]); err != nil {
    78  			ErrorAndExit(err.Error())
    79  		}
    80  		subcommand = what[0]
    81  	}
    82  
    83  	switch strings.ToLower(subcommand) {
    84  	case "create":
    85  		/*if *auraentitytype == "" || *auraentityname == "" {
    86  			fmt.Println("Must specify entity type and name")
    87  			os.Exit(2)
    88  		}*/
    89  		ErrorAndExit("force aura create not yet implemented")
    90  
    91  	case "delete":
    92  		runDeleteAura()
    93  	case "list":
    94  		bundles, err := force.GetAuraBundlesList()
    95  		if err != nil {
    96  			ErrorAndExit(err.Error())
    97  		}
    98  		for _, bundle := range bundles.Records {
    99  			fmt.Println(bundle["DeveloperName"])
   100  		}
   101  	case "push":
   102  		runPushAura(cmd, args)
   103  	}
   104  }
   105  
   106  func runDeleteAura() {
   107  	absPath, _ := filepath.Abs(*fileName)
   108  	*fileName = absPath
   109  
   110  	if InAuraBundlesFolder(*fileName) {
   111  		info, err := os.Stat(*fileName)
   112  		if err != nil {
   113  			ErrorAndExit(err.Error())
   114  		}
   115  		manifest, err := GetManifest(*fileName)
   116  		isBundle := false
   117  		if info.IsDir() {
   118  			force, _ := ActiveForce()
   119  			manifest, err = GetManifest(filepath.Join(*fileName, ".manifest"))
   120  			bid := ""
   121  			if err != nil { // Could not find a manifest, use bundle name
   122  				// Try to look up the bundle by name
   123  				b, err := force.GetAuraBundleByName(filepath.Base(*fileName))
   124  				if err != nil {
   125  					ErrorAndExit(err.Error())
   126  				} else {
   127  					if len(b.Records) == 0 {
   128  						ErrorAndExit(fmt.Sprintf("No bundle definition named %q", filepath.Base(*fileName)))
   129  					} else {
   130  						bid = b.Records[0]["Id"].(string)
   131  					}
   132  				}
   133  			} else {
   134  				bid = manifest.Id
   135  			}
   136  
   137  			err = force.DeleteToolingRecord("AuraDefinitionBundle", bid)
   138  			if err != nil {
   139  				ErrorAndExit(err.Error())
   140  			}
   141  			// Now walk the bundle and remove all the atrifacts
   142  			filepath.Walk(*fileName, func(path string, inf os.FileInfo, err error) error {
   143  				os.Remove(path)
   144  				return nil
   145  			})
   146  			os.Remove(*fileName)
   147  			fmt.Println("Bundle ", filepath.Base(*fileName), " deleted.")
   148  			return
   149  		}
   150  
   151  		for key := range manifest.Files {
   152  			mfile := manifest.Files[key].FileName
   153  			cfile := *fileName
   154  			if !filepath.IsAbs(mfile) {
   155  				cfile = filepath.Base(cfile)
   156  			}
   157  			if isBundle {
   158  				if !filepath.IsAbs(mfile) {
   159  					cfile = filepath.Join(*fileName, mfile)
   160  				} else {
   161  					cfile = mfile
   162  					deleteAuraDefinition(manifest, key)
   163  				}
   164  			} else {
   165  				if mfile == cfile {
   166  					deleteAuraDefinition(manifest, key)
   167  					return
   168  				}
   169  			}
   170  		}
   171  		if isBundle {
   172  			// Need to remove the bundle using the id in the manifest
   173  			deleteAuraDefinitionBundle(manifest)
   174  		}
   175  	}
   176  }
   177  func deleteAuraDefinitionBundle(manifest BundleManifest) {
   178  	force, err := ActiveForce()
   179  	err = force.DeleteToolingRecord("AuraDefinitionBundle", manifest.Id)
   180  	if err != nil {
   181  		ErrorAndExit(err.Error())
   182  	}
   183  	os.Remove(filepath.Join(*fileName, ".manifest"))
   184  	os.Remove(*fileName)
   185  }
   186  
   187  func deleteAuraDefinition(manifest BundleManifest, key int) {
   188  	force, err := ActiveForce()
   189  	err = force.DeleteToolingRecord("AuraDefinition", manifest.Files[key].ComponentId)
   190  	if err != nil {
   191  		ErrorAndExit(err.Error())
   192  	}
   193  	fname := manifest.Files[key].FileName
   194  	os.Remove(fname)
   195  	manifest.Files = append(manifest.Files[:key], manifest.Files[key+1:]...)
   196  	bmBody, _ := json.Marshal(manifest)
   197  	ioutil.WriteFile(filepath.Join(filepath.Dir(fname), ".manifest"), bmBody, 0644)
   198  }