Ejemplo n.º 1
0
func ReadPackageDefinitions(pkg *tp.Package, projectPath, scriptPath, fileName string) {
	//pkg.Println(" -- reading definitions")
	_, err := ioutil.ReadFile(filepath.Join(projectPath, scriptPath, fileName))
	//()("READING DEFINITIONS:", location)

	if err != nil {
		//pkg.Log.Infof("\t -- no user defined functions found")
		// msg := fmt.Sprintf("unable to open function definition file: %s", location)
		// println(msg)
		// panic(msg)
		return
	}
	definitions := parser.ParseFile(projectPath, scriptPath, fileName, false, make([]string, 0))

	// Create a map of pre-packaged function signatures
	prepackaged := make(map[string]bool)
	for _, f := range pkg.Functions {
		var sig string
		baseSig := f.Stub(pkg)
		if baseSig == "name,Text" ||
			baseSig == "text" {
			sig = fmt.Sprintf("%s.%s", f.ScopeTypeString(pkg), f.Stub(pkg))
		} else {
			sig = baseSig
		}
		prepackaged[sig] = true
	}
	// println("*****************")
	// println("*****************")
	// println()
	// println()

	for _, function := range definitions.Functions {
		if function.GetName() == "@import" { // check if it's an import stub first ...
			importPath := function.GetDescription()
			// Verify the existence of the imported file from here, so that we can
			// report the name of the file that contains the import statement.
			importExists, existsErr := exists(filepath.Join(projectPath, importPath))
			if !importExists || (existsErr != nil) {
				errURL := "http://help.moovweb.com/entries/22335641-importing-non-existent-files-in-functions-main-ts"
				msg := fmt.Sprintf("\n********\nin file %s:\nattempting to import nonexistent file %s\nPlease consult %s for more information about this error.\n********\n", filepath.Join(scriptPath, fileName), importPath, errURL)
				panic(msg)
			}
			ReadPackageDefinitions(pkg, projectPath, filepath.Dir(importPath), filepath.Base(importPath))
		} else { // otherwise if it's not an import stub ...
			//pkg.Log.Infof("\t -- function: %v", function)
			resolveDefinition(pkg, function, filepath.Join(scriptPath, fileName))

			// After resolving a user-defined function, see if its fully resolved signature
			// is the same as the signature of a prepackaged function. If so, throw an error.
			// var newSig string
			// newBaseSig := function.Stub(pkg)
			// if newBaseSig == "name,Text" ||
			// 	newBaseSig == "text" {
			// 	newSig = fmt.Sprintf("%s.%s", function.ScopeTypeString(pkg), function.Stub(pkg))
			// } else {
			// 	newSig = newBaseSig
			// }
			// // present := false
			// _, present := prepackaged[newSig]
			// if present {
			// 	msg := fmt.Sprintf("Attempt to redefine prepackaged function: %s", strings.Replace(newSig, ",", "(", 1)+")")
			// 	println(msg)
			// 	panic(msg)
			// }

			pkg.Functions = append(pkg.Functions, function)
		}
	}

}