func registerTypes(ctx context.Context, path string, imports map[string]shared.ImportInfo) (*sysctx.SysPackageInfo, error) { system.RegisterJsonTypes(ctx) scache := sysctx.FromContext(ctx) var current *sysctx.SysPackageInfo for _, info := range imports { env := &envctx.Env{Path: info.Path, Aliases: info.Aliases} pcache := scache.SetEnv(env) for _, ti := range info.Types { if err := parser.ProcessTypeFileBytes(ctx, env, ti.File, ti.Bytes, pcache, nil); err != nil { return nil, kerr.Wrap("UJLXYWCVUC", err) } } if path == info.Path { current = pcache } } return current, nil }
func parse(ctx context.Context, path string, queue []string) (*sysctx.SysPackageInfo, error) { scache := sysctx.FromContext(ctx) cmd := cmdctx.FromContext(ctx) for _, q := range queue { if q == path { return nil, kerr.New("SCSCFJPPHD", "Circular import %v -> %v", queue, path) } } if _, found := scache.Get("kego.io/json"); !found { system.RegisterJsonTypes(ctx) } hash := &PackageHasher{Path: path, Aliases: map[string]string{}, Types: map[string]uint64{}, Exports: map[string]uint64{}} importPackage := func(importPath string, importAlias string) error { if _, found := scache.Get(importPath); !found { _, err := parse(ctx, importPath, append(queue, path)) if err != nil { return kerr.Wrap("RIARRSCMVE", err) } } hash.Aliases[importAlias] = importPath return nil } packageDirectoryExists := true _, err := packages.GetDirFromPackage(ctx, path) if err != nil { _, ok := kerr.Source(err).(gopackages.NotFoundError) if ok { packageDirectoryExists = false } } if !packageDirectoryExists || cmd.Update { if err := GoGet(ctx, path); err != nil { return nil, kerr.Wrap("SBALWXUPKN", err) } } env, err := ScanForEnv(ctx, path) if err != nil { return nil, kerr.Wrap("GJRHNGGWFD", err) } // Always scan the system package first if we don't have it already if path != "kego.io/system" { if err := importPackage("kego.io/system", "system"); err != nil { return nil, kerr.Wrap("ORRCDNUPOX", err) } } for aliasName, aliasPath := range env.Aliases { if aliasPath == "kego.io/system" || aliasName == "system" { return nil, kerr.New("EWMLNJDXKC", "Illegal import %s", aliasName) } if err := importPackage(aliasPath, aliasName); err != nil { return nil, kerr.Wrap("NOVMGYKHHI", err) } } pcache := scache.SetEnv(env) cmd.Printf("Parsing %s...", path) if err := scanForTypesAndExports(ctx, env, pcache, hash); err != nil { return nil, kerr.Wrap("VFUNPHUFHD", err) } cmd.Println(" OK.") h, err := hash.Hash() if err != nil { return nil, kerr.Wrap("MIODRYNEJQ", err) } env.Hash = h return pcache, nil }